git-gui: Default selection to first matching ref
[git/jrn.git] / lib / remote.tcl
blobfabec05fff3edb1e1709b2557ec0ede1470ec7ae
1 # git-gui remote management
2 # Copyright (C) 2006, 2007 Shawn Pearce
4 set some_heads_tracking 0; # assume not
6 proc is_tracking_branch {name} {
7 global tracking_branches
8 foreach spec $tracking_branches {
9 set t [lindex $spec 0]
10 if {$t eq $name || [string match $t $name]} {
11 return 1
14 return 0
17 proc all_tracking_branches {} {
18 global tracking_branches
20 set all [list]
21 set pat [list]
22 set cmd [list]
24 foreach spec $tracking_branches {
25 set dst [lindex $spec 0]
26 if {[string range $dst end-1 end] eq {/*}} {
27 lappend pat $spec
28 lappend cmd [string range $dst 0 end-2]
29 } else {
30 lappend all $spec
34 if {$pat ne {}} {
35 set fd [open "| git for-each-ref --format=%(refname) $cmd" r]
36 while {[gets $fd n] > 0} {
37 foreach spec $pat {
38 set dst [string range [lindex $spec 0] 0 end-2]
39 set len [string length $dst]
40 if {[string equal -length $len $dst $n]} {
41 set src [string range [lindex $spec 2] 0 end-2]
42 set spec [list \
43 $n \
44 [lindex $spec 1] \
45 $src[string range $n $len end] \
47 lappend all $spec
51 close $fd
54 return [lsort -index 0 -unique $all]
57 proc load_all_remotes {} {
58 global repo_config
59 global all_remotes tracking_branches some_heads_tracking
61 set some_heads_tracking 0
62 set all_remotes [list]
63 set trck [list]
65 set rh_str refs/heads/
66 set rh_len [string length $rh_str]
67 set rm_dir [gitdir remotes]
68 if {[file isdirectory $rm_dir]} {
69 set all_remotes [glob \
70 -types f \
71 -tails \
72 -nocomplain \
73 -directory $rm_dir *]
75 foreach name $all_remotes {
76 catch {
77 set fd [open [file join $rm_dir $name] r]
78 while {[gets $fd line] >= 0} {
79 if {![regexp {^Pull:[ ]*([^:]+):(.+)$} \
80 $line line src dst]} continue
81 if {[string index $src 0] eq {+}} {
82 set src [string range $src 1 end]
84 if {![string equal -length 5 refs/ $src]} {
85 set src $rh_str$src
87 if {![string equal -length 5 refs/ $dst]} {
88 set dst $rh_str$dst
90 if {[string equal -length $rh_len $rh_str $dst]} {
91 set some_heads_tracking 1
93 lappend trck [list $dst $name $src]
95 close $fd
100 foreach line [array names repo_config remote.*.url] {
101 if {![regexp ^remote\.(.*)\.url\$ $line line name]} continue
102 lappend all_remotes $name
104 if {[catch {set fl $repo_config(remote.$name.fetch)}]} {
105 set fl {}
107 foreach line $fl {
108 if {![regexp {^([^:]+):(.+)$} $line line src dst]} continue
109 if {[string index $src 0] eq {+}} {
110 set src [string range $src 1 end]
112 if {![string equal -length 5 refs/ $src]} {
113 set src $rh_str$src
115 if {![string equal -length 5 refs/ $dst]} {
116 set dst $rh_str$dst
118 if {[string equal -length $rh_len $rh_str $dst]} {
119 set some_heads_tracking 1
121 lappend trck [list $dst $name $src]
125 set tracking_branches [lsort -index 0 -unique $trck]
126 set all_remotes [lsort -unique $all_remotes]
129 proc populate_fetch_menu {} {
130 global all_remotes repo_config
132 set m .mbar.fetch
133 set prune_list [list]
134 foreach r $all_remotes {
135 set enable 0
136 if {![catch {set a $repo_config(remote.$r.url)}]} {
137 if {![catch {set a $repo_config(remote.$r.fetch)}]} {
138 set enable 1
140 } else {
141 catch {
142 set fd [open [gitdir remotes $r] r]
143 while {[gets $fd n] >= 0} {
144 if {[regexp {^Pull:[ \t]*([^:]+):} $n]} {
145 set enable 1
146 break
149 close $fd
153 if {$enable} {
154 lappend prune_list $r
155 $m add command \
156 -label "Fetch from $r..." \
157 -command [list fetch_from $r]
161 if {$prune_list ne {}} {
162 $m add separator
164 foreach r $prune_list {
165 $m add command \
166 -label "Prune from $r..." \
167 -command [list prune_from $r]
171 proc populate_push_menu {} {
172 global all_remotes repo_config
174 set m .mbar.push
175 set fast_count 0
176 foreach r $all_remotes {
177 set enable 0
178 if {![catch {set a $repo_config(remote.$r.url)}]} {
179 if {![catch {set a $repo_config(remote.$r.push)}]} {
180 set enable 1
182 } else {
183 catch {
184 set fd [open [gitdir remotes $r] r]
185 while {[gets $fd n] >= 0} {
186 if {[regexp {^Push:[ \t]*([^:]+):} $n]} {
187 set enable 1
188 break
191 close $fd
195 if {$enable} {
196 if {!$fast_count} {
197 $m add separator
199 $m add command \
200 -label "Push to $r..." \
201 -command [list push_to $r]
202 incr fast_count