Move dash is previous branch check to get_sha1_basic.
Introduce helper function that gets nth prior branch switch from reflog.

Signed-off-by: mash <mash+...@crossperf.com>
---
RE: [PATCH 4/6 v5] sha1_name.c: teach get_sha1_1 "-" shorthand for "@{-1}"
> +     if (*name == '-' && len == 1) {
> +             name = "@{-1}";
> +             len = 5;
> +     }

We could avoid parsing @{-1} unnecessarily with something like this patch.

Forgive me I don't understand how the patch numbering works just yet. This is
6/6 because format-patch made it 6/6 with however I got the patches applied on
my end. This should apply cleanly on pu anyways.

Thanks to Stefan since he suggested that I might want to review this.

mash

 sha1_name.c | 39 ++++++++++++++++++++++++---------------
 1 file changed, 24 insertions(+), 15 deletions(-)

diff --git a/sha1_name.c b/sha1_name.c
index 2f86bc9..363bbe7 100644
--- a/sha1_name.c
+++ b/sha1_name.c
@@ -568,6 +568,7 @@ static inline int push_mark(const char *string, int len)
 }
 
 static int get_sha1_1(const char *name, int len, unsigned char *sha1, unsigned 
lookup_flags);
+static int get_branch_switch(int nth, struct strbuf *buf);
 static int interpret_nth_prior_checkout(const char *name, int namelen, struct 
strbuf *buf);
 
 static int get_sha1_basic(const char *str, int len, unsigned char *sha1,
@@ -628,11 +629,12 @@ static int get_sha1_basic(const char *str, int len, 
unsigned char *sha1,
        if (len && ambiguous_path(str, len))
                return -1;
 
-       if (nth_prior) {
+       if (nth_prior || !strcmp(str, "-")) {
                struct strbuf buf = STRBUF_INIT;
                int detached;
 
-               if (interpret_nth_prior_checkout(str, len, &buf) > 0) {
+               if (nth_prior ? interpret_nth_prior_checkout(str, len, &buf) > 0
+                             : get_branch_switch(1, &buf) > 0) {
                        detached = (buf.len == 40 && !get_sha1_hex(buf.buf, 
sha1));
                        strbuf_release(&buf);
                        if (detached)
@@ -1078,6 +1080,25 @@ static int grab_nth_branch_switch(unsigned char *osha1, 
unsigned char *nsha1,
        return 0;
 }
 
+static int get_branch_switch(int nth, struct strbuf *buf)
+{
+       int retval;
+       struct grab_nth_branch_switch_cbdata cb;
+
+       cb.remaining = nth;
+       strbuf_init(&cb.buf, 20);
+
+       retval = for_each_reflog_ent_reverse("HEAD", grab_nth_branch_switch,
+                                            &cb);
+       if (0 < retval) {
+               strbuf_reset(buf);
+               strbuf_addbuf(buf, &cb.buf);
+       }
+
+       strbuf_release(&cb.buf);
+       return retval;
+}
+
 /*
  * Parse @{-N} syntax, return the number of characters parsed
  * if successful; otherwise signal an error with negative value.
@@ -1086,8 +1107,6 @@ static int interpret_nth_prior_checkout(const char *name, 
int namelen,
                                        struct strbuf *buf)
 {
        long nth;
-       int retval;
-       struct grab_nth_branch_switch_cbdata cb;
        const char *brace;
        char *num_end;
 
@@ -1103,18 +1122,8 @@ static int interpret_nth_prior_checkout(const char 
*name, int namelen,
                return -1;
        if (nth <= 0)
                return -1;
-       cb.remaining = nth;
-       strbuf_init(&cb.buf, 20);
 
-       retval = 0;
-       if (0 < for_each_reflog_ent_reverse("HEAD", grab_nth_branch_switch, 
&cb)) {
-               strbuf_reset(buf);
-               strbuf_addbuf(buf, &cb.buf);
-               retval = brace - name + 1;
-       }
-
-       strbuf_release(&cb.buf);
-       return retval;
+       return 0 < get_branch_switch(nth, buf) ? brace - name + 1 : 0;
 }
 
 int get_oid_mb(const char *name, struct object_id *oid)
-- 
2.9.3

Reply via email to