Skip to content

Commit 8f416f6

Browse files
peffgitster
authored andcommitted
shorten_unambiguous_ref(): use NUM_REV_PARSE_RULES constant
The ref_rev_parse_rules[] array is terminated with a NULL entry, and we count it and store the result in the local nr_rules variable. But we don't need to do so; since the array is a constant, we can compute its size directly. The original code probably didn't do that because it was written as part of for-each-ref, and saw the array only as a pointer. It was migrated in 7c2b302 (make get_short_ref a public function, 2009-04-07) and could have been updated then, but that subtlety was not noticed. We even have a constant that represents this value already, courtesy of 60650a4 (remote: make refspec follow the same disambiguation rule as local refs, 2018-08-01), though again, nobody noticed at the time that it could be used here, too. The current count-up isn't a big deal, as we need to preprocess that array anyway. But it will become more cumbersome as we refactor the shortening code. So let's get rid of it and just use the constant everywhere. Note that there are two things here that aren't just simple text replacements: 1. We also use nr_rules to see if a previous call has initialized the static pre-processing variables. We can just use the scanf_fmts pointer to do the same thing, as it is non-NULL only after we've done that initialization. 2. If nr_rules is zero after we've counted it up, we bail from the function. This code is unreachable, though, as the set of rules is hard-coded and non-empty. And that becomes even more apparent now that we are using the constant. So we can drop this conditional completely (and ironically, the code would have the same output if it _did_ trigger, as we'd simply skip the loop entirely and return the whole refname). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent dd5e4d3 commit 8f416f6

File tree

1 file changed

+8
-14
lines changed

1 file changed

+8
-14
lines changed

refs.c

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,11 +1315,10 @@ char *refs_shorten_unambiguous_ref(struct ref_store *refs,
13151315
{
13161316
int i;
13171317
static char **scanf_fmts;
1318-
static int nr_rules;
13191318
char *short_name;
13201319
struct strbuf resolved_buf = STRBUF_INIT;
13211320

1322-
if (!nr_rules) {
1321+
if (!scanf_fmts) {
13231322
/*
13241323
* Pre-generate scanf formats from ref_rev_parse_rules[].
13251324
* Generate a format suitable for scanf from a
@@ -1329,31 +1328,26 @@ char *refs_shorten_unambiguous_ref(struct ref_store *refs,
13291328
size_t total_len = 0;
13301329
size_t offset = 0;
13311330

1332-
/* the rule list is NULL terminated, count them first */
1333-
for (nr_rules = 0; ref_rev_parse_rules[nr_rules]; nr_rules++)
1331+
for (i = 0; i < NUM_REV_PARSE_RULES; i++)
13341332
/* -2 for strlen("%.*s") - strlen("%s"); +1 for NUL */
1335-
total_len += strlen(ref_rev_parse_rules[nr_rules]) - 2 + 1;
1333+
total_len += strlen(ref_rev_parse_rules[i]) - 2 + 1;
13361334

1337-
scanf_fmts = xmalloc(st_add(st_mult(sizeof(char *), nr_rules), total_len));
1335+
scanf_fmts = xmalloc(st_add(st_mult(sizeof(char *), NUM_REV_PARSE_RULES), total_len));
13381336

13391337
offset = 0;
1340-
for (i = 0; i < nr_rules; i++) {
1338+
for (i = 0; i < NUM_REV_PARSE_RULES; i++) {
13411339
assert(offset < total_len);
1342-
scanf_fmts[i] = (char *)&scanf_fmts[nr_rules] + offset;
1340+
scanf_fmts[i] = (char *)&scanf_fmts[NUM_REV_PARSE_RULES] + offset;
13431341
offset += xsnprintf(scanf_fmts[i], total_len - offset,
13441342
ref_rev_parse_rules[i], 2, "%s") + 1;
13451343
}
13461344
}
13471345

1348-
/* bail out if there are no rules */
1349-
if (!nr_rules)
1350-
return xstrdup(refname);
1351-
13521346
/* buffer for scanf result, at most refname must fit */
13531347
short_name = xstrdup(refname);
13541348

13551349
/* skip first rule, it will always match */
1356-
for (i = nr_rules - 1; i > 0 ; --i) {
1350+
for (i = NUM_REV_PARSE_RULES - 1; i > 0 ; --i) {
13571351
int j;
13581352
int rules_to_fail = i;
13591353
size_t short_name_len;
@@ -1368,7 +1362,7 @@ char *refs_shorten_unambiguous_ref(struct ref_store *refs,
13681362
* must fail to resolve to a valid non-ambiguous ref
13691363
*/
13701364
if (strict)
1371-
rules_to_fail = nr_rules;
1365+
rules_to_fail = NUM_REV_PARSE_RULES;
13721366

13731367
/*
13741368
* check if the short name resolves to a valid ref,

0 commit comments

Comments
 (0)