@matheusbsbs / matheus-bagliano / commits / ca6d8f58a1
Fork 0

gitk: Add a menu item for cherry-picking commits

This does a git-cherry-pick -r to cherry-pick the commit that was right-clicked on to the head of the current branch. This would work better with some minor changes to the git-cherry-pick script. Along the way, this changes desc_heads to record the names of the descendent heads rather than their IDs. Signed-off-by: Paul Mackerras <paulus@samba.org>

Paul Mackerras committed Aug 6, 2006 at 21:08 UTC ca6d8f58a15b9db621dd2b905a04d06bdff44bf8
1 file changed +210 -43
gitk
+210 -43
@@ -730,6 +730,8 @@ 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 + $rowctxmenu add command -label "Cherry-pick this commit" \
734 + -command cherrypick
735
736 set headctxmenu .headctxmenu
737 menu $headctxmenu -tearoff 0
@@ -3302,6 +3304,104 @@ proc finishcommits {} {
3304 catch {unset pending_select}
3305 }
3306
3307 +# Inserting a new commit as the child of the commit on row $row.
3308 +# The new commit will be displayed on row $row and the commits
3309 +# on that row and below will move down one row.
3310 +proc insertrow {row newcmit} {
3311 + global displayorder parentlist childlist commitlisted
3312 + global commitrow curview rowidlist rowoffsets numcommits
3313 + global rowrangelist idrowranges rowlaidout rowoptim numcommits
3314 + global linesegends
3315 +
3316 + if {$row >= $numcommits} {
3317 + puts "oops, inserting new row $row but only have $numcommits rows"
3318 + return
3319 + }
3320 + set p [lindex $displayorder $row]
3321 + set displayorder [linsert $displayorder $row $newcmit]
3322 + set parentlist [linsert $parentlist $row $p]
3323 + set kids [lindex $childlist $row]
3324 + lappend kids $newcmit
3325 + lset childlist $row $kids
3326 + set childlist [linsert $childlist $row {}]
3327 + set l [llength $displayorder]
3328 + for {set r $row} {$r < $l} {incr r} {
3329 + set id [lindex $displayorder $r]
3330 + set commitrow($curview,$id) $r
3331 + }
3332 +
3333 + set idlist [lindex $rowidlist $row]
3334 + set offs [lindex $rowoffsets $row]
3335 + set newoffs {}
3336 + foreach x $idlist {
3337 + if {$x eq {} || ($x eq $p && [llength $kids] == 1)} {
3338 + lappend newoffs {}
3339 + } else {
3340 + lappend newoffs 0
3341 + }
3342 + }
3343 + if {[llength $kids] == 1} {
3344 + set col [lsearch -exact $idlist $p]
3345 + lset idlist $col $newcmit
3346 + } else {
3347 + set col [llength $idlist]
3348 + lappend idlist $newcmit
3349 + lappend offs {}
3350 + lset rowoffsets $row $offs
3351 + }
3352 + set rowidlist [linsert $rowidlist $row $idlist]
3353 + set rowoffsets [linsert $rowoffsets [expr {$row+1}] $newoffs]
3354 +
3355 + set rowrangelist [linsert $rowrangelist $row {}]
3356 + set l [llength $rowrangelist]
3357 + for {set r 0} {$r < $l} {incr r} {
3358 + set ranges [lindex $rowrangelist $r]
3359 + if {$ranges ne {} && [lindex $ranges end] >= $row} {
3360 + set newranges {}
3361 + foreach x $ranges {
3362 + if {$x >= $row} {
3363 + lappend newranges [expr {$x + 1}]
3364 + } else {
3365 + lappend newranges $x
3366 + }
3367 + }
3368 + lset rowrangelist $r $newranges
3369 + }
3370 + }
3371 + if {[llength $kids] > 1} {
3372 + set rp1 [expr {$row + 1}]
3373 + set ranges [lindex $rowrangelist $rp1]
3374 + if {$ranges eq {}} {
3375 + set ranges [list $row $rp1]
3376 + } elseif {[lindex $ranges end-1] == $rp1} {
3377 + lset ranges end-1 $row
3378 + }
3379 + lset rowrangelist $rp1 $ranges
3380 + }
3381 + foreach id [array names idrowranges] {
3382 + set ranges $idrowranges($id)
3383 + if {$ranges ne {} && [lindex $ranges end] >= $row} {
3384 + set newranges {}
3385 + foreach x $ranges {
3386 + if {$x >= $row} {
3387 + lappend newranges [expr {$x + 1}]
3388 + } else {
3389 + lappend newranges $x
3390 + }
3391 + }
3392 + set idrowranges($id) $newranges
3393 + }
3394 + }
3395 +
3396 + set linesegends [linsert $linesegends $row {}]
3397 +
3398 + incr rowlaidout
3399 + incr rowoptim
3400 + incr numcommits
3401 +
3402 + redisplay
3403 +}
3404 +
3405 # Don't change the text pane cursor if it is currently the hand cursor,
3406 # showing that we are over a sha1 ID link.
3407 proc settextcursor {c} {
@@ -3629,27 +3729,20 @@ proc viewnextline {dir} {
3729
3730 # add a list of tag or branch names at position pos
3731 # returns the number of names inserted
3632 -proc appendrefs {pos l var} {
3633 - global ctext commitrow linknum curview idtags $var
3732 +proc appendrefs {pos tags var} {
3733 + global ctext commitrow linknum curview $var
3734
3735 if {[catch {$ctext index $pos}]} {
3736 return 0
3737 }
3638 - set tags {}
3639 - foreach id $l {
3640 - foreach tag [set $var\($id\)] {
3641 - lappend tags [concat $tag $id]
3642 - }
3643 - }
3644 - set tags [lsort -index 1 $tags]
3738 + set tags [lsort $tags]
3739 set sep {}
3740 foreach tag $tags {
3647 - set name [lindex $tag 0]
3648 - set id [lindex $tag 1]
3741 + set id [set $var\($tag\)]
3742 set lk link$linknum
3743 incr linknum
3744 $ctext insert $pos $sep
3652 - $ctext insert $pos $name $lk
3745 + $ctext insert $pos $tag $lk
3746 $ctext tag conf $lk -foreground blue
3747 if {[info exists commitrow($curview,$id)]} {
3748 $ctext tag bind $lk <1> \
@@ -3663,6 +3756,18 @@ proc appendrefs {pos l var} {
3756 return [llength $tags]
3757 }
3758
3759 +proc taglist {ids} {
3760 + global idtags
3761 +
3762 + set tags {}
3763 + foreach id $ids {
3764 + foreach tag $idtags($id) {
3765 + lappend tags $tag
3766 + }
3767 + }
3768 + return $tags
3769 +}
3770 +
3771 # called when we have finished computing the nearby tags
3772 proc dispneartags {} {
3773 global selectedline currentid ctext anc_tags desc_tags showneartags
@@ -3672,15 +3777,15 @@ proc dispneartags {} {
3777 set id $currentid
3778 $ctext conf -state normal
3779 if {[info exists desc_heads($id)]} {
3675 - if {[appendrefs branch $desc_heads($id) idheads] > 1} {
3780 + if {[appendrefs branch $desc_heads($id) headids] > 1} {
3781 $ctext insert "branch -2c" "es"
3782 }
3783 }
3784 if {[info exists anc_tags($id)]} {
3680 - appendrefs follows $anc_tags($id) idtags
3785 + appendrefs follows [taglist $anc_tags($id)] tagids
3786 }
3787 if {[info exists desc_tags($id)]} {
3683 - appendrefs precedes $desc_tags($id) idtags
3788 + appendrefs precedes [taglist $desc_tags($id)] tagids
3789 }
3790 $ctext conf -state disabled
3791 }
@@ -3813,7 +3918,7 @@ proc selectline {l isnew} {
3918 $ctext mark set branch "end -1c"
3919 $ctext mark gravity branch left
3920 if {[info exists desc_heads($id)]} {
3816 - if {[appendrefs branch $desc_heads($id) idheads] > 1} {
3921 + if {[appendrefs branch $desc_heads($id) headids] > 1} {
3922 # turn "Branch" into "Branches"
3923 $ctext insert "branch -2c" "es"
3924 }
@@ -3822,13 +3927,13 @@ proc selectline {l isnew} {
3927 $ctext mark set follows "end -1c"
3928 $ctext mark gravity follows left
3929 if {[info exists anc_tags($id)]} {
3825 - appendrefs follows $anc_tags($id) idtags
3930 + appendrefs follows [taglist $anc_tags($id)] tagids
3931 }
3932 $ctext insert end "\nPrecedes: "
3933 $ctext mark set precedes "end -1c"
3934 $ctext mark gravity precedes left
3935 if {[info exists desc_tags($id)]} {
3831 - appendrefs precedes $desc_tags($id) idtags
3936 + appendrefs precedes [taglist $desc_tags($id)] tagids
3937 }
3938 $ctext insert end "\n"
3939 }
@@ -4489,6 +4594,7 @@ proc redisplay {} {
4594 drawvisible
4595 if {[info exists selectedline]} {
4596 selectline $selectedline 0
4597 + allcanvs yview moveto [lindex $span 0]
4598 }
4599 }
4600
@@ -5090,17 +5196,57 @@ proc mkbrgo {top} {
5196 notbusy newbranch
5197 error_popup $err
5198 } else {
5093 - set headids($name) $id
5094 - if {![info exists idheads($id)]} {
5095 - addedhead $id
5096 - }
5097 - lappend idheads($id) $name
5199 + addedhead $id $name
5200 # XXX should update list of heads displayed for selected commit
5201 notbusy newbranch
5202 redrawtags $id
5203 }
5204 }
5205
5206 +proc cherrypick {} {
5207 + global rowmenuid curview commitrow
5208 + global mainhead desc_heads anc_tags desc_tags allparents allchildren
5209 +
5210 + if {[info exists desc_heads($rowmenuid)]
5211 + && [lsearch -exact $desc_heads($rowmenuid) $mainhead] >= 0} {
5212 + set ok [confirm_popup "Commit [string range $rowmenuid 0 7] is already\
5213 + included in branch $mainhead -- really re-apply it?"]
5214 + if {!$ok} return
5215 + }
5216 + nowbusy cherrypick
5217 + update
5218 + set oldhead [exec git rev-parse HEAD]
5219 + # Unfortunately git-cherry-pick writes stuff to stderr even when
5220 + # no error occurs, and exec takes that as an indication of error...
5221 + if {[catch {exec sh -c "git cherry-pick -r $rowmenuid 2>&1"} err]} {
5222 + notbusy cherrypick
5223 + error_popup $err
5224 + return
5225 + }
5226 + set newhead [exec git rev-parse HEAD]
5227 + if {$newhead eq $oldhead} {
5228 + notbusy cherrypick
5229 + error_popup "No changes committed"
5230 + return
5231 + }
5232 + set allparents($newhead) $oldhead
5233 + lappend allchildren($oldhead) $newhead
5234 + set desc_heads($newhead) $mainhead
5235 + if {[info exists anc_tags($oldhead)]} {
5236 + set anc_tags($newhead) $anc_tags($oldhead)
5237 + }
5238 + set desc_tags($newhead) {}
5239 + if {[info exists commitrow($curview,$oldhead)]} {
5240 + insertrow $commitrow($curview,$oldhead) $newhead
5241 + if {$mainhead ne {}} {
5242 + movedhead $newhead $mainhead
5243 + }
5244 + redrawtags $oldhead
5245 + redrawtags $newhead
5246 + }
5247 + notbusy cherrypick
5248 +}
5249 +
5250 # context menu for a head
5251 proc headmenu {x y id head} {
5252 global headmenuid headmenuhead headctxmenu
@@ -5142,7 +5288,7 @@ proc rmbranch {} {
5288 error_popup "Cannot delete the currently checked-out branch"
5289 return
5290 }
5145 - if {$desc_heads($id) eq $id && $idheads($id) eq [list $head]} {
5291 + if {$desc_heads($id) eq $head} {
5292 # the stuff on this branch isn't on any other branch
5293 if {![confirm_popup "The commits on branch $head aren't on any other\
5294 branch.\nReally delete branch $head?"]} return
@@ -5154,16 +5300,7 @@ proc rmbranch {} {
5300 error_popup $err
5301 return
5302 }
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 - }
5303 + removedhead $id $head
5304 redrawtags $id
5305 notbusy rmbranch
5306 }
@@ -5293,7 +5430,7 @@ proc forward_pass {id children} {
5430 }
5431 }
5432 if {[info exists idheads($id)]} {
5296 - lappend dheads $id
5433 + set dheads [concat $dheads $idheads($id)]
5434 }
5435 set desc_heads($id) $dheads
5436 }
@@ -5301,7 +5438,7 @@ proc forward_pass {id children} {
5438 proc getallclines {fd} {
5439 global allparents allchildren allcommits allcstart
5440 global desc_tags anc_tags idtags tagisdesc allids
5304 - global desc_heads idheads travindex
5441 + global idheads travindex
5442
5443 while {[gets $fd line] >= 0} {
5444 set id [lindex $line 0]
@@ -5369,17 +5506,20 @@ proc restartatags {} {
5506 }
5507
5508 # update the desc_heads array for a new head just added
5372 -proc addedhead {hid} {
5373 - global desc_heads allparents
5509 +proc addedhead {hid head} {
5510 + global desc_heads allparents headids idheads
5511 +
5512 + set headids($head) $hid
5513 + lappend idheads($hid) $head
5514
5515 set todo [list $hid]
5516 while {$todo ne {}} {
5517 set do [lindex $todo 0]
5518 set todo [lrange $todo 1 end]
5519 if {![info exists desc_heads($do)] ||
5380 - [lsearch -exact $desc_heads($do) $hid] >= 0} continue
5520 + [lsearch -exact $desc_heads($do) $head] >= 0} continue
5521 set oldheads $desc_heads($do)
5382 - lappend desc_heads($do) $hid
5522 + lappend desc_heads($do) $head
5523 set heads $desc_heads($do)
5524 while {1} {
5525 set p $allparents($do)
@@ -5393,15 +5533,25 @@ proc addedhead {hid} {
5533 }
5534
5535 # update the desc_heads array for a head just removed
5396 -proc removedhead {hid} {
5397 - global desc_heads allparents
5536 +proc removedhead {hid head} {
5537 + global desc_heads allparents headids idheads
5538 +
5539 + unset headids($head)
5540 + if {$idheads($hid) eq $head} {
5541 + unset idheads($hid)
5542 + } else {
5543 + set i [lsearch -exact $idheads($hid) $head]
5544 + if {$i >= 0} {
5545 + set idheads($hid) [lreplace $idheads($hid) $i $i]
5546 + }
5547 + }
5548
5549 set todo [list $hid]
5550 while {$todo ne {}} {
5551 set do [lindex $todo 0]
5552 set todo [lrange $todo 1 end]
5553 if {![info exists desc_heads($do)]} continue
5404 - set i [lsearch -exact $desc_heads($do) $hid]
5554 + set i [lsearch -exact $desc_heads($do) $head]
5555 if {$i < 0} continue
5556 set oldheads $desc_heads($do)
5557 set heads [lreplace $desc_heads($do) $i $i]
@@ -5416,6 +5566,23 @@ proc removedhead {hid} {
5566 }
5567 }
5568
5569 +# update things for a head moved to a child of its previous location
5570 +proc movedhead {id name} {
5571 + global headids idheads
5572 +
5573 + set oldid $headids($name)
5574 + set headids($name) $id
5575 + if {$idheads($oldid) eq $name} {
5576 + unset idheads($oldid)
5577 + } else {
5578 + set i [lsearch -exact $idheads($oldid) $name]
5579 + if {$i >= 0} {
5580 + set idheads($oldid) [lreplace $idheads($oldid) $i $i]
5581 + }
5582 + }
5583 + lappend idheads($id) $name
5584 +}
5585 +
5586 proc changedrefs {} {
5587 global desc_heads desc_tags anc_tags allcommits allids
5588 global allchildren allparents idtags travindex