pack-objects: turn off bitmaps when we see --shallow lines
[git/mingw.git] / contrib / examples / git-remote.perl
blobd42df7b4188672e01dd7c95d5dcd73939f0fd600
1 #!/usr/bin/perl -w
3 use strict;
4 use Git;
5 my $git = Git->repository();
7 sub add_remote_config {
8 my ($hash, $name, $what, $value) = @_;
9 if ($what eq 'url') {
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';
33 return;
36 my $fh;
37 if (!open($fh, '<', $file)) {
38 print STDERR "Warning: cannot open $file\n";
39 return;
41 my $it = { 'SOURCE' => 'remotes' };
42 $hash->{$name} = $it;
43 while (<$fh>) {
44 chomp;
45 if (/^URL:\s*(.*)$/) {
46 # Having more than one is Ok -- it is used for push.
47 if (! exists $it->{'URL'}) {
48 $it->{'URL'} = $1;
51 elsif (/^Push:\s*(.*)$/) {
52 $it->{'PUSH'} ||= [];
53 push @{$it->{'PUSH'}}, $1;
55 elsif (/^Pull:\s*(.*)$/) {
56 $it->{'FETCH'} ||= [];
57 push @{$it->{'FETCH'}}, $1;
59 elsif (/^\#/) {
60 ; # ignore
62 else {
63 print STDERR "Warning: funny line in $file: $_\n";
66 close($fh);
69 sub list_remote {
70 my ($git) = @_;
71 my %seen = ();
72 my @remotes = eval {
73 $git->command(qw(config --get-regexp), '^remote\.');
75 for (@remotes) {
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)) {
83 local $_;
84 while ($_ = readdir($dh)) {
85 chomp;
86 next if (! -f "$dir/$_" || ! -r _);
87 add_remote_remotes(\%seen, "$dir/$_", $_);
91 return \%seen;
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;
108 sub list_branch {
109 my ($git) = @_;
110 my %seen = ();
111 my @branches = eval {
112 $git->command(qw(config --get-regexp), '^branch\.');
114 for (@branches) {
115 if (/^branch\.([^.]*)\.(\S*)\s+(.*)$/) {
116 add_branch_config(\%seen, $1, $2, $3);
120 return \%seen;
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'}));
132 my @ref = map { s|refs/heads/||; $_; } keys %{$git->remote_refs($info->{'URL'}, [ 'heads' ])};
133 $info->{'LS_REMOTE'} = \@ref;
136 sub list_wildcard_mapping {
137 my ($forced, $ours, $ls) = @_;
138 my %refs;
139 for (@$ls) {
140 $refs{$_} = 01; # bit #0 to say "they have"
142 for ($git->command('for-each-ref', "refs/remotes/$ours")) {
143 chomp;
144 next unless (s|^[0-9a-f]{40}\s[a-z]+\srefs/remotes/$ours/||);
145 next if ($_ eq 'HEAD');
146 $refs{$_} ||= 0;
147 $refs{$_} |= 02; # bit #1 to say "we have"
149 my (@new, @stale, @tracked);
150 for (sort keys %refs) {
151 my $have = $refs{$_};
152 if ($have == 1) {
153 push @new, $_;
155 elsif ($have == 2) {
156 push @stale, $_;
158 elsif ($have == 3) {
159 push @tracked, $_;
162 return \@new, \@stale, \@tracked;
165 sub list_mapping {
166 my ($name, $info) = @_;
167 my $fetch = $info->{'FETCH'};
168 my $ls = $info->{'LS_REMOTE'};
169 my (@new, @stale, @tracked);
171 for (@$fetch) {
172 next unless (/(\+)?([^:]+):(.*)/);
173 my ($forced, $theirs, $ours) = ($1, $2, $3);
174 if ($theirs eq 'refs/heads/*' &&
175 $ours =~ /^refs\/remotes\/(.*)\/\*$/) {
176 # wildcard mapping
177 my ($w_new, $w_stale, $w_tracked)
178 = list_wildcard_mapping($forced, $1, $ls);
179 push @new, @$w_new;
180 push @stale, @$w_stale;
181 push @tracked, @$w_tracked;
183 elsif ($theirs =~ /\*/ || $ours =~ /\*/) {
184 print STDERR "Warning: unrecognized mapping in remotes.$name.fetch: $_\n";
186 elsif ($theirs =~ s|^refs/heads/||) {
187 if (!grep { $_ eq $theirs } @$ls) {
188 push @stale, $theirs;
190 elsif ($ours ne '') {
191 push @tracked, $theirs;
195 return \@new, \@stale, \@tracked;
198 sub show_mapping {
199 my ($name, $info) = @_;
200 my ($new, $stale, $tracked) = list_mapping($name, $info);
201 if (@$new) {
202 print " New remote branches (next fetch will store in remotes/$name)\n";
203 print " @$new\n";
205 if (@$stale) {
206 print " Stale tracking branches in remotes/$name (use 'git remote prune')\n";
207 print " @$stale\n";
209 if (@$tracked) {
210 print " Tracked remote branches\n";
211 print " @$tracked\n";
215 sub prune_remote {
216 my ($name, $ls_remote) = @_;
217 if (!exists $remote->{$name}) {
218 print STDERR "No such remote $name\n";
219 return 1;
221 my $info = $remote->{$name};
222 update_ls_remote($ls_remote, $info);
224 my ($new, $stale, $tracked) = list_mapping($name, $info);
225 my $prefix = "refs/remotes/$name";
226 foreach my $to_prune (@$stale) {
227 my @v = $git->command(qw(rev-parse --verify), "$prefix/$to_prune");
228 $git->command(qw(update-ref -d), "$prefix/$to_prune", $v[0]);
230 return 0;
233 sub show_remote {
234 my ($name, $ls_remote) = @_;
235 if (!exists $remote->{$name}) {
236 print STDERR "No such remote $name\n";
237 return 1;
239 my $info = $remote->{$name};
240 update_ls_remote($ls_remote, $info);
242 print "* remote $name\n";
243 print " URL: $info->{'URL'}\n";
244 for my $branchname (sort keys %$branch) {
245 next unless (defined $branch->{$branchname}{'REMOTE'} &&
246 $branch->{$branchname}{'REMOTE'} eq $name);
247 my @merged = map {
248 s|^refs/heads/||;
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";
255 if ($info->{'LS_REMOTE'}) {
256 show_mapping($name, $info);
258 if ($info->{'PUSH'}) {
259 my @pushed = map {
260 s|^refs/heads/||;
261 s|^\+refs/heads/|+|;
262 s|:refs/heads/|:|;
264 } @{$info->{'PUSH'}};
265 print " Local branch(es) pushed with 'git push'\n";
266 print " @pushed\n";
268 return 0;
271 sub add_remote {
272 my ($name, $url, $opts) = @_;
273 if (exists $remote->{$name}) {
274 print STDERR "remote $name already exists.\n";
275 exit(1);
277 $git->command('config', "remote.$name.url", $url);
278 my $track = $opts->{'track'} || ["*"];
280 for (@$track) {
281 $git->command('config', '--add', "remote.$name.fetch",
282 $opts->{'mirror'} ?
283 "+refs/$_:refs/$_" :
284 "+refs/heads/$_:refs/remotes/$name/$_");
286 if ($opts->{'fetch'}) {
287 $git->command('fetch', $name);
289 if (exists $opts->{'master'}) {
290 $git->command('symbolic-ref', "refs/remotes/$name/HEAD",
291 "refs/remotes/$name/$opts->{'master'}");
295 sub update_remote {
296 my ($name) = @_;
297 my @remotes;
299 my $conf = $git->config("remotes." . $name);
300 if (defined($conf)) {
301 @remotes = split(' ', $conf);
302 } elsif ($name eq 'default') {
303 @remotes = ();
304 for (sort keys %$remote) {
305 my $do_fetch = $git->config_bool("remote." . $_ .
306 ".skipDefaultUpdate");
307 unless ($do_fetch) {
308 push @remotes, $_;
311 } else {
312 print STDERR "Remote group $name does not exist.\n";
313 exit(1);
315 for (@remotes) {
316 print "Updating $_\n";
317 $git->command('fetch', "$_");
321 sub rm_remote {
322 my ($name) = @_;
323 if (!exists $remote->{$name}) {
324 print STDERR "No such remote $name\n";
325 return 1;
328 $git->command('config', '--remove-section', "remote.$name");
330 eval {
331 my @trackers = $git->command('config', '--get-regexp',
332 'branch.*.remote', $name);
333 for (@trackers) {
334 /^branch\.(.*)?\.remote/;
335 $git->config('--unset', "branch.$1.remote");
336 $git->config('--unset', "branch.$1.merge");
340 my @refs = $git->command('for-each-ref',
341 '--format=%(refname) %(objectname)', "refs/remotes/$name");
342 for (@refs) {
343 my ($ref, $object) = split;
344 $git->command(qw(update-ref -d), $ref, $object);
346 return 0;
349 sub add_usage {
350 print STDERR "usage: git remote add [-f] [-t track]* [-m master] <name> <url>\n";
351 exit(1);
354 my $VERBOSE = 0;
355 @ARGV = grep {
356 if ($_ eq '-v' or $_ eq '--verbose') {
357 $VERBOSE=1;
359 } else {
362 } @ARGV;
364 if (!@ARGV) {
365 for (sort keys %$remote) {
366 print "$_";
367 print "\t$remote->{$_}->{URL}" if $VERBOSE;
368 print "\n";
371 elsif ($ARGV[0] eq 'show') {
372 my $ls_remote = 1;
373 my $i;
374 for ($i = 1; $i < @ARGV; $i++) {
375 if ($ARGV[$i] eq '-n') {
376 $ls_remote = 0;
378 else {
379 last;
382 if ($i >= @ARGV) {
383 print STDERR "usage: git remote show <remote>\n";
384 exit(1);
386 my $status = 0;
387 for (; $i < @ARGV; $i++) {
388 $status |= show_remote($ARGV[$i], $ls_remote);
390 exit($status);
392 elsif ($ARGV[0] eq 'update') {
393 if (@ARGV <= 1) {
394 update_remote("default");
395 exit(1);
397 for (my $i = 1; $i < @ARGV; $i++) {
398 update_remote($ARGV[$i]);
401 elsif ($ARGV[0] eq 'prune') {
402 my $ls_remote = 1;
403 my $i;
404 for ($i = 1; $i < @ARGV; $i++) {
405 if ($ARGV[$i] eq '-n') {
406 $ls_remote = 0;
408 else {
409 last;
412 if ($i >= @ARGV) {
413 print STDERR "usage: git remote prune <remote>\n";
414 exit(1);
416 my $status = 0;
417 for (; $i < @ARGV; $i++) {
418 $status |= prune_remote($ARGV[$i], $ls_remote);
420 exit($status);
422 elsif ($ARGV[0] eq 'add') {
423 my %opts = ();
424 while (1 < @ARGV && $ARGV[1] =~ /^-/) {
425 my $opt = $ARGV[1];
426 shift @ARGV;
427 if ($opt eq '-f' || $opt eq '--fetch') {
428 $opts{'fetch'} = 1;
429 next;
431 if ($opt eq '-t' || $opt eq '--track') {
432 if (@ARGV < 1) {
433 add_usage();
435 $opts{'track'} ||= [];
436 push @{$opts{'track'}}, $ARGV[1];
437 shift @ARGV;
438 next;
440 if ($opt eq '-m' || $opt eq '--master') {
441 if ((@ARGV < 1) || exists $opts{'master'}) {
442 add_usage();
444 $opts{'master'} = $ARGV[1];
445 shift @ARGV;
446 next;
448 if ($opt eq '--mirror') {
449 $opts{'mirror'} = 1;
450 next;
452 add_usage();
454 if (@ARGV != 3) {
455 add_usage();
457 add_remote($ARGV[1], $ARGV[2], \%opts);
459 elsif ($ARGV[0] eq 'rm') {
460 if (@ARGV <= 1) {
461 print STDERR "usage: git remote rm <remote>\n";
462 exit(1);
464 exit(rm_remote($ARGV[1]));
466 else {
467 print STDERR "usage: git remote\n";
468 print STDERR " git remote add <name> <url>\n";
469 print STDERR " git remote rm <name>\n";
470 print STDERR " git remote show <name>\n";
471 print STDERR " git remote prune <name>\n";
472 print STDERR " git remote update [group]\n";
473 exit(1);