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 if (!exists $hash->{$name}{'SOURCE'}) {
19 $hash->{$name}{'SOURCE'} = 'config';
23 sub add_remote_remotes
{
24 my ($hash, $file, $name) = @_;
26 if (exists $hash->{$name}) {
27 $hash->{$name}{'WARNING'} = 'ignored due to config';
32 if (!open($fh, '<', $file)) {
33 print STDERR
"Warning: cannot open $file\n";
36 my $it = { 'SOURCE' => 'remotes' };
40 if (/^URL:\s*(.*)$/) {
41 # Having more than one is Ok -- it is used for push.
42 if (! exists $it->{'URL'}) {
46 elsif (/^Push:\s*(.*)$/) {
49 elsif (/^Pull:\s*(.*)$/) {
50 $it->{'FETCH'} ||= [];
51 push @
{$it->{'FETCH'}}, $1;
57 print STDERR
"Warning: funny line in $file: $_\n";
67 $git->command(qw(config --get-regexp), '^remote\.');
70 if (/^remote\.(\S+?)\.([^.\s]+)\s+(.*)$/) {
71 add_remote_config
(\
%seen, $1, $2, $3);
75 my $dir = $git->repo_path() . "/remotes";
76 if (opendir(my $dh, $dir)) {
78 while ($_ = readdir($dh)) {
80 next if (! -f
"$dir/$_" || ! -r _
);
81 add_remote_remotes
(\
%seen, "$dir/$_", $_);
88 sub add_branch_config
{
89 my ($hash, $name, $what, $value) = @_;
90 if ($what eq 'remote') {
91 if (exists $hash->{$name}{'REMOTE'}) {
92 print STDERR
"Warning: more than one branch.$name.remote\n";
94 $hash->{$name}{'REMOTE'} = $value;
96 elsif ($what eq 'merge') {
97 $hash->{$name}{'MERGE'} ||= [];
98 push @
{$hash->{$name}{'MERGE'}}, $value;
105 my @branches = eval {
106 $git->command(qw(config --get-regexp), '^branch\.');
109 if (/^branch\.([^.]*)\.(\S*)\s+(.*)$/) {
110 add_branch_config
(\
%seen, $1, $2, $3);
117 my $remote = list_remote
($git);
118 my $branch = list_branch
($git);
120 sub update_ls_remote
{
121 my ($harder, $info) = @_;
123 return if (($harder == 0) ||
124 (($harder == 1) && exists $info->{'LS_REMOTE'}));
127 s
|^[0-9a
-f
]{40}\s
+refs
/heads/||;
129 } $git->command(qw(ls-remote --heads), $info->{'URL'});
130 $info->{'LS_REMOTE'} = \
@ref;
133 sub list_wildcard_mapping
{
134 my ($forced, $ours, $ls) = @_;
137 $refs{$_} = 01; # bit #0 to say "they have"
139 for ($git->command('for-each-ref', "refs/remotes/$ours")) {
141 next unless (s
|^[0-9a
-f
]{40}\s
[a
-z
]+\srefs
/remotes
/$ours/||);
142 next if ($_ eq 'HEAD');
144 $refs{$_} |= 02; # bit #1 to say "we have"
146 my (@new, @stale, @tracked);
147 for (sort keys %refs) {
148 my $have = $refs{$_};
159 return \
@new, \
@stale, \
@tracked;
163 my ($name, $info) = @_;
164 my $fetch = $info->{'FETCH'};
165 my $ls = $info->{'LS_REMOTE'};
166 my (@new, @stale, @tracked);
169 next unless (/(\+)?([^:]+):(.*)/);
170 my ($forced, $theirs, $ours) = ($1, $2, $3);
171 if ($theirs eq 'refs/heads/*' &&
172 $ours =~ /^refs\/remotes\
/(.*)\/\
*$/) {
174 my ($w_new, $w_stale, $w_tracked)
175 = list_wildcard_mapping
($forced, $1, $ls);
177 push @stale, @
$w_stale;
178 push @tracked, @
$w_tracked;
180 elsif ($theirs =~ /\*/ || $ours =~ /\*/) {
181 print STDERR
"Warning: unrecognized mapping in remotes.$name.fetch: $_\n";
183 elsif ($theirs =~ s
|^refs
/heads/||) {
184 if (!grep { $_ eq $theirs } @
$ls) {
185 push @stale, $theirs;
187 elsif ($ours ne '') {
188 push @tracked, $theirs;
192 return \
@new, \
@stale, \
@tracked;
196 my ($name, $info) = @_;
197 my ($new, $stale, $tracked) = list_mapping
($name, $info);
199 print " New remote branches (next fetch will store in remotes/$name)\n";
203 print " Stale tracking branches in remotes/$name (use 'git remote prune')\n";
207 print " Tracked remote branches\n";
208 print " @$tracked\n";
213 my ($name, $ls_remote) = @_;
214 if (!exists $remote->{$name}) {
215 print STDERR
"No such remote $name\n";
218 my $info = $remote->{$name};
219 update_ls_remote
($ls_remote, $info);
221 my ($new, $stale, $tracked) = list_mapping
($name, $info);
222 my $prefix = "refs/remotes/$name";
223 foreach my $to_prune (@
$stale) {
224 my @v = $git->command(qw(rev-parse --verify), "$prefix/$to_prune");
225 $git->command(qw(update-ref -d), "$prefix/$to_prune", $v[0]);
230 my ($name, $ls_remote) = @_;
231 if (!exists $remote->{$name}) {
232 print STDERR
"No such remote $name\n";
235 my $info = $remote->{$name};
236 update_ls_remote
($ls_remote, $info);
238 print "* remote $name\n";
239 print " URL: $info->{'URL'}\n";
240 for my $branchname (sort keys %$branch) {
241 next if ($branch->{$branchname}{'REMOTE'} ne $name);
245 } split(' ',"@{$branch->{$branchname}{'MERGE'}}");
246 next unless (@merged);
247 print " Remote branch(es) merged with 'git pull' while on branch $branchname\n";
250 if ($info->{'LS_REMOTE'}) {
251 show_mapping
($name, $info);
256 my ($name, $url, $opts) = @_;
257 if (exists $remote->{$name}) {
258 print STDERR
"remote $name already exists.\n";
261 $git->command('config', "remote.$name.url", $url);
262 my $track = $opts->{'track'} || ["*"];
265 $git->command('config', '--add', "remote.$name.fetch",
266 "+refs/heads/$_:refs/remotes/$name/$_");
268 if ($opts->{'fetch'}) {
269 $git->command('fetch', $name);
271 if (exists $opts->{'master'}) {
272 $git->command('symbolic-ref', "refs/remotes/$name/HEAD",
273 "refs/remotes/$name/$opts->{'master'}");
280 my $conf = $git->config("remotes." . $name);
281 if (defined($conf)) {
282 @remotes = split(' ', $conf);
283 } elsif ($name eq 'default') {
285 for (sort keys %$remote) {
286 my $do_fetch = $git->config_boolean("remote." . $_ .
287 ".skipDefaultUpdate");
288 if (!defined($do_fetch) || $do_fetch ne "true") {
293 print STDERR
"Remote group $name does not exists.\n";
297 print "Updating $_\n";
298 $git->command('fetch', "$_");
303 print STDERR
"Usage: git remote add [-f] [-t track]* [-m master] <name> <url>\n";
308 for (sort keys %$remote) {
312 elsif ($ARGV[0] eq 'show') {
315 for ($i = 1; $i < @ARGV; $i++) {
316 if ($ARGV[$i] eq '-n') {
324 print STDERR
"Usage: git remote show <remote>\n";
327 for (; $i < @ARGV; $i++) {
328 show_remote
($ARGV[$i], $ls_remote);
331 elsif ($ARGV[0] eq 'update') {
333 update_remote
("default");
336 for ($i = 1; $i < @ARGV; $i++) {
337 update_remote
($ARGV[$i]);
340 elsif ($ARGV[0] eq 'prune') {
343 for ($i = 1; $i < @ARGV; $i++) {
344 if ($ARGV[$i] eq '-n') {
352 print STDERR
"Usage: git remote prune <remote>\n";
355 for (; $i < @ARGV; $i++) {
356 prune_remote
($ARGV[$i], $ls_remote);
359 elsif ($ARGV[0] eq 'add') {
361 while (1 < @ARGV && $ARGV[1] =~ /^-/) {
364 if ($opt eq '-f' || $opt eq '--fetch') {
368 if ($opt eq '-t' || $opt eq '--track') {
372 $opts{'track'} ||= [];
373 push @
{$opts{'track'}}, $ARGV[1];
377 if ($opt eq '-m' || $opt eq '--master') {
378 if ((@ARGV < 1) || exists $opts{'master'}) {
381 $opts{'master'} = $ARGV[1];
390 add_remote
($ARGV[1], $ARGV[2], \
%opts);
393 print STDERR
"Usage: git remote\n";
394 print STDERR
" git remote add <name> <url>\n";
395 print STDERR
" git remote show <name>\n";
396 print STDERR
" git remote prune <name>\n";
397 print STDERR
" git remote update [group]\n";