Skip to content

Commit cf99a76

Browse files
felipecgitster
authored andcommitted
sha1-name: pass len argument to interpret_branch_name()
This is useful to make sure we don't step outside the boundaries of what we are interpreting at the moment. For example while interpreting foobar@{u}~1, the job of interpret_branch_name() ends right before ~1, but there's no way to figure that out inside the function, unless the len argument is passed. So let's do that. Signed-off-by: Felipe Contreras <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e230c56 commit cf99a76

File tree

4 files changed

+9
-7
lines changed

4 files changed

+9
-7
lines changed

cache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,7 @@ extern char *resolve_refdup(const char *ref, unsigned char *sha1, int reading, i
893893

894894
extern int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref);
895895
extern int dwim_log(const char *str, int len, unsigned char *sha1, char **ref);
896-
extern int interpret_branch_name(const char *str, struct strbuf *);
896+
extern int interpret_branch_name(const char *str, int len, struct strbuf *);
897897
extern int get_sha1_mb(const char *str, unsigned char *sha1);
898898

899899
extern int refname_match(const char *abbrev_name, const char *full_name, const char **rules);

refs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1951,7 +1951,7 @@ static int remove_empty_directories(const char *file)
19511951
static char *substitute_branch_name(const char **string, int *len)
19521952
{
19531953
struct strbuf buf = STRBUF_INIT;
1954-
int ret = interpret_branch_name(*string, &buf);
1954+
int ret = interpret_branch_name(*string, *len, &buf);
19551955

19561956
if (ret == *len) {
19571957
size_t size;

revision.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ static void add_pending_object_with_mode(struct rev_info *revs,
200200
revs->no_walk = 0;
201201
if (revs->reflog_info && obj->type == OBJ_COMMIT) {
202202
struct strbuf buf = STRBUF_INIT;
203-
int len = interpret_branch_name(name, &buf);
203+
int len = interpret_branch_name(name, 0, &buf);
204204
int st;
205205

206206
if (0 < len && name[len] && buf.len)

sha1_name.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,7 @@ static int reinterpret(const char *name, int namelen, int len, struct strbuf *bu
10121012
int ret;
10131013

10141014
strbuf_add(buf, name + len, namelen - len);
1015-
ret = interpret_branch_name(buf->buf, &tmp);
1015+
ret = interpret_branch_name(buf->buf, buf->len, &tmp);
10161016
/* that data was not interpreted, remove our cruft */
10171017
if (ret < 0) {
10181018
strbuf_setlen(buf, used);
@@ -1046,14 +1046,16 @@ static int reinterpret(const char *name, int namelen, int len, struct strbuf *bu
10461046
* If the input was ok but there are not N branch switches in the
10471047
* reflog, it returns 0.
10481048
*/
1049-
int interpret_branch_name(const char *name, struct strbuf *buf)
1049+
int interpret_branch_name(const char *name, int namelen, struct strbuf *buf)
10501050
{
10511051
char *cp;
10521052
struct branch *upstream;
1053-
int namelen = strlen(name);
10541053
int len = interpret_nth_prior_checkout(name, buf);
10551054
int tmp_len;
10561055

1056+
if (!namelen)
1057+
namelen = strlen(name);
1058+
10571059
if (!len) {
10581060
return len; /* syntax Ok, not enough switches */
10591061
} else if (len > 0) {
@@ -1100,7 +1102,7 @@ int interpret_branch_name(const char *name, struct strbuf *buf)
11001102
int strbuf_branchname(struct strbuf *sb, const char *name)
11011103
{
11021104
int len = strlen(name);
1103-
int used = interpret_branch_name(name, sb);
1105+
int used = interpret_branch_name(name, len, sb);
11041106

11051107
if (used == len)
11061108
return 0;

0 commit comments

Comments
 (0)