interpret_branch_name(): handle auto-namelen for @{-1}

The interpret_branch_name() function takes a ptr/len pair for the name, but you can pass "0" for "namelen", which will cause it to check the length with strlen(). However, before we do that auto-namelen magic, we call interpret_nth_prior_checkout(), which gets fed the bogus "0". This was broken by 8cd4249c4 (interpret_branch_name: always respect "namelen" parameter, 2014-01-15). Though to be fair to that commit, it was broken in the _opposite_ direction before, where we would always treat "name" as a string even if a length was passed. You can see the bug with "git log -g @{-1}". That code path always passes "0", and without this patch it cannot figure out which branch's reflog to show. We can fix it by a small reordering of the code. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Feb 27, 2017 at 04:25 UTC 13228c30a6476456ee64eb7cefb7786d82fd2ca7
2 files changed +10 -1
sha1_name.c
+2 -1
@@ -1263,11 +1263,12 @@ int interpret_branch_name(const char *name, int namelen, struct strbuf *buf)
1263 {
1264 char *at;
1265 const char *start;
1266 - int len = interpret_nth_prior_checkout(name, namelen, buf);
1266 + int len;
1267
1268 if (!namelen)
1269 namelen = strlen(name);
1270
1271 + len = interpret_nth_prior_checkout(name, namelen, buf);
1272 if (!len) {
1273 return len; /* syntax Ok, not enough switches */
1274 } else if (len > 0) {
t/t0100-previous.sh
+8
@@ -56,5 +56,13 @@ test_expect_success 'merge @{-100} before checking out that many branches yet' '
56 test_must_fail git merge @{-100}
57 '
58
59 +test_expect_success 'log -g @{-1}' '
60 + git checkout -b last_branch &&
61 + git checkout -b new_branch &&
62 + echo "last_branch@{0}" >expect &&
63 + git log -g --format=%gd @{-1} >actual &&
64 + test_cmp expect actual
65 +'
66 +
67 test_done
68