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 {
10 if {$t eq
$name ||
[string match
$t $name]} {
17 proc all_tracking_branches
{} {
18 global tracking_branches
24 foreach spec
$tracking_branches {
25 set dst
[lindex $spec 0]
26 if {[string range
$dst end-1 end
] eq
{/*}} {
28 lappend cmd
[string range
$dst 0 end-2
]
35 set fd
[eval git_read for-each-ref
--format=%(refname
) $cmd]
36 while {[gets $fd n
] > 0} {
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
]
45 $src[string range
$n $len end
] \
54 return [lsort -index 0 -unique $all]
57 proc load_all_remotes
{} {
59 global all_remotes tracking_branches some_heads_tracking
62 set some_heads_tracking
0
63 set all_remotes
[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 \
76 foreach name
$all_remotes {
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
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]} {
92 if {![string equal
-length 5 refs
/ $dst]} {
95 if {[string equal
-length $rh_len $rh_str $dst]} {
96 set some_heads_tracking
1
98 lappend trck
[list $dst $name $src]
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
)}]} {
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]} {
121 if {![string equal
-length 5 refs
/ $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 add_fetch_entry
{r
} {
137 set remote_m .mbar.remote
138 set fetch_m
$remote_m.fetch
139 set prune_m
$remote_m.prune
140 set remove_m
$remote_m.remove
142 if {![catch {set a
$repo_config(remote.
$r.url
)}]} {
143 if {![catch {set a
$repo_config(remote.
$r.fetch
)}]} {
148 set fd
[open [gitdir remotes
$r] r
]
149 while {[gets $fd n
] >= 0} {
150 if {[regexp {^Pull
:[ \t]*([^
:]+):} $n]} {
160 make_sure_remote_submenues_exist
$remote_m
162 $fetch_m add command
\
164 -command [list fetch_from
$r]
165 $prune_m add command
\
167 -command [list prune_from
$r]
168 $remove_m add command
\
170 -command [list remove_remote
$r]
174 proc add_push_entry
{r
} {
176 set remote_m .mbar.remote
177 set push_m
$remote_m.push
179 if {![catch {set a
$repo_config(remote.
$r.url
)}]} {
180 if {![catch {set a
$repo_config(remote.
$r.push
)}]} {
185 set fd
[open [gitdir remotes
$r] r
]
186 while {[gets $fd n
] >= 0} {
187 if {[regexp {^Push
:[ \t]*([^
:]+):} $n]} {
197 if {![winfo exists
$push_m]} {
199 $remote_m insert
0 cascade
\
200 -label [mc
"Push to"] \
204 $push_m add command
\
206 -command [list push_to
$r]
210 proc make_sure_remote_submenues_exist
{remote_m
} {
211 set fetch_m
$remote_m.fetch
212 set prune_m
$remote_m.prune
213 set remove_m
$remote_m.remove
215 if {![winfo exists
$fetch_m]} {
217 $remote_m insert
0 cascade
\
218 -label [mc
"Remove Remote"] \
222 $remote_m insert
0 cascade
\
223 -label [mc
"Prune from"] \
227 $remote_m insert
0 cascade
\
228 -label [mc
"Fetch from"] \
233 proc update_all_remotes_menu_entry
{} {
236 if {[git-version
< 1.6.6]} { return }
239 foreach r
$all_remotes {
243 set remote_m .mbar.remote
244 set fetch_m
$remote_m.fetch
245 set prune_m
$remote_m.prune
246 if {$have_remote > 1} {
247 make_sure_remote_submenues_exist
$remote_m
248 if {[$fetch_m entrycget end
-label] ne
"All"} {
250 $fetch_m insert end separator
251 $fetch_m insert end command
\
253 -command fetch_from_all
255 $prune_m insert end separator
256 $prune_m insert end command
\
258 -command prune_from_all
261 if {[winfo exists
$fetch_m]} {
262 if {[$fetch_m entrycget end
-label] eq
"All"} {
264 delete_from_menu
$fetch_m end
265 delete_from_menu
$fetch_m end
267 delete_from_menu
$prune_m end
268 delete_from_menu
$prune_m end
274 proc populate_remotes_menu
{} {
277 foreach r
$all_remotes {
282 update_all_remotes_menu_entry
285 proc add_single_remote
{name location
} {
286 global all_remotes repo_config
287 lappend all_remotes
$name
289 git remote add
$name $location
291 # XXX: Better re-read the config so that we will never get out
292 # of sync with git remote implementation?
293 set repo_config
(remote.
$name.url
) $location
294 set repo_config
(remote.
$name.fetch
) "+refs/heads/*:refs/remotes/$name/*"
296 add_fetch_entry
$name
299 update_all_remotes_menu_entry
302 proc delete_from_menu
{menu name
} {
303 if {[winfo exists
$menu]} {
308 proc remove_remote
{name
} {
309 global all_remotes repo_config
314 # Missing values are ok
315 unset repo_config
(remote.
$name.url
)
316 unset repo_config
(remote.
$name.fetch
)
317 unset repo_config
(remote.
$name.push
)
320 set i
[lsearch -exact $all_remotes $name]
321 set all_remotes
[lreplace $all_remotes $i $i]
323 set remote_m .mbar.remote
324 delete_from_menu
$remote_m.fetch
$name
325 delete_from_menu
$remote_m.prune
$name
326 delete_from_menu
$remote_m.remove
$name
327 # Not all remotes are in the push menu
328 catch { delete_from_menu
$remote_m.push
$name }
330 update_all_remotes_menu_entry