Code

Plug a resource leak in threaded pack-objects code.
[git.git] / git-remote.perl
index bd70bf1ddd663db7d0af49b8513fdf234767e422..d13e4c1fea93f0c345f6638bfd8a3715c73fa693 100755 (executable)
@@ -15,6 +15,10 @@ sub add_remote_config {
                $hash->{$name}{'FETCH'} ||= [];
                push @{$hash->{$name}{'FETCH'}}, $value;
        }
+       elsif ($what eq 'push') {
+               $hash->{$name}{'PUSH'} ||= [];
+               push @{$hash->{$name}{'PUSH'}}, $value;
+       }
        if (!exists $hash->{$name}{'SOURCE'}) {
                $hash->{$name}{'SOURCE'} = 'config';
        }
@@ -44,7 +48,8 @@ sub add_remote_remotes {
                        }
                }
                elsif (/^Push:\s*(.*)$/) {
-                       ; # later
+                       $it->{'PUSH'} ||= [];
+                       push @{$it->{'PUSH'}}, $1;
                }
                elsif (/^Pull:\s*(.*)$/) {
                        $it->{'FETCH'} ||= [];
@@ -213,7 +218,7 @@ sub prune_remote {
        my ($name, $ls_remote) = @_;
        if (!exists $remote->{$name}) {
                print STDERR "No such remote $name\n";
-               return;
+               return 1;
        }
        my $info = $remote->{$name};
        update_ls_remote($ls_remote, $info);
@@ -224,13 +229,14 @@ sub prune_remote {
                my @v = $git->command(qw(rev-parse --verify), "$prefix/$to_prune");
                $git->command(qw(update-ref -d), "$prefix/$to_prune", $v[0]);
        }
+       return 0;
 }
 
 sub show_remote {
        my ($name, $ls_remote) = @_;
        if (!exists $remote->{$name}) {
                print STDERR "No such remote $name\n";
-               return;
+               return 1;
        }
        my $info = $remote->{$name};
        update_ls_remote($ls_remote, $info);
@@ -238,7 +244,8 @@ sub show_remote {
        print "* remote $name\n";
        print "  URL: $info->{'URL'}\n";
        for my $branchname (sort keys %$branch) {
-               next if ($branch->{$branchname}{'REMOTE'} ne $name);
+               next unless (defined $branch->{$branchname}{'REMOTE'} &&
+                            $branch->{$branchname}{'REMOTE'} eq $name);
                my @merged = map {
                        s|^refs/heads/||;
                        $_;
@@ -250,6 +257,17 @@ sub show_remote {
        if ($info->{'LS_REMOTE'}) {
                show_mapping($name, $info);
        }
+       if ($info->{'PUSH'}) {
+               my @pushed = map {
+                       s|^refs/heads/||;
+                       s|^\+refs/heads/|+|;
+                       s|:refs/heads/|:|;
+                       $_;
+               } @{$info->{'PUSH'}};
+               print "  Local branch(es) pushed with 'git push'\n";
+               print "    @pushed\n";
+       }
+       return 0;
 }
 
 sub add_remote {
@@ -263,7 +281,9 @@ sub add_remote {
 
        for (@$track) {
                $git->command('config', '--add', "remote.$name.fetch",
-                             "+refs/heads/$_:refs/remotes/$name/$_");
+                               $opts->{'mirror'} ?
+                               "+refs/$_:refs/$_" :
+                               "+refs/heads/$_:refs/remotes/$name/$_");
        }
        if ($opts->{'fetch'}) {
                $git->command('fetch', $name);
@@ -283,9 +303,9 @@ sub update_remote {
        } elsif ($name eq 'default') {
                undef @remotes;
                for (sort keys %$remote) {
-                       my $do_fetch = $git->config_boolean("remote." . $_ .
+                       my $do_fetch = $git->config_bool("remote." . $_ .
                                                    ".skipDefaultUpdate");
-                       if (!defined($do_fetch) || $do_fetch ne "true") {
+                       unless ($do_fetch) {
                                push @remotes, $_;
                        }
                }
@@ -299,14 +319,54 @@ sub update_remote {
        }
 }
 
+sub rm_remote {
+       my ($name) = @_;
+       if (!exists $remote->{$name}) {
+               print STDERR "No such remote $name\n";
+               return 1;
+       }
+
+       $git->command('config', '--remove-section', "remote.$name");
+
+       eval {
+           my @trackers = $git->command('config', '--get-regexp',
+                       'branch.*.remote', $name);
+               for (@trackers) {
+                       /^branch\.(.*)?\.remote/;
+                       $git->config('--unset', "branch.$1.remote");
+                       $git->config('--unset', "branch.$1.merge");
+               }
+       };
+
+       my @refs = $git->command('for-each-ref',
+               '--format=%(refname) %(objectname)', "refs/remotes/$name");
+       for (@refs) {
+               ($ref, $object) = split;
+               $git->command(qw(update-ref -d), $ref, $object);
+       }
+       return 0;
+}
+
 sub add_usage {
        print STDERR "Usage: git remote add [-f] [-t track]* [-m master] <name> <url>\n";
        exit(1);
 }
 
+local $VERBOSE = 0;
+@ARGV = grep {
+       if ($_ eq '-v' or $_ eq '--verbose') {
+               $VERBOSE=1;
+               0
+       } else {
+               1
+       }
+} @ARGV;
+
 if (!@ARGV) {
        for (sort keys %$remote) {
-               print "$_\n";
+               print "$_";
+               print "\t$remote->{$_}->{URL}" if $VERBOSE;
+               print "\n";
        }
 }
 elsif ($ARGV[0] eq 'show') {
@@ -324,9 +384,11 @@ elsif ($ARGV[0] eq 'show') {
                print STDERR "Usage: git remote show <remote>\n";
                exit(1);
        }
+       my $status = 0;
        for (; $i < @ARGV; $i++) {
-               show_remote($ARGV[$i], $ls_remote);
+               $status |= show_remote($ARGV[$i], $ls_remote);
        }
+       exit($status);
 }
 elsif ($ARGV[0] eq 'update') {
        if (@ARGV <= 1) {
@@ -352,9 +414,11 @@ elsif ($ARGV[0] eq 'prune') {
                print STDERR "Usage: git remote prune <remote>\n";
                exit(1);
        }
+       my $status = 0;
        for (; $i < @ARGV; $i++) {
-               prune_remote($ARGV[$i], $ls_remote);
+               $status |= prune_remote($ARGV[$i], $ls_remote);
        }
+        exit($status);
 }
 elsif ($ARGV[0] eq 'add') {
        my %opts = ();
@@ -382,6 +446,10 @@ elsif ($ARGV[0] eq 'add') {
                        shift @ARGV;
                        next;
                }
+               if ($opt eq '--mirror') {
+                       $opts{'mirror'} = 1;
+                       next;
+               }
                add_usage();
        }
        if (@ARGV != 3) {
@@ -389,9 +457,17 @@ elsif ($ARGV[0] eq 'add') {
        }
        add_remote($ARGV[1], $ARGV[2], \%opts);
 }
+elsif ($ARGV[0] eq 'rm') {
+       if (@ARGV <= 1) {
+               print STDERR "Usage: git remote rm <remote>\n";
+               exit(1);
+       }
+       exit(rm_remote($ARGV[1]));
+}
 else {
        print STDERR "Usage: git remote\n";
        print STDERR "       git remote add <name> <url>\n";
+       print STDERR "       git remote rm <name>\n";
        print STDERR "       git remote show <name>\n";
        print STDERR "       git remote prune <name>\n";
        print STDERR "       git remote update [group]\n";