Skip to content

Commit 9a92cd1

Browse files
committed
Merge branch 'jc/strbuf-branchname-fix'
"git merge @{-1}~22" was rewritten to "git merge frotz@{1}~22" incorrectly when your previous branch was "frotz" (it should be rewritten to "git merge frotz~22" instead). * jc/strbuf-branchname-fix: strbuf_branchname(): do not double-expand @{-1}~22
2 parents db40094 + 84cf246 commit 9a92cd1

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)