gitk: Allow checking out a remote branch

Git allows checking out remote branches, creating a local tracking branch in the process. Allow gitk to do this as well, provided a local branch of the same name does not yet exist. Signed-off-by: Rogier Goossens <goossens.rogier@gmail.com> Signed-off-by: Paul Mackerras <paulus@ozlabs.org>

Rogier Goossens committed Mar 19, 2016 at 19:33 UTC 02e6a0601b846d197e062f5efdf29fdcef54d54c
1 file changed +31 -5
gitk
+31 -5
@@ -9824,14 +9824,18 @@ proc readresetstat {fd} {
9824
9825 # context menu for a head
9826 proc headmenu {x y id head} {
9827 - global headmenuid headmenuhead headctxmenu mainhead
9827 + global headmenuid headmenuhead headctxmenu mainhead headids
9828
9829 stopfinding
9830 set headmenuid $id
9831 set headmenuhead $head
9832 array set state {0 normal 1 normal 2 normal}
9833 if {[string match "remotes/*" $head]} {
9834 - array set state {0 disabled 1 disabled 2 disabled}
9834 + set localhead [string range $head [expr [string last / $head] + 1] end]
9835 + if {[info exists headids($localhead)]} {
9836 + set state(0) disabled
9837 + }
9838 + array set state {1 disabled 2 disabled}
9839 }
9840 if {$head eq $mainhead} {
9841 array set state {0 disabled 2 disabled}
@@ -9847,11 +9851,27 @@ proc cobranch {} {
9851 global showlocalchanges
9852
9853 # check the tree is clean first??
9854 + set newhead $headmenuhead
9855 + set command [list | git checkout]
9856 + if {[string match "remotes/*" $newhead]} {
9857 + set remote $newhead
9858 + set newhead [string range $newhead [expr [string last / $newhead] + 1] end]
9859 + # The following check is redundant - the menu option should
9860 + # be disabled to begin with...
9861 + if {[info exists headids($newhead)]} {
9862 + error_popup [mc "A local branch named %s exists already" $newhead]
9863 + return
9864 + }
9865 + lappend command -b $newhead --track $remote
9866 + } else {
9867 + lappend command $newhead
9868 + }
9869 + lappend command 2>@1
9870 nowbusy checkout [mc "Checking out"]
9871 update
9872 dohidelocalchanges
9873 if {[catch {
9854 - set fd [open [list | git checkout $headmenuhead 2>@1] r]
9874 + set fd [open $command r]
9875 } err]} {
9876 notbusy checkout
9877 error_popup $err
@@ -9859,12 +9879,12 @@ proc cobranch {} {
9879 dodiffindex
9880 }
9881 } else {
9862 - filerun $fd [list readcheckoutstat $fd $headmenuhead $headmenuid]
9882 + filerun $fd [list readcheckoutstat $fd $newhead $headmenuid]
9883 }
9884 }
9885
9886 proc readcheckoutstat {fd newhead newheadid} {
9867 - global mainhead mainheadid headids showlocalchanges progresscoords
9887 + global mainhead mainheadid headids idheads showlocalchanges progresscoords
9888 global viewmainheadid curview
9889
9890 if {[gets $fd line] >= 0} {
@@ -9879,8 +9899,14 @@ proc readcheckoutstat {fd newhead newheadid} {
9899 notbusy checkout
9900 if {[catch {close $fd} err]} {
9901 error_popup $err
9902 + return
9903 }
9904 set oldmainid $mainheadid
9905 + if {! [info exists headids($newhead)]} {
9906 + set headids($newhead) $newheadid
9907 + lappend idheads($newheadid) $newhead
9908 + addedhead $newheadid $newhead
9909 + }
9910 set mainhead $newhead
9911 set mainheadid $newheadid
9912 set viewmainheadid($curview) $newheadid