git-svn: fix find-rev error message when missing arg
[git/git-svn.git] / git-remote.perl
blobd13e4c1fea93f0c345f6638bfd8a3715c73fa693
1 #!/usr/bin/perl -w
3 use Git;
4 my $git = Git->repository();
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";
12 $hash->{$name}{'URL'} = $value;
14 elsif ($what eq 'fetch') {
15 $hash->{$name}{'FETCH'} ||= [];
16 push @{$hash->{$name}{'FETCH'}}, $value;
18 elsif ($what eq 'push') {
19 $hash->{$name}{'PUSH'} ||= [];
20 push @{$hash->{$name}{'PUSH'}}, $value;
22 if (!exists $hash->{$name}{'SOURCE'}) {
23 $hash->{$name}{'SOURCE'} = 'config';
27 sub add_remote_remotes {
28 my ($hash, $file, $name) = @_;
30 if (exists $hash->{$name}) {
31 $hash->{$name}{'WARNING'} = 'ignored due to config';
32 return;
35 my $fh;
36 if (!open($fh, '<', $file)) {
37 print STDERR "Warning: cannot open $file\n";
38 return;
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;
50 elsif (/^Push:\s*(.*)$/) {
51 $it->{'PUSH'} ||= [];
52 push @{$it->{'PUSH'}}, $1;
54 elsif (/^Pull:\s*(.*)$/) {
55 $it->{'FETCH'} ||= [];
56 push @{$it->{'FETCH'}}, $1;
58 elsif (/^\#/) {
59 ; # ignore
61 else {
62 print STDERR "Warning: funny line in $file: $_\n";
65 close($fh);
68 sub list_remote {
69 my ($git) = @_;
70 my %seen = ();
71 my @remotes = eval {
72 $git->command(qw(config --get-regexp), '^remote\.');
74 for (@remotes) {
75 if (/^remote\.(\S+?)\.([^.\s]+)\s+(.*)$/) {
76 add_remote_config(\%seen, $1, $2, $3);
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/$_", $_);
90 return \%seen;
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";
99 $hash->{$name}{'REMOTE'} = $value;
101 elsif ($what eq 'merge') {
102 $hash->{$name}{'MERGE'} ||= [];
103 push @{$hash->{$name}{'MERGE'}}, $value;
107 sub list_branch {
108 my ($git) = @_;
109 my %seen = ();
110 my @branches = eval {
111 $git->command(qw(config --get-regexp), '^branch\.');
113 for (@branches) {
114 if (/^branch\.([^.]*)\.(\S*)\s+(.*)$/) {
115 add_branch_config(\%seen, $1, $2, $3);
119 return \%seen;
122 my $remote = list_remote($git);
123 my $branch = list_branch($git);
125 sub update_ls_remote {
126 my ($harder, $info) = @_;
128 return if (($harder == 0) ||
129 (($harder == 1) && exists $info->{'LS_REMOTE'}));
131 my @ref = map {
132 s|^[0-9a-f]{40}\s+refs/heads/||;
134 } $git->command(qw(ls-remote --heads), $info->{'URL'});
135 $info->{'LS_REMOTE'} = \@ref;
138 sub list_wildcard_mapping {
139 my ($forced, $ours, $ls) = @_;
140 my %refs;
141 for (@$ls) {
142 $refs{$_} = 01; # bit #0 to say "they have"
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"
151 my (@new, @stale, @tracked);
152 for (sort keys %refs) {
153 my $have = $refs{$_};
154 if ($have == 1) {
155 push @new, $_;
157 elsif ($have == 2) {
158 push @stale, $_;
160 elsif ($have == 3) {
161 push @tracked, $_;
164 return \@new, \@stale, \@tracked;
167 sub list_mapping {
168 my ($name, $info) = @_;
169 my $fetch = $info->{'FETCH'};
170 my $ls = $info->{'LS_REMOTE'};
171 my (@new, @stale, @tracked);
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
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;
185 elsif ($theirs =~ /\*/ || $ours =~ /\*/) {
186 print STDERR "Warning: unrecognized mapping in remotes.$name.fetch: $_\n";
188 elsif ($theirs =~ s|^refs/heads/||) {
189 if (!grep { $_ eq $theirs } @$ls) {
190 push @stale, $theirs;
192 elsif ($ours ne '') {
193 push @tracked, $theirs;
197 return \@new, \@stale, \@tracked;
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";
207 if (@$stale) {
208 print " Stale tracking branches in remotes/$name (use 'git remote prune')\n";
209 print " @$stale\n";
211 if (@$tracked) {
212 print " Tracked remote branches\n";
213 print " @$tracked\n";
217 sub prune_remote {
218 my ($name, $ls_remote) = @_;
219 if (!exists $remote->{$name}) {
220 print STDERR "No such remote $name\n";
221 return 1;
223 my $info = $remote->{$name};
224 update_ls_remote($ls_remote, $info);
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]);
232 return 0;
235 sub show_remote {
236 my ($name, $ls_remote) = @_;
237 if (!exists $remote->{$name}) {
238 print STDERR "No such remote $name\n";
239 return 1;
241 my $info = $remote->{$name};
242 update_ls_remote($ls_remote, $info);
244 print "* remote $name\n";
245 print " URL: $info->{'URL'}\n";
246 for my $branchname (sort keys %$branch) {
247 next unless (defined $branch->{$branchname}{'REMOTE'} &&
248 $branch->{$branchname}{'REMOTE'} eq $name);
249 my @merged = map {
250 s|^refs/heads/||;
252 } split(' ',"@{$branch->{$branchname}{'MERGE'}}");
253 next unless (@merged);
254 print " Remote branch(es) merged with 'git pull' while on branch $branchname\n";
255 print " @merged\n";
257 if ($info->{'LS_REMOTE'}) {
258 show_mapping($name, $info);
260 if ($info->{'PUSH'}) {
261 my @pushed = map {
262 s|^refs/heads/||;
263 s|^\+refs/heads/|+|;
264 s|:refs/heads/|:|;
266 } @{$info->{'PUSH'}};
267 print " Local branch(es) pushed with 'git push'\n";
268 print " @pushed\n";
270 return 0;
273 sub add_remote {
274 my ($name, $url, $opts) = @_;
275 if (exists $remote->{$name}) {
276 print STDERR "remote $name already exists.\n";
277 exit(1);
279 $git->command('config', "remote.$name.url", $url);
280 my $track = $opts->{'track'} || ["*"];
282 for (@$track) {
283 $git->command('config', '--add', "remote.$name.fetch",
284 $opts->{'mirror'} ?
285 "+refs/$_:refs/$_" :
286 "+refs/heads/$_:refs/remotes/$name/$_");
288 if ($opts->{'fetch'}) {
289 $git->command('fetch', $name);
291 if (exists $opts->{'master'}) {
292 $git->command('symbolic-ref', "refs/remotes/$name/HEAD",
293 "refs/remotes/$name/$opts->{'master'}");
297 sub update_remote {
298 my ($name) = @_;
300 my $conf = $git->config("remotes." . $name);
301 if (defined($conf)) {
302 @remotes = split(' ', $conf);
303 } elsif ($name eq 'default') {
304 undef @remotes;
305 for (sort keys %$remote) {
306 my $do_fetch = $git->config_bool("remote." . $_ .
307 ".skipDefaultUpdate");
308 unless ($do_fetch) {
309 push @remotes, $_;
312 } else {
313 print STDERR "Remote group $name does not exists.\n";
314 exit(1);
316 for (@remotes) {
317 print "Updating $_\n";
318 $git->command('fetch', "$_");
322 sub rm_remote {
323 my ($name) = @_;
324 if (!exists $remote->{$name}) {
325 print STDERR "No such remote $name\n";
326 return 1;
329 $git->command('config', '--remove-section', "remote.$name");
331 eval {
332 my @trackers = $git->command('config', '--get-regexp',
333 'branch.*.remote', $name);
334 for (@trackers) {
335 /^branch\.(.*)?\.remote/;
336 $git->config('--unset', "branch.$1.remote");
337 $git->config('--unset', "branch.$1.merge");
341 my @refs = $git->command('for-each-ref',
342 '--format=%(refname) %(objectname)', "refs/remotes/$name");
343 for (@refs) {
344 ($ref, $object) = split;
345 $git->command(qw(update-ref -d), $ref, $object);
347 return 0;
350 sub add_usage {
351 print STDERR "Usage: git remote add [-f] [-t track]* [-m master] <name> <url>\n";
352 exit(1);
355 local $VERBOSE = 0;
356 @ARGV = grep {
357 if ($_ eq '-v' or $_ eq '--verbose') {
358 $VERBOSE=1;
360 } else {
363 } @ARGV;
365 if (!@ARGV) {
366 for (sort keys %$remote) {
367 print "$_";
368 print "\t$remote->{$_}->{URL}" if $VERBOSE;
369 print "\n";
372 elsif ($ARGV[0] eq 'show') {
373 my $ls_remote = 1;
374 my $i;
375 for ($i = 1; $i < @ARGV; $i++) {
376 if ($ARGV[$i] eq '-n') {
377 $ls_remote = 0;
379 else {
380 last;
383 if ($i >= @ARGV) {
384 print STDERR "Usage: git remote show <remote>\n";
385 exit(1);
387 my $status = 0;
388 for (; $i < @ARGV; $i++) {
389 $status |= show_remote($ARGV[$i], $ls_remote);
391 exit($status);
393 elsif ($ARGV[0] eq 'update') {
394 if (@ARGV <= 1) {
395 update_remote("default");
396 exit(1);
398 for ($i = 1; $i < @ARGV; $i++) {
399 update_remote($ARGV[$i]);
402 elsif ($ARGV[0] eq 'prune') {
403 my $ls_remote = 1;
404 my $i;
405 for ($i = 1; $i < @ARGV; $i++) {
406 if ($ARGV[$i] eq '-n') {
407 $ls_remote = 0;
409 else {
410 last;
413 if ($i >= @ARGV) {
414 print STDERR "Usage: git remote prune <remote>\n";
415 exit(1);
417 my $status = 0;
418 for (; $i < @ARGV; $i++) {
419 $status |= prune_remote($ARGV[$i], $ls_remote);
421 exit($status);
423 elsif ($ARGV[0] eq 'add') {
424 my %opts = ();
425 while (1 < @ARGV && $ARGV[1] =~ /^-/) {
426 my $opt = $ARGV[1];
427 shift @ARGV;
428 if ($opt eq '-f' || $opt eq '--fetch') {
429 $opts{'fetch'} = 1;
430 next;
432 if ($opt eq '-t' || $opt eq '--track') {
433 if (@ARGV < 1) {
434 add_usage();
436 $opts{'track'} ||= [];
437 push @{$opts{'track'}}, $ARGV[1];
438 shift @ARGV;
439 next;
441 if ($opt eq '-m' || $opt eq '--master') {
442 if ((@ARGV < 1) || exists $opts{'master'}) {
443 add_usage();
445 $opts{'master'} = $ARGV[1];
446 shift @ARGV;
447 next;
449 if ($opt eq '--mirror') {
450 $opts{'mirror'} = 1;
451 next;
453 add_usage();
455 if (@ARGV != 3) {
456 add_usage();
458 add_remote($ARGV[1], $ARGV[2], \%opts);
460 elsif ($ARGV[0] eq 'rm') {
461 if (@ARGV <= 1) {
462 print STDERR "Usage: git remote rm <remote>\n";
463 exit(1);
465 exit(rm_remote($ARGV[1]));
467 else {
468 print STDERR "Usage: git remote\n";
469 print STDERR " git remote add <name> <url>\n";
470 print STDERR " git remote rm <name>\n";
471 print STDERR " git remote show <name>\n";
472 print STDERR " git remote prune <name>\n";
473 print STDERR " git remote update [group]\n";
474 exit(1);