git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/2] gitk: Allow checking out a remote branch

From
RGRogier Goossens <goossens.rogier@gmail.com>
Date
Mar 19, 2016, 18:33 UTC
Message-ID
<4410053.4PRisWLqAX@wiske>
In-Reply-To
<10662590.KWXHt2RUKZ@wiske>

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>
---
 gitk | 36 +++++++++++++++++++++++++++++++-----
 1 file changed, 31 insertions(+), 5 deletions(-)
diff --git a/gitk b/gitk
index 84b49bc..dc75c97 100755
--- a/gitk
+++ b/gitk
@@ -9824,14 +9824,18 @@ proc readresetstat {fd} {
 
 # context menu for a head
 proc headmenu {x y id head} {
-    global headmenuid headmenuhead headctxmenu mainhead
+    global headmenuid headmenuhead headctxmenu mainhead headids
 
     stopfinding
     set headmenuid $id
     set headmenuhead $head
     array set state {0 normal 1 normal 2 normal}
     if {[string match "remotes/*" $head]} {
-	array set state {0 disabled 1 disabled 2 disabled}
+	set localhead [string range $head [expr [string last / $head] + 1] end]
+	if {[info exists headids($localhead)]} {
+	    set state(0) disabled
+	}
+	array set state {1 disabled 2 disabled}
     }
     if {$head eq $mainhead} {
 	array set state {0 disabled 2 disabled}
@@ -9847,11 +9851,27 @@ proc cobranch {} {
     global showlocalchanges
 
     # check the tree is clean first??
+    set newhead $headmenuhead
+    set command [list | git checkout]
+    if {[string match "remotes/*" $newhead]} {
+	set remote $newhead
+	set newhead [string range $newhead [expr [string last / $newhead] + 1] end]
+	# The following check is redundant - the menu option should
+	# be disabled to begin with...
+	if {[info exists headids($newhead)]} {
+	    error_popup [mc "A local branch named %s exists already" $newhead]
+	    return
+	}
+	lappend command -b $newhead --track $remote
+    } else {
+	lappend command $newhead
+    }
+    lappend command 2>@1
     nowbusy checkout [mc "Checking out"]
     update
     dohidelocalchanges
     if {[catch {
-	set fd [open [list | git checkout $headmenuhead 2>@1] r]
+	set fd [open $command r]
     } err]} {
 	notbusy checkout
 	error_popup $err
@@ -9859,12 +9879,12 @@ proc cobranch {} {
 	    dodiffindex
 	}
     } else {
-	filerun $fd [list readcheckoutstat $fd $headmenuhead $headmenuid]
+	filerun $fd [list readcheckoutstat $fd $newhead $headmenuid]
     }
 }
 
 proc readcheckoutstat {fd newhead newheadid} {
-    global mainhead mainheadid headids showlocalchanges progresscoords
+    global mainhead mainheadid headids idheads showlocalchanges progresscoords
     global viewmainheadid curview
 
     if {[gets $fd line] >= 0} {
@@ -9879,8 +9899,14 @@ proc readcheckoutstat {fd newhead newheadid} {
     notbusy checkout
     if {[catch {close $fd} err]} {
 	error_popup $err
+	return
     }
     set oldmainid $mainheadid
+    if {! [info exists headids($newhead)]} {
+	set headids($newhead) $newheadid
+	lappend idheads($newheadid) $newhead
+	addedhead $newheadid $newhead
+    }
     set mainhead $newhead
     set mainheadid $newheadid
     set viewmainheadid($curview) $newheadid
-- 
2.1.4
Previous: Rogier GoossensNext: Rogier Goossens
Message 7 of 9 in “gitk: Two improvements to the branch context menu”
  1. 0/2 gitk: Two improvements to the branch context menuRogier Goossens, Jan 15, 2016
  2. 1/2 gitk: Add a 'rename' option to the branch context menuRogier Goossens, Jan 15, 2016
  3. Paul MackerrasMar 19, 2016
  4. 2/2 gitk: Allow checking out a remote branchRogier Goossens, Jan 15, 2016
  5. 0/2 gitk: Two improvements to the branch context menuRogier Goossens, Mar 19, 2016
  6. 1/2 gitk: Add a 'rename' option to the branch context menuRogier Goossens, Mar 19, 2016
  7. 2/2 gitk: Allow checking out a remote branchRogier Goossens, Mar 19, 2016
  8. 3/3 gitk: Include commit title in branch dialogRogier Goossens, Mar 27, 2016
  9. Paul MackerrasDec 12, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.