Subject: [PATCH 3/6] Add a tree view to the local branches, remote branches and tags, where / is treated as a directory seperator.

From: Pierre Dumuid <[email-masked]>
Date: Thu, 15 Dec 2016 21:58:44 +1030

Signed-off-by: Pierre Dumuid <[email-masked]>
---
 gitk | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 117 insertions(+)

diff --git a/gitk b/gitk
index 36cba49..a894f1d 100755
--- a/gitk
+++ b/gitk
@@ -2089,6 +2089,10 @@ proc makewindow {} {
 	    {mc "Reread re&ferences" command rereadrefs}
 	    {mc "&List references" command showrefs -accelerator F2}
 	    {xx "" separator}
+	    {mc "List Local Branches"  command {show_tree_of_references_dialog "localBranches"}  -accelerator F6}
+	    {mc "List Remote Branches" command {show_tree_of_references_dialog "remoteBranches"} -accelerator F7}
+	    {mc "List Tags"            command {show_tree_of_references_dialog "tags"}           -accelerator F8}
+	    {xx "" separator}
 	    {mc "Start git &gui" command {exec git gui &}}
 	    {xx "" separator}
 	    {mc "&Quit" command doquit -accelerator Meta1-Q}
@@ -2601,6 +2605,9 @@ proc makewindow {} {
     bind . <F5> updatecommits
     bindmodfunctionkey Shift 5 reloadcommits
     bind . <F2> showrefs
+    bind . <F6> {show_tree_of_references_dialog "localBranches"}
+    bind . <F7> {show_tree_of_references_dialog "remoteBranches"}
+    bind . <F8> {show_tree_of_references_dialog "tags"}
     bindmodfunctionkey Shift 4 {newview 0}
     bind . <F4> edit_or_newview
     bind . <$M1B-q> doquit
@@ -10146,6 +10153,116 @@ proc rmbranch {} {
     run refill_reflist
 }
 
+# Display a tree view of local branches, remote branches, and tags according to view_type.
+#
+# @param string view_type
+#    Must be one of "localBranches", "remoteBranches", or "tags".
+#
+proc show_tree_of_references_dialog {view_type} {
+    global NS
+    global treefilelist
+    global headids tagids
+
+    switch -- $view_type {
+	"localBranches" {
+	    set dialogName "Local Branches"
+	    set top .show_tree_of_local_branches
+	    set listOfReferences [lsort [array names headids -regexp {^(?!remotes/)} ]]
+	    set truncateFrom 0
+	}
+	"remoteBranches" {
+	    set dialogName "Remote Branches"
+	    set top .show_tree_of_remote_branches
+	    set listOfReferences [lsort [array names headids -regexp {^remotes/} ]]
+	    set truncateFrom 8
+	}
+	"tags" {
+	    set dialogName "Tags"
+	    set top .show_tree_of_tags
+	    set listOfReferences [lsort [array names tagids]]
+	    set truncateFrom 0
+	}
+    }
+
+    if {[winfo exists $top]} {
+	raise $top
+	return
+    }
+
+    ttk_toplevel $top
+    wm title $top [mc "$dialogName: %s" [file tail [pwd]]]
+    wm geometry $top "600x900"
+
+    make_transient $top .
+
+    ## See http://www.tkdocs.com/tutorial/tree.html
+    ttk::treeview $top.referenceList -xscrollcommand "$top.horizontalScrollBar set" -yscrollcommand "$top.verticalScrollBar set"
+
+    # Populate the dialog
+    foreach reference $listOfReferences {
+	# The display name omits some leading characters (such as "remotes/")
+	set referenceDisplayName [string range $reference $truncateFrom end]
+
+	# Split the branch/tag by slashes, and incrementally ensure that each leaf in the treeview exists..
+	# otherwise add it.
+	set treeLeaves [split $referenceDisplayName "/"]
+	for {set i 0} {$i < [llength $treeLeaves]} {} {
+	    set leafReferenceId [join [lrange $treeLeaves 0 $i] "/"]
+	    if {![$top.referenceList exists $leafReferenceId]} {
+		if {$i > 0} {
+		    set parentLeafId [join [lrange $treeLeaves 0 $i-1] "/"]
+		} else {
+		    set parentLeafId {}
+		}
+		$top.referenceList insert $parentLeafId end -id $leafReferenceId -text [lindex $treeLeaves $i]
+	    }
+	    incr i
+	}
+    }
+
+    ${NS}::scrollbar $top.verticalScrollBar   -command "$top.referenceList yview" -orient vertical
+    ${NS}::scrollbar $top.horizontalScrollBar -command "$top.referenceList xview" -orient horizontal
+
+    grid $top.referenceList $top.verticalScrollBar -sticky nsew
+    grid $top.horizontalScrollBar x -sticky ew
+
+    bind $top <Key-Escape> [list destroy $top]
+
+    bind $top.referenceList <<TreeviewSelect>> {callback_tree_of_references_item_selected %W; break}
+
+    grid columnconfigure $top 0 -weight 1
+    grid rowconfigure    $top 0 -weight 1
+}
+
+# Call back for selecting a branch / tag in the tree of references
+#
+# @param w
+#
+proc callback_tree_of_references_item_selected {w} {
+    global headids tagids
+
+    set itemId [$w focus]
+    switch -- $w {
+	".show_tree_of_local_branches.referenceList" {
+	    if {[info exists headids($itemId)]} {
+		selbyid $headids($itemId)
+	    }
+	}
+	".show_tree_of_remote_branches.referenceList" {
+	    set itemId "remotes/$itemId"
+	    if {[info exists headids($itemId)]} {
+		selbyid $headids($itemId)
+	    }
+	}
+	".show_tree_of_tags.referenceList" {
+	    if {[info exists tagids($itemId)]} {
+		selbyid $tagids($itemId)
+	    }
+	}
+    }
+}
+
+
 # Display a list of tags and heads
 proc showrefs {} {
     global showrefstop bgcolor fgcolor selectbgcolor NS
-- 
2.10.2