builtin/index-pack.c: allow stripping arbitrary extensions

To derive the filename for a .idx file, 'git index-pack' uses derive_filename() to strip the '.pack' suffix and add the new suffix. Prepare for stripping off suffixes other than '.pack' by making the suffix to strip a parameter of derive_filename(). In order to make this consistent with the "suffix" parameter which does not begin with a ".", an additional check in derive_filename. Suggested-by: Jeff King <peff@peff.net> Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Taylor Blau committed Jan 25, 2021 at 18:37 UTC 84d544943c27a1540826730b6a8f588200c65fe6
1 file changed +8 -8
builtin/index-pack.c
+8 -8
@@ -1436,15 +1436,15 @@ static void fix_unresolved_deltas(struct hashfile *f)
1436 free(sorted_by_pos);
1437 }
1438
1439 -static const char *derive_filename(const char *pack_name, const char *suffix,
1440 - struct strbuf *buf)
1439 +static const char *derive_filename(const char *pack_name, const char *strip,
1440 + const char *suffix, struct strbuf *buf)
1441 {
1442 size_t len;
1443 - if (!strip_suffix(pack_name, ".pack", &len))
1444 - die(_("packfile name '%s' does not end with '.pack'"),
1445 - pack_name);
1443 + if (!strip_suffix(pack_name, strip, &len) || !len ||
1444 + pack_name[len - 1] != '.')
1445 + die(_("packfile name '%s' does not end with '.%s'"),
1446 + pack_name, strip);
1447 strbuf_add(buf, pack_name, len);
1447 - strbuf_addch(buf, '.');
1448 strbuf_addstr(buf, suffix);
1449 return buf->buf;
1450 }
@@ -1459,7 +1459,7 @@ static void write_special_file(const char *suffix, const char *msg,
1459 int msg_len = strlen(msg);
1460
1461 if (pack_name)
1462 - filename = derive_filename(pack_name, suffix, &name_buf);
1462 + filename = derive_filename(pack_name, "pack", suffix, &name_buf);
1463 else
1464 filename = odb_pack_name(&name_buf, hash, suffix);
1465
@@ -1824,7 +1824,7 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
1824 if (from_stdin && hash_algo)
1825 die(_("--object-format cannot be used with --stdin"));
1826 if (!index_name && pack_name)
1827 - index_name = derive_filename(pack_name, "idx", &index_name_buf);
1827 + index_name = derive_filename(pack_name, "pack", "idx", &index_name_buf);
1828
1829 if (verify) {
1830 if (!index_name)