@matheusbsbs / matheus-bagliano / commits / 7b99befef7
Fork 0

git-branch --track: fix tracking branch computation.

The original code did not take hierarchical branch names into account at all. [jc: cherry-picked 11f68d9 from 'master'] Tested-by: Gerrit Pape <pape@smarden.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jun 15, 2007 at 23:35 UTC 7b99befef7d0ae173980f531e80916126011ca41
2 files changed +53 -30
builtin-branch.c
+41 -26
@@ -317,8 +317,6 @@ static void print_ref_list(int kinds, int detached, int verbose, int abbrev)
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;
320
321 static int get_remote_branch_name(const char *value)
322 {
@@ -334,26 +332,41 @@ static int get_remote_branch_name(const char *value)
332
333 end = value + strlen(value);
334
337 - /* Try an exact match first. */
335 + /*
336 + * Try an exact match first. I.e. handle the case where the
337 + * value is "$anything:refs/foo/bar/baz" and start_ref is exactly
338 + * "refs/foo/bar/baz". Then the name at the remote is $anything.
339 + */
340 if (!strcmp(colon + 1, start_ref)) {
339 - /* Truncate the value before the colon. */
341 + /* Truncate the value before the colon. */
342 nfasprintf(&config_repo, "%.*s", colon - value, value);
343 return 1;
344 }
345
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 - }
346 + /*
347 + * Is this a wildcard match?
348 + */
349 + if ((end - 2 <= value) || end[-2] != '/' || end[-1] != '*' ||
350 + (colon - 2 <= value) || colon[-2] != '/' || colon[-1] != '*')
351 + return 0;
352
356 - return 0;
353 + /*
354 + * Value is "refs/foo/bar/<asterisk>:refs/baz/boa/<asterisk>"
355 + * and start_ref begins with "refs/baz/boa/"; the name at the
356 + * remote is refs/foo/bar/ with the remaining part of the
357 + * start_ref. The length of the prefix on the RHS is (end -
358 + * colon - 2), including the slash immediately before the
359 + * asterisk.
360 + */
361 + if ((strlen(start_ref) < end - colon - 2) ||
362 + memcmp(start_ref, colon + 1, end - colon - 2))
363 + return 0; /* does not match prefix */
364 +
365 + /* Replace the asterisk with the remote branch name. */
366 + nfasprintf(&config_repo, "%.*s%s",
367 + (colon - 1) - value, value,
368 + start_ref + (end - colon - 2));
369 + return 1;
370 }
371
372 static int get_remote_config(const char *key, const char *value)
@@ -363,10 +376,12 @@ static int get_remote_config(const char *key, const char *value)
376 return 0;
377
378 var = strrchr(key, '.');
366 - if (var == key + 6)
379 + if (var == key + 6 || strcmp(var, ".fetch"))
380 return 0;
368 -
369 - if (!strcmp(var, ".fetch") && get_remote_branch_name(value))
381 + /*
382 + * Ok, we are looking at key == "remote.$foo.fetch";
383 + */
384 + if (get_remote_branch_name(value))
385 nfasprintf(&config_remote, "%.*s", var - (key + 7), key + 7);
386
387 return 0;
@@ -392,14 +407,14 @@ static void set_branch_merge(const char *name, const char *config_remote,
407
408 static void set_branch_defaults(const char *name, const char *real_ref)
409 {
395 - const char *slash = strrchr(real_ref, '/');
396 -
397 - if (!slash)
398 - return;
399 -
410 + /*
411 + * name is the name of new branch under refs/heads;
412 + * real_ref is typically refs/remotes/$foo/$bar, where
413 + * $foo is the remote name (there typically are no slashes)
414 + * and $bar is the branch name we map from the remote
415 + * (it could have slashes).
416 + */
417 start_ref = real_ref;
401 - start_len = strlen(real_ref);
402 - base_len = slash - real_ref;
418 git_config(get_remote_config);
419 if (!config_repo && !config_remote &&
420 !prefixcmp(real_ref, "refs/heads/")) {
t/t3200-branch.sh
+12 -4
@@ -136,8 +136,8 @@ test_expect_success 'test tracking setup (non-wildcard, not matching)' \
136 git-config remote.local.fetch refs/heads/s:refs/remotes/local/s &&
137 (git-show-ref -q refs/remotes/local/master || git-fetch local) &&
138 git-branch --track my5 local/master &&
139 - ! test $(git-config branch.my5.remote) = local &&
140 - ! test $(git-config branch.my5.merge) = refs/heads/master'
139 + ! test "$(git-config branch.my5.remote)" = local &&
140 + ! test "$(git-config branch.my5.merge)" = refs/heads/master'
141
142 test_expect_success 'test tracking setup via config' \
143 'git-config branch.autosetupmerge true &&
@@ -155,14 +155,22 @@ test_expect_success 'test overriding tracking setup via --no-track' \
155 (git-show-ref -q refs/remotes/local/master || git-fetch local) &&
156 git-branch --no-track my2 local/master &&
157 git-config branch.autosetupmerge false &&
158 - ! test $(git-config branch.my2.remote) = local &&
159 - ! test $(git-config branch.my2.merge) = refs/heads/master'
158 + ! test "$(git-config branch.my2.remote)" = local &&
159 + ! test "$(git-config branch.my2.merge)" = refs/heads/master'
160
161 test_expect_success 'test local tracking setup' \
162 'git branch --track my6 s &&
163 test $(git-config branch.my6.remote) = . &&
164 test $(git-config branch.my6.merge) = refs/heads/s'
165
166 +test_expect_success 'test tracking setup via --track but deeper' \
167 + 'git-config remote.local.url . &&
168 + git-config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
169 + (git-show-ref -q refs/remotes/local/o/o || git-fetch local) &&
170 + git-branch --track my7 local/o/o &&
171 + test "$(git-config branch.my7.remote)" = local &&
172 + test "$(git-config branch.my7.merge)" = refs/heads/o/o'
173 +
174 # Keep this test last, as it changes the current branch
175 cat >expect <<EOF
176 0000000000000000000000000000000000000000 $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from master