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