Skip to content

Commit 84cf246

Browse files
committed
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 <[email protected]>
1 parent 6a3ac18 commit 84cf246

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

sha1_name.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,9 +1055,13 @@ int interpret_branch_name(const char *name, struct strbuf *buf)
10551055
int strbuf_branchname(struct strbuf *sb, const char *name)
10561056
{
10571057
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)
10591061
return 0;
1060-
strbuf_add(sb, name, len);
1062+
if (used < 0)
1063+
used = 0;
1064+
strbuf_add(sb, name + used, len - used);
10611065
return len;
10621066
}
10631067

t/t0100-previous.sh

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ test_expect_success 'merge @{-1}' '
2727
test_commit B &&
2828
git checkout A &&
2929
test_commit C &&
30+
test_commit D &&
3031
git branch -f master B &&
3132
git branch -f other &&
3233
git checkout other &&
@@ -35,14 +36,24 @@ test_expect_success 'merge @{-1}' '
3536
git cat-file commit HEAD | grep "Merge branch '\''other'\''"
3637
'
3738

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' '
3950
git reflog expire --expire=now &&
4051
git checkout -f master &&
4152
git reset --hard B &&
4253
git branch -f other C &&
4354
git checkout other &&
4455
git checkout master &&
45-
test_must_fail git merge @{-12}
56+
test_must_fail git merge @{-100}
4657
'
4758

4859
test_done

0 commit comments

Comments
 (0)