Skip to content

Commit 7a0a49a

Browse files
felipecgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 83d16bc commit 7a0a49a

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

sha1_name.c

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -972,6 +972,27 @@ int get_sha1_mb(const char *name, unsigned char *sha1)
972972
return st;
973973
}
974974

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+
975996
/*
976997
* This reads short-hand syntax that not only evaluates to a commit
977998
* 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)
10051026
return len; /* syntax Ok, not enough switches */
10061027
if (0 < len && len == namelen)
10071028
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);
10271031

10281032
cp = strchr(name, '@');
10291033
if (!cp)

0 commit comments

Comments
 (0)