# [PATCH v2 1/2] gitk: alter the ordering for the "Tags and heads" view

5 messages from 2016-03-18 to 2016-03-19. Participants: Michael Rappazzo, Paul Mackerras.
Thread: https://gitlist.dev/t/41739

## Michael Rappazzo, 2016-03-18 18:51

Subject: [PATCH v2 0/2] gitk: changes for the "Tags and heads" view
Message-ID: <1458327097-92126-1-git-send-email-rappazzo@gmail.com>
URL: https://gitlist.dev/e/1458327097-92126-1-git-send-email-rappazzo%40gmail.com

```
I found a small bug in the v1 of this patch[1], when a tracked upstream
branch is no longer present.  The changes are only in the first commit.

[1] http://thread.gmane.org/gmane.comp.version-control.git/288544

Michael Rappazzo (2):
  gitk: alter the ordering for the "Tags and heads" view
  gitk: add an option to enable sorting the "Tags and heads" view by ref
    type

 gitk | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 52 insertions(+), 7 deletions(-)

-- 
2.7.4

```

## Michael Rappazzo, 2016-03-18 18:51

Subject: [PATCH v2 1/2] gitk: alter the ordering for the "Tags and heads" view
Message-ID: <1458327097-92126-2-git-send-email-rappazzo@gmail.com>
URL: https://gitlist.dev/e/1458327097-92126-2-git-send-email-rappazzo%40gmail.com
In-Reply-To: <1458327097-92126-1-git-send-email-rappazzo@gmail.com>

```
In the "Tags and heads" view, the list of refs is globally sorted.
Because of this, the list of local refs (heads) can be interrupted by the
list of remote refs.  This change re-orders the view to be: local refs,
remote refs tracked by local refs, remote refs, tags, and then other refs.

Signed-off-by: Michael Rappazzo <rappazzo@gmail.com>
---
 gitk | 48 ++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 42 insertions(+), 6 deletions(-)

diff --git a/gitk b/gitk
index 5f1255c..d6d3136 100755
--- a/gitk
+++ b/gitk
@@ -9933,35 +9933,71 @@ proc refill_reflist {} {
     global curview
 
     if {![info exists showrefstop] || ![winfo exists $showrefstop]} return
-    set refs {}
+    set localrefs {}
+    set remoterefs {}
+    set locally_tracked_remote_refs {}
+    set tagrefs {}
+    set otherrefs {}
     foreach n [array names headids] {
-	if {[string match $reflistfilter $n]} {
+	if {![string match "remotes/*" $n] && [string match $reflistfilter $n]} {
 	    if {[commitinview $headids($n) $curview]} {
-		lappend refs [list $n H]
+		lappend localrefs [list $n H]
+		catch {set remote_name [exec git config --get branch.$n.remote]}
+		if {$remote_name ne ""} {
+		    catch {set remote_ref [exec git config --get branch.$n.merge]}
+		    set remote_ref [string map {"refs/heads/" ""} $remote_ref]
+		    set locally_tracked_remote_ref "remotes/$remote_name/$remote_ref"
+		    set upstream_exists ""
+		    catch {set upstream_exists [exec git rev-parse --verify $locally_tracked_remote_ref]}
+		    if {$upstream_exists ne ""} {
+			if {[lsearch $locally_tracked_remote_refs [list $locally_tracked_remote_ref H]] < 0} {
+			    lappend locally_tracked_remote_refs [list $locally_tracked_remote_ref H]
+			}
+		    }
+		}
 	    } else {
 		interestedin $headids($n) {run refill_reflist}
 	    }
 	}
     }
+    set locally_tracked_remote_refs [lsort -index 0 $locally_tracked_remote_refs]
+    set localrefs [lsort -index 0 $localrefs]
+
+    foreach n [array names headids] {
+	if {[string match "remotes/*" $n] && [string match $reflistfilter $n]} {
+	    if {[commitinview $headids($n) $curview]} {
+		if {[lsearch $locally_tracked_remote_refs [list $n H]] < 0} {
+		    lappend remoterefs [list $n H]
+		}
+	    } else {
+		interestedin $headids($n) {run refill_reflist}
+	    }
+	}
+    }
+    set remoterefs [lsort -index 0 $remoterefs]
+
     foreach n [array names tagids] {
 	if {[string match $reflistfilter $n]} {
 	    if {[commitinview $tagids($n) $curview]} {
-		lappend refs [list $n T]
+		lappend tagrefs [list $n T]
 	    } else {
 		interestedin $tagids($n) {run refill_reflist}
 	    }
 	}
     }
+    set tagrefs [lsort -index 0 $tagrefs]
+
     foreach n [array names otherrefids] {
 	if {[string match $reflistfilter $n]} {
 	    if {[commitinview $otherrefids($n) $curview]} {
-		lappend refs [list $n o]
+		lappend otherrefs [list "$n" o]
 	    } else {
 		interestedin $otherrefids($n) {run refill_reflist}
 	    }
 	}
     }
-    set refs [lsort -index 0 $refs]
+    set otherrefs [lsort -index 0 $otherrefs]
+    lappend refs {*}$localrefs {*}$locally_tracked_remote_refs {*}$remoterefs {*}$tagrefs {*}$otherrefs
     if {$refs eq $reflist} return
 
     # Update the contents of $showrefstop.list according to the
-- 
2.7.4

```

## Michael Rappazzo, 2016-03-18 18:51

Subject: [PATCH v2 2/2] gitk: add an option to enable sorting the "Tags and heads" view by ref type
Message-ID: <1458327097-92126-3-git-send-email-rappazzo@gmail.com>
URL: https://gitlist.dev/e/1458327097-92126-3-git-send-email-rappazzo%40gmail.com
In-Reply-To: <1458327097-92126-1-git-send-email-rappazzo@gmail.com>

```
Signed-off-by: Michael Rappazzo <rappazzo@gmail.com>
---
 gitk | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/gitk b/gitk
index d6d3136..6d376e3 100755
--- a/gitk
+++ b/gitk
@@ -158,6 +158,7 @@ proc parseviewargs {n arglist} {
     global vdatemode vmergeonly vflags vdflags vrevs vfiltered vorigargs env
     global vinlinediff
     global worddiff git_version
+    global sort_refs_by_type
 
     set vdatemode($n) 0
     set vmergeonly($n) 0
@@ -170,6 +171,7 @@ proc parseviewargs {n arglist} {
     set allknown 1
     set filtered 0
     set i -1
+    set sort_refs_by_type "0"
     foreach arg $arglist {
 	incr i
 	if {$nextisval} {
@@ -261,6 +263,9 @@ proc parseviewargs {n arglist} {
 	    "--no-replace-objects" {
 		set env(GIT_NO_REPLACE_OBJECTS) "1"
 	    }
+	    "--sort-refs-by-type" {
+		set sort_refs_by_type "1"
+	    }
 	    "-*" {
 		# Other flag arguments including -<n>
 		if {[string is digit -strict [string range $arg 1 end]]} {
@@ -9929,7 +9934,7 @@ proc reflistfilter_change {n1 n2 op} {
 }
 
 proc refill_reflist {} {
-    global reflist reflistfilter showrefstop headids tagids otherrefids
+    global reflist reflistfilter showrefstop headids tagids otherrefids sort_refs_by_type
     global curview
 
     if {![info exists showrefstop] || ![winfo exists $showrefstop]} return
@@ -9998,6 +10003,10 @@ proc refill_reflist {} {
     }
     set otherrefs [lsort -index 0 $otherrefs]
     lappend refs {*}$localrefs {*}$locally_tracked_remote_refs {*}$remoterefs {*}$tagrefs {*}$otherrefs
+    if {$sort_refs_by_type ne "1"} {
+       set refs [lsort -index 0 $refs]
+    }
+
     if {$refs eq $reflist} return
 
     # Update the contents of $showrefstop.list according to the
-- 
2.7.4

```

## Paul Mackerras, 2016-03-19 06:53

Subject: Re: [PATCH v2 1/2] gitk: alter the ordering for the "Tags and heads" view
Message-ID: <20160319065347.GC27126@fergus.ozlabs.ibm.com>
URL: https://gitlist.dev/e/20160319065347.GC27126%40fergus.ozlabs.ibm.com
In-Reply-To: <1458327097-92126-2-git-send-email-rappazzo@gmail.com>

```
On Fri, Mar 18, 2016 at 02:51:36PM -0400, Michael Rappazzo wrote:
> In the "Tags and heads" view, the list of refs is globally sorted.
> Because of this, the list of local refs (heads) can be interrupted by the
> list of remote refs.  This change re-orders the view to be: local refs,
> remote refs tracked by local refs, remote refs, tags, and then other refs.

This seems like a nice idea, and could lead on to having a section that
can be opened and closed for each of these categories.  However, I
have some comments on the implementation:

> @@ -9933,35 +9933,71 @@ proc refill_reflist {} {
>      global curview
>  
>      if {![info exists showrefstop] || ![winfo exists $showrefstop]} return
> -    set refs {}
> +    set localrefs {}
> +    set remoterefs {}
> +    set locally_tracked_remote_refs {}
> +    set tagrefs {}
> +    set otherrefs {}
>      foreach n [array names headids] {
> -	if {[string match $reflistfilter $n]} {
> +	if {![string match "remotes/*" $n] && [string match $reflistfilter $n]} {
>  	    if {[commitinview $headids($n) $curview]} {
> -		lappend refs [list $n H]
> +		lappend localrefs [list $n H]
> +		catch {set remote_name [exec git config --get branch.$n.remote]}
> +		if {$remote_name ne ""} {
> +		    catch {set remote_ref [exec git config --get branch.$n.merge]}
> +		    set remote_ref [string map {"refs/heads/" ""} $remote_ref]
> +		    set locally_tracked_remote_ref "remotes/$remote_name/$remote_ref"
> +		    set upstream_exists ""
> +		    catch {set upstream_exists [exec git rev-parse --verify $locally_tracked_remote_ref]}
> +		    if {$upstream_exists ne ""} {
> +			if {[lsearch $locally_tracked_remote_refs [list $locally_tracked_remote_ref H]] < 0} {
> +			    lappend locally_tracked_remote_refs [list $locally_tracked_remote_ref H]
> +			}
> +		    }
> +		}

I'm worried about the number of external git invocations we're
potentially doing here, and how long that would take when you have a
lot of heads.  Can we cache the results perhaps?  Or is there a way to
do a single git command and get a list of tracking branches with their
remotes etc.?

Also, the default for lsearch is glob-style matching.  It's unlikely
that ref names would have any of *?[\ in them, but AFAIK it's not
impossible.  You probably want -exact in there.

Further, the kind of thing you are using lsearch for can often be done
more efficiently using an array (which becomes essentially a hash
table underneath).

Paul.

```

## Paul Mackerras, 2016-03-19 06:55

Subject: Re: [PATCH v2 2/2] gitk: add an option to enable sorting the "Tags and heads" view by ref type
Message-ID: <20160319065528.GD27126@fergus.ozlabs.ibm.com>
URL: https://gitlist.dev/e/20160319065528.GD27126%40fergus.ozlabs.ibm.com
In-Reply-To: <1458327097-92126-3-git-send-email-rappazzo@gmail.com>

```
On Fri, Mar 18, 2016 at 02:51:37PM -0400, Michael Rappazzo wrote:
> Signed-off-by: Michael Rappazzo <rappazzo@gmail.com>

This looks to me like the kind of thing that could have a checkbox in
the Preferences window and get saved on disk along with other
preferences, rather than only being available via a command-line
option.

Paul.

```
