Web lists-archives.com

[PATCH 03/12] packfile: factor out .pack to .idx name conversion




When we open a pack .idx, we have to convert the "foo.pack" name stored
in the packed_git struct to "foo.idx". This isn't too complicated, but
we do encode some policy in the form of a BUG(). Let's pull this into
its own function so it can be reused.

Signed-off-by: Jeff King <peff@xxxxxxxx>
---
 packfile.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/packfile.c b/packfile.c
index 16bcb75262..054269ae5d 100644
--- a/packfile.c
+++ b/packfile.c
@@ -186,18 +186,24 @@ int load_idx(const char *path, const unsigned int hashsz, void *idx_map,
 	return 0;
 }
 
+static char *pack_name_to_idx(const char *pack_name)
+{
+	size_t len;
+
+	if (!strip_suffix(pack_name, ".pack", &len))
+		BUG("pack_name does not end in .pack");
+	return xstrfmt("%.*s.idx", (int)len, pack_name);
+}
+
 int open_pack_index(struct packed_git *p)
 {
 	char *idx_name;
-	size_t len;
 	int ret;
 
 	if (p->index_data)
 		return 0;
 
-	if (!strip_suffix(p->pack_name, ".pack", &len))
-		BUG("pack_name does not end in .pack");
-	idx_name = xstrfmt("%.*s.idx", (int)len, p->pack_name);
+	idx_name = pack_name_to_idx(p->pack_name);
 	ret = check_packed_git_idx(idx_name, p);
 	free(idx_name);
 	return ret;
-- 
2.21.0.714.gd1be1d035b