git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 4/6] packfile: introduce function to free a packfile

From
Patrick Steinhardt <ps@pks.im>
Date
Nov 20, 2026, 12:02 UTC
Message-ID
<20261120-pks-packfile-global-statistics-v1-4-19beb83b145f@pks.im>
In-Reply-To
<20261120-pks-packfile-global-statistics-v1-0-19beb83b145f@pks.im>

We have a bunch of callsites that want to free a packfile, which requires them to first close it followed by a call to free(3p). This feels rather leaky as an abstraction.

Introduce `packfile_free()` to handle this properly. This function will also be extended in a subsequent patch to maintain a list of all packs.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 builtin/index-pack.c | 10 +---------
 commit-graph.c       |  6 ++----
 connected.c          |  6 ++----
 http-walker.c        |  6 ++----
 http.c               |  2 +-
 midx-write.c         |  9 +++------
 odb/source-packed.c  |  2 +-
 packfile.c           | 10 ++++++++--
 packfile.h           |  1 +
 9 files changed, 21 insertions(+), 31 deletions(-)
diff --git a/builtin/index-pack.c b/builtin/index-pack.c
index 71048a5a8af..663375fbb36 100644
--- a/builtin/index-pack.c
+++ b/builtin/index-pack.c
@@ -1755,15 +1755,7 @@ static void read_idx_option(struct pack_idx_option *opts, const char *pack_name)
 	if (opts->version == 2)
 		read_v2_anomalous_offsets(p, opts);
 
-	/*
-	 * Get rid of the idx file as we do not need it anymore.
-	 * NEEDSWORK: extract this bit from free_pack_by_name() in
-	 * object-file.c, perhaps?  It shouldn't matter very much as we
-	 * know we haven't installed this pack (hence we never have
-	 * read anything from it).
-	 */
-	packfile_close_index(p);
-	free(p);
+	packfile_free(p);
 }
 
 static void show_pack_info(int stat_only)
diff --git a/commit-graph.c b/commit-graph.c
index c3b04e8c1f4..03df5fb7c1f 100644
--- a/commit-graph.c
+++ b/commit-graph.c
@@ -1958,14 +1958,12 @@ static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
 		}
 		if (packfile_open_index(p)) {
 			ret = error(_("error opening index for %s"), pack_indexes->items[i].string);
-			packfile_close(p);
-			free(p);
+			packfile_free(p);
 			goto cleanup;
 		}
 		for_each_object_in_pack(p, add_packed_commits, ctx,
 					ODB_FOR_EACH_OBJECT_PACK_ORDER);
-		packfile_close(p);
-		free(p);
+		packfile_free(p);
 	}
 
 cleanup:
diff --git a/connected.c b/connected.c
index fbacff734b6..78f08b41383 100644
--- a/connected.c
+++ b/connected.c
@@ -238,9 +238,7 @@ int check_connected(struct repository *repo,
 		err = error_errno(_("failed to close rev-list's stdin"));
 
 	sigchain_pop(SIGPIPE);
-	if (new_pack) {
-		packfile_close(new_pack);
-		free(new_pack);
-	}
+	if (new_pack)
+		packfile_free(new_pack);
 	return finish_command(&rev_list) || err;
 }
diff --git a/http-walker.c b/http-walker.c
index de43d9cf673..616a5fd234d 100644
--- a/http-walker.c
+++ b/http-walker.c
@@ -587,10 +587,8 @@ static void cleanup(struct walker *walker)
 
 			alt_next = alt->next;
 
-			for (e = alt->packs.head; e; e = e->next) {
-				packfile_close(e->pack);
-				free(e->pack);
-			}
+			for (e = alt->packs.head; e; e = e->next)
+				packfile_free(e->pack);
 			packfile_list_clear(&alt->packs);
 
 			free(alt->base);
diff --git a/http.c b/http.c
index e86bebdffb2..d518683e15e 100644
--- a/http.c
+++ b/http.c
@@ -2661,7 +2661,7 @@ static int fetch_and_setup_pack_index(struct packfile_list *packs,
 	packfile_close_index(new_pack);
 	free(tmp_idx);
 	if (ret) {
-		free(new_pack);
+		packfile_free(new_pack);
 		return -1;
 	}
 
diff --git a/midx-write.c b/midx-write.c
index d5be4e40843..3e227bb5e57 100644
--- a/midx-write.c
+++ b/midx-write.c
@@ -182,8 +182,7 @@ static void add_pack_to_midx(const char *full_path, size_t full_path_len,
 		if (packfile_open_index(p)) {
 			warning(_("failed to open pack-index '%s'"),
 				full_path);
-			packfile_close(p);
-			free(p);
+			packfile_free(p);
 			return;
 		}
 
@@ -1827,10 +1826,8 @@ static int write_midx_internal(struct write_midx_opts *opts)
 
 cleanup:
 	for (size_t i = 0; i < ctx.nr; i++) {
-		if (ctx.info[i].p) {
-			packfile_close(ctx.info[i].p);
-			free(ctx.info[i].p);
-		}
+		if (ctx.info[i].p)
+			packfile_free(ctx.info[i].p);
 		free(ctx.info[i].pack_name);
 	}
 
diff --git a/odb/source-packed.c b/odb/source-packed.c
index 87e2dcdf52e..866b4e890b6 100644
--- a/odb/source-packed.c
+++ b/odb/source-packed.c
@@ -862,7 +862,7 @@ static void odb_source_packed_free(struct odb_source *source)
 
 	odb_source_close(source);
 	for (struct packfile_list_entry *e = packed->packs.head; e; e = e->next)
-		free(e->pack);
+		packfile_free(e->pack);
 	packfile_list_clear(&packed->packs);
 
 	strmap_clear(&packed->packs_by_path, 0);
diff --git a/packfile.c b/packfile.c
index 9b03258311f..077b0395a79 100644
--- a/packfile.c
+++ b/packfile.c
@@ -217,7 +217,7 @@ struct packed_git *parse_pack_index(struct repository *r, unsigned char *sha1,
 	free(path);
 	hashcpy(p->hash, sha1, p->repo->hash_algo);
 	if (check_packed_git_idx(idx_path, p)) {
-		free(p);
+		packfile_free(p);
 		return NULL;
 	}
 
@@ -732,7 +732,7 @@ struct packed_git *packfile_from_idx(struct repository *r, const char *path,
 
 	xsnprintf(p->pack_name + path_len, alloc - path_len, ".pack");
 	if (stat(p->pack_name, &st) || !S_ISREG(st.st_mode)) {
-		free(p);
+		packfile_free(p);
 		return NULL;
 	}
 
@@ -752,6 +752,12 @@ struct packed_git *packfile_from_idx(struct repository *r, const char *path,
 	return p;
 }
 
+void packfile_free(struct packed_git *p)
+{
+	packfile_close(p);
+	free(p);
+}
+
 void packfile_store_add_pack(struct odb_source_packed *store,
 			     struct packed_git *pack)
 {
diff --git a/packfile.h b/packfile.h
index f817bc69e29..f314a55a904 100644
--- a/packfile.h
+++ b/packfile.h
@@ -223,6 +223,7 @@ struct object_database;
 
 struct packed_git *packfile_from_idx(struct repository *r, const char *path,
 				     size_t path_len, int local);
+void packfile_free(struct packed_git *p);
 
 /*
  * mmap the index file for the specified packfile (if it is not
-- 
2.56.1.831.g720f3f5937.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 5 of 7 in “packfile: some lifecycle improvements”
  1. 0/6 packfile: some lifecycle improvementsPatrick Steinhardt, Nov 20, 2026
  2. 1/6 builtin/fast-import: stop reporting packfile statisticsPatrick Steinhardt, Nov 20, 2026
  3. 2/6 packfile: rename lifecycle functionsPatrick Steinhardt, Nov 20, 2026
  4. 3/6 packfile: inline `open_packed_git_1()`Patrick Steinhardt, Nov 20, 2026
  5. 4/6 packfile: introduce function to free a packfilePatrick Steinhardt, Nov 20, 2026
  6. 5/6 packfile: maintain process-wide limits globallyPatrick Steinhardt, Nov 20, 2026
  7. 6/6 packfile: skip closing packs marked as do-not-closePatrick Steinhardt, Nov 20, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.