{"thread":{"id":"6626","subject":"[PATCH] Teach git-remote add to fetch and track","startedAt":"2007-02-02T07:30:03Z","lastAt":"2007-02-02T07:30:03Z","messageCount":1,"participants":["Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"33351","messageId":"7v1wl9j9tw.fsf@assigned-by-dhcp.cox.net","threadId":"6626","inReplyTo":null,"subject":"[PATCH] Teach git-remote add to fetch and track","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2007-02-02T07:30:03Z","receivedAt":"2007-02-02T07:30:03Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"This adds three options to 'git-remote add'.\n\n * -f (or --fetch) option tells it to also run the initial \"git\n    fetch\" using the newly created remote shorthand.\n\n * -t (or --track) option tells it not to use the default\n    wildcard to track all branches.\n\n * -m (or --master) option tells it to make the\n    remote/$name/HEAD point at a remote tracking branch other\n    than master.\n\nFor example, with this I can say:\n\n  $ git remote add -f -t master -t quick-start -m master \\\n    jbf-um git://linux-nfs.org/~bfields/git.git/\n\nto\n\n (1) create remote.jbf-um.url;\n\n (2) track master and quick-start branches (and no other); the\n     two -t options create these two lines:\n\n       fetch = +refs/heads/master:refs/remotes/jbf-um/master\n       fetch = +refs/heads/quick-start:refs/remotes/jbf-um/quick-start\n\n (3) set up remotes/jbf-um/HEAD to point at jbf-um/master so\n     that later I can say \"git log jbf-um\"\n\nOr I could do\n\n  $ git remote add -t 'ap/*' andy /home/andy/git.git\n\nto make Andy's topic branches kept track of under refs/remotes/andy/ap/.\n\nOther possible improvements I considered but haven't implemented\n(hint, hint) are:\n\n * reject wildcard letters other than a trailing '*' to the -t\n   parameter;\n\n * make -m optional and when the first -t parameter does not\n   have the trailing '*' default to that value (so the above\n   example does not need to say \"-m master\");\n\n * if -m is not given, and -t parameter ends with '*' (i.e. the\n   above defaulting did not tell us where to point HEAD at), and\n   if we did the fetch with -f, check if 'master' was fetched\n   and make HEAD point at it.\n\nSigned-off-by: Junio C Hamano <junkio@cox.net>\n\n---\n git-remote.perl |   54 ++++++++++++++++++++++++++++++++++++++++++++++++------\n 1 files changed, 48 insertions(+), 6 deletions(-)\n\ndiff --git a/git-remote.perl b/git-remote.perl\nindex f16ff21..c56c5a8 100755\n--- a/git-remote.perl\n+++ b/git-remote.perl\n@@ -253,14 +253,30 @@ sub show_remote {\n }\n \n sub add_remote {\n-\tmy ($name, $url) = @_;\n+\tmy ($name, $url, $opts) = @_;\n \tif (exists $remote->{$name}) {\n \t\tprint STDERR \"remote $name already exists.\\n\";\n \t\texit(1);\n \t}\n \t$git->command('config', \"remote.$name.url\", $url);\n-\t$git->command('config', \"remote.$name.fetch\",\n-\t\t      \"+refs/heads/*:refs/remotes/$name/*\");\n+\tmy $track = $opts->{'track'} || [\"*\"];\n+\n+\tfor (@$track) {\n+\t\t$git->command('config', '--add', \"remote.$name.fetch\",\n+\t\t\t      \"+refs/heads/$_:refs/remotes/$name/$_\");\n+\t}\n+\tif ($opts->{'fetch'}) {\n+\t\t$git->command('fetch', $name);\n+\t}\n+\tif (exists $opts->{'master'}) {\n+\t\t$git->command('symbolic-ref', \"refs/remotes/$name/HEAD\",\n+\t\t\t      \"refs/remotes/$name/$opts->{'master'}\");\n+\t}\n+}\n+\n+sub add_usage {\n+\tprint STDERR \"Usage: git remote add [-f] [-t track]* [-m master] <name> <url>\\n\";\n+\texit(1);\n }\n \n if (!@ARGV) {\n@@ -307,11 +323,37 @@ elsif ($ARGV[0] eq 'prune') {\n \t}\n }\n elsif ($ARGV[0] eq 'add') {\n+\tmy %opts = ();\n+\twhile (1 < @ARGV && $ARGV[1] =~ /^-/) {\n+\t\tmy $opt = $ARGV[1];\n+\t\tshift @ARGV;\n+\t\tif ($opt eq '-f' || $opt eq '--fetch') {\n+\t\t\t$opts{'fetch'} = 1;\n+\t\t\tnext;\n+\t\t}\n+\t\tif ($opt eq '-t' || $opt eq '--track') {\n+\t\t\tif (@ARGV < 1) {\n+\t\t\t\tadd_usage();\n+\t\t\t}\n+\t\t\t$opts{'track'} ||= [];\n+\t\t\tpush @{$opts{'track'}}, $ARGV[1];\n+\t\t\tshift @ARGV;\n+\t\t\tnext;\n+\t\t}\n+\t\tif ($opt eq '-m' || $opt eq '--master') {\n+\t\t\tif ((@ARGV < 1) || exists $opts{'master'}) {\n+\t\t\t\tadd_usage();\n+\t\t\t}\n+\t\t\t$opts{'master'} = $ARGV[1];\n+\t\t\tshift @ARGV;\n+\t\t\tnext;\n+\t\t}\n+\t\tadd_usage();\n+\t}\n \tif (@ARGV != 3) {\n-\t\tprint STDERR \"Usage: git remote add <name> <url>\\n\";\n-\t\texit(1);\n+\t\tadd_usage();\n \t}\n-\tadd_remote($ARGV[1], $ARGV[2]);\n+\tadd_remote($ARGV[1], $ARGV[2], \\%opts);\n }\n else {\n \tprint STDERR \"Usage: git remote\\n\";\n"}]}