index-pack: add a helper function to derive .idx/.keep filename
These are automatically named by replacing .pack suffix in the name of the packfile. Add a small helper to do so, as I'll be adding another one soonish. Signed-off-by: Junio C Hamano <gitster@pobox.com>
Junio C Hamano committed
Mar 3, 2016 at 11:29 UTC
bfee614a2f5838e03378888d78e8247b8400d5ce
1 file changed
+17
-18
builtin/index-pack.c
+17
-18
@@ -1598,6 +1598,18 @@ static void show_pack_info(int stat_only)
1598
}
1599
}
1600
1601
+static const char *derive_filename(const char *pack_name, const char *suffix,
1602
+ struct strbuf *buf)
1603
+{
1604
+ size_t len;
1605
+ if (!strip_suffix(pack_name, ".pack", &len))
1606
+ die(_("packfile name '%s' does not end with '.pack'"),
1607
+ pack_name);
1608
+ strbuf_add(buf, pack_name, len);
1609
+ strbuf_addstr(buf, suffix);
1610
+ return buf->buf;
1611
+}
1612
+
1613
int cmd_index_pack(int argc, const char **argv, const char *prefix)
1614
{
1615
int i, fix_thin_pack = 0, verify = 0, stat_only = 0;
@@ -1706,24 +1718,11 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
1718
usage(index_pack_usage);
1719
if (fix_thin_pack && !from_stdin)
1720
die(_("--fix-thin cannot be used without --stdin"));
1709
- if (!index_name && pack_name) {
1710
- size_t len;
1711
- if (!strip_suffix(pack_name, ".pack", &len))
1712
- die(_("packfile name '%s' does not end with '.pack'"),
1713
- pack_name);
1714
- strbuf_add(&index_name_buf, pack_name, len);
1715
- strbuf_addstr(&index_name_buf, ".idx");
1716
- index_name = index_name_buf.buf;
1717
- }
1718
- if (keep_msg && !keep_name && pack_name) {
1719
- size_t len;
1720
- if (!strip_suffix(pack_name, ".pack", &len))
1721
- die(_("packfile name '%s' does not end with '.pack'"),
1722
- pack_name);
1723
- strbuf_add(&keep_name_buf, pack_name, len);
1724
- strbuf_addstr(&keep_name_buf, ".keep");
1725
- keep_name = keep_name_buf.buf;
1726
- }
1721
+ if (!index_name && pack_name)
1722
+ index_name = derive_filename(pack_name, ".idx", &index_name_buf);
1723
+ if (keep_msg && !keep_name && pack_name)
1724
+ keep_name = derive_filename(pack_name, ".keep", &keep_name_buf);
1725
+
1726
if (verify) {
1727
if (!index_name)
1728
die(_("--verify with no packfile name given"));