gitk: Add a context menu for heads
This menu allows you to check out a branch and to delete a branch. If you ask to delete a branch that has commits that aren't on any other branch, gitk will prompt for confirmation before doing the deletion. Signed-off-by: Paul Mackerras <paulus@samba.org>
Paul Mackerras committed
Aug 2, 2006 at 09:52 UTC
10299152ca10107a4570764286e129572ed2f3c3
1 file changed
+118
gitk
+118
@@ -384,6 +384,23 @@ proc error_popup msg {
384
show_error $w $w $msg
385
}
386
387
+proc confirm_popup msg {
388
+ global confirm_ok
389
+ set confirm_ok 0
390
+ set w .confirm
391
+ toplevel $w
392
+ wm transient $w .
393
+ message $w.m -text $msg -justify center -aspect 400
394
+ pack $w.m -side top -fill x -padx 20 -pady 20
395
+ button $w.ok -text OK -command "set confirm_ok 1; destroy $w"
396
+ pack $w.ok -side left -fill x
397
+ button $w.cancel -text Cancel -command "destroy $w"
398
+ pack $w.cancel -side right -fill x
399
+ bind $w <Visibility> "grab $w; focus $w"
400
+ tkwait window $w
401
+ return $confirm_ok
402
+}
403
+
404
proc makewindow {} {
405
global canv canv2 canv3 linespc charspc ctext cflist
406
global textfont mainfont uifont
@@ -394,6 +411,7 @@ proc makewindow {} {
411
global highlight_files gdttype
412
global searchstring sstring
413
global bgcolor fgcolor bglist fglist diffcolors
414
+ global headctxmenu
415
416
menu .bar
417
.bar add cascade -label "File" -menu .bar.file
@@ -712,6 +730,13 @@ proc makewindow {} {
730
$rowctxmenu add command -label "Create tag" -command mktag
731
$rowctxmenu add command -label "Write commit to file" -command writecommit
732
$rowctxmenu add command -label "Create new branch" -command mkbranch
733
+
734
+ set headctxmenu .headctxmenu
735
+ menu $headctxmenu -tearoff 0
736
+ $headctxmenu add command -label "Check out this branch" \
737
+ -command cobranch
738
+ $headctxmenu add command -label "Remove this branch" \
739
+ -command rmbranch
740
}
741
742
# mouse-2 makes all windows scan vertically, but only the one
@@ -3237,6 +3262,8 @@ proc drawtags {id x xt y1} {
3262
-font $font -tags [list tag.$id text]]
3263
if {$ntags >= 0} {
3264
$canv bind $t <1> [list showtag $tag 1]
3265
+ } elseif {$nheads >= 0} {
3266
+ $canv bind $t <Button-3> [list headmenu %X %Y $id $tag]
3267
}
3268
}
3269
return $xt
@@ -5074,6 +5101,73 @@ proc mkbrgo {top} {
5101
}
5102
}
5103
5104
+# context menu for a head
5105
+proc headmenu {x y id head} {
5106
+ global headmenuid headmenuhead headctxmenu
5107
+
5108
+ set headmenuid $id
5109
+ set headmenuhead $head
5110
+ tk_popup $headctxmenu $x $y
5111
+}
5112
+
5113
+proc cobranch {} {
5114
+ global headmenuid headmenuhead mainhead headids
5115
+
5116
+ # check the tree is clean first??
5117
+ set oldmainhead $mainhead
5118
+ nowbusy checkout
5119
+ update
5120
+ if {[catch {
5121
+ exec git checkout $headmenuhead
5122
+ } err]} {
5123
+ notbusy checkout
5124
+ error_popup $err
5125
+ } else {
5126
+ notbusy checkout
5127
+ set maainhead $headmenuhead
5128
+ if {[info exists headids($oldmainhead)]} {
5129
+ redrawtags $headids($oldmainhead)
5130
+ }
5131
+ redrawtags $headmenuid
5132
+ }
5133
+}
5134
+
5135
+proc rmbranch {} {
5136
+ global desc_heads headmenuid headmenuhead mainhead
5137
+ global headids idheads
5138
+
5139
+ set head $headmenuhead
5140
+ set id $headmenuid
5141
+ if {$head eq $mainhead} {
5142
+ error_popup "Cannot delete the currently checked-out branch"
5143
+ return
5144
+ }
5145
+ if {$desc_heads($id) eq $id} {
5146
+ # the stuff on this branch isn't on any other branch
5147
+ if {![confirm_popup "The commits on branch $head aren't on any other\
5148
+ branch.\nReally delete branch $head?"]} return
5149
+ }
5150
+ nowbusy rmbranch
5151
+ update
5152
+ if {[catch {exec git branch -D $head} err]} {
5153
+ notbusy rmbranch
5154
+ error_popup $err
5155
+ return
5156
+ }
5157
+ unset headids($head)
5158
+ if {$idheads($id) eq $head} {
5159
+ unset idheads($id)
5160
+ removedhead $id
5161
+ } else {
5162
+ set i [lsearch -exact $idheads($id) $head]
5163
+ if {$i >= 0} {
5164
+ set idheads($id) [lreplace $idheads($id) $i $i]
5165
+ }
5166
+ }
5167
+ redrawtags $id
5168
+ notbusy rmbranch
5169
+}
5170
+
5171
# Stuff for finding nearby tags
5172
proc getallcommits {} {
5173
global allcstart allcommits allcfd allids
@@ -5298,6 +5392,30 @@ proc addedhead {hid} {
5392
}
5393
}
5394
5395
+# update the desc_heads array for a head just removed
5396
+proc removedhead {hid} {
5397
+ global desc_heads allparents
5398
+
5399
+ set todo [list $hid]
5400
+ while {$todo ne {}} {
5401
+ set do [lindex $todo 0]
5402
+ set todo [lrange $todo 1 end]
5403
+ if {![info exists desc_heads($do)]} continue
5404
+ set i [lsearch -exact $desc_heads($do) $hid]
5405
+ if {$i < 0} continue
5406
+ set oldheads $desc_heads($do)
5407
+ set heads [lreplace $desc_heads($do) $i $i]
5408
+ while {1} {
5409
+ set desc_heads($do) $heads
5410
+ set p $allparents($do)
5411
+ if {[llength $p] != 1 || ![info exists desc_heads($p)] ||
5412
+ $desc_heads($p) ne $oldheads} break
5413
+ set do $p
5414
+ }
5415
+ set todo [concat $todo $p]
5416
+ }
5417
+}
5418
+
5419
proc changedrefs {} {
5420
global desc_heads desc_tags anc_tags allcommits allids
5421
global allchildren allparents idtags travindex