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