Skip to content

Commit aa9c55b

Browse files
dschogitster
authored andcommitted
Fix parsing of @{-1}@{1}
To do that, Git no longer looks forward for the '@{' corresponding to the closing '}' but backward, and dwim_ref() as well as dwim_log() learnt about the @{-<N>} notation. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c2883e6 commit aa9c55b

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

sha1_name.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,28 @@ static int ambiguous_path(const char *path, int len)
238238
return slash;
239239
}
240240

241+
/*
242+
* *string and *len will only be substituted, and *string returned (for
243+
* later free()ing) if the string passed in is of the form @{-<n>}.
244+
*/
245+
static char *substitute_nth_last_branch(const char **string, int *len)
246+
{
247+
struct strbuf buf = STRBUF_INIT;
248+
int ret = interpret_nth_last_branch(*string, &buf);
249+
250+
if (ret == *len) {
251+
size_t size;
252+
*string = strbuf_detach(&buf, &size);
253+
*len = size;
254+
return (char *)*string;
255+
}
256+
257+
return NULL;
258+
}
259+
241260
int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref)
242261
{
262+
char *last_branch = substitute_nth_last_branch(&str, &len);
243263
const char **p, *r;
244264
int refs_found = 0;
245265

@@ -259,11 +279,13 @@ int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref)
259279
break;
260280
}
261281
}
282+
free(last_branch);
262283
return refs_found;
263284
}
264285

265286
int dwim_log(const char *str, int len, unsigned char *sha1, char **log)
266287
{
288+
char *last_branch = substitute_nth_last_branch(&str, &len);
267289
const char **p;
268290
int logs_found = 0;
269291

@@ -294,6 +316,7 @@ int dwim_log(const char *str, int len, unsigned char *sha1, char **log)
294316
if (!warn_ambiguous_refs)
295317
break;
296318
}
319+
free(last_branch);
297320
return logs_found;
298321
}
299322

@@ -312,7 +335,7 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
312335
/* basic@{time or number or -number} format to query ref-log */
313336
reflog_len = at = 0;
314337
if (str[len-1] == '}') {
315-
for (at = 0; at < len - 1; at++) {
338+
for (at = len-2; at >= 0; at--) {
316339
if (str[at] == '@' && str[at+1] == '{') {
317340
reflog_len = (len-1) - (at+2);
318341
len = at;

t/t1505-rev-parse-last.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ test_expect_success '@{-1}^2 works' '
5454
test_rev_equivalent side^2 @{-1}^2
5555
'
5656

57-
test_expect_failure '@{-1}@{1} works' '
57+
test_expect_success '@{-1}@{1} works' '
5858
test_rev_equivalent side@{1} @{-1}@{1}
5959
'
6060

0 commit comments

Comments
 (0)