@matheusbsbs / matheus-bagliano / commits / b6f5da1e0f
Fork 0

Teach git-remote add to fetch and track

This adds three options to 'git-remote add'. * -f (or --fetch) option tells it to also run the initial "git fetch" using the newly created remote shorthand. * -t (or --track) option tells it not to use the default wildcard to track all branches. * -m (or --master) option tells it to make the remote/$name/HEAD point at a remote tracking branch other than master. For example, with this I can say: $ git remote add -f -t master -t quick-start -m master \ jbf-um git://linux-nfs.org/~bfields/git.git/ to (1) create remote.jbf-um.url; (2) track master and quick-start branches (and no other); the two -t options create these two lines: fetch = +refs/heads/master:refs/remotes/jbf-um/master fetch = +refs/heads/quick-start:refs/remotes/jbf-um/quick-start (3) set up remotes/jbf-um/HEAD to point at jbf-um/master so that later I can say "git log jbf-um" Or I could do $ git remote add -t 'ap/*' andy /home/andy/git.git to make Andy's topic branches kept track of under refs/remotes/andy/ap/. Other possible improvements I considered but haven't implemented (hint, hint) are: * reject wildcard letters other than a trailing '*' to the -t parameter; * make -m optional and when the first -t parameter does not have the trailing '*' default to that value (so the above example does not need to say "-m master"); * if -m is not given, and -t parameter ends with '*' (i.e. the above defaulting did not tell us where to point HEAD at), and if we did the fetch with -f, check if 'master' was fetched and make HEAD point at it. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Feb 1, 2007 at 23:30 UTC b6f5da1e0f4eeb59798b320f97d27f83d19f89df
1 file changed +48 -6
git-remote.perl
+48 -6
@@ -253,14 +253,30 @@ sub show_remote {
253 }
254
255 sub add_remote {
256 - my ($name, $url) = @_;
256 + my ($name, $url, $opts) = @_;
257 if (exists $remote->{$name}) {
258 print STDERR "remote $name already exists.\n";
259 exit(1);
260 }
261 $git->command('config', "remote.$name.url", $url);
262 - $git->command('config', "remote.$name.fetch",
263 - "+refs/heads/*:refs/remotes/$name/*");
262 + my $track = $opts->{'track'} || ["*"];
263 +
264 + for (@$track) {
265 + $git->command('config', '--add', "remote.$name.fetch",
266 + "+refs/heads/$_:refs/remotes/$name/$_");
267 + }
268 + if ($opts->{'fetch'}) {
269 + $git->command('fetch', $name);
270 + }
271 + if (exists $opts->{'master'}) {
272 + $git->command('symbolic-ref', "refs/remotes/$name/HEAD",
273 + "refs/remotes/$name/$opts->{'master'}");
274 + }
275 +}
276 +
277 +sub add_usage {
278 + print STDERR "Usage: git remote add [-f] [-t track]* [-m master] <name> <url>\n";
279 + exit(1);
280 }
281
282 if (!@ARGV) {
@@ -307,11 +323,37 @@ elsif ($ARGV[0] eq 'prune') {
323 }
324 }
325 elsif ($ARGV[0] eq 'add') {
326 + my %opts = ();
327 + while (1 < @ARGV && $ARGV[1] =~ /^-/) {
328 + my $opt = $ARGV[1];
329 + shift @ARGV;
330 + if ($opt eq '-f' || $opt eq '--fetch') {
331 + $opts{'fetch'} = 1;
332 + next;
333 + }
334 + if ($opt eq '-t' || $opt eq '--track') {
335 + if (@ARGV < 1) {
336 + add_usage();
337 + }
338 + $opts{'track'} ||= [];
339 + push @{$opts{'track'}}, $ARGV[1];
340 + shift @ARGV;
341 + next;
342 + }
343 + if ($opt eq '-m' || $opt eq '--master') {
344 + if ((@ARGV < 1) || exists $opts{'master'}) {
345 + add_usage();
346 + }
347 + $opts{'master'} = $ARGV[1];
348 + shift @ARGV;
349 + next;
350 + }
351 + add_usage();
352 + }
353 if (@ARGV != 3) {
311 - print STDERR "Usage: git remote add <name> <url>\n";
312 - exit(1);
354 + add_usage();
355 }
314 - add_remote($ARGV[1], $ARGV[2]);
356 + add_remote($ARGV[1], $ARGV[2], \%opts);
357 }
358 else {
359 print STDERR "Usage: git remote\n";