@matheusbsbs / matheus-bagliano / commits / 0746d19a82
Fork 0

git-branch, git-checkout: autosetup for remote branch tracking

In order to track and build on top of a branch 'topic' you track from your upstream repository, you often would end up doing this sequence: git checkout -b mytopic origin/topic git config --add branch.mytopic.remote origin git config --add branch.mytopic.merge refs/heads/topic This would first fork your own 'mytopic' branch from the 'topic' branch you track from the 'origin' repository; then it would set up two configuration variables so that 'git pull' without parameters does the right thing while you are on your own 'mytopic' branch. This commit adds a --track option to git-branch, so that "git branch --track mytopic origin/topic" performs the latter two actions when creating your 'mytopic' branch. If the configuration variable branch.autosetupmerge is set to true, you do not have to pass the --track option explicitly; further patches in this series allow setting the variable with a "git remote add" option. The configuration variable is off by default, and there is a --no-track option to countermand it even if the variable is set. Signed-off-by: Paolo Bonzini <bonzini@gnu.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Paolo Bonzini committed Mar 8, 2007 at 10:58 UTC 0746d19a823aa12239df9d32db6fa6795d8c8539
7 files changed +229 -36
Documentation/git-branch.txt
+8 -1
@@ -10,7 +10,7 @@ SYNOPSIS
10 [verse]
11 'git-branch' [--color | --no-color] [-r | -a]
12 [-v [--abbrev=<length> | --no-abbrev]]
13 -'git-branch' [-l] [-f] <branchname> [<start-point>]
13 +'git-branch' [--track | --no-track] [-l] [-f] <branchname> [<start-point>]
14 'git-branch' (-m | -M) [<oldbranch>] <newbranch>
15 'git-branch' (-d | -D) [-r] <branchname>...
16
@@ -26,6 +26,13 @@ It will start out with a head equal to the one given as <start-point>.
26 If no <start-point> is given, the branch will be created with a head
27 equal to that of the currently checked out branch.
28
29 +When a local branch is started off a remote branch, git can setup the
30 +branch so that gitlink:git-pull[1] will appropriately merge from that
31 +remote branch. If this behavior is desired, it is possible to make it
32 +the default using the global `branch.autosetupmerge` configuration
33 +flag. Otherwise, it can be chosen per-branch using the `--track`
34 +and `--no-track` options.
35 +
36 With a '-m' or '-M' option, <oldbranch> will be renamed to <newbranch>.
37 If <oldbranch> had a corresponding reflog, it is renamed to match
38 <newbranch>, and a reflog entry is created to remember the branch
Documentation/git-checkout.txt
+13 -2
@@ -8,7 +8,7 @@ git-checkout - Checkout and switch to a branch
8 SYNOPSIS
9 --------
10 [verse]
11 -'git-checkout' [-q] [-f] [-b <new_branch> [-l]] [-m] [<branch>]
11 +'git-checkout' [-q] [-f] [-b [--track | --no-track] <new_branch> [-l]] [-m] [<branch>]
12 'git-checkout' [<tree-ish>] <paths>...
13
14 DESCRIPTION
@@ -18,7 +18,8 @@ When <paths> are not given, this command switches branches by
18 updating the index and working tree to reflect the specified
19 branch, <branch>, and updating HEAD to be <branch> or, if
20 specified, <new_branch>. Using -b will cause <new_branch> to
21 -be created.
21 +be created; in this case you can use the --track or --no-track
22 +options, which will be passed to `git branch`.
23
24 When <paths> are given, this command does *not* switch
25 branches. It updates the named paths in the working tree from
@@ -45,6 +46,16 @@ OPTIONS
46 by gitlink:git-check-ref-format[1]. Some of these checks
47 may restrict the characters allowed in a branch name.
48
49 +--track::
50 + When -b is given and a branch is created off a remote branch,
51 + setup so that git-pull will automatically retrieve data from
52 + the remote branch.
53 +
54 +--no-track::
55 + When -b is given and a branch is created off a remote branch,
56 + force that git-pull will automatically retrieve data from
57 + the remote branch independent of the configuration settings.
58 +
59 -l::
60 Create the new branch's ref log. This activates recording of
61 all changes to made the branch ref, enabling use of date
builtin-branch.c
+132 -8
@@ -12,7 +12,7 @@
12 #include "builtin.h"
13
14 static const char builtin_branch_usage[] =
15 - "git-branch [-r] (-d | -D) <branchname> | [-l] [-f] <branchname> [<start-point>] | (-m | -M) [<oldbranch>] <newbranch> | [--color | --no-color] [-r | -a] [-v [--abbrev=<length> | --no-abbrev]]";
15 + "git-branch [-r] (-d | -D) <branchname> | [--track | --no-track] [-l] [-f] <branchname> [<start-point>] | (-m | -M) [<oldbranch>] <newbranch> | [--color | --no-color] [-r | -a] [-v [--abbrev=<length> | --no-abbrev]]";
16
17 #define REF_UNKNOWN_TYPE 0x00
18 #define REF_LOCAL_BRANCH 0x01
@@ -22,6 +22,8 @@ static const char builtin_branch_usage[] =
22 static const char *head;
23 static unsigned char head_sha1[20];
24
25 +static int branch_track_remotes;
26 +
27 static int branch_use_color;
28 static char branch_colors[][COLOR_MAXLEN] = {
29 "\033[m", /* reset */
@@ -64,6 +66,9 @@ int git_branch_config(const char *var, const char *value)
66 color_parse(value, var, branch_colors[slot]);
67 return 0;
68 }
69 + if (!strcmp(var, "branch.autosetupmerge"))
70 + branch_track_remotes = git_config_bool(var, value);
71 +
72 return git_default_config(var, value);
73 }
74
@@ -309,14 +314,109 @@ static void print_ref_list(int kinds, int detached, int verbose, int abbrev)
314 free_ref_list(&ref_list);
315 }
316
317 +static char *config_repo;
318 +static char *config_remote;
319 +static const char *start_ref;
320 +static int start_len;
321 +static int base_len;
322 +
323 +static int get_remote_branch_name(const char *value)
324 +{
325 + const char *colon;
326 + const char *end;
327 +
328 + if (*value == '+')
329 + value++;
330 +
331 + colon = strchr(value, ':');
332 + if (!colon)
333 + return 0;
334 +
335 + end = value + strlen(value);
336 +
337 + /* Try an exact match first. */
338 + if (!strcmp(colon + 1, start_ref)) {
339 + /* Truncate the value before the colon. */
340 + nfasprintf(&config_repo, "%.*s", colon - value, value);
341 + return 1;
342 + }
343 +
344 + /* Try with a wildcard match now. */
345 + if (end - value > 2 && end[-2] == '/' && end[-1] == '*' &&
346 + colon - value > 2 && colon[-2] == '/' && colon[-1] == '*' &&
347 + (end - 2) - (colon + 1) == base_len &&
348 + !strncmp(colon + 1, start_ref, base_len)) {
349 + /* Replace the star with the remote branch name. */
350 + nfasprintf(&config_repo, "%.*s%s",
351 + (colon - 2) - value, value,
352 + start_ref + base_len);
353 + return 1;
354 + }
355 +
356 + return 0;
357 +}
358 +
359 +static int get_remote_config(const char *key, const char *value)
360 +{
361 + const char *var;
362 + if (prefixcmp(key, "remote."))
363 + return 0;
364 +
365 + var = strrchr(key, '.');
366 + if (var == key + 6)
367 + return 0;
368 +
369 + if (!strcmp(var, ".fetch") && get_remote_branch_name(value))
370 + nfasprintf(&config_remote, "%.*s", var - (key + 7), key + 7);
371 +
372 + return 0;
373 +}
374 +
375 +static void set_branch_defaults(const char *name, const char *real_ref)
376 +{
377 + char key[1024];
378 + const char *slash = strrchr(real_ref, '/');
379 +
380 + if (!slash)
381 + return;
382 +
383 + start_ref = real_ref;
384 + start_len = strlen(real_ref);
385 + base_len = slash - real_ref;
386 + git_config(get_remote_config);
387 +
388 + if (config_repo && config_remote) {
389 + if (sizeof(key) <=
390 + snprintf(key, sizeof(key), "branch.%s.remote", name))
391 + die("what a long branch name you have!");
392 + git_config_set(key, config_remote);
393 +
394 + /*
395 + * We do not have to check if we have enough space for
396 + * the 'merge' key, since it's shorter than the
397 + * previous 'remote' key, which we already checked.
398 + */
399 + snprintf(key, sizeof(key), "branch.%s.merge", name);
400 + git_config_set(key, config_repo);
401 +
402 + printf("Branch %s set up to track remote branch %s.\n",
403 + name, real_ref);
404 + }
405 +
406 + if (config_repo)
407 + free(config_repo);
408 + if (config_remote)
409 + free(config_remote);
410 +}
411 +
412 static void create_branch(const char *name, const char *start_name,
413 unsigned char *start_sha1,
314 - int force, int reflog)
414 + int force, int reflog, int track)
415 {
416 struct ref_lock *lock;
417 struct commit *commit;
418 unsigned char sha1[20];
319 - char ref[PATH_MAX], msg[PATH_MAX + 20];
419 + char *real_ref, ref[PATH_MAX], msg[PATH_MAX + 20];
420 int forcing = 0;
421
422 snprintf(ref, sizeof ref, "refs/heads/%s", name);
@@ -331,10 +431,14 @@ static void create_branch(const char *name, const char *start_name,
431 forcing = 1;
432 }
433
334 - if (start_sha1)
434 + if (start_sha1) {
435 /* detached HEAD */
436 hashcpy(sha1, start_sha1);
337 - else if (get_sha1(start_name, sha1))
437 + real_ref = NULL;
438 + }
439 + else if (dwim_ref(start_name, strlen(start_name), sha1, &real_ref) > 1)
440 + die("Ambiguous object name: '%s'.", start_name);
441 + else if (real_ref == NULL)
442 die("Not a valid object name: '%s'.", start_name);
443
444 if ((commit = lookup_commit_reference(sha1)) == NULL)
@@ -355,8 +459,17 @@ static void create_branch(const char *name, const char *start_name,
459 snprintf(msg, sizeof msg, "branch: Created from %s",
460 start_name);
461
462 + /* When branching off a remote branch, set up so that git-pull
463 + automatically merges from there. So far, this is only done for
464 + remotes registered via .git/config. */
465 + if (real_ref && track)
466 + set_branch_defaults(name, real_ref);
467 +
468 if (write_ref_sha1(lock, sha1, msg) < 0)
469 die("Failed to write ref: %s.", strerror(errno));
470 +
471 + if (real_ref)
472 + free(real_ref);
473 }
474
475 static void rename_branch(const char *oldname, const char *newname, int force)
@@ -398,11 +511,12 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
511 int delete = 0, force_delete = 0, force_create = 0;
512 int rename = 0, force_rename = 0;
513 int verbose = 0, abbrev = DEFAULT_ABBREV, detached = 0;
401 - int reflog = 0;
514 + int reflog = 0, track;
515 int kinds = REF_LOCAL_BRANCH;
516 int i;
517
518 git_config(git_branch_config);
519 + track = branch_track_remotes;
520
521 for (i = 1; i < argc; i++) {
522 const char *arg = argv[i];
@@ -413,6 +527,14 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
527 i++;
528 break;
529 }
530 + if (!strcmp(arg, "--track")) {
531 + track = 1;
532 + continue;
533 + }
534 + if (!strcmp(arg, "--no-track")) {
535 + track = 0;
536 + continue;
537 + }
538 if (!strcmp(arg, "-d")) {
539 delete = 1;
540 continue;
@@ -499,9 +621,11 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
621 else if (rename && (i == argc - 2))
622 rename_branch(argv[i], argv[i + 1], force_rename);
623 else if (i == argc - 1)
502 - create_branch(argv[i], head, head_sha1, force_create, reflog);
624 + create_branch(argv[i], head, head_sha1, force_create, reflog,
625 + track);
626 else if (i == argc - 2)
504 - create_branch(argv[i], argv[i+1], NULL, force_create, reflog);
627 + create_branch(argv[i], argv[i+1], NULL, force_create, reflog,
628 + track);
629 else
630 usage(builtin_branch_usage);
631
cache.h
+1
@@ -480,6 +480,7 @@ extern struct tag *alloc_tag_node(void);
480 extern void alloc_report(void);
481
482 /* trace.c */
483 +extern int nfasprintf(char **str, const char *fmt, ...);
484 extern int nfvasprintf(char **str, const char *fmt, va_list va);
485 extern void trace_printf(const char *format, ...);
486 extern void trace_argv_printf(const char **argv, int count, const char *format, ...);
git-checkout.sh
+11 -6
@@ -12,6 +12,7 @@ new=
12 new_name=
13 force=
14 branch=
15 +track=
16 newbranch=
17 newbranch_log=
18 merge=
@@ -33,7 +34,10 @@ while [ "$#" != "0" ]; do
34 die "git checkout: we do not like '$newbranch' as a branch name."
35 ;;
36 "-l")
36 - newbranch_log=1
37 + newbranch_log=-l
38 + ;;
39 + "--track"|"--no-track")
40 + track="$arg"
41 ;;
42 "-f")
43 force=1
@@ -85,6 +89,11 @@ while [ "$#" != "0" ]; do
89 esac
90 done
91
92 +case "$new_branch,$track" in
93 +,--*)
94 + die "git checkout: --track and --no-track require -b"
95 +esac
96 +
97 case "$force$merge" in
98 11)
99 die "git checkout: -f and -m are incompatible"
@@ -235,11 +244,7 @@ fi
244 #
245 if [ "$?" -eq 0 ]; then
246 if [ "$newbranch" ]; then
238 - if [ "$newbranch_log" ]; then
239 - mkdir -p $(dirname "$GIT_DIR/logs/refs/heads/$newbranch")
240 - touch "$GIT_DIR/logs/refs/heads/$newbranch"
241 - fi
242 - git-update-ref -m "checkout: Created from $new_name" "refs/heads/$newbranch" $new || exit
247 + git-branch $track $newbranch_log "$newbranch" "$new_name" || exit
248 branch="$newbranch"
249 fi
250 if test -n "$branch"
t/t3200-branch.sh
+54 -11
@@ -47,17 +47,6 @@ test_expect_success \
47 test ! -f .git/refs/heads/d/e/f &&
48 test ! -f .git/logs/refs/heads/d/e/f'
49
50 -cat >expect <<EOF
51 -0000000000000000000000000000000000000000 $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 checkout: Created from master
52 -EOF
53 -test_expect_success \
54 - 'git checkout -b g/h/i -l should create a branch and a log' \
55 - 'GIT_COMMITTER_DATE="2005-05-26 23:30" \
56 - git-checkout -b g/h/i -l master &&
57 - test -f .git/refs/heads/g/h/i &&
58 - test -f .git/logs/refs/heads/g/h/i &&
59 - diff expect .git/logs/refs/heads/g/h/i'
60 -
50 test_expect_success \
51 'git branch j/k should work after branch j has been deleted' \
52 'git-branch j &&
@@ -117,4 +106,58 @@ test_expect_failure \
106 ln -s real-u .git/logs/refs/heads/u &&
107 git-branch -m u v'
108
109 +test_expect_success 'test tracking setup via --track' \
110 + 'git-config remote.local.url . &&
111 + git-config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
112 + (git-show-ref -q refs/remotes/local/master || git-fetch local) &&
113 + git-branch --track my1 local/master &&
114 + test $(git-config branch.my1.remote) = local &&
115 + test $(git-config branch.my1.merge) = refs/heads/master'
116 +
117 +test_expect_success 'test tracking setup (non-wildcard, matching)' \
118 + 'git-config remote.local.url . &&
119 + git-config remote.local.fetch refs/heads/master:refs/remotes/local/master &&
120 + (git-show-ref -q refs/remotes/local/master || git-fetch local) &&
121 + git-branch --track my4 local/master &&
122 + test $(git-config branch.my4.remote) = local &&
123 + test $(git-config branch.my4.merge) = refs/heads/master'
124 +
125 +test_expect_success 'test tracking setup (non-wildcard, not matching)' \
126 + 'git-config remote.local.url . &&
127 + git-config remote.local.fetch refs/heads/s:refs/remotes/local/s &&
128 + (git-show-ref -q refs/remotes/local/master || git-fetch local) &&
129 + git-branch --track my5 local/master &&
130 + ! test $(git-config branch.my5.remote) = local &&
131 + ! test $(git-config branch.my5.merge) = refs/heads/master'
132 +
133 +test_expect_success 'test tracking setup via config' \
134 + 'git-config branch.autosetupmerge true &&
135 + git-config remote.local.url . &&
136 + git-config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
137 + (git-show-ref -q refs/remotes/local/master || git-fetch local) &&
138 + git-branch my3 local/master &&
139 + test $(git-config branch.my3.remote) = local &&
140 + test $(git-config branch.my3.merge) = refs/heads/master'
141 +
142 +test_expect_success 'test overriding tracking setup via --no-track' \
143 + 'git-config branch.autosetupmerge true &&
144 + git-config remote.local.url . &&
145 + git-config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
146 + (git-show-ref -q refs/remotes/local/master || git-fetch local) &&
147 + git-branch --no-track my2 local/master &&
148 + ! test $(git-config branch.my2.remote) = local &&
149 + ! test $(git-config branch.my2.merge) = refs/heads/master'
150 +
151 +# Keep this test last, as it changes the current branch
152 +cat >expect <<EOF
153 +0000000000000000000000000000000000000000 $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from master
154 +EOF
155 +test_expect_success \
156 + 'git checkout -b g/h/i -l should create a branch and a log' \
157 + 'GIT_COMMITTER_DATE="2005-05-26 23:30" \
158 + git-checkout -b g/h/i -l master &&
159 + test -f .git/refs/heads/g/h/i &&
160 + test -f .git/logs/refs/heads/g/h/i &&
161 + diff expect .git/logs/refs/heads/g/h/i'
162 +
163 test_done
trace.c
+10 -8
@@ -26,14 +26,14 @@
26 #include "quote.h"
27
28 /* Stolen from "imap-send.c". */
29 -static int git_vasprintf(char **strp, const char *fmt, va_list ap)
29 +int nfvasprintf(char **strp, const char *fmt, va_list ap)
30 {
31 int len;
32 char tmp[1024];
33
34 if ((len = vsnprintf(tmp, sizeof(tmp), fmt, ap)) < 0 ||
35 !(*strp = xmalloc(len + 1)))
36 - return -1;
36 + die("Fatal: Out of memory\n");
37 if (len >= (int)sizeof(tmp))
38 vsprintf(*strp, fmt, ap);
39 else
@@ -41,13 +41,15 @@ static int git_vasprintf(char **strp, const char *fmt, va_list ap)
41 return len;
42 }
43
44 -/* Stolen from "imap-send.c". */
45 -int nfvasprintf(char **str, const char *fmt, va_list va)
44 +int nfasprintf(char **str, const char *fmt, ...)
45 {
47 - int ret = git_vasprintf(str, fmt, va);
48 - if (ret < 0)
49 - die("Fatal: Out of memory\n");
50 - return ret;
46 + int rc;
47 + va_list args;
48 +
49 + va_start(args, fmt);
50 + rc = nfvasprintf(str, fmt, args);
51 + va_end(args);
52 + return rc;
53 }
54
55 /* Get a trace file descriptor from GIT_TRACE env variable. */