Junio C Hamano | e194cd1 | 2007-01-03 12:13:04 -0800 | [diff] [blame] | 1 | #!/usr/bin/perl -w |
| 2 | |
| 3 | use Git; |
| 4 | my $git = Git->repository(); |
| 5 | |
| 6 | sub add_remote_config { |
| 7 | my ($hash, $name, $what, $value) = @_; |
| 8 | if ($what eq 'url') { |
| 9 | if (exists $hash->{$name}{'URL'}) { |
| 10 | print STDERR "Warning: more than one remote.$name.url\n"; |
| 11 | } |
| 12 | $hash->{$name}{'URL'} = $value; |
| 13 | } |
| 14 | elsif ($what eq 'fetch') { |
| 15 | $hash->{$name}{'FETCH'} ||= []; |
| 16 | push @{$hash->{$name}{'FETCH'}}, $value; |
| 17 | } |
Johannes Sixt | 8bf0e3d | 2007-03-18 21:34:46 +0100 | [diff] [blame] | 18 | elsif ($what eq 'push') { |
| 19 | $hash->{$name}{'PUSH'} ||= []; |
| 20 | push @{$hash->{$name}{'PUSH'}}, $value; |
| 21 | } |
Junio C Hamano | e194cd1 | 2007-01-03 12:13:04 -0800 | [diff] [blame] | 22 | if (!exists $hash->{$name}{'SOURCE'}) { |
| 23 | $hash->{$name}{'SOURCE'} = 'config'; |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | sub add_remote_remotes { |
| 28 | my ($hash, $file, $name) = @_; |
| 29 | |
| 30 | if (exists $hash->{$name}) { |
| 31 | $hash->{$name}{'WARNING'} = 'ignored due to config'; |
| 32 | return; |
| 33 | } |
| 34 | |
| 35 | my $fh; |
| 36 | if (!open($fh, '<', $file)) { |
| 37 | print STDERR "Warning: cannot open $file\n"; |
| 38 | return; |
| 39 | } |
| 40 | my $it = { 'SOURCE' => 'remotes' }; |
| 41 | $hash->{$name} = $it; |
| 42 | while (<$fh>) { |
| 43 | chomp; |
| 44 | if (/^URL:\s*(.*)$/) { |
| 45 | # Having more than one is Ok -- it is used for push. |
| 46 | if (! exists $it->{'URL'}) { |
| 47 | $it->{'URL'} = $1; |
| 48 | } |
| 49 | } |
| 50 | elsif (/^Push:\s*(.*)$/) { |
Johannes Sixt | 8bf0e3d | 2007-03-18 21:34:46 +0100 | [diff] [blame] | 51 | $it->{'PUSH'} ||= []; |
| 52 | push @{$it->{'PUSH'}}, $1; |
Junio C Hamano | e194cd1 | 2007-01-03 12:13:04 -0800 | [diff] [blame] | 53 | } |
| 54 | elsif (/^Pull:\s*(.*)$/) { |
| 55 | $it->{'FETCH'} ||= []; |
| 56 | push @{$it->{'FETCH'}}, $1; |
| 57 | } |
| 58 | elsif (/^\#/) { |
| 59 | ; # ignore |
| 60 | } |
| 61 | else { |
| 62 | print STDERR "Warning: funny line in $file: $_\n"; |
| 63 | } |
| 64 | } |
| 65 | close($fh); |
| 66 | } |
| 67 | |
| 68 | sub list_remote { |
| 69 | my ($git) = @_; |
| 70 | my %seen = (); |
| 71 | my @remotes = eval { |
Tom Prince | e0d10e1 | 2007-01-28 16:16:53 -0800 | [diff] [blame] | 72 | $git->command(qw(config --get-regexp), '^remote\.'); |
Junio C Hamano | e194cd1 | 2007-01-03 12:13:04 -0800 | [diff] [blame] | 73 | }; |
| 74 | for (@remotes) { |
Pavel Roskin | b5a40a5 | 2007-02-21 00:03:36 -0500 | [diff] [blame] | 75 | if (/^remote\.(\S+?)\.([^.\s]+)\s+(.*)$/) { |
Junio C Hamano | e194cd1 | 2007-01-03 12:13:04 -0800 | [diff] [blame] | 76 | add_remote_config(\%seen, $1, $2, $3); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | my $dir = $git->repo_path() . "/remotes"; |
| 81 | if (opendir(my $dh, $dir)) { |
| 82 | local $_; |
| 83 | while ($_ = readdir($dh)) { |
| 84 | chomp; |
| 85 | next if (! -f "$dir/$_" || ! -r _); |
| 86 | add_remote_remotes(\%seen, "$dir/$_", $_); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | return \%seen; |
| 91 | } |
| 92 | |
| 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"; |
| 98 | } |
| 99 | $hash->{$name}{'REMOTE'} = $value; |
| 100 | } |
| 101 | elsif ($what eq 'merge') { |
| 102 | $hash->{$name}{'MERGE'} ||= []; |
| 103 | push @{$hash->{$name}{'MERGE'}}, $value; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | sub list_branch { |
| 108 | my ($git) = @_; |
| 109 | my %seen = (); |
| 110 | my @branches = eval { |
Tom Prince | e0d10e1 | 2007-01-28 16:16:53 -0800 | [diff] [blame] | 111 | $git->command(qw(config --get-regexp), '^branch\.'); |
Junio C Hamano | e194cd1 | 2007-01-03 12:13:04 -0800 | [diff] [blame] | 112 | }; |
| 113 | for (@branches) { |
| 114 | if (/^branch\.([^.]*)\.(\S*)\s+(.*)$/) { |
| 115 | add_branch_config(\%seen, $1, $2, $3); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | return \%seen; |
| 120 | } |
| 121 | |
| 122 | my $remote = list_remote($git); |
| 123 | my $branch = list_branch($git); |
| 124 | |
| 125 | sub update_ls_remote { |
| 126 | my ($harder, $info) = @_; |
| 127 | |
| 128 | return if (($harder == 0) || |
| 129 | (($harder == 1) && exists $info->{'LS_REMOTE'})); |
| 130 | |
| 131 | my @ref = map { |
| 132 | s|^[0-9a-f]{40}\s+refs/heads/||; |
| 133 | $_; |
| 134 | } $git->command(qw(ls-remote --heads), $info->{'URL'}); |
| 135 | $info->{'LS_REMOTE'} = \@ref; |
| 136 | } |
| 137 | |
Shawn O. Pearce | 7a8c9ec | 2007-02-02 00:05:55 -0500 | [diff] [blame] | 138 | sub list_wildcard_mapping { |
Junio C Hamano | e194cd1 | 2007-01-03 12:13:04 -0800 | [diff] [blame] | 139 | my ($forced, $ours, $ls) = @_; |
| 140 | my %refs; |
| 141 | for (@$ls) { |
| 142 | $refs{$_} = 01; # bit #0 to say "they have" |
| 143 | } |
| 144 | for ($git->command('for-each-ref', "refs/remotes/$ours")) { |
| 145 | chomp; |
| 146 | next unless (s|^[0-9a-f]{40}\s[a-z]+\srefs/remotes/$ours/||); |
| 147 | next if ($_ eq 'HEAD'); |
| 148 | $refs{$_} ||= 0; |
| 149 | $refs{$_} |= 02; # bit #1 to say "we have" |
| 150 | } |
| 151 | my (@new, @stale, @tracked); |
| 152 | for (sort keys %refs) { |
| 153 | my $have = $refs{$_}; |
| 154 | if ($have == 1) { |
| 155 | push @new, $_; |
| 156 | } |
| 157 | elsif ($have == 2) { |
| 158 | push @stale, $_; |
| 159 | } |
| 160 | elsif ($have == 3) { |
| 161 | push @tracked, $_; |
| 162 | } |
| 163 | } |
Shawn O. Pearce | 7a8c9ec | 2007-02-02 00:05:55 -0500 | [diff] [blame] | 164 | return \@new, \@stale, \@tracked; |
Junio C Hamano | e194cd1 | 2007-01-03 12:13:04 -0800 | [diff] [blame] | 165 | } |
| 166 | |
Shawn O. Pearce | 7a8c9ec | 2007-02-02 00:05:55 -0500 | [diff] [blame] | 167 | sub list_mapping { |
Junio C Hamano | e194cd1 | 2007-01-03 12:13:04 -0800 | [diff] [blame] | 168 | my ($name, $info) = @_; |
| 169 | my $fetch = $info->{'FETCH'}; |
| 170 | my $ls = $info->{'LS_REMOTE'}; |
Shawn O. Pearce | 7a8c9ec | 2007-02-02 00:05:55 -0500 | [diff] [blame] | 171 | my (@new, @stale, @tracked); |
Junio C Hamano | e194cd1 | 2007-01-03 12:13:04 -0800 | [diff] [blame] | 172 | |
| 173 | for (@$fetch) { |
| 174 | next unless (/(\+)?([^:]+):(.*)/); |
| 175 | my ($forced, $theirs, $ours) = ($1, $2, $3); |
| 176 | if ($theirs eq 'refs/heads/*' && |
| 177 | $ours =~ /^refs\/remotes\/(.*)\/\*$/) { |
| 178 | # wildcard mapping |
Shawn O. Pearce | 7a8c9ec | 2007-02-02 00:05:55 -0500 | [diff] [blame] | 179 | my ($w_new, $w_stale, $w_tracked) |
| 180 | = list_wildcard_mapping($forced, $1, $ls); |
| 181 | push @new, @$w_new; |
| 182 | push @stale, @$w_stale; |
| 183 | push @tracked, @$w_tracked; |
Junio C Hamano | e194cd1 | 2007-01-03 12:13:04 -0800 | [diff] [blame] | 184 | } |
| 185 | elsif ($theirs =~ /\*/ || $ours =~ /\*/) { |
| 186 | print STDERR "Warning: unrecognized mapping in remotes.$name.fetch: $_\n"; |
| 187 | } |
| 188 | elsif ($theirs =~ s|^refs/heads/||) { |
| 189 | if (!grep { $_ eq $theirs } @$ls) { |
| 190 | push @stale, $theirs; |
| 191 | } |
| 192 | elsif ($ours ne '') { |
| 193 | push @tracked, $theirs; |
| 194 | } |
| 195 | } |
| 196 | } |
Shawn O. Pearce | 7a8c9ec | 2007-02-02 00:05:55 -0500 | [diff] [blame] | 197 | return \@new, \@stale, \@tracked; |
| 198 | } |
| 199 | |
| 200 | sub show_mapping { |
| 201 | my ($name, $info) = @_; |
| 202 | my ($new, $stale, $tracked) = list_mapping($name, $info); |
| 203 | if (@$new) { |
| 204 | print " New remote branches (next fetch will store in remotes/$name)\n"; |
| 205 | print " @$new\n"; |
Junio C Hamano | e194cd1 | 2007-01-03 12:13:04 -0800 | [diff] [blame] | 206 | } |
Shawn O. Pearce | 7a8c9ec | 2007-02-02 00:05:55 -0500 | [diff] [blame] | 207 | if (@$stale) { |
Shawn O. Pearce | 859607d | 2007-02-02 00:06:08 -0500 | [diff] [blame] | 208 | print " Stale tracking branches in remotes/$name (use 'git remote prune')\n"; |
Shawn O. Pearce | 7a8c9ec | 2007-02-02 00:05:55 -0500 | [diff] [blame] | 209 | print " @$stale\n"; |
| 210 | } |
| 211 | if (@$tracked) { |
Junio C Hamano | e194cd1 | 2007-01-03 12:13:04 -0800 | [diff] [blame] | 212 | print " Tracked remote branches\n"; |
Shawn O. Pearce | 7a8c9ec | 2007-02-02 00:05:55 -0500 | [diff] [blame] | 213 | print " @$tracked\n"; |
Junio C Hamano | e194cd1 | 2007-01-03 12:13:04 -0800 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
Shawn O. Pearce | 859607d | 2007-02-02 00:06:08 -0500 | [diff] [blame] | 217 | sub prune_remote { |
| 218 | my ($name, $ls_remote) = @_; |
| 219 | if (!exists $remote->{$name}) { |
| 220 | print STDERR "No such remote $name\n"; |
| 221 | return; |
| 222 | } |
| 223 | my $info = $remote->{$name}; |
| 224 | update_ls_remote($ls_remote, $info); |
| 225 | |
| 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]); |
| 231 | } |
| 232 | } |
| 233 | |
Junio C Hamano | e194cd1 | 2007-01-03 12:13:04 -0800 | [diff] [blame] | 234 | sub show_remote { |
| 235 | my ($name, $ls_remote) = @_; |
| 236 | if (!exists $remote->{$name}) { |
| 237 | print STDERR "No such remote $name\n"; |
| 238 | return; |
| 239 | } |
| 240 | my $info = $remote->{$name}; |
| 241 | update_ls_remote($ls_remote, $info); |
| 242 | |
| 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); |
| 247 | my @merged = map { |
| 248 | s|^refs/heads/||; |
| 249 | $_; |
| 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"; |
| 254 | } |
| 255 | if ($info->{'LS_REMOTE'}) { |
| 256 | show_mapping($name, $info); |
| 257 | } |
Johannes Sixt | 8bf0e3d | 2007-03-18 21:34:46 +0100 | [diff] [blame] | 258 | if ($info->{'PUSH'}) { |
| 259 | my @pushed = map { |
| 260 | s|^refs/heads/||; |
| 261 | s|:refs/heads/|:|; |
| 262 | $_; |
| 263 | } @{$info->{'PUSH'}}; |
| 264 | print " Local branch(es) pushed with 'git push'\n"; |
| 265 | print " @pushed\n"; |
| 266 | } |
Junio C Hamano | e194cd1 | 2007-01-03 12:13:04 -0800 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | sub add_remote { |
Junio C Hamano | b6f5da1 | 2007-02-01 23:30:03 -0800 | [diff] [blame] | 270 | my ($name, $url, $opts) = @_; |
Junio C Hamano | e194cd1 | 2007-01-03 12:13:04 -0800 | [diff] [blame] | 271 | if (exists $remote->{$name}) { |
| 272 | print STDERR "remote $name already exists.\n"; |
| 273 | exit(1); |
| 274 | } |
Tom Prince | e0d10e1 | 2007-01-28 16:16:53 -0800 | [diff] [blame] | 275 | $git->command('config', "remote.$name.url", $url); |
Junio C Hamano | b6f5da1 | 2007-02-01 23:30:03 -0800 | [diff] [blame] | 276 | my $track = $opts->{'track'} || ["*"]; |
| 277 | |
| 278 | for (@$track) { |
| 279 | $git->command('config', '--add', "remote.$name.fetch", |
| 280 | "+refs/heads/$_:refs/remotes/$name/$_"); |
| 281 | } |
| 282 | if ($opts->{'fetch'}) { |
| 283 | $git->command('fetch', $name); |
| 284 | } |
| 285 | if (exists $opts->{'master'}) { |
| 286 | $git->command('symbolic-ref', "refs/remotes/$name/HEAD", |
| 287 | "refs/remotes/$name/$opts->{'master'}"); |
| 288 | } |
| 289 | } |
| 290 | |
Theodore Ts'o | 1918278 | 2007-02-20 15:13:43 -0500 | [diff] [blame] | 291 | sub update_remote { |
| 292 | my ($name) = @_; |
| 293 | |
| 294 | my $conf = $git->config("remotes." . $name); |
| 295 | if (defined($conf)) { |
| 296 | @remotes = split(' ', $conf); |
| 297 | } elsif ($name eq 'default') { |
| 298 | undef @remotes; |
| 299 | for (sort keys %$remote) { |
| 300 | my $do_fetch = $git->config_boolean("remote." . $_ . |
| 301 | ".skipDefaultUpdate"); |
| 302 | if (!defined($do_fetch) || $do_fetch ne "true") { |
| 303 | push @remotes, $_; |
| 304 | } |
| 305 | } |
| 306 | } else { |
| 307 | print STDERR "Remote group $name does not exists.\n"; |
| 308 | exit(1); |
| 309 | } |
| 310 | for (@remotes) { |
| 311 | print "Updating $_\n"; |
| 312 | $git->command('fetch', "$_"); |
| 313 | } |
| 314 | } |
| 315 | |
Junio C Hamano | b6f5da1 | 2007-02-01 23:30:03 -0800 | [diff] [blame] | 316 | sub add_usage { |
| 317 | print STDERR "Usage: git remote add [-f] [-t track]* [-m master] <name> <url>\n"; |
| 318 | exit(1); |
Junio C Hamano | e194cd1 | 2007-01-03 12:13:04 -0800 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | if (!@ARGV) { |
| 322 | for (sort keys %$remote) { |
| 323 | print "$_\n"; |
| 324 | } |
| 325 | } |
| 326 | elsif ($ARGV[0] eq 'show') { |
| 327 | my $ls_remote = 1; |
| 328 | my $i; |
| 329 | for ($i = 1; $i < @ARGV; $i++) { |
| 330 | if ($ARGV[$i] eq '-n') { |
| 331 | $ls_remote = 0; |
| 332 | } |
| 333 | else { |
| 334 | last; |
| 335 | } |
| 336 | } |
| 337 | if ($i >= @ARGV) { |
| 338 | print STDERR "Usage: git remote show <remote>\n"; |
| 339 | exit(1); |
| 340 | } |
| 341 | for (; $i < @ARGV; $i++) { |
| 342 | show_remote($ARGV[$i], $ls_remote); |
| 343 | } |
| 344 | } |
Theodore Ts'o | 1e592d6 | 2007-02-18 23:00:00 -0500 | [diff] [blame] | 345 | elsif ($ARGV[0] eq 'update') { |
Theodore Ts'o | 1918278 | 2007-02-20 15:13:43 -0500 | [diff] [blame] | 346 | if (@ARGV <= 1) { |
| 347 | update_remote("default"); |
| 348 | exit(1); |
Theodore Ts'o | 1e592d6 | 2007-02-18 23:00:00 -0500 | [diff] [blame] | 349 | } |
Theodore Ts'o | 1918278 | 2007-02-20 15:13:43 -0500 | [diff] [blame] | 350 | for ($i = 1; $i < @ARGV; $i++) { |
| 351 | update_remote($ARGV[$i]); |
Theodore Ts'o | 1e592d6 | 2007-02-18 23:00:00 -0500 | [diff] [blame] | 352 | } |
| 353 | } |
Shawn O. Pearce | 859607d | 2007-02-02 00:06:08 -0500 | [diff] [blame] | 354 | elsif ($ARGV[0] eq 'prune') { |
| 355 | my $ls_remote = 1; |
| 356 | my $i; |
| 357 | for ($i = 1; $i < @ARGV; $i++) { |
| 358 | if ($ARGV[$i] eq '-n') { |
| 359 | $ls_remote = 0; |
| 360 | } |
| 361 | else { |
| 362 | last; |
| 363 | } |
| 364 | } |
| 365 | if ($i >= @ARGV) { |
| 366 | print STDERR "Usage: git remote prune <remote>\n"; |
| 367 | exit(1); |
| 368 | } |
| 369 | for (; $i < @ARGV; $i++) { |
| 370 | prune_remote($ARGV[$i], $ls_remote); |
| 371 | } |
| 372 | } |
Junio C Hamano | e194cd1 | 2007-01-03 12:13:04 -0800 | [diff] [blame] | 373 | elsif ($ARGV[0] eq 'add') { |
Junio C Hamano | b6f5da1 | 2007-02-01 23:30:03 -0800 | [diff] [blame] | 374 | my %opts = (); |
| 375 | while (1 < @ARGV && $ARGV[1] =~ /^-/) { |
| 376 | my $opt = $ARGV[1]; |
| 377 | shift @ARGV; |
| 378 | if ($opt eq '-f' || $opt eq '--fetch') { |
| 379 | $opts{'fetch'} = 1; |
| 380 | next; |
| 381 | } |
| 382 | if ($opt eq '-t' || $opt eq '--track') { |
| 383 | if (@ARGV < 1) { |
| 384 | add_usage(); |
| 385 | } |
| 386 | $opts{'track'} ||= []; |
| 387 | push @{$opts{'track'}}, $ARGV[1]; |
| 388 | shift @ARGV; |
| 389 | next; |
| 390 | } |
| 391 | if ($opt eq '-m' || $opt eq '--master') { |
| 392 | if ((@ARGV < 1) || exists $opts{'master'}) { |
| 393 | add_usage(); |
| 394 | } |
| 395 | $opts{'master'} = $ARGV[1]; |
| 396 | shift @ARGV; |
| 397 | next; |
| 398 | } |
| 399 | add_usage(); |
Junio C Hamano | e194cd1 | 2007-01-03 12:13:04 -0800 | [diff] [blame] | 400 | } |
Junio C Hamano | b6f5da1 | 2007-02-01 23:30:03 -0800 | [diff] [blame] | 401 | if (@ARGV != 3) { |
| 402 | add_usage(); |
| 403 | } |
| 404 | add_remote($ARGV[1], $ARGV[2], \%opts); |
Junio C Hamano | e194cd1 | 2007-01-03 12:13:04 -0800 | [diff] [blame] | 405 | } |
Quy Tonthat | c03f775 | 2007-01-13 22:55:21 +1100 | [diff] [blame] | 406 | else { |
| 407 | print STDERR "Usage: git remote\n"; |
| 408 | print STDERR " git remote add <name> <url>\n"; |
| 409 | print STDERR " git remote show <name>\n"; |
Shawn O. Pearce | 859607d | 2007-02-02 00:06:08 -0500 | [diff] [blame] | 410 | print STDERR " git remote prune <name>\n"; |
Theodore Ts'o | 1918278 | 2007-02-20 15:13:43 -0500 | [diff] [blame] | 411 | print STDERR " git remote update [group]\n"; |
Quy Tonthat | c03f775 | 2007-01-13 22:55:21 +1100 | [diff] [blame] | 412 | exit(1); |
| 413 | } |