5 my $git = Git->repository();
7 sub add_remote_config {
8 my ($hash, $name, $what, $value) = @_;
10 # Having more than one is Ok -- it is used for push.
11 if (! exists $hash->{'URL'}) {
12 $hash->{$name}{'URL'} = $value;
15 elsif ($what eq 'fetch') {
16 $hash->{$name}{'FETCH'} ||= [];
17 push @{$hash->{$name}{'FETCH'}}, $value;
19 elsif ($what eq 'push') {
20 $hash->{$name}{'PUSH'} ||= [];
21 push @{$hash->{$name}{'PUSH'}}, $value;
23 if (!exists $hash->{$name}{'SOURCE'}) {
24 $hash->{$name}{'SOURCE'} = 'config';
28 sub add_remote_remotes {
29 my ($hash, $file, $name) = @_;
31 if (exists $hash->{$name}) {
32 $hash->{$name}{'WARNING'} = 'ignored due to config';
37 if (!open($fh, '<', $file)) {
38 print STDERR "Warning: cannot open $file\n";
41 my $it = { 'SOURCE' => 'remotes' };
45 if (/^URL:\s*(.*)$/) {
46 # Having more than one is Ok -- it is used for push.
47 if (! exists $it->{'URL'}) {
51 elsif (/^Push:\s*(.*)$/) {
53 push @{$it->{'PUSH'}}, $1;
55 elsif (/^Pull:\s*(.*)$/) {
56 $it->{'FETCH'} ||= [];
57 push @{$it->{'FETCH'}}, $1;
63 print STDERR "Warning: funny line in $file: $_\n";
73 $git->command(qw(config --get-regexp), '^remote\.');
76 if (/^remote\.(\S+?)\.([^.\s]+)\s+(.*)$/) {
77 add_remote_config(\%seen, $1, $2, $3);
81 my $dir = $git->repo_path() . "/remotes";
82 if (opendir(my $dh, $dir)) {
84 while ($_ = readdir($dh)) {
86 next if (! -f "$dir/$_" || ! -r _);
87 add_remote_remotes(\%seen, "$dir/$_", $_);
94 sub add_branch_config {
95 my ($hash, $name, $what, $value) = @_;
96 if ($what eq 'remote') {
97 if (exists $hash->{$name}{'REMOTE'}) {
98 print STDERR "Warning: more than one branch.$name.remote\n";
100 $hash->{$name}{'REMOTE'} = $value;
102 elsif ($what eq 'merge') {
103 $hash->{$name}{'MERGE'} ||= [];
104 push @{$hash->{$name}{'MERGE'}}, $value;
111 my @branches = eval {
112 $git->command(qw(config --get-regexp), '^branch\.');
115 if (/^branch\.([^.]*)\.(\S*)\s+(.*)$/) {
116 add_branch_config(\%seen, $1, $2, $3);
123 my $remote = list_remote($git);
124 my $branch = list_branch($git);
126 sub update_ls_remote {
127 my ($harder, $info) = @_;
129 return if (($harder == 0) ||
130 (($harder == 1) && exists $info->{'LS_REMOTE'}));
133 s|^[0-9a-f]{40}\s+refs/heads/||;
135 } $git->command(qw(ls-remote --heads), $info->{'URL'});
136 $info->{'LS_REMOTE'} = \@ref;
139 sub list_wildcard_mapping {
140 my ($forced, $ours, $ls) = @_;
143 $refs{$_} = 01; # bit #0 to say "they have"
145 for ($git->command('for-each-ref', "refs/remotes/$ours")) {
147 next unless (s|^[0-9a-f]{40}\s[a-z]+\srefs/remotes/$ours/||);
148 next if ($_ eq 'HEAD');
150 $refs{$_} |= 02; # bit #1 to say "we have"
152 my (@new, @stale, @tracked);
153 for (sort keys %refs) {
154 my $have = $refs{$_};
165 return \@new, \@stale, \@tracked;
169 my ($name, $info) = @_;
170 my $fetch = $info->{'FETCH'};
171 my $ls = $info->{'LS_REMOTE'};
172 my (@new, @stale, @tracked);
175 next unless (/(\+)?([^:]+):(.*)/);
176 my ($forced, $theirs, $ours) = ($1, $2, $3);
177 if ($theirs eq 'refs/heads/*' &&
178 $ours =~ /^refs\/remotes\/(.*)\/\*$/) {
180 my ($w_new, $w_stale, $w_tracked)
181 = list_wildcard_mapping($forced, $1, $ls);
183 push @stale, @$w_stale;
184 push @tracked, @$w_tracked;
186 elsif ($theirs =~ /\*/ || $ours =~ /\*/) {
187 print STDERR "Warning: unrecognized mapping in remotes.$name.fetch: $_\n";
189 elsif ($theirs =~ s|^refs/heads/||) {
190 if (!grep { $_ eq $theirs } @$ls) {
191 push @stale, $theirs;
193 elsif ($ours ne '') {
194 push @tracked, $theirs;
198 return \@new, \@stale, \@tracked;
202 my ($name, $info) = @_;
203 my ($new, $stale, $tracked) = list_mapping($name, $info);
205 print " New remote branches (next fetch will store in remotes/$name)\n";
209 print " Stale tracking branches in remotes/$name (use 'git remote prune')\n";
213 print " Tracked remote branches\n";
214 print " @$tracked\n";
219 my ($name, $ls_remote) = @_;
220 if (!exists $remote->{$name}) {
221 print STDERR "No such remote $name\n";
224 my $info = $remote->{$name};
225 update_ls_remote($ls_remote, $info);
227 my ($new, $stale, $tracked) = list_mapping($name, $info);
228 my $prefix = "refs/remotes/$name";
229 foreach my $to_prune (@$stale) {
230 my @v = $git->command(qw(rev-parse --verify), "$prefix/$to_prune");
231 $git->command(qw(update-ref -d), "$prefix/$to_prune", $v[0]);
237 my ($name, $ls_remote) = @_;
238 if (!exists $remote->{$name}) {
239 print STDERR "No such remote $name\n";
242 my $info = $remote->{$name};
243 update_ls_remote($ls_remote, $info);
245 print "* remote $name\n";
246 print " URL: $info->{'URL'}\n";
247 for my $branchname (sort keys %$branch) {
248 next unless (defined $branch->{$branchname}{'REMOTE'} &&
249 $branch->{$branchname}{'REMOTE'} eq $name);
253 } split(' ',"@{$branch->{$branchname}{'MERGE'}}");
254 next unless (@merged);
255 print " Remote branch(es) merged with 'git pull' while on branch $branchname\n";
258 if ($info->{'LS_REMOTE'}) {
259 show_mapping($name, $info);
261 if ($info->{'PUSH'}) {
267 } @{$info->{'PUSH'}};
268 print " Local branch(es) pushed with 'git push'\n";
275 my ($name, $url, $opts) = @_;
276 if (exists $remote->{$name}) {
277 print STDERR "remote $name already exists.\n";
280 $git->command('config', "remote.$name.url", $url);
281 my $track = $opts->{'track'} || ["*"];
284 $git->command('config', '--add', "remote.$name.fetch",
287 "+refs/heads/$_:refs/remotes/$name/$_");
289 if ($opts->{'fetch'}) {
290 $git->command('fetch', $name);
292 if (exists $opts->{'master'}) {
293 $git->command('symbolic-ref', "refs/remotes/$name/HEAD",
294 "refs/remotes/$name/$opts->{'master'}");
302 my $conf = $git->config("remotes." . $name);
303 if (defined($conf)) {
304 @remotes = split(' ', $conf);
305 } elsif ($name eq 'default') {
307 for (sort keys %$remote) {
308 my $do_fetch = $git->config_bool("remote." . $_ .
309 ".skipDefaultUpdate");
315 print STDERR "Remote group $name does not exists.\n";
319 print "Updating $_\n";
320 $git->command('fetch', "$_");
326 if (!exists $remote->{$name}) {
327 print STDERR "No such remote $name\n";
331 $git->command('config', '--remove-section', "remote.$name");
334 my @trackers = $git->command('config', '--get-regexp',
335 'branch.*.remote', $name);
337 /^branch\.(.*)?\.remote/;
338 $git->config('--unset', "branch.$1.remote");
339 $git->config('--unset', "branch.$1.merge");
343 my @refs = $git->command('for-each-ref',
344 '--format=%(refname) %(objectname)', "refs/remotes/$name");
346 my ($ref, $object) = split;
347 $git->command(qw(update-ref -d), $ref, $object);
353 print STDERR "Usage: git remote add [-f] [-t track]* [-m master] <name> <url>\n";
359 if ($_ eq '-v' or $_ eq '--verbose') {
368 for (sort keys %$remote) {
370 print "\t$remote->{$_}->{URL}" if $VERBOSE;
374 elsif ($ARGV[0] eq 'show') {
377 for ($i = 1; $i < @ARGV; $i++) {
378 if ($ARGV[$i] eq '-n') {
386 print STDERR "Usage: git remote show <remote>\n";
390 for (; $i < @ARGV; $i++) {
391 $status |= show_remote($ARGV[$i], $ls_remote);
395 elsif ($ARGV[0] eq 'update') {
397 update_remote("default");
400 for (my $i = 1; $i < @ARGV; $i++) {
401 update_remote($ARGV[$i]);
404 elsif ($ARGV[0] eq 'prune') {
407 for ($i = 1; $i < @ARGV; $i++) {
408 if ($ARGV[$i] eq '-n') {
416 print STDERR "Usage: git remote prune <remote>\n";
420 for (; $i < @ARGV; $i++) {
421 $status |= prune_remote($ARGV[$i], $ls_remote);
425 elsif ($ARGV[0] eq 'add') {
427 while (1 < @ARGV && $ARGV[1] =~ /^-/) {
430 if ($opt eq '-f' || $opt eq '--fetch') {
434 if ($opt eq '-t' || $opt eq '--track') {
438 $opts{'track'} ||= [];
439 push @{$opts{'track'}}, $ARGV[1];
443 if ($opt eq '-m' || $opt eq '--master') {
444 if ((@ARGV < 1) || exists $opts{'master'}) {
447 $opts{'master'} = $ARGV[1];
451 if ($opt eq '--mirror') {
460 add_remote($ARGV[1], $ARGV[2], \%opts);
462 elsif ($ARGV[0] eq 'rm') {
464 print STDERR "Usage: git remote rm <remote>\n";
467 exit(rm_remote($ARGV[1]));
470 print STDERR "Usage: git remote\n";
471 print STDERR " git remote add <name> <url>\n";
472 print STDERR " git remote rm <name>\n";
473 print STDERR " git remote show <name>\n";
474 print STDERR " git remote prune <name>\n";
475 print STDERR " git remote update [group]\n";