@matheusbsbs / matheus-bagliano / commits / aa9c55b667
Fork 0

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 <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Jan 17, 2009 at 19:08 UTC aa9c55b66719c86896d134d35de8c263c078a481
2 files changed +25 -2
sha1_name.c
+24 -1
@@ -238,8 +238,28 @@ static int ambiguous_path(const char *path, int len)
238 return slash;
239 }
240
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 +
260 int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref)
261 {
262 + char *last_branch = substitute_nth_last_branch(&str, &len);
263 const char **p, *r;
264 int refs_found = 0;
265
@@ -259,11 +279,13 @@ int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref)
279 break;
280 }
281 }
282 + free(last_branch);
283 return refs_found;
284 }
285
286 int dwim_log(const char *str, int len, unsigned char *sha1, char **log)
287 {
288 + char *last_branch = substitute_nth_last_branch(&str, &len);
289 const char **p;
290 int logs_found = 0;
291
@@ -294,6 +316,7 @@ int dwim_log(const char *str, int len, unsigned char *sha1, char **log)
316 if (!warn_ambiguous_refs)
317 break;
318 }
319 + free(last_branch);
320 return logs_found;
321 }
322
@@ -312,7 +335,7 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
335 /* basic@{time or number or -number} format to query ref-log */
336 reflog_len = at = 0;
337 if (str[len-1] == '}') {
315 - for (at = 0; at < len - 1; at++) {
338 + for (at = len-2; at >= 0; at--) {
339 if (str[at] == '@' && str[at+1] == '{') {
340 reflog_len = (len-1) - (at+2);
341 len = at;
t/t1505-rev-parse-last.sh
+1 -1
@@ -54,7 +54,7 @@ test_expect_success '@{-1}^2 works' '
54 test_rev_equivalent side^2 @{-1}^2
55 '
56
57 -test_expect_failure '@{-1}@{1} works' '
57 +test_expect_success '@{-1}@{1} works' '
58 test_rev_equivalent side@{1} @{-1}@{1}
59 '
60