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

sha1_name: support @{-N} syntax in get_sha1()

Let get_sha1() parse the @{-N} syntax, with docs and tests. Note that while @{-1}^2, @{-2}~5 and such are supported, @{-1}@{1} is currently not allowed. Signed-off-by: Thomas Rast <trast@student.ethz.ch> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Thomas Rast committed Jan 17, 2009 at 17:09 UTC d18ba22154574390dbff2c060f44b9715477e95a
3 files changed +87 -3
Documentation/git-rev-parse.txt
+3
@@ -212,6 +212,9 @@ when you run 'git-merge'.
212 reflog of the current branch. For example, if you are on the
213 branch 'blabla', then '@\{1\}' means the same as 'blabla@\{1\}'.
214
215 +* The special construct '@\{-<n>\}' means the <n>th branch checked out
216 + before the current one.
217 +
218 * A suffix '{caret}' to a revision parameter means the first parent of
219 that commit object. '{caret}<n>' means the <n>th parent (i.e.
220 'rev{caret}'
sha1_name.c
+13 -3
@@ -297,6 +297,8 @@ int dwim_log(const char *str, int len, unsigned char *sha1, char **log)
297 return logs_found;
298 }
299
300 +static int get_sha1_1(const char *name, int len, unsigned char *sha1);
301 +
302 static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
303 {
304 static const char *warning = "warning: refname '%.*s' is ambiguous.\n";
@@ -307,7 +309,7 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
309 if (len == 40 && !get_sha1_hex(str, sha1))
310 return 0;
311
310 - /* basic@{time or number} format to query ref-log */
312 + /* basic@{time or number or -number} format to query ref-log */
313 reflog_len = at = 0;
314 if (str[len-1] == '}') {
315 for (at = 0; at < len - 1; at++) {
@@ -324,6 +326,16 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
326 return -1;
327
328 if (!len && reflog_len) {
329 + struct strbuf buf = STRBUF_INIT;
330 + int ret;
331 + /* try the @{-N} syntax for n-th checkout */
332 + ret = interpret_nth_last_branch(str+at, &buf);
333 + if (ret > 0) {
334 + /* substitute this branch name and restart */
335 + return get_sha1_1(buf.buf, buf.len, sha1);
336 + } else if (ret == 0) {
337 + return -1;
338 + }
339 /* allow "@{...}" to mean the current branch reflog */
340 refs_found = dwim_ref("HEAD", 4, sha1, &real_ref);
341 } else if (reflog_len)
@@ -379,8 +391,6 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
391 return 0;
392 }
393
382 -static int get_sha1_1(const char *name, int len, unsigned char *sha1);
383 -
394 static int get_parent(const char *name, int len,
395 unsigned char *result, int idx)
396 {
t/t1505-rev-parse-last.sh new
+71
@@ -0,0 +1,71 @@
1 +#!/bin/sh
2 +
3 +test_description='test @{-N} syntax'
4 +
5 +. ./test-lib.sh
6 +
7 +
8 +make_commit () {
9 + echo "$1" > "$1" &&
10 + git add "$1" &&
11 + git commit -m "$1"
12 +}
13 +
14 +
15 +test_expect_success 'setup' '
16 +
17 + make_commit 1 &&
18 + git branch side &&
19 + make_commit 2 &&
20 + make_commit 3 &&
21 + git checkout side &&
22 + make_commit 4 &&
23 + git merge master &&
24 + git checkout master
25 +
26 +'
27 +
28 +# 1 -- 2 -- 3 master
29 +# \ \
30 +# \ \
31 +# --- 4 --- 5 side
32 +#
33 +# and 'side' should be the last branch
34 +
35 +git log --graph --all --pretty=oneline --decorate
36 +
37 +test_rev_equivalent () {
38 +
39 + git rev-parse "$1" > expect &&
40 + git rev-parse "$2" > output &&
41 + test_cmp expect output
42 +
43 +}
44 +
45 +test_expect_success '@{-1} works' '
46 + test_rev_equivalent side @{-1}
47 +'
48 +
49 +test_expect_success '@{-1}~2 works' '
50 + test_rev_equivalent side~2 @{-1}~2
51 +'
52 +
53 +test_expect_success '@{-1}^2 works' '
54 + test_rev_equivalent side^2 @{-1}^2
55 +'
56 +
57 +test_expect_failure '@{-1}@{1} works' '
58 + test_rev_equivalent side@{1} @{-1}@{1}
59 +'
60 +
61 +test_expect_success '@{-2} works' '
62 + test_rev_equivalent master @{-2}
63 +'
64 +
65 +test_expect_success '@{-3} fails' '
66 + test_must_fail git rev-parse @{-3}
67 +'
68 +
69 +test_done
70 +
71 +