]>
Commit | Line | Data |
---|---|---|
e194cd1e JH |
1 | #!/usr/bin/perl -w |
2 | ||
3 | use Git; | |
4 | my $git = Git->repository(); | |
5 | ||
6 | sub add_remote_config { | |
7 | my ($hash, $name, $what, $value) = @_; | |
8 | if ($what eq 'url') { | |
9 | if (exists $hash->{$name}{'URL'}) { | |
10 | print STDERR "Warning: more than one remote.$name.url\n"; | |
11 | } | |
12 | $hash->{$name}{'URL'} = $value; | |
13 | } | |
14 | elsif ($what eq 'fetch') { | |
15 | $hash->{$name}{'FETCH'} ||= []; | |
16 | push @{$hash->{$name}{'FETCH'}}, $value; | |
17 | } | |
8bf0e3d1 JS |
18 | elsif ($what eq 'push') { |
19 | $hash->{$name}{'PUSH'} ||= []; | |
20 | push @{$hash->{$name}{'PUSH'}}, $value; | |
21 | } | |
e194cd1e JH |
22 | if (!exists $hash->{$name}{'SOURCE'}) { |
23 | $hash->{$name}{'SOURCE'} = 'config'; | |
24 | } | |
25 | } | |
26 | ||
27 | sub add_remote_remotes { | |
28 | my ($hash, $file, $name) = @_; | |
29 | ||
30 | if (exists $hash->{$name}) { | |
31 | $hash->{$name}{'WARNING'} = 'ignored due to config'; | |
32 | return; | |
33 | } | |
34 | ||
35 | my $fh; | |
36 | if (!open($fh, '<', $file)) { | |
37 | print STDERR "Warning: cannot open $file\n"; | |
38 | return; | |
39 | } | |
40 | my $it = { 'SOURCE' => 'remotes' }; | |
41 | $hash->{$name} = $it; | |
42 | while (<$fh>) { | |
43 | chomp; | |
44 | if (/^URL:\s*(.*)$/) { | |
45 | # Having more than one is Ok -- it is used for push. | |
46 | if (! exists $it->{'URL'}) { | |
47 | $it->{'URL'} = $1; | |
48 | } | |
49 | } | |
50 | elsif (/^Push:\s*(.*)$/) { | |
8bf0e3d1 JS |
51 | $it->{'PUSH'} ||= []; |
52 | push @{$it->{'PUSH'}}, $1; | |
e194cd1e JH |
53 | } |
54 | elsif (/^Pull:\s*(.*)$/) { | |
55 | $it->{'FETCH'} ||= []; | |
56 | push @{$it->{'FETCH'}}, $1; | |
57 | } | |
58 | elsif (/^\#/) { | |
59 | ; # ignore | |
60 | } | |
61 | else { | |
62 | print STDERR "Warning: funny line in $file: $_\n"; | |
63 | } | |
64 | } | |
65 | close($fh); | |
66 | } | |
67 | ||
68 | sub list_remote { | |
69 | my ($git) = @_; | |
70 | my %seen = (); | |
71 | my @remotes = eval { | |
e0d10e1c | 72 | $git->command(qw(config --get-regexp), '^remote\.'); |
e194cd1e JH |
73 | }; |
74 | for (@remotes) { | |
b5a40a57 | 75 | if (/^remote\.(\S+?)\.([^.\s]+)\s+(.*)$/) { |
e194cd1e JH |
76 | add_remote_config(\%seen, $1, $2, $3); |
77 | } | |
78 | } | |
79 | ||
80 | my $dir = $git->repo_path() . "/remotes"; | |
81 | if (opendir(my $dh, $dir)) { | |
82 | local $_; | |
83 | while ($_ = readdir($dh)) { | |
84 | chomp; | |
85 | next if (! -f "$dir/$_" || ! -r _); | |
86 | add_remote_remotes(\%seen, "$dir/$_", $_); | |
87 | } | |
88 | } | |
89 | ||
90 | return \%seen; | |
91 | } | |
92 | ||
93 | sub add_branch_config { | |
94 | my ($hash, $name, $what, $value) = @_; | |
95 | if ($what eq 'remote') { | |
96 | if (exists $hash->{$name}{'REMOTE'}) { | |
97 | print STDERR "Warning: more than one branch.$name.remote\n"; | |
98 | } | |
99 | $hash->{$name}{'REMOTE'} = $value; | |
100 | } | |
101 | elsif ($what eq 'merge') { | |
102 | $hash->{$name}{'MERGE'} ||= []; | |
103 | push @{$hash->{$name}{'MERGE'}}, $value; | |
104 | } | |
105 | } | |
106 | ||
107 | sub list_branch { | |
108 | my ($git) = @_; | |
109 | my %seen = (); | |
110 | my @branches = eval { | |
e0d10e1c | 111 | $git->command(qw(config --get-regexp), '^branch\.'); |
e194cd1e JH |
112 | }; |
113 | for (@branches) { | |
114 | if (/^branch\.([^.]*)\.(\S*)\s+(.*)$/) { | |
115 | add_branch_config(\%seen, $1, $2, $3); | |
116 | } | |
117 | } | |
118 | ||
119 | return \%seen; | |
120 | } | |
121 | ||
122 | my $remote = list_remote($git); | |
123 | my $branch = list_branch($git); | |
124 | ||
125 | sub update_ls_remote { | |
126 | my ($harder, $info) = @_; | |
127 | ||
128 | return if (($harder == 0) || | |
129 | (($harder == 1) && exists $info->{'LS_REMOTE'})); | |
130 | ||
131 | my @ref = map { | |
132 | s|^[0-9a-f]{40}\s+refs/heads/||; | |
133 | $_; | |
134 | } $git->command(qw(ls-remote --heads), $info->{'URL'}); | |
135 | $info->{'LS_REMOTE'} = \@ref; | |
136 | } | |
137 | ||
7a8c9ec1 | 138 | sub list_wildcard_mapping { |
e194cd1e JH |
139 | my ($forced, $ours, $ls) = @_; |
140 | my %refs; | |
141 | for (@$ls) { | |
142 | $refs{$_} = 01; # bit #0 to say "they have" | |
143 | } | |
144 | for ($git->command('for-each-ref', "refs/remotes/$ours")) { | |
145 | chomp; | |
146 | next unless (s|^[0-9a-f]{40}\s[a-z]+\srefs/remotes/$ours/||); | |
147 | next if ($_ eq 'HEAD'); | |
148 | $refs{$_} ||= 0; | |
149 | $refs{$_} |= 02; # bit #1 to say "we have" | |
150 | } | |
151 | my (@new, @stale, @tracked); | |
152 | for (sort keys %refs) { | |
153 | my $have = $refs{$_}; | |
154 | if ($have == 1) { | |
155 | push @new, $_; | |
156 | } | |
157 | elsif ($have == 2) { | |
158 | push @stale, $_; | |
159 | } | |
160 | elsif ($have == 3) { | |
161 | push @tracked, $_; | |
162 | } | |
163 | } | |
7a8c9ec1 | 164 | return \@new, \@stale, \@tracked; |
e194cd1e JH |
165 | } |
166 | ||
7a8c9ec1 | 167 | sub list_mapping { |
e194cd1e JH |
168 | my ($name, $info) = @_; |
169 | my $fetch = $info->{'FETCH'}; | |
170 | my $ls = $info->{'LS_REMOTE'}; | |
7a8c9ec1 | 171 | my (@new, @stale, @tracked); |
e194cd1e JH |
172 | |
173 | for (@$fetch) { | |
174 | next unless (/(\+)?([^:]+):(.*)/); | |
175 | my ($forced, $theirs, $ours) = ($1, $2, $3); | |
176 | if ($theirs eq 'refs/heads/*' && | |
177 | $ours =~ /^refs\/remotes\/(.*)\/\*$/) { | |
178 | # wildcard mapping | |
7a8c9ec1 SP |
179 | my ($w_new, $w_stale, $w_tracked) |
180 | = list_wildcard_mapping($forced, $1, $ls); | |
181 | push @new, @$w_new; | |
182 | push @stale, @$w_stale; | |
183 | push @tracked, @$w_tracked; | |
e194cd1e JH |
184 | } |
185 | elsif ($theirs =~ /\*/ || $ours =~ /\*/) { | |
186 | print STDERR "Warning: unrecognized mapping in remotes.$name.fetch: $_\n"; | |
187 | } | |
188 | elsif ($theirs =~ s|^refs/heads/||) { | |
189 | if (!grep { $_ eq $theirs } @$ls) { | |
190 | push @stale, $theirs; | |
191 | } | |
192 | elsif ($ours ne '') { | |
193 | push @tracked, $theirs; | |
194 | } | |
195 | } | |
196 | } | |
7a8c9ec1 SP |
197 | return \@new, \@stale, \@tracked; |
198 | } | |
199 | ||
200 | sub show_mapping { | |
201 | my ($name, $info) = @_; | |
202 | my ($new, $stale, $tracked) = list_mapping($name, $info); | |
203 | if (@$new) { | |
204 | print " New remote branches (next fetch will store in remotes/$name)\n"; | |
205 | print " @$new\n"; | |
206 | } | |
207 | if (@$stale) { | |
859607df | 208 | print " Stale tracking branches in remotes/$name (use 'git remote prune')\n"; |
7a8c9ec1 | 209 | print " @$stale\n"; |
e194cd1e | 210 | } |
7a8c9ec1 | 211 | if (@$tracked) { |
e194cd1e | 212 | print " Tracked remote branches\n"; |
7a8c9ec1 | 213 | print " @$tracked\n"; |
e194cd1e JH |
214 | } |
215 | } | |
216 | ||
859607df SP |
217 | sub prune_remote { |
218 | my ($name, $ls_remote) = @_; | |
219 | if (!exists $remote->{$name}) { | |
220 | print STDERR "No such remote $name\n"; | |
f4bb20cc | 221 | return 1; |
859607df SP |
222 | } |
223 | my $info = $remote->{$name}; | |
224 | update_ls_remote($ls_remote, $info); | |
225 | ||
226 | my ($new, $stale, $tracked) = list_mapping($name, $info); | |
227 | my $prefix = "refs/remotes/$name"; | |
228 | foreach my $to_prune (@$stale) { | |
229 | my @v = $git->command(qw(rev-parse --verify), "$prefix/$to_prune"); | |
230 | $git->command(qw(update-ref -d), "$prefix/$to_prune", $v[0]); | |
231 | } | |
f4bb20cc | 232 | return 0; |
859607df SP |
233 | } |
234 | ||
e194cd1e JH |
235 | sub show_remote { |
236 | my ($name, $ls_remote) = @_; | |
237 | if (!exists $remote->{$name}) { | |
238 | print STDERR "No such remote $name\n"; | |
f4bb20cc | 239 | return 1; |
e194cd1e JH |
240 | } |
241 | my $info = $remote->{$name}; | |
242 | update_ls_remote($ls_remote, $info); | |
243 | ||
244 | print "* remote $name\n"; | |
245 | print " URL: $info->{'URL'}\n"; | |
246 | for my $branchname (sort keys %$branch) { | |
247 | next if ($branch->{$branchname}{'REMOTE'} ne $name); | |
248 | my @merged = map { | |
249 | s|^refs/heads/||; | |
250 | $_; | |
251 | } split(' ',"@{$branch->{$branchname}{'MERGE'}}"); | |
252 | next unless (@merged); | |
253 | print " Remote branch(es) merged with 'git pull' while on branch $branchname\n"; | |
254 | print " @merged\n"; | |
255 | } | |
256 | if ($info->{'LS_REMOTE'}) { | |
257 | show_mapping($name, $info); | |
258 | } | |
8bf0e3d1 JS |
259 | if ($info->{'PUSH'}) { |
260 | my @pushed = map { | |
261 | s|^refs/heads/||; | |
6718f1f0 | 262 | s|^\+refs/heads/|+|; |
8bf0e3d1 JS |
263 | s|:refs/heads/|:|; |
264 | $_; | |
265 | } @{$info->{'PUSH'}}; | |
266 | print " Local branch(es) pushed with 'git push'\n"; | |
267 | print " @pushed\n"; | |
268 | } | |
f4bb20cc | 269 | return 0; |
e194cd1e JH |
270 | } |
271 | ||
272 | sub add_remote { | |
b6f5da1e | 273 | my ($name, $url, $opts) = @_; |
e194cd1e JH |
274 | if (exists $remote->{$name}) { |
275 | print STDERR "remote $name already exists.\n"; | |
276 | exit(1); | |
277 | } | |
e0d10e1c | 278 | $git->command('config', "remote.$name.url", $url); |
b6f5da1e JH |
279 | my $track = $opts->{'track'} || ["*"]; |
280 | ||
281 | for (@$track) { | |
282 | $git->command('config', '--add', "remote.$name.fetch", | |
38944390 JS |
283 | $opts->{'mirror'} ? |
284 | "+refs/$_:refs/$_" : | |
285 | "+refs/heads/$_:refs/remotes/$name/$_"); | |
b6f5da1e JH |
286 | } |
287 | if ($opts->{'fetch'}) { | |
288 | $git->command('fetch', $name); | |
289 | } | |
290 | if (exists $opts->{'master'}) { | |
291 | $git->command('symbolic-ref', "refs/remotes/$name/HEAD", | |
292 | "refs/remotes/$name/$opts->{'master'}"); | |
293 | } | |
294 | } | |
295 | ||
1918278e TT |
296 | sub update_remote { |
297 | my ($name) = @_; | |
298 | ||
299 | my $conf = $git->config("remotes." . $name); | |
300 | if (defined($conf)) { | |
301 | @remotes = split(' ', $conf); | |
302 | } elsif ($name eq 'default') { | |
303 | undef @remotes; | |
304 | for (sort keys %$remote) { | |
35c49eea | 305 | my $do_fetch = $git->config_bool("remote." . $_ . |
1918278e | 306 | ".skipDefaultUpdate"); |
35c49eea | 307 | unless ($do_fetch) { |
1918278e TT |
308 | push @remotes, $_; |
309 | } | |
310 | } | |
311 | } else { | |
312 | print STDERR "Remote group $name does not exists.\n"; | |
313 | exit(1); | |
314 | } | |
315 | for (@remotes) { | |
316 | print "Updating $_\n"; | |
317 | $git->command('fetch', "$_"); | |
318 | } | |
319 | } | |
320 | ||
61136044 | 321 | sub rm_remote { |
683b5679 | 322 | my ($name) = @_; |
61136044 JB |
323 | if (!exists $remote->{$name}) { |
324 | print STDERR "No such remote $name\n"; | |
325 | return; | |
326 | } | |
327 | ||
328 | $git->command('config', '--remove-section', "remote.$name"); | |
329 | ||
330 | eval { | |
331 | my @trackers = $git->command('config', '--get-regexp', | |
332 | 'branch.*.remote', $name); | |
333 | for (@trackers) { | |
334 | /^branch\.(.*)?\.remote/; | |
335 | $git->config('--unset', "branch.$1.remote"); | |
336 | $git->config('--unset', "branch.$1.merge"); | |
337 | } | |
338 | }; | |
339 | ||
340 | ||
683b5679 | 341 | my @refs = $git->command('for-each-ref', |
61136044 JB |
342 | '--format=%(refname) %(objectname)', "refs/remotes/$name"); |
343 | for (@refs) { | |
344 | ($ref, $object) = split; | |
345 | $git->command(qw(update-ref -d), $ref, $object); | |
346 | } | |
347 | } | |
348 | ||
b6f5da1e JH |
349 | sub add_usage { |
350 | print STDERR "Usage: git remote add [-f] [-t track]* [-m master] <name> <url>\n"; | |
351 | exit(1); | |
e194cd1e JH |
352 | } |
353 | ||
09ff69bb AR |
354 | local $VERBOSE = 0; |
355 | @ARGV = grep { | |
356 | if ($_ eq '-v' or $_ eq '--verbose') { | |
357 | $VERBOSE=1; | |
358 | 0 | |
359 | } else { | |
360 | 1 | |
361 | } | |
362 | } @ARGV; | |
363 | ||
e194cd1e JH |
364 | if (!@ARGV) { |
365 | for (sort keys %$remote) { | |
09ff69bb AR |
366 | print "$_"; |
367 | print "\t$remote->{$_}->{URL}" if $VERBOSE; | |
368 | print "\n"; | |
e194cd1e JH |
369 | } |
370 | } | |
371 | elsif ($ARGV[0] eq 'show') { | |
372 | my $ls_remote = 1; | |
373 | my $i; | |
374 | for ($i = 1; $i < @ARGV; $i++) { | |
375 | if ($ARGV[$i] eq '-n') { | |
376 | $ls_remote = 0; | |
377 | } | |
378 | else { | |
379 | last; | |
380 | } | |
381 | } | |
382 | if ($i >= @ARGV) { | |
383 | print STDERR "Usage: git remote show <remote>\n"; | |
384 | exit(1); | |
385 | } | |
f4bb20cc | 386 | my $status = 0; |
e194cd1e | 387 | for (; $i < @ARGV; $i++) { |
f4bb20cc | 388 | $status |= show_remote($ARGV[$i], $ls_remote); |
e194cd1e | 389 | } |
f4bb20cc | 390 | exit($status); |
e194cd1e | 391 | } |
1e592d65 | 392 | elsif ($ARGV[0] eq 'update') { |
1918278e TT |
393 | if (@ARGV <= 1) { |
394 | update_remote("default"); | |
395 | exit(1); | |
1e592d65 | 396 | } |
1918278e TT |
397 | for ($i = 1; $i < @ARGV; $i++) { |
398 | update_remote($ARGV[$i]); | |
1e592d65 TT |
399 | } |
400 | } | |
859607df SP |
401 | elsif ($ARGV[0] eq 'prune') { |
402 | my $ls_remote = 1; | |
403 | my $i; | |
404 | for ($i = 1; $i < @ARGV; $i++) { | |
405 | if ($ARGV[$i] eq '-n') { | |
406 | $ls_remote = 0; | |
407 | } | |
408 | else { | |
409 | last; | |
410 | } | |
411 | } | |
412 | if ($i >= @ARGV) { | |
413 | print STDERR "Usage: git remote prune <remote>\n"; | |
414 | exit(1); | |
415 | } | |
f4bb20cc | 416 | my $status = 0; |
859607df | 417 | for (; $i < @ARGV; $i++) { |
f4bb20cc | 418 | $status |= prune_remote($ARGV[$i], $ls_remote); |
859607df | 419 | } |
f4bb20cc | 420 | exit($status); |
859607df | 421 | } |
e194cd1e | 422 | elsif ($ARGV[0] eq 'add') { |
b6f5da1e JH |
423 | my %opts = (); |
424 | while (1 < @ARGV && $ARGV[1] =~ /^-/) { | |
425 | my $opt = $ARGV[1]; | |
426 | shift @ARGV; | |
427 | if ($opt eq '-f' || $opt eq '--fetch') { | |
428 | $opts{'fetch'} = 1; | |
429 | next; | |
430 | } | |
431 | if ($opt eq '-t' || $opt eq '--track') { | |
432 | if (@ARGV < 1) { | |
433 | add_usage(); | |
434 | } | |
435 | $opts{'track'} ||= []; | |
436 | push @{$opts{'track'}}, $ARGV[1]; | |
437 | shift @ARGV; | |
438 | next; | |
439 | } | |
440 | if ($opt eq '-m' || $opt eq '--master') { | |
441 | if ((@ARGV < 1) || exists $opts{'master'}) { | |
442 | add_usage(); | |
443 | } | |
444 | $opts{'master'} = $ARGV[1]; | |
445 | shift @ARGV; | |
446 | next; | |
447 | } | |
38944390 JS |
448 | if ($opt eq '--mirror') { |
449 | $opts{'mirror'} = 1; | |
450 | next; | |
451 | } | |
b6f5da1e JH |
452 | add_usage(); |
453 | } | |
e194cd1e | 454 | if (@ARGV != 3) { |
b6f5da1e | 455 | add_usage(); |
e194cd1e | 456 | } |
b6f5da1e | 457 | add_remote($ARGV[1], $ARGV[2], \%opts); |
e194cd1e | 458 | } |
61136044 JB |
459 | elsif ($ARGV[0] eq 'rm') { |
460 | if (@ARGV <= 1) { | |
461 | print STDERR "Usage: git remote rm <remote>\n"; | |
683b5679 | 462 | exit(1); |
61136044 | 463 | } |
683b5679 | 464 | rm_remote($ARGV[1]); |
61136044 | 465 | } |
c03f7757 QT |
466 | else { |
467 | print STDERR "Usage: git remote\n"; | |
468 | print STDERR " git remote add <name> <url>\n"; | |
61136044 | 469 | print STDERR " git remote rm <name>\n"; |
c03f7757 | 470 | print STDERR " git remote show <name>\n"; |
859607df | 471 | print STDERR " git remote prune <name>\n"; |
1918278e | 472 | print STDERR " git remote update [group]\n"; |
c03f7757 QT |
473 | exit(1); |
474 | } |