Skip to content

Commit 7b99bef

Browse files
committed
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1367214 commit 7b99bef

File tree

2 files changed

+53
-30
lines changed

2 files changed

+53
-30
lines changed

builtin-branch.c

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,6 @@ static void print_ref_list(int kinds, int detached, int verbose, int abbrev)
317317
static char *config_repo;
318318
static char *config_remote;
319319
static const char *start_ref;
320-
static int start_len;
321-
static int base_len;
322320

323321
static int get_remote_branch_name(const char *value)
324322
{
@@ -334,26 +332,41 @@ static int get_remote_branch_name(const char *value)
334332

335333
end = value + strlen(value);
336334

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+
*/
338340
if (!strcmp(colon + 1, start_ref)) {
339-
/* Truncate the value before the colon. */
341+
/* Truncate the value before the colon. */
340342
nfasprintf(&config_repo, "%.*s", colon - value, value);
341343
return 1;
342344
}
343345

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;
355352

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;
357370
}
358371

359372
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)
363376
return 0;
364377

365378
var = strrchr(key, '.');
366-
if (var == key + 6)
379+
if (var == key + 6 || strcmp(var, ".fetch"))
367380
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))
370385
nfasprintf(&config_remote, "%.*s", var - (key + 7), key + 7);
371386

372387
return 0;
@@ -392,14 +407,14 @@ static void set_branch_merge(const char *name, const char *config_remote,
392407

393408
static void set_branch_defaults(const char *name, const char *real_ref)
394409
{
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+
*/
400417
start_ref = real_ref;
401-
start_len = strlen(real_ref);
402-
base_len = slash - real_ref;
403418
git_config(get_remote_config);
404419
if (!config_repo && !config_remote &&
405420
!prefixcmp(real_ref, "refs/heads/")) {

t/t3200-branch.sh

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ test_expect_success 'test tracking setup (non-wildcard, not matching)' \
136136
git-config remote.local.fetch refs/heads/s:refs/remotes/local/s &&
137137
(git-show-ref -q refs/remotes/local/master || git-fetch local) &&
138138
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'
141141

142142
test_expect_success 'test tracking setup via config' \
143143
'git-config branch.autosetupmerge true &&
@@ -155,14 +155,22 @@ test_expect_success 'test overriding tracking setup via --no-track' \
155155
(git-show-ref -q refs/remotes/local/master || git-fetch local) &&
156156
git-branch --no-track my2 local/master &&
157157
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'
160160

161161
test_expect_success 'test local tracking setup' \
162162
'git branch --track my6 s &&
163163
test $(git-config branch.my6.remote) = . &&
164164
test $(git-config branch.my6.merge) = refs/heads/s'
165165

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+
166174
# Keep this test last, as it changes the current branch
167175
cat >expect <<EOF
168176
0000000000000000000000000000000000000000 $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from master

0 commit comments

Comments
 (0)