1#!/usr/bin/perl -w23use Git;4my $git = Git->repository();56sub add_remote_config {7my ($hash, $name, $what, $value) = @_;8if ($what eq 'url') {9if (exists $hash->{$name}{'URL'}) {10print STDERR "Warning: more than one remote.$name.url\n";11}12$hash->{$name}{'URL'} = $value;13}14elsif ($what eq 'fetch') {15$hash->{$name}{'FETCH'} ||= [];16push @{$hash->{$name}{'FETCH'}}, $value;17}18if (!exists $hash->{$name}{'SOURCE'}) {19$hash->{$name}{'SOURCE'} = 'config';20}21}2223sub add_remote_remotes {24my ($hash, $file, $name) = @_;2526if (exists $hash->{$name}) {27$hash->{$name}{'WARNING'} = 'ignored due to config';28return;29}3031my $fh;32if (!open($fh, '<', $file)) {33print STDERR "Warning: cannot open $file\n";34return;35}36my $it = { 'SOURCE' => 'remotes' };37$hash->{$name} = $it;38while (<$fh>) {39chomp;40if (/^URL:\s*(.*)$/) {41# Having more than one is Ok -- it is used for push.42if (! exists $it->{'URL'}) {43$it->{'URL'} = $1;44}45}46elsif (/^Push:\s*(.*)$/) {47; # later48}49elsif (/^Pull:\s*(.*)$/) {50$it->{'FETCH'} ||= [];51push @{$it->{'FETCH'}}, $1;52}53elsif (/^\#/) {54; # ignore55}56else {57print STDERR "Warning: funny line in $file: $_\n";58}59}60close($fh);61}6263sub list_remote {64my ($git) = @_;65my %seen = ();66my @remotes = eval {67$git->command(qw(config --get-regexp), '^remote\.');68};69for (@remotes) {70if (/^remote\.([^.]*)\.(\S*)\s+(.*)$/) {71add_remote_config(\%seen, $1, $2, $3);72}73}7475my $dir = $git->repo_path() . "/remotes";76if (opendir(my $dh, $dir)) {77local $_;78while ($_ = readdir($dh)) {79chomp;80next if (! -f "$dir/$_" || ! -r _);81add_remote_remotes(\%seen, "$dir/$_", $_);82}83}8485return \%seen;86}8788sub add_branch_config {89my ($hash, $name, $what, $value) = @_;90if ($what eq 'remote') {91if (exists $hash->{$name}{'REMOTE'}) {92print STDERR "Warning: more than one branch.$name.remote\n";93}94$hash->{$name}{'REMOTE'} = $value;95}96elsif ($what eq 'merge') {97$hash->{$name}{'MERGE'} ||= [];98push @{$hash->{$name}{'MERGE'}}, $value;99}100}101102sub list_branch {103my ($git) = @_;104my %seen = ();105my @branches = eval {106$git->command(qw(config --get-regexp), '^branch\.');107};108for (@branches) {109if (/^branch\.([^.]*)\.(\S*)\s+(.*)$/) {110add_branch_config(\%seen, $1, $2, $3);111}112}113114return \%seen;115}116117my $remote = list_remote($git);118my $branch = list_branch($git);119120sub update_ls_remote {121my ($harder, $info) = @_;122123return if (($harder == 0) ||124(($harder == 1) && exists $info->{'LS_REMOTE'}));125126my @ref = map {127s|^[0-9a-f]{40}\s+refs/heads/||;128$_;129} $git->command(qw(ls-remote --heads), $info->{'URL'});130$info->{'LS_REMOTE'} = \@ref;131}132133sub list_wildcard_mapping {134my ($forced, $ours, $ls) = @_;135my %refs;136for (@$ls) {137$refs{$_} = 01; # bit #0 to say "they have"138}139for ($git->command('for-each-ref', "refs/remotes/$ours")) {140chomp;141next unless (s|^[0-9a-f]{40}\s[a-z]+\srefs/remotes/$ours/||);142next if ($_ eq 'HEAD');143$refs{$_} ||= 0;144$refs{$_} |= 02; # bit #1 to say "we have"145}146my (@new, @stale, @tracked);147for (sort keys %refs) {148my $have = $refs{$_};149if ($have == 1) {150push @new, $_;151}152elsif ($have == 2) {153push @stale, $_;154}155elsif ($have == 3) {156push @tracked, $_;157}158}159return \@new, \@stale, \@tracked;160}161162sub list_mapping {163my ($name, $info) = @_;164my $fetch = $info->{'FETCH'};165my $ls = $info->{'LS_REMOTE'};166my (@new, @stale, @tracked);167168for (@$fetch) {169next unless (/(\+)?([^:]+):(.*)/);170my ($forced, $theirs, $ours) = ($1, $2, $3);171if ($theirs eq 'refs/heads/*' &&172$ours =~ /^refs\/remotes\/(.*)\/\*$/) {173# wildcard mapping174my ($w_new, $w_stale, $w_tracked)175= list_wildcard_mapping($forced, $1, $ls);176push @new, @$w_new;177push @stale, @$w_stale;178push @tracked, @$w_tracked;179}180elsif ($theirs =~ /\*/ || $ours =~ /\*/) {181print STDERR "Warning: unrecognized mapping in remotes.$name.fetch: $_\n";182}183elsif ($theirs =~ s|^refs/heads/||) {184if (!grep { $_ eq $theirs } @$ls) {185push @stale, $theirs;186}187elsif ($ours ne '') {188push @tracked, $theirs;189}190}191}192return \@new, \@stale, \@tracked;193}194195sub show_mapping {196my ($name, $info) = @_;197my ($new, $stale, $tracked) = list_mapping($name, $info);198if (@$new) {199print " New remote branches (next fetch will store in remotes/$name)\n";200print " @$new\n";201}202if (@$stale) {203print " Stale tracking branches in remotes/$name (use 'git remote prune')\n";204print " @$stale\n";205}206if (@$tracked) {207print " Tracked remote branches\n";208print " @$tracked\n";209}210}211212sub prune_remote {213my ($name, $ls_remote) = @_;214if (!exists $remote->{$name}) {215print STDERR "No such remote $name\n";216return;217}218my $info = $remote->{$name};219update_ls_remote($ls_remote, $info);220221my ($new, $stale, $tracked) = list_mapping($name, $info);222my $prefix = "refs/remotes/$name";223foreach my $to_prune (@$stale) {224my @v = $git->command(qw(rev-parse --verify), "$prefix/$to_prune");225$git->command(qw(update-ref -d), "$prefix/$to_prune", $v[0]);226}227}228229sub show_remote {230my ($name, $ls_remote) = @_;231if (!exists $remote->{$name}) {232print STDERR "No such remote $name\n";233return;234}235my $info = $remote->{$name};236update_ls_remote($ls_remote, $info);237238print "* remote $name\n";239print " URL: $info->{'URL'}\n";240for my $branchname (sort keys %$branch) {241next if ($branch->{$branchname}{'REMOTE'} ne $name);242my @merged = map {243s|^refs/heads/||;244$_;245} split(' ',"@{$branch->{$branchname}{'MERGE'}}");246next unless (@merged);247print " Remote branch(es) merged with 'git pull' while on branch $branchname\n";248print " @merged\n";249}250if ($info->{'LS_REMOTE'}) {251show_mapping($name, $info);252}253}254255sub add_remote {256my ($name, $url, $opts) = @_;257if (exists $remote->{$name}) {258print STDERR "remote $name already exists.\n";259exit(1);260}261$git->command('config', "remote.$name.url", $url);262my $track = $opts->{'track'} || ["*"];263264for (@$track) {265$git->command('config', '--add', "remote.$name.fetch",266"+refs/heads/$_:refs/remotes/$name/$_");267}268if ($opts->{'fetch'}) {269$git->command('fetch', $name);270}271if (exists $opts->{'master'}) {272$git->command('symbolic-ref', "refs/remotes/$name/HEAD",273"refs/remotes/$name/$opts->{'master'}");274}275}276277sub add_usage {278print STDERR "Usage: git remote add [-f] [-t track]* [-m master] <name> <url>\n";279exit(1);280}281282if (!@ARGV) {283for (sort keys %$remote) {284print "$_\n";285}286}287elsif ($ARGV[0] eq 'show') {288my $ls_remote = 1;289my $i;290for ($i = 1; $i < @ARGV; $i++) {291if ($ARGV[$i] eq '-n') {292$ls_remote = 0;293}294else {295last;296}297}298if ($i >= @ARGV) {299print STDERR "Usage: git remote show <remote>\n";300exit(1);301}302for (; $i < @ARGV; $i++) {303show_remote($ARGV[$i], $ls_remote);304}305}306elsif ($ARGV[0] eq 'prune') {307my $ls_remote = 1;308my $i;309for ($i = 1; $i < @ARGV; $i++) {310if ($ARGV[$i] eq '-n') {311$ls_remote = 0;312}313else {314last;315}316}317if ($i >= @ARGV) {318print STDERR "Usage: git remote prune <remote>\n";319exit(1);320}321for (; $i < @ARGV; $i++) {322prune_remote($ARGV[$i], $ls_remote);323}324}325elsif ($ARGV[0] eq 'add') {326my %opts = ();327while (1 < @ARGV && $ARGV[1] =~ /^-/) {328my $opt = $ARGV[1];329shift @ARGV;330if ($opt eq '-f' || $opt eq '--fetch') {331$opts{'fetch'} = 1;332next;333}334if ($opt eq '-t' || $opt eq '--track') {335if (@ARGV < 1) {336add_usage();337}338$opts{'track'} ||= [];339push @{$opts{'track'}}, $ARGV[1];340shift @ARGV;341next;342}343if ($opt eq '-m' || $opt eq '--master') {344if ((@ARGV < 1) || exists $opts{'master'}) {345add_usage();346}347$opts{'master'} = $ARGV[1];348shift @ARGV;349next;350}351add_usage();352}353if (@ARGV != 3) {354add_usage();355}356add_remote($ARGV[1], $ARGV[2], \%opts);357}358else {359print STDERR "Usage: git remote\n";360print STDERR " git remote add <name> <url>\n";361print STDERR " git remote show <name>\n";362print STDERR " git remote prune <name>\n";363exit(1);364}