t6000lib: tr portability fix
[git/peff.git] / git-gui / lib / remote.tcl
blob0e86ddac0981fbb575a7dd5294ddaed29f7c3917
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 [eval git_read for-each-ref --format=%(refname) $cmd]
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
60 global remote_url
62 set some_heads_tracking 0
63 set all_remotes [list]
64 set trck [list]
66 set rh_str refs/heads/
67 set rh_len [string length $rh_str]
68 set rm_dir [gitdir remotes]
69 if {[file isdirectory $rm_dir]} {
70 set all_remotes [glob \
71 -types f \
72 -tails \
73 -nocomplain \
74 -directory $rm_dir *]
76 foreach name $all_remotes {
77 catch {
78 set fd [open [file join $rm_dir $name] r]
79 while {[gets $fd line] >= 0} {
80 if {[regexp {^URL:[ ]*(.+)$} $line line url]} {
81 set remote_url($name) $url
82 continue
84 if {![regexp {^Pull:[ ]*([^:]+):(.+)$} \
85 $line line src dst]} continue
86 if {[string index $src 0] eq {+}} {
87 set src [string range $src 1 end]
89 if {![string equal -length 5 refs/ $src]} {
90 set src $rh_str$src
92 if {![string equal -length 5 refs/ $dst]} {
93 set dst $rh_str$dst
95 if {[string equal -length $rh_len $rh_str $dst]} {
96 set some_heads_tracking 1
98 lappend trck [list $dst $name $src]
100 close $fd
105 foreach line [array names repo_config remote.*.url] {
106 if {![regexp ^remote\.(.*)\.url\$ $line line name]} continue
107 lappend all_remotes $name
108 set remote_url($name) $repo_config(remote.$name.url)
110 if {[catch {set fl $repo_config(remote.$name.fetch)}]} {
111 set fl {}
113 foreach line $fl {
114 if {![regexp {^([^:]+):(.+)$} $line line src dst]} continue
115 if {[string index $src 0] eq {+}} {
116 set src [string range $src 1 end]
118 if {![string equal -length 5 refs/ $src]} {
119 set src $rh_str$src
121 if {![string equal -length 5 refs/ $dst]} {
122 set dst $rh_str$dst
124 if {[string equal -length $rh_len $rh_str $dst]} {
125 set some_heads_tracking 1
127 lappend trck [list $dst $name $src]
131 set tracking_branches [lsort -index 0 -unique $trck]
132 set all_remotes [lsort -unique $all_remotes]
135 proc populate_fetch_menu {} {
136 global all_remotes repo_config
138 set remote_m .mbar.remote
139 set fetch_m $remote_m.fetch
140 set prune_m $remote_m.prune
142 foreach r $all_remotes {
143 set enable 0
144 if {![catch {set a $repo_config(remote.$r.url)}]} {
145 if {![catch {set a $repo_config(remote.$r.fetch)}]} {
146 set enable 1
148 } else {
149 catch {
150 set fd [open [gitdir remotes $r] r]
151 while {[gets $fd n] >= 0} {
152 if {[regexp {^Pull:[ \t]*([^:]+):} $n]} {
153 set enable 1
154 break
157 close $fd
161 if {$enable} {
162 if {![winfo exists $fetch_m]} {
163 menu $prune_m
164 $remote_m insert 0 cascade \
165 -label [mc "Prune from"] \
166 -menu $prune_m
168 menu $fetch_m
169 $remote_m insert 0 cascade \
170 -label [mc "Fetch from"] \
171 -menu $fetch_m
174 $fetch_m add command \
175 -label $r \
176 -command [list fetch_from $r]
177 $prune_m add command \
178 -label $r \
179 -command [list prune_from $r]
184 proc populate_push_menu {} {
185 global all_remotes repo_config
187 set remote_m .mbar.remote
188 set push_m $remote_m.push
190 foreach r $all_remotes {
191 set enable 0
192 if {![catch {set a $repo_config(remote.$r.url)}]} {
193 if {![catch {set a $repo_config(remote.$r.push)}]} {
194 set enable 1
196 } else {
197 catch {
198 set fd [open [gitdir remotes $r] r]
199 while {[gets $fd n] >= 0} {
200 if {[regexp {^Push:[ \t]*([^:]+):} $n]} {
201 set enable 1
202 break
205 close $fd
209 if {$enable} {
210 if {![winfo exists $push_m]} {
211 menu $push_m
212 $remote_m insert 0 cascade \
213 -label [mc "Push to"] \
214 -menu $push_m
217 $push_m add command \
218 -label $r \
219 -command [list push_to $r]