sha1_name: refactor reinterpret()

This code essentially replaces part of ref with another ref, for example '@{-1}@{u}' is replaced with 'master@{u}', but this can be reused for other purposes other than nth prior checkouts. Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Felipe Contreras committed May 7, 2013 at 17:04 UTC 7a0a49a7ca787433255280b66e7652b56f7beb6e
1 file changed +23 -19
sha1_name.c
+23 -19
@@ -972,6 +972,27 @@ int get_sha1_mb(const char *name, unsigned char *sha1)
972 return st;
973 }
974
975 +static int reinterpret(const char *name, int namelen, int len, struct strbuf *buf)
976 +{
977 + /* we have extra data, which might need further processing */
978 + struct strbuf tmp = STRBUF_INIT;
979 + int used = buf->len;
980 + int ret;
981 +
982 + strbuf_add(buf, name + len, namelen - len);
983 + ret = interpret_branch_name(buf->buf, &tmp);
984 + /* that data was not interpreted, remove our cruft */
985 + if (ret < 0) {
986 + strbuf_setlen(buf, used);
987 + return len;
988 + }
989 + strbuf_reset(buf);
990 + strbuf_addbuf(buf, &tmp);
991 + strbuf_release(&tmp);
992 + /* tweak for size of {-N} versus expanded ref name */
993 + return ret - used + len;
994 +}
995 +
996 /*
997 * This reads short-hand syntax that not only evaluates to a commit
998 * object name, but also can act as if the end user spelled the name
@@ -1005,25 +1026,8 @@ int interpret_branch_name(const char *name, struct strbuf *buf)
1026 return len; /* syntax Ok, not enough switches */
1027 if (0 < len && len == namelen)
1028 return len; /* consumed all */
1008 - else if (0 < len) {
1009 - /* we have extra data, which might need further processing */
1010 - struct strbuf tmp = STRBUF_INIT;
1011 - int used = buf->len;
1012 - int ret;
1013 -
1014 - strbuf_add(buf, name + len, namelen - len);
1015 - ret = interpret_branch_name(buf->buf, &tmp);
1016 - /* that data was not interpreted, remove our cruft */
1017 - if (ret < 0) {
1018 - strbuf_setlen(buf, used);
1019 - return len;
1020 - }
1021 - strbuf_reset(buf);
1022 - strbuf_addbuf(buf, &tmp);
1023 - strbuf_release(&tmp);
1024 - /* tweak for size of {-N} versus expanded ref name */
1025 - return ret - used + len;
1026 - }
1029 + else if (0 < len)
1030 + return reinterpret(name, namelen, len, buf);
1031
1032 cp = strchr(name, '@');
1033 if (!cp)