4 my $git = Git->repository();
6 sub add_remote_config {
7 my ($hash, $name, $what, $value) = @_;
9 if (exists $hash->{$name}{'URL'}) {
10 print STDERR "Warning: more than one remote.$name.url\n";
12 $hash->{$name}{'URL'} = $value;
14 elsif ($what eq 'fetch') {
15 $hash->{$name}{'FETCH'} ||= [];
16 push @{$hash->{$name}{'FETCH'}}, $value;
18 elsif ($what eq 'push') {
19 $hash->{$name}{'PUSH'} ||= [];
20 push @{$hash->{$name}{'PUSH'}}, $value;
22 if (!exists $hash->{$name}{'SOURCE'}) {
23 $hash->{$name}{'SOURCE'} = 'config';
27 sub add_remote_remotes {
28 my ($hash, $file, $name) = @_;
30 if (exists $hash->{$name}) {
31 $hash->{$name}{'WARNING'} = 'ignored due to config';
36 if (!open($fh, '<', $file)) {
37 print STDERR "Warning: cannot open $file\n";
40 my $it = { 'SOURCE' => 'remotes' };
44 if (/^URL:\s*(.*)$/) {
45 # Having more than one is Ok -- it is used for push.
46 if (! exists $it->{'URL'}) {
50 elsif (/^Push:\s*(.*)$/) {
52 push @{$it->{'PUSH'}}, $1;
54 elsif (/^Pull:\s*(.*)$/) {
55 $it->{'FETCH'} ||= [];
56 push @{$it->{'FETCH'}}, $1;
62 print STDERR "Warning: funny line in $file: $_\n";
72 $git->command(qw(config --get-regexp), '^remote\.');
75 if (/^remote\.(\S+?)\.([^.\s]+)\s+(.*)$/) {
76 add_remote_config(\%seen, $1, $2, $3);
80 my $dir = $git->repo_path() . "/remotes";
81 if (opendir(my $dh, $dir)) {
83 while ($_ = readdir($dh)) {
85 next if (! -f "$dir/$_" || ! -r _);
86 add_remote_remotes(\%seen, "$dir/$_", $_);
93 sub 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";
99 $hash->{$name}{'REMOTE'} = $value;
101 elsif ($what eq 'merge') {
102 $hash->{$name}{'MERGE'} ||= [];
103 push @{$hash->{$name}{'MERGE'}}, $value;
110 my @branches = eval {
111 $git->command(qw(config --get-regexp), '^branch\.');
114 if (/^branch\.([^.]*)\.(\S*)\s+(.*)$/) {
115 add_branch_config(\%seen, $1, $2, $3);
122 my $remote = list_remote($git);
123 my $branch = list_branch($git);
125 sub update_ls_remote {
126 my ($harder, $info) = @_;
128 return if (($harder == 0) ||
129 (($harder == 1) && exists $info->{'LS_REMOTE'}));
132 s|^[0-9a-f]{40}\s+refs/heads/||;
134 } $git->command(qw(ls-remote --heads), $info->{'URL'});
135 $info->{'LS_REMOTE'} = \@ref;
138 sub list_wildcard_mapping {
139 my ($forced, $ours, $ls) = @_;
142 $refs{$_} = 01; # bit #0 to say "they have"
144 for ($git->command('for-each-ref', "refs/remotes/$ours")) {
146 next unless (s|^[0-9a-f]{40}\s[a-z]+\srefs/remotes/$ours/||);
147 next if ($_ eq 'HEAD');
149 $refs{$_} |= 02; # bit #1 to say "we have"
151 my (@new, @stale, @tracked);
152 for (sort keys %refs) {
153 my $have = $refs{$_};
164 return \@new, \@stale, \@tracked;
168 my ($name, $info) = @_;
169 my $fetch = $info->{'FETCH'};
170 my $ls = $info->{'LS_REMOTE'};
171 my (@new, @stale, @tracked);
174 next unless (/(\+)?([^:]+):(.*)/);
175 my ($forced, $theirs, $ours) = ($1, $2, $3);
176 if ($theirs eq 'refs/heads/*' &&
177 $ours =~ /^refs\/remotes\/(.*)\/\*$/) {
179 my ($w_new, $w_stale, $w_tracked)
180 = list_wildcard_mapping($forced, $1, $ls);
182 push @stale, @$w_stale;
183 push @tracked, @$w_tracked;
185 elsif ($theirs =~ /\*/ || $ours =~ /\*/) {
186 print STDERR "Warning: unrecognized mapping in remotes.$name.fetch: $_\n";
188 elsif ($theirs =~ s|^refs/heads/||) {
189 if (!grep { $_ eq $theirs } @$ls) {
190 push @stale, $theirs;
192 elsif ($ours ne '') {
193 push @tracked, $theirs;
197 return \@new, \@stale, \@tracked;
201 my ($name, $info) = @_;
202 my ($new, $stale, $tracked) = list_mapping($name, $info);
204 print " New remote branches (next fetch will store in remotes/$name)\n";
208 print " Stale tracking branches in remotes/$name (use 'git remote prune')\n";
212 print " Tracked remote branches\n";
213 print " @$tracked\n";
218 my ($name, $ls_remote) = @_;
219 if (!exists $remote->{$name}) {
220 print STDERR "No such remote $name\n";
223 my $info = $remote->{$name};
224 update_ls_remote($ls_remote, $info);
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]);
235 my ($name, $ls_remote) = @_;
236 if (!exists $remote->{$name}) {
237 print STDERR "No such remote $name\n";
240 my $info = $remote->{$name};
241 update_ls_remote($ls_remote, $info);
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);
250 } split(' ',"@{$branch->{$branchname}{'MERGE'}}");
251 next unless (@merged);
252 print " Remote branch(es) merged with 'git pull' while on branch $branchname\n";
255 if ($info->{'LS_REMOTE'}) {
256 show_mapping($name, $info);
258 if ($info->{'PUSH'}) {
264 } @{$info->{'PUSH'}};
265 print " Local branch(es) pushed with 'git push'\n";
271 my ($name, $url, $opts) = @_;
272 if (exists $remote->{$name}) {
273 print STDERR "remote $name already exists.\n";
276 $git->command('config', "remote.$name.url", $url);
277 my $track = $opts->{'track'} || ["*"];
280 $git->command('config', '--add', "remote.$name.fetch",
281 "+refs/heads/$_:refs/remotes/$name/$_");
283 if ($opts->{'fetch'}) {
284 $git->command('fetch', $name);
286 if (exists $opts->{'master'}) {
287 $git->command('symbolic-ref', "refs/remotes/$name/HEAD",
288 "refs/remotes/$name/$opts->{'master'}");
295 my $conf = $git->config("remotes." . $name);
296 if (defined($conf)) {
297 @remotes = split(' ', $conf);
298 } elsif ($name eq 'default') {
300 for (sort keys %$remote) {
301 my $do_fetch = $git->config_bool("remote." . $_ .
302 ".skipDefaultUpdate");
308 print STDERR "Remote group $name does not exists.\n";
312 print "Updating $_\n";
313 $git->command('fetch', "$_");
318 print STDERR "Usage: git remote add [-f] [-t track]* [-m master] <name> <url>\n";
323 for (sort keys %$remote) {
327 elsif ($ARGV[0] eq 'show') {
330 for ($i = 1; $i < @ARGV; $i++) {
331 if ($ARGV[$i] eq '-n') {
339 print STDERR "Usage: git remote show <remote>\n";
342 for (; $i < @ARGV; $i++) {
343 show_remote($ARGV[$i], $ls_remote);
346 elsif ($ARGV[0] eq 'update') {
348 update_remote("default");
351 for ($i = 1; $i < @ARGV; $i++) {
352 update_remote($ARGV[$i]);
355 elsif ($ARGV[0] eq 'prune') {
358 for ($i = 1; $i < @ARGV; $i++) {
359 if ($ARGV[$i] eq '-n') {
367 print STDERR "Usage: git remote prune <remote>\n";
370 for (; $i < @ARGV; $i++) {
371 prune_remote($ARGV[$i], $ls_remote);
374 elsif ($ARGV[0] eq 'add') {
376 while (1 < @ARGV && $ARGV[1] =~ /^-/) {
379 if ($opt eq '-f' || $opt eq '--fetch') {
383 if ($opt eq '-t' || $opt eq '--track') {
387 $opts{'track'} ||= [];
388 push @{$opts{'track'}}, $ARGV[1];
392 if ($opt eq '-m' || $opt eq '--master') {
393 if ((@ARGV < 1) || exists $opts{'master'}) {
396 $opts{'master'} = $ARGV[1];
405 add_remote($ARGV[1], $ARGV[2], \%opts);
408 print STDERR "Usage: git remote\n";
409 print STDERR " git remote add <name> <url>\n";
410 print STDERR " git remote show <name>\n";
411 print STDERR " git remote prune <name>\n";
412 print STDERR " git remote update [group]\n";