@matheusbsbs / matheus-bagliano / commits / 6f2a3fc812
Fork 0

git-gui: Optimize for newstyle refs/remotes layout

Most people using Git 1.5.x and later are using the newer style of remotes layout where all of their tracking branches are in refs/remotes and refs/heads contains only the user's own local branches. In such a situation we can avoid calling is_tracking_branch for each head we are considering because we know that all of the heads must be local branches if no fetch option or Pull: line maps a branch into that namespace. If however any remote maps a remote branch into a local tracking branch that resides in refs/heads we do exactly what we did before, which requires scanning through all fetch lines in case any patterns are matched. I also switched some regexp/regsub calls to string match as this can be a faster operation for prefix matching. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Shawn O. Pearce committed Jul 4, 2007 at 00:15 UTC 6f2a3fc812623bc82fb4997adb5a9a547b5dae1a
2 files changed +40 -21
lib/branch.tcl
+7 -4
@@ -3,13 +3,16 @@
3
4 proc load_all_heads {} {
5 global all_heads
6 + global some_heads_tracking
7
8 + set rh refs/heads
9 + set rh_len [expr {[string length $rh] + 1}]
10 set all_heads [list]
8 - set fd [open "| git for-each-ref --format=%(refname) refs/heads" r]
11 + set fd [open "| git for-each-ref --format=%(refname) $rh" r]
12 while {[gets $fd line] > 0} {
10 - if {[is_tracking_branch $line]} continue
11 - if {![regsub ^refs/heads/ $line {} name]} continue
12 - lappend all_heads $name
13 + if {!$some_heads_tracking || ![is_tracking_branch $line]} {
14 + lappend all_heads [string range $line $rh_len end]
15 + }
16 }
17 close $fd
18
lib/remote.tcl
+33 -17
@@ -1,14 +1,13 @@
1 # git-gui remote management
2 # Copyright (C) 2006, 2007 Shawn Pearce
3
4 +set some_heads_tracking 0; # assume not
5 +
6 proc is_tracking_branch {name} {
7 global tracking_branches
6 -
7 - if {![catch {set info $tracking_branches($name)}]} {
8 - return 1
9 - }
10 - foreach t [array names tracking_branches] {
11 - if {[string match {*/\*} $t] && [string match $t $name]} {
8 + foreach spec $tracking_branches {
9 + set t [lindex $spec 0]
10 + if {$t eq $name || [string match $t $name]} {
11 return 1
12 }
13 }
@@ -20,9 +19,10 @@ proc all_tracking_branches {} {
19
20 set all_trackings {}
21 set cmd {}
23 - foreach name [array names tracking_branches] {
24 - if {[regsub {/\*$} $name {} name]} {
25 - lappend cmd $name
22 + foreach spec $tracking_branches {
23 + set name [lindex $spec 0]
24 + if {[string range $name end-1 end] eq {/*}} {
25 + lappend cmd [string range $name 0 end-2]
26 } else {
27 regsub ^refs/(heads|remotes)/ $name {} name
28 lappend all_trackings $name
@@ -43,11 +43,14 @@ proc all_tracking_branches {} {
43
44 proc load_all_remotes {} {
45 global repo_config
46 - global all_remotes tracking_branches
46 + global all_remotes tracking_branches some_heads_tracking
47
48 + set some_heads_tracking 0
49 set all_remotes [list]
49 - array unset tracking_branches
50 + set trck [list]
51
52 + set rh_str refs/heads/
53 + set rh_len [string length $rh_str]
54 set rm_dir [gitdir remotes]
55 if {[file isdirectory $rm_dir]} {
56 set all_remotes [glob \
@@ -62,10 +65,16 @@ proc load_all_remotes {} {
65 while {[gets $fd line] >= 0} {
66 if {![regexp {^Pull:[ ]*([^:]+):(.+)$} \
67 $line line src dst]} continue
65 - if {![regexp ^refs/ $dst]} {
66 - set dst "refs/heads/$dst"
68 + if {![string equal -length 5 refs/ $src]} {
69 + set src $rh_str$src
70 + }
71 + if {![string equal -length 5 refs/ $dst]} {
72 + set dst $rh_str$dst
73 }
68 - set tracking_branches($dst) [list $name $src]
74 + if {[string equal -length $rh_len $rh_str $dst]} {
75 + set some_heads_tracking 1
76 + }
77 + lappend trck [list $dst $name $src]
78 }
79 close $fd
80 }
@@ -81,13 +90,20 @@ proc load_all_remotes {} {
90 }
91 foreach line $fl {
92 if {![regexp {^([^:]+):(.+)$} $line line src dst]} continue
84 - if {![regexp ^refs/ $dst]} {
85 - set dst "refs/heads/$dst"
93 + if {![string equal -length 5 refs/ $src]} {
94 + set src $rh_str$src
95 + }
96 + if {![string equal -length 5 refs/ $dst]} {
97 + set dst $rh_str$dst
98 + }
99 + if {[string equal -length $rh_len $rh_str $dst]} {
100 + set some_heads_tracking 1
101 }
87 - set tracking_branches($dst) [list $name $src]
102 + lappend trck [list $dst $name $src]
103 }
104 }
105
106 + set tracking_branches [lsort -index 0 -unique $trck]
107 set all_remotes [lsort -unique $all_remotes]
108 }
109