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 7c2b3029df (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 60650a48c0 (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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Feb 15, 2023 at 10:16 UTC 8f416f65c9b1a42d7e6bf747c7ac5cf6a49250f8
1 file changed +8 -14
refs.c
+8 -14
@@ -1315,11 +1315,10 @@ char *refs_shorten_unambiguous_ref(struct ref_store *refs,
1315 {
1316 int i;
1317 static char **scanf_fmts;
1318 - static int nr_rules;
1318 char *short_name;
1319 struct strbuf resolved_buf = STRBUF_INIT;
1320
1322 - if (!nr_rules) {
1321 + if (!scanf_fmts) {
1322 /*
1323 * Pre-generate scanf formats from ref_rev_parse_rules[].
1324 * Generate a format suitable for scanf from a
@@ -1329,31 +1328,26 @@ char *refs_shorten_unambiguous_ref(struct ref_store *refs,
1328 size_t total_len = 0;
1329 size_t offset = 0;
1330
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++)
1332 /* -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;
1334
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));
1336
1337 offset = 0;
1340 - for (i = 0; i < nr_rules; i++) {
1338 + for (i = 0; i < NUM_REV_PARSE_RULES; i++) {
1339 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;
1341 offset += xsnprintf(scanf_fmts[i], total_len - offset,
1342 ref_rev_parse_rules[i], 2, "%s") + 1;
1343 }
1344 }
1345
1348 - /* bail out if there are no rules */
1349 - if (!nr_rules)
1350 - return xstrdup(refname);
1351 -
1346 /* buffer for scanf result, at most refname must fit */
1347 short_name = xstrdup(refname);
1348
1349 /* 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) {
1351 int j;
1352 int rules_to_fail = i;
1353 size_t short_name_len;
@@ -1368,7 +1362,7 @@ char *refs_shorten_unambiguous_ref(struct ref_store *refs,
1362 * must fail to resolve to a valid non-ambiguous ref
1363 */
1364 if (strict)
1371 - rules_to_fail = nr_rules;
1365 + rules_to_fail = NUM_REV_PARSE_RULES;
1366
1367 /*
1368 * check if the short name resolves to a valid ref,