]> git.ipfire.org Git - thirdparty/git.git/blobdiff - git-remote.perl
Unset NO_C99_FORMAT on Cygwin.
[thirdparty/git.git] / git-remote.perl
index 969d33bc5e7ab321d4462dbb246e471e22b451d1..670bafb6d05db7f152f53cac235d4d60c219ee6d 100755 (executable)
@@ -67,7 +67,7 @@ sub list_remote {
                $git->command(qw(config --get-regexp), '^remote\.');
        };
        for (@remotes) {
-               if (/^remote\.([^.]*)\.(\S*)\s+(.*)$/) {
+               if (/^remote\.(\S+?)\.([^.\s]+)\s+(.*)$/) {
                        add_remote_config(\%seen, $1, $2, $3);
                }
        }
@@ -200,7 +200,7 @@ sub show_mapping {
                print "    @$new\n";
        }
        if (@$stale) {
-               print "  Stale tracking branches in remotes/$name (you'd better remove them)\n";
+               print "  Stale tracking branches in remotes/$name (use 'git remote prune')\n";
                print "    @$stale\n";
        }
        if (@$tracked) {
@@ -209,6 +209,23 @@ sub show_mapping {
        }
 }
 
+sub prune_remote {
+       my ($name, $ls_remote) = @_;
+       if (!exists $remote->{$name}) {
+               print STDERR "No such remote $name\n";
+               return;
+       }
+       my $info = $remote->{$name};
+       update_ls_remote($ls_remote, $info);
+
+       my ($new, $stale, $tracked) = list_mapping($name, $info);
+       my $prefix = "refs/remotes/$name";
+       foreach my $to_prune (@$stale) {
+               my @v = $git->command(qw(rev-parse --verify), "$prefix/$to_prune");
+               $git->command(qw(update-ref -d), "$prefix/$to_prune", $v[0]);
+       }
+}
+
 sub show_remote {
        my ($name, $ls_remote) = @_;
        if (!exists $remote->{$name}) {
@@ -236,14 +253,30 @@ sub show_remote {
 }
 
 sub add_remote {
-       my ($name, $url) = @_;
+       my ($name, $url, $opts) = @_;
        if (exists $remote->{$name}) {
                print STDERR "remote $name already exists.\n";
                exit(1);
        }
        $git->command('config', "remote.$name.url", $url);
-       $git->command('config', "remote.$name.fetch",
-                     "+refs/heads/*:refs/remotes/$name/*");
+       my $track = $opts->{'track'} || ["*"];
+
+       for (@$track) {
+               $git->command('config', '--add', "remote.$name.fetch",
+                             "+refs/heads/$_:refs/remotes/$name/$_");
+       }
+       if ($opts->{'fetch'}) {
+               $git->command('fetch', $name);
+       }
+       if (exists $opts->{'master'}) {
+               $git->command('symbolic-ref', "refs/remotes/$name/HEAD",
+                             "refs/remotes/$name/$opts->{'master'}");
+       }
+}
+
+sub add_usage {
+       print STDERR "Usage: git remote add [-f] [-t track]* [-m master] <name> <url>\n";
+       exit(1);
 }
 
 if (!@ARGV) {
@@ -270,16 +303,62 @@ elsif ($ARGV[0] eq 'show') {
                show_remote($ARGV[$i], $ls_remote);
        }
 }
+elsif ($ARGV[0] eq 'prune') {
+       my $ls_remote = 1;
+       my $i;
+       for ($i = 1; $i < @ARGV; $i++) {
+               if ($ARGV[$i] eq '-n') {
+                       $ls_remote = 0;
+               }
+               else {
+                       last;
+               }
+       }
+       if ($i >= @ARGV) {
+               print STDERR "Usage: git remote prune <remote>\n";
+               exit(1);
+       }
+       for (; $i < @ARGV; $i++) {
+               prune_remote($ARGV[$i], $ls_remote);
+       }
+}
 elsif ($ARGV[0] eq 'add') {
+       my %opts = ();
+       while (1 < @ARGV && $ARGV[1] =~ /^-/) {
+               my $opt = $ARGV[1];
+               shift @ARGV;
+               if ($opt eq '-f' || $opt eq '--fetch') {
+                       $opts{'fetch'} = 1;
+                       next;
+               }
+               if ($opt eq '-t' || $opt eq '--track') {
+                       if (@ARGV < 1) {
+                               add_usage();
+                       }
+                       $opts{'track'} ||= [];
+                       push @{$opts{'track'}}, $ARGV[1];
+                       shift @ARGV;
+                       next;
+               }
+               if ($opt eq '-m' || $opt eq '--master') {
+                       if ((@ARGV < 1) || exists $opts{'master'}) {
+                               add_usage();
+                       }
+                       $opts{'master'} = $ARGV[1];
+                       shift @ARGV;
+                       next;
+               }
+               add_usage();
+       }
        if (@ARGV != 3) {
-               print STDERR "Usage: git remote add <name> <url>\n";
-               exit(1);
+               add_usage();
        }
-       add_remote($ARGV[1], $ARGV[2]);
+       add_remote($ARGV[1], $ARGV[2], \%opts);
 }
 else {
        print STDERR "Usage: git remote\n";
        print STDERR "       git remote add <name> <url>\n";
        print STDERR "       git remote show <name>\n";
+       print STDERR "       git remote prune <name>\n";
        exit(1);
 }