blob: d42df7b4188672e01dd7c95d5dcd73939f0fd600 [file] [log] [blame]
Junio C Hamanoe194cd12007-01-03 12:13:04 -08001#!/usr/bin/perl -w
2
Rafael Garcia-Suarez7deaec92008-02-04 11:09:00 +01003use strict;
Junio C Hamanoe194cd12007-01-03 12:13:04 -08004use Git;
5my $git = Git->repository();
6
7sub add_remote_config {
8 my ($hash, $name, $what, $value) = @_;
9 if ($what eq 'url') {
Junio C Hamano95775e52008-02-27 13:50:44 -080010 # Having more than one is Ok -- it is used for push.
11 if (! exists $hash->{'URL'}) {
12 $hash->{$name}{'URL'} = $value;
Junio C Hamanoe194cd12007-01-03 12:13:04 -080013 }
Junio C Hamanoe194cd12007-01-03 12:13:04 -080014 }
15 elsif ($what eq 'fetch') {
16 $hash->{$name}{'FETCH'} ||= [];
17 push @{$hash->{$name}{'FETCH'}}, $value;
18 }
Johannes Sixt8bf0e3d2007-03-18 21:34:46 +010019 elsif ($what eq 'push') {
20 $hash->{$name}{'PUSH'} ||= [];
21 push @{$hash->{$name}{'PUSH'}}, $value;
22 }
Junio C Hamanoe194cd12007-01-03 12:13:04 -080023 if (!exists $hash->{$name}{'SOURCE'}) {
24 $hash->{$name}{'SOURCE'} = 'config';
25 }
26}
27
28sub 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*(.*)$/) {
Johannes Sixt8bf0e3d2007-03-18 21:34:46 +010052 $it->{'PUSH'} ||= [];
53 push @{$it->{'PUSH'}}, $1;
Junio C Hamanoe194cd12007-01-03 12:13:04 -080054 }
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
69sub list_remote {
70 my ($git) = @_;
71 my %seen = ();
72 my @remotes = eval {
Tom Princee0d10e12007-01-28 16:16:53 -080073 $git->command(qw(config --get-regexp), '^remote\.');
Junio C Hamanoe194cd12007-01-03 12:13:04 -080074 };
75 for (@remotes) {
Pavel Roskinb5a40a52007-02-21 00:03:36 -050076 if (/^remote\.(\S+?)\.([^.\s]+)\s+(.*)$/) {
Junio C Hamanoe194cd12007-01-03 12:13:04 -080077 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
94sub 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
108sub list_branch {
109 my ($git) = @_;
110 my %seen = ();
111 my @branches = eval {
Tom Princee0d10e12007-01-28 16:16:53 -0800112 $git->command(qw(config --get-regexp), '^branch\.');
Junio C Hamanoe194cd12007-01-03 12:13:04 -0800113 };
114 for (@branches) {
115 if (/^branch\.([^.]*)\.(\S*)\s+(.*)$/) {
116 add_branch_config(\%seen, $1, $2, $3);
117 }
118 }
119
120 return \%seen;
121}
122
123my $remote = list_remote($git);
124my $branch = list_branch($git);
125
126sub update_ls_remote {
127 my ($harder, $info) = @_;
128
129 return if (($harder == 0) ||
130 (($harder == 1) && exists $info->{'LS_REMOTE'}));
131
Petr Baudis31a92f62008-07-08 19:48:04 +0200132 my @ref = map { s|refs/heads/||; $_; } keys %{$git->remote_refs($info->{'URL'}, [ 'heads' ])};
Junio C Hamanoe194cd12007-01-03 12:13:04 -0800133 $info->{'LS_REMOTE'} = \@ref;
134}
135
Shawn O. Pearce7a8c9ec2007-02-02 00:05:55 -0500136sub list_wildcard_mapping {
Junio C Hamanoe194cd12007-01-03 12:13:04 -0800137 my ($forced, $ours, $ls) = @_;
138 my %refs;
139 for (@$ls) {
140 $refs{$_} = 01; # bit #0 to say "they have"
141 }
142 for ($git->command('for-each-ref', "refs/remotes/$ours")) {
143 chomp;
144 next unless (s|^[0-9a-f]{40}\s[a-z]+\srefs/remotes/$ours/||);
145 next if ($_ eq 'HEAD');
146 $refs{$_} ||= 0;
147 $refs{$_} |= 02; # bit #1 to say "we have"
148 }
149 my (@new, @stale, @tracked);
150 for (sort keys %refs) {
151 my $have = $refs{$_};
152 if ($have == 1) {
153 push @new, $_;
154 }
155 elsif ($have == 2) {
156 push @stale, $_;
157 }
158 elsif ($have == 3) {
159 push @tracked, $_;
160 }
161 }
Shawn O. Pearce7a8c9ec2007-02-02 00:05:55 -0500162 return \@new, \@stale, \@tracked;
Junio C Hamanoe194cd12007-01-03 12:13:04 -0800163}
164
Shawn O. Pearce7a8c9ec2007-02-02 00:05:55 -0500165sub list_mapping {
Junio C Hamanoe194cd12007-01-03 12:13:04 -0800166 my ($name, $info) = @_;
167 my $fetch = $info->{'FETCH'};
168 my $ls = $info->{'LS_REMOTE'};
Shawn O. Pearce7a8c9ec2007-02-02 00:05:55 -0500169 my (@new, @stale, @tracked);
Junio C Hamanoe194cd12007-01-03 12:13:04 -0800170
171 for (@$fetch) {
172 next unless (/(\+)?([^:]+):(.*)/);
173 my ($forced, $theirs, $ours) = ($1, $2, $3);
174 if ($theirs eq 'refs/heads/*' &&
175 $ours =~ /^refs\/remotes\/(.*)\/\*$/) {
176 # wildcard mapping
Shawn O. Pearce7a8c9ec2007-02-02 00:05:55 -0500177 my ($w_new, $w_stale, $w_tracked)
178 = list_wildcard_mapping($forced, $1, $ls);
179 push @new, @$w_new;
180 push @stale, @$w_stale;
181 push @tracked, @$w_tracked;
Junio C Hamanoe194cd12007-01-03 12:13:04 -0800182 }
183 elsif ($theirs =~ /\*/ || $ours =~ /\*/) {
184 print STDERR "Warning: unrecognized mapping in remotes.$name.fetch: $_\n";
185 }
186 elsif ($theirs =~ s|^refs/heads/||) {
187 if (!grep { $_ eq $theirs } @$ls) {
188 push @stale, $theirs;
189 }
190 elsif ($ours ne '') {
191 push @tracked, $theirs;
192 }
193 }
194 }
Shawn O. Pearce7a8c9ec2007-02-02 00:05:55 -0500195 return \@new, \@stale, \@tracked;
196}
197
198sub show_mapping {
199 my ($name, $info) = @_;
200 my ($new, $stale, $tracked) = list_mapping($name, $info);
201 if (@$new) {
202 print " New remote branches (next fetch will store in remotes/$name)\n";
203 print " @$new\n";
Junio C Hamanoe194cd12007-01-03 12:13:04 -0800204 }
Shawn O. Pearce7a8c9ec2007-02-02 00:05:55 -0500205 if (@$stale) {
Shawn O. Pearce859607d2007-02-02 00:06:08 -0500206 print " Stale tracking branches in remotes/$name (use 'git remote prune')\n";
Shawn O. Pearce7a8c9ec2007-02-02 00:05:55 -0500207 print " @$stale\n";
208 }
209 if (@$tracked) {
Junio C Hamanoe194cd12007-01-03 12:13:04 -0800210 print " Tracked remote branches\n";
Shawn O. Pearce7a8c9ec2007-02-02 00:05:55 -0500211 print " @$tracked\n";
Junio C Hamanoe194cd12007-01-03 12:13:04 -0800212 }
213}
214
Shawn O. Pearce859607d2007-02-02 00:06:08 -0500215sub prune_remote {
216 my ($name, $ls_remote) = @_;
217 if (!exists $remote->{$name}) {
218 print STDERR "No such remote $name\n";
Jari Aaltof4bb20c2007-09-29 23:29:43 -0700219 return 1;
Shawn O. Pearce859607d2007-02-02 00:06:08 -0500220 }
221 my $info = $remote->{$name};
222 update_ls_remote($ls_remote, $info);
223
224 my ($new, $stale, $tracked) = list_mapping($name, $info);
225 my $prefix = "refs/remotes/$name";
226 foreach my $to_prune (@$stale) {
227 my @v = $git->command(qw(rev-parse --verify), "$prefix/$to_prune");
228 $git->command(qw(update-ref -d), "$prefix/$to_prune", $v[0]);
229 }
Jari Aaltof4bb20c2007-09-29 23:29:43 -0700230 return 0;
Shawn O. Pearce859607d2007-02-02 00:06:08 -0500231}
232
Junio C Hamanoe194cd12007-01-03 12:13:04 -0800233sub show_remote {
234 my ($name, $ls_remote) = @_;
235 if (!exists $remote->{$name}) {
236 print STDERR "No such remote $name\n";
Jari Aaltof4bb20c2007-09-29 23:29:43 -0700237 return 1;
Junio C Hamanoe194cd12007-01-03 12:13:04 -0800238 }
239 my $info = $remote->{$name};
240 update_ls_remote($ls_remote, $info);
241
242 print "* remote $name\n";
243 print " URL: $info->{'URL'}\n";
244 for my $branchname (sort keys %$branch) {
Junio C Hamano59b20232007-10-24 04:49:51 -0700245 next unless (defined $branch->{$branchname}{'REMOTE'} &&
246 $branch->{$branchname}{'REMOTE'} eq $name);
Junio C Hamanoe194cd12007-01-03 12:13:04 -0800247 my @merged = map {
248 s|^refs/heads/||;
249 $_;
250 } split(' ',"@{$branch->{$branchname}{'MERGE'}}");
251 next unless (@merged);
252 print " Remote branch(es) merged with 'git pull' while on branch $branchname\n";
253 print " @merged\n";
254 }
255 if ($info->{'LS_REMOTE'}) {
256 show_mapping($name, $info);
257 }
Johannes Sixt8bf0e3d2007-03-18 21:34:46 +0100258 if ($info->{'PUSH'}) {
259 my @pushed = map {
260 s|^refs/heads/||;
Johannes Sixt6718f1f2007-06-09 22:34:16 +0200261 s|^\+refs/heads/|+|;
Johannes Sixt8bf0e3d2007-03-18 21:34:46 +0100262 s|:refs/heads/|:|;
263 $_;
264 } @{$info->{'PUSH'}};
265 print " Local branch(es) pushed with 'git push'\n";
266 print " @pushed\n";
267 }
Jari Aaltof4bb20c2007-09-29 23:29:43 -0700268 return 0;
Junio C Hamanoe194cd12007-01-03 12:13:04 -0800269}
270
271sub add_remote {
Junio C Hamanob6f5da12007-02-01 23:30:03 -0800272 my ($name, $url, $opts) = @_;
Junio C Hamanoe194cd12007-01-03 12:13:04 -0800273 if (exists $remote->{$name}) {
274 print STDERR "remote $name already exists.\n";
275 exit(1);
276 }
Tom Princee0d10e12007-01-28 16:16:53 -0800277 $git->command('config', "remote.$name.url", $url);
Junio C Hamanob6f5da12007-02-01 23:30:03 -0800278 my $track = $opts->{'track'} || ["*"];
279
280 for (@$track) {
281 $git->command('config', '--add', "remote.$name.fetch",
Johannes Schindelin38944392007-09-02 21:10:14 +0100282 $opts->{'mirror'} ?
283 "+refs/$_:refs/$_" :
284 "+refs/heads/$_:refs/remotes/$name/$_");
Junio C Hamanob6f5da12007-02-01 23:30:03 -0800285 }
286 if ($opts->{'fetch'}) {
287 $git->command('fetch', $name);
288 }
289 if (exists $opts->{'master'}) {
290 $git->command('symbolic-ref', "refs/remotes/$name/HEAD",
291 "refs/remotes/$name/$opts->{'master'}");
292 }
293}
294
Theodore Ts'o19182782007-02-20 15:13:43 -0500295sub update_remote {
296 my ($name) = @_;
Rafael Garcia-Suarez7deaec92008-02-04 11:09:00 +0100297 my @remotes;
Theodore Ts'o19182782007-02-20 15:13:43 -0500298
299 my $conf = $git->config("remotes." . $name);
300 if (defined($conf)) {
301 @remotes = split(' ', $conf);
302 } elsif ($name eq 'default') {
Rafael Garcia-Suarez7deaec92008-02-04 11:09:00 +0100303 @remotes = ();
Theodore Ts'o19182782007-02-20 15:13:43 -0500304 for (sort keys %$remote) {
Petr Baudis35c49ee2007-05-09 12:49:41 +0200305 my $do_fetch = $git->config_bool("remote." . $_ .
Theodore Ts'o19182782007-02-20 15:13:43 -0500306 ".skipDefaultUpdate");
Petr Baudis35c49ee2007-05-09 12:49:41 +0200307 unless ($do_fetch) {
Theodore Ts'o19182782007-02-20 15:13:43 -0500308 push @remotes, $_;
309 }
310 }
311 } else {
Mikael Magnusson6c2a6022008-09-19 15:48:08 +0200312 print STDERR "Remote group $name does not exist.\n";
Theodore Ts'o19182782007-02-20 15:13:43 -0500313 exit(1);
314 }
315 for (@remotes) {
316 print "Updating $_\n";
317 $git->command('fetch', "$_");
318 }
319}
320
James Bowes61136042007-06-05 19:25:23 -0400321sub rm_remote {
Junio C Hamano683b5672007-09-23 22:29:12 -0700322 my ($name) = @_;
James Bowes61136042007-06-05 19:25:23 -0400323 if (!exists $remote->{$name}) {
324 print STDERR "No such remote $name\n";
Jari Aalto6982cce2007-09-29 23:34:19 -0700325 return 1;
James Bowes61136042007-06-05 19:25:23 -0400326 }
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
Junio C Hamano683b5672007-09-23 22:29:12 -0700340 my @refs = $git->command('for-each-ref',
James Bowes61136042007-06-05 19:25:23 -0400341 '--format=%(refname) %(objectname)', "refs/remotes/$name");
342 for (@refs) {
Rafael Garcia-Suarez7deaec92008-02-04 11:09:00 +0100343 my ($ref, $object) = split;
James Bowes61136042007-06-05 19:25:23 -0400344 $git->command(qw(update-ref -d), $ref, $object);
345 }
Jari Aalto6982cce2007-09-29 23:34:19 -0700346 return 0;
James Bowes61136042007-06-05 19:25:23 -0400347}
348
Junio C Hamanob6f5da12007-02-01 23:30:03 -0800349sub add_usage {
David Aguilarbeb5ab12013-02-23 16:50:21 -0800350 print STDERR "usage: git remote add [-f] [-t track]* [-m master] <name> <url>\n";
Junio C Hamanob6f5da12007-02-01 23:30:03 -0800351 exit(1);
Junio C Hamanoe194cd12007-01-03 12:13:04 -0800352}
353
Rafael Garcia-Suarez7deaec92008-02-04 11:09:00 +0100354my $VERBOSE = 0;
Alex Riesen09ff69b2007-07-06 00:06:56 +0200355@ARGV = grep {
356 if ($_ eq '-v' or $_ eq '--verbose') {
357 $VERBOSE=1;
358 0
359 } else {
360 1
361 }
362} @ARGV;
363
Junio C Hamanoe194cd12007-01-03 12:13:04 -0800364if (!@ARGV) {
365 for (sort keys %$remote) {
Alex Riesen09ff69b2007-07-06 00:06:56 +0200366 print "$_";
367 print "\t$remote->{$_}->{URL}" if $VERBOSE;
368 print "\n";
Junio C Hamanoe194cd12007-01-03 12:13:04 -0800369 }
370}
371elsif ($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) {
David Aguilarf86cad72013-02-24 14:48:40 -0800383 print STDERR "usage: git remote show <remote>\n";
Junio C Hamanoe194cd12007-01-03 12:13:04 -0800384 exit(1);
385 }
Jari Aaltof4bb20c2007-09-29 23:29:43 -0700386 my $status = 0;
Junio C Hamanoe194cd12007-01-03 12:13:04 -0800387 for (; $i < @ARGV; $i++) {
Jari Aaltof4bb20c2007-09-29 23:29:43 -0700388 $status |= show_remote($ARGV[$i], $ls_remote);
Junio C Hamanoe194cd12007-01-03 12:13:04 -0800389 }
Jari Aaltof4bb20c2007-09-29 23:29:43 -0700390 exit($status);
Junio C Hamanoe194cd12007-01-03 12:13:04 -0800391}
Theodore Ts'o1e592d62007-02-18 23:00:00 -0500392elsif ($ARGV[0] eq 'update') {
Theodore Ts'o19182782007-02-20 15:13:43 -0500393 if (@ARGV <= 1) {
394 update_remote("default");
395 exit(1);
Theodore Ts'o1e592d62007-02-18 23:00:00 -0500396 }
Rafael Garcia-Suarez7deaec92008-02-04 11:09:00 +0100397 for (my $i = 1; $i < @ARGV; $i++) {
Theodore Ts'o19182782007-02-20 15:13:43 -0500398 update_remote($ARGV[$i]);
Theodore Ts'o1e592d62007-02-18 23:00:00 -0500399 }
400}
Shawn O. Pearce859607d2007-02-02 00:06:08 -0500401elsif ($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) {
David Aguilarf86cad72013-02-24 14:48:40 -0800413 print STDERR "usage: git remote prune <remote>\n";
Shawn O. Pearce859607d2007-02-02 00:06:08 -0500414 exit(1);
415 }
Jari Aaltof4bb20c2007-09-29 23:29:43 -0700416 my $status = 0;
Shawn O. Pearce859607d2007-02-02 00:06:08 -0500417 for (; $i < @ARGV; $i++) {
Jari Aaltof4bb20c2007-09-29 23:29:43 -0700418 $status |= prune_remote($ARGV[$i], $ls_remote);
Shawn O. Pearce859607d2007-02-02 00:06:08 -0500419 }
Jari Aaltof4bb20c2007-09-29 23:29:43 -0700420 exit($status);
Shawn O. Pearce859607d2007-02-02 00:06:08 -0500421}
Junio C Hamanoe194cd12007-01-03 12:13:04 -0800422elsif ($ARGV[0] eq 'add') {
Junio C Hamanob6f5da12007-02-01 23:30:03 -0800423 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 }
Johannes Schindelin38944392007-09-02 21:10:14 +0100448 if ($opt eq '--mirror') {
449 $opts{'mirror'} = 1;
450 next;
451 }
Junio C Hamanob6f5da12007-02-01 23:30:03 -0800452 add_usage();
Junio C Hamanoe194cd12007-01-03 12:13:04 -0800453 }
Junio C Hamanob6f5da12007-02-01 23:30:03 -0800454 if (@ARGV != 3) {
455 add_usage();
456 }
457 add_remote($ARGV[1], $ARGV[2], \%opts);
Junio C Hamanoe194cd12007-01-03 12:13:04 -0800458}
James Bowes61136042007-06-05 19:25:23 -0400459elsif ($ARGV[0] eq 'rm') {
460 if (@ARGV <= 1) {
David Aguilarf86cad72013-02-24 14:48:40 -0800461 print STDERR "usage: git remote rm <remote>\n";
Junio C Hamano683b5672007-09-23 22:29:12 -0700462 exit(1);
James Bowes61136042007-06-05 19:25:23 -0400463 }
Jari Aalto6982cce2007-09-29 23:34:19 -0700464 exit(rm_remote($ARGV[1]));
James Bowes61136042007-06-05 19:25:23 -0400465}
Quy Tonthatc03f7752007-01-13 22:55:21 +1100466else {
David Aguilarf86cad72013-02-24 14:48:40 -0800467 print STDERR "usage: git remote\n";
Quy Tonthatc03f7752007-01-13 22:55:21 +1100468 print STDERR " git remote add <name> <url>\n";
James Bowes61136042007-06-05 19:25:23 -0400469 print STDERR " git remote rm <name>\n";
Quy Tonthatc03f7752007-01-13 22:55:21 +1100470 print STDERR " git remote show <name>\n";
Shawn O. Pearce859607d2007-02-02 00:06:08 -0500471 print STDERR " git remote prune <name>\n";
Theodore Ts'o19182782007-02-20 15:13:43 -0500472 print STDERR " git remote update [group]\n";
Quy Tonthatc03f7752007-01-13 22:55:21 +1100473 exit(1);
474}