@matheusbsbs / matheus-bagliano / commits / 6f084a56fc
Fork 0

branch --track: code cleanup and saner handling of local branches

This patch cleans up some complicated code, and replaces it with a cleaner version, using code from remote.[ch], which got extended a little in the process. This also enables us to fix two cases: The earlier "fix" to setup tracking only when the original ref started with "refs/remotes" is wrong. You are absolutely allowed to use a separate layout for your tracking branches. The correct fix, of course, is to set up tracking information only when there is a matching remote.<nick>.fetch line containing a colon. Another corner case was not handled properly. If two remotes write to the original ref, just warn the user and do not set up tracking. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Jul 10, 2007 at 18:50 UTC 6f084a56fcb3543d88d252bb49c1d2bbf2bd0cf3
4 files changed +69 -132
Documentation/config.txt
+1 -4
@@ -309,10 +309,7 @@ branch.autosetupmerge::
309 so that gitlink:git-pull[1] will appropriately merge from that
310 remote branch. Note that even if this option is not set,
311 this behavior can be chosen per-branch using the `--track`
312 - and `--no-track` options. This option can have values
313 - 'false' (never touch the configuration), 'all' (do this
314 - for all branches), or 'true' (do this only when
315 - branching from a remote tracking branch), and defaults to 'true'.
312 + and `--no-track` options. This option defaults to false.
313
314 branch.<name>.remote::
315 When in branch <name>, it tells `git fetch` which remote to fetch.
Documentation/git-checkout.txt
+2 -3
@@ -52,9 +52,8 @@ OPTIONS
52 set up configuration so that git-pull will automatically
53 retrieve data from the remote branch. Set the
54 branch.autosetupmerge configuration variable to true if you
55 - want git-checkout and git-branch to behave as if
56 - '--track' were given when you branch from a remote
57 - tracking branch.
55 + want git-checkout and git-branch to always behave as if
56 + '--track' were given.
57
58 --no-track::
59 When -b is given and a branch is created off a remote branch,
builtin-branch.c
+55 -115
@@ -10,6 +10,7 @@
10 #include "refs.h"
11 #include "commit.h"
12 #include "builtin.h"
13 +#include "remote.h"
14
15 static const char builtin_branch_usage[] =
16 "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]]";
@@ -22,7 +23,7 @@ static const char builtin_branch_usage[] =
23 static const char *head;
24 static unsigned char head_sha1[20];
25
25 -static int branch_track = 1; /* 0 = none, 1 = remotes, 2 = all */
26 +static int branch_track = 1;
27
28 static int branch_use_color;
29 static char branch_colors[][COLOR_MAXLEN] = {
@@ -66,12 +67,8 @@ static int git_branch_config(const char *var, const char *value)
67 color_parse(value, var, branch_colors[slot]);
68 return 0;
69 }
69 - if (!strcmp(var, "branch.autosetupmerge")) {
70 - if (!strcmp(value, "all"))
71 - branch_track = 2;
72 - else
70 + if (!strcmp(var, "branch.autosetupmerge"))
71 branch_track = git_config_bool(var, value);
74 - }
72
73 return git_default_config(var, value);
74 }
@@ -328,125 +325,70 @@ static void print_ref_list(int kinds, int detached, int verbose, int abbrev)
325 free_ref_list(&ref_list);
326 }
327
331 -static char *config_repo;
332 -static char *config_remote;
333 -static const char *start_ref;
328 +struct tracking {
329 + struct refspec spec;
330 + char *src;
331 + const char *remote;
332 + int matches;
333 +};
334
335 -static int get_remote_branch_name(const char *value)
335 +static int find_tracked_branch(struct remote *remote, void *priv)
336 {
337 - const char *colon;
338 - const char *end;
337 + struct tracking *tracking = priv;
338
340 - if (*value == '+')
341 - value++;
342 -
343 - colon = strchr(value, ':');
344 - if (!colon)
345 - return 0;
346 -
347 - end = value + strlen(value);
348 -
349 - /*
350 - * Try an exact match first. I.e. handle the case where the
351 - * value is "$anything:refs/foo/bar/baz" and start_ref is exactly
352 - * "refs/foo/bar/baz". Then the name at the remote is $anything.
353 - */
354 - if (!strcmp(colon + 1, start_ref)) {
355 - /* Truncate the value before the colon. */
356 - nfasprintf(&config_repo, "%.*s", colon - value, value);
357 - return 1;
339 + if (!remote_find_tracking(remote, &tracking->spec)) {
340 + if (++tracking->matches == 1) {
341 + tracking->src = tracking->spec.src;
342 + tracking->remote = remote->name;
343 + } else {
344 + free(tracking->spec.src);
345 + if (tracking->src) {
346 + free(tracking->src);
347 + tracking->src = NULL;
348 + }
349 + }
350 + tracking->spec.src = NULL;
351 }
352
360 - /*
361 - * Is this a wildcard match?
362 - */
363 - if ((end - 2 <= value) || end[-2] != '/' || end[-1] != '*' ||
364 - (colon - 2 <= value) || colon[-2] != '/' || colon[-1] != '*')
365 - return 0;
366 -
367 - /*
368 - * Value is "refs/foo/bar/<asterisk>:refs/baz/boa/<asterisk>"
369 - * and start_ref begins with "refs/baz/boa/"; the name at the
370 - * remote is refs/foo/bar/ with the remaining part of the
371 - * start_ref. The length of the prefix on the RHS is (end -
372 - * colon - 2), including the slash immediately before the
373 - * asterisk.
374 - */
375 - if ((strlen(start_ref) < end - colon - 2) ||
376 - memcmp(start_ref, colon + 1, end - colon - 2))
377 - return 0; /* does not match prefix */
378 -
379 - /* Replace the asterisk with the remote branch name. */
380 - nfasprintf(&config_repo, "%.*s%s",
381 - (colon - 1) - value, value,
382 - start_ref + (end - colon - 2));
383 - return 1;
384 -}
385 -
386 -static int get_remote_config(const char *key, const char *value)
387 -{
388 - const char *var;
389 - if (prefixcmp(key, "remote."))
390 - return 0;
391 -
392 - var = strrchr(key, '.');
393 - if (var == key + 6 || strcmp(var, ".fetch"))
394 - return 0;
395 - /*
396 - * Ok, we are looking at key == "remote.$foo.fetch";
397 - */
398 - if (get_remote_branch_name(value))
399 - nfasprintf(&config_remote, "%.*s", var - (key + 7), key + 7);
400 -
353 return 0;
354 }
355
404 -static void set_branch_merge(const char *name, const char *config_remote,
405 - const char *config_repo)
356 +
357 +/*
358 + * This is called when new_ref is branched off of orig_ref, and tries
359 + * to infer the settings for branch.<new_ref>.{remote,merge} from the
360 + * config.
361 + */
362 +static int setup_tracking(const char *new_ref, const char *orig_ref)
363 {
364 char key[1024];
408 - if (sizeof(key) <=
409 - snprintf(key, sizeof(key), "branch.%s.remote", name))
410 - die("what a long branch name you have!");
411 - git_config_set(key, config_remote);
412 -
413 - /*
414 - * We do not have to check if we have enough space for
415 - * the 'merge' key, since it's shorter than the
416 - * previous 'remote' key, which we already checked.
417 - */
418 - snprintf(key, sizeof(key), "branch.%s.merge", name);
419 - git_config_set(key, config_repo);
420 -}
365 + struct tracking tracking;
366
422 -static void set_branch_defaults(const char *name, const char *real_ref)
423 -{
424 - /*
425 - * name is the name of new branch under refs/heads;
426 - * real_ref is typically refs/remotes/$foo/$bar, where
427 - * $foo is the remote name (there typically are no slashes)
428 - * and $bar is the branch name we map from the remote
429 - * (it could have slashes).
430 - */
431 - start_ref = real_ref;
432 - git_config(get_remote_config);
433 - if (!config_repo && !config_remote &&
434 - !prefixcmp(real_ref, "refs/heads/")) {
435 - set_branch_merge(name, ".", real_ref);
436 - printf("Branch %s set up to track local branch %s.\n",
437 - name, real_ref);
438 - }
367 + if (strlen(new_ref) > 1024 - 7 - 7 - 1)
368 + return error("Tracking not set up: name too long: %s",
369 + new_ref);
370
440 - if (config_repo && config_remote) {
441 - set_branch_merge(name, config_remote, config_repo);
371 + memset(&tracking, 0, sizeof(tracking));
372 + tracking.spec.dst = (char *)orig_ref;
373 + if (for_each_remote(find_tracked_branch, &tracking) ||
374 + !tracking.matches)
375 + return 1;
376 +
377 + if (tracking.matches > 1)
378 + return error("Not tracking: ambiguous information for ref %s",
379 + orig_ref);
380 +
381 + if (tracking.matches == 1) {
382 + sprintf(key, "branch.%s.remote", new_ref);
383 + git_config_set(key, tracking.remote ? tracking.remote : ".");
384 + sprintf(key, "branch.%s.merge", new_ref);
385 + git_config_set(key, tracking.src);
386 + free(tracking.src);
387 printf("Branch %s set up to track remote branch %s.\n",
443 - name, real_ref);
388 + new_ref, orig_ref);
389 }
390
446 - if (config_repo)
447 - free(config_repo);
448 - if (config_remote)
449 - free(config_remote);
391 + return 0;
392 }
393
394 static void create_branch(const char *name, const char *start_name,
@@ -508,10 +450,8 @@ static void create_branch(const char *name, const char *start_name,
450 /* When branching off a remote branch, set up so that git-pull
451 automatically merges from there. So far, this is only done for
452 remotes registered via .git/config. */
511 - if (real_ref && (track == 2 ||
512 - (track == 1 &&
513 - !prefixcmp(real_ref, "refs/remotes/"))))
514 - set_branch_defaults(name, real_ref);
453 + if (real_ref && track)
454 + setup_tracking(name, real_ref);
455
456 if (write_ref_sha1(lock, sha1, msg) < 0)
457 die("Failed to write ref: %s.", strerror(errno));
@@ -582,7 +522,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
522 break;
523 }
524 if (!strcmp(arg, "--track")) {
585 - track = 2;
525 + track = 1;
526 continue;
527 }
528 if (!strcmp(arg, "--no-track")) {
t/t3200-branch.sh
+11 -10
@@ -148,13 +148,14 @@ test_expect_success 'test tracking setup via config' \
148 test $(git config branch.my3.remote) = local &&
149 test $(git config branch.my3.merge) = refs/heads/master'
150
151 -test_expect_success 'autosetupmerge = all' '
151 +test_expect_success 'avoid ambiguous track' '
152 git config branch.autosetupmerge true &&
153 + git config remote.ambi1.url = lalala &&
154 + git config remote.ambi1.fetch = refs/heads/lalala:refs/heads/master &&
155 + git config remote.ambi2.url = lilili &&
156 + git config remote.ambi2.fetch = refs/heads/lilili:refs/heads/master &&
157 git branch all1 master &&
154 - test -z "$(git config branch.all1.merge)" &&
155 - git config branch.autosetupmerge all &&
156 - git branch all2 master &&
157 - test $(git config branch.all2.merge) = refs/heads/master
158 + test -z "$(git config branch.all1.merge)"
159 '
160
161 test_expect_success 'test overriding tracking setup via --no-track' \
@@ -167,10 +168,10 @@ test_expect_success 'test overriding tracking setup via --no-track' \
168 ! test "$(git config branch.my2.remote)" = local &&
169 ! test "$(git config branch.my2.merge)" = refs/heads/master'
170
170 -test_expect_success 'test local tracking setup' \
171 +test_expect_success 'no tracking without .fetch entries' \
172 'git branch --track my6 s &&
172 - test $(git config branch.my6.remote) = . &&
173 - test $(git config branch.my6.merge) = refs/heads/s'
173 + test -z "$(git config branch.my6.remote)" &&
174 + test -z "$(git config branch.my6.merge)"'
175
176 test_expect_success 'test tracking setup via --track but deeper' \
177 'git config remote.local.url . &&
@@ -182,8 +183,8 @@ test_expect_success 'test tracking setup via --track but deeper' \
183
184 test_expect_success 'test deleting branch deletes branch config' \
185 'git branch -d my7 &&
185 - test "$(git config branch.my7.remote)" = "" &&
186 - test "$(git config branch.my7.merge)" = ""'
186 + test -z "$(git config branch.my7.remote)" &&
187 + test -z "$(git config branch.my7.merge)"'
188
189 test_expect_success 'test deleting branch without config' \
190 'git branch my7 s &&