Skip to content

Commit 1029915

Browse files
committed
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 <[email protected]>
1 parent d6ac1a8 commit 1029915

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed

gitk

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,23 @@ proc error_popup msg {
384384
show_error $w $w $msg
385385
}
386386

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+
387404
proc makewindow {} {
388405
global canv canv2 canv3 linespc charspc ctext cflist
389406
global textfont mainfont uifont
@@ -394,6 +411,7 @@ proc makewindow {} {
394411
global highlight_files gdttype
395412
global searchstring sstring
396413
global bgcolor fgcolor bglist fglist diffcolors
414+
global headctxmenu
397415

398416
menu .bar
399417
.bar add cascade -label "File" -menu .bar.file
@@ -712,6 +730,13 @@ proc makewindow {} {
712730
$rowctxmenu add command -label "Create tag" -command mktag
713731
$rowctxmenu add command -label "Write commit to file" -command writecommit
714732
$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
715740
}
716741

717742
# mouse-2 makes all windows scan vertically, but only the one
@@ -3237,6 +3262,8 @@ proc drawtags {id x xt y1} {
32373262
-font $font -tags [list tag.$id text]]
32383263
if {$ntags >= 0} {
32393264
$canv bind $t <1> [list showtag $tag 1]
3265+
} elseif {$nheads >= 0} {
3266+
$canv bind $t <Button-3> [list headmenu %X %Y $id $tag]
32403267
}
32413268
}
32423269
return $xt
@@ -5074,6 +5101,73 @@ proc mkbrgo {top} {
50745101
}
50755102
}
50765103

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+
50775171
# Stuff for finding nearby tags
50785172
proc getallcommits {} {
50795173
global allcstart allcommits allcfd allids
@@ -5298,6 +5392,30 @@ proc addedhead {hid} {
52985392
}
52995393
}
53005394

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+
53015419
proc changedrefs {} {
53025420
global desc_heads desc_tags anc_tags allcommits allids
53035421
global allchildren allparents idtags travindex

0 commit comments

Comments
 (0)