strbuf_branchname(): do not double-expand @{-1}~22

If you were on 'frotz' branch before you checked out your current branch, "git merge @{-1}~22" means the same as "git merge frotz~22". The strbuf_branchname() function, when interpret_branch_name() gives up resolving "@{-1}~22" fully, returns "frotz" and tells the caller that it only resolved "@{-1}" part of the input, mistakes this as a total failure, and appends the whole thing to the result, yielding "frotz@{-1}~22", which does not make any sense. Inspect the return value from interpret_branch_name() a bit more carefully. When it errored out without consuming anything, we will get -1 and we should return the whole thing. Otherwise, we should append the remainder (i.e. "~22" in the earlier example) to the partially resolved name (i.e. "frotz"). The test suite adds enough number of checkout to make @{-12} in the last test in t0100 that tried to check "we haven't flipped branches that many times" error case succeed; raise the number to a hundred. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed May 15, 2013 at 14:32 UTC 84cf246670eab56a23ed5554ed084053a0f19f2d
2 files changed +19 -4
sha1_name.c
+6 -2
@@ -1055,9 +1055,13 @@ int interpret_branch_name(const char *name, struct strbuf *buf)
1055 int strbuf_branchname(struct strbuf *sb, const char *name)
1056 {
1057 int len = strlen(name);
1058 - if (interpret_branch_name(name, sb) == len)
1058 + int used = interpret_branch_name(name, sb);
1059 +
1060 + if (used == len)
1061 return 0;
1060 - strbuf_add(sb, name, len);
1062 + if (used < 0)
1063 + used = 0;
1064 + strbuf_add(sb, name + used, len - used);
1065 return len;
1066 }
1067
t/t0100-previous.sh
+13 -2
@@ -27,6 +27,7 @@ test_expect_success 'merge @{-1}' '
27 test_commit B &&
28 git checkout A &&
29 test_commit C &&
30 + test_commit D &&
31 git branch -f master B &&
32 git branch -f other &&
33 git checkout other &&
@@ -35,14 +36,24 @@ test_expect_success 'merge @{-1}' '
36 git cat-file commit HEAD | grep "Merge branch '\''other'\''"
37 '
38
38 -test_expect_success 'merge @{-1} when there is not enough switches yet' '
39 +test_expect_success 'merge @{-1}~1' '
40 + git checkout master &&
41 + git reset --hard B &&
42 + git checkout other &&
43 + git checkout master &&
44 + git merge @{-1}~1 &&
45 + git cat-file commit HEAD >actual &&
46 + grep "Merge branch '\''other'\''" actual
47 +'
48 +
49 +test_expect_success 'merge @{-100} before checking out that many branches yet' '
50 git reflog expire --expire=now &&
51 git checkout -f master &&
52 git reset --hard B &&
53 git branch -f other C &&
54 git checkout other &&
55 git checkout master &&
45 - test_must_fail git merge @{-12}
56 + test_must_fail git merge @{-100}
57 '
58
59 test_done