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

[PATCH v2 2/8] builtin/repack.c: extract marking packs for deletion

From
Taylor Blau <me@ttaylorr.com>
Date
Sep 13, 2023, 19:17 UTC
Message-ID
<62d916169d33a7569d83b72dfb1cb90d56568f01.1694632644.git.me@ttaylorr.com>
In-Reply-To
<cover.1694632644.git.me@ttaylorr.com>

At the end of a repack (when given `-d`), Git attempts to remove any packs which have been made "redundant" as a result of the repacking operation. For example, an all-into-one (`-A` or `-a`) repack makes every pre-existing pack which is not marked as kept redundant. Geometric repacks (with `--geometric=<n>`) make any packs which were rolled up redundant, and so on.

But before deleting the set of packs we think are redundant, we first check to see whether or not we just wrote a pack which is identical to any one of the packs we were going to delete. When this is the case, Git must avoid deleting that pack, since it matches a pack we just wrote (so deleting it may cause the repository to become corrupt).

Right now we only process the list of non-kept packs in a single pass. But a future change will split the existing non-kept packs further into two lists: one for cruft packs, and another for non-cruft packs.

Factor out this routine to prepare for calling it twice on two separate lists in a future patch.

Signed-off-by: Taylor Blau <me@ttaylorr.com>
---
 builtin/repack.c | 50 +++++++++++++++++++++++++++++++-----------------
 1 file changed, 32 insertions(+), 18 deletions(-)
diff --git a/builtin/repack.c b/builtin/repack.c
index 67bf86567f..0eee430951 100644
--- a/builtin/repack.c
+++ b/builtin/repack.c
@@ -105,6 +105,36 @@ struct existing_packs {
 	.non_kept_packs = STRING_LIST_INIT_DUP, \
 }
 
+static void mark_packs_for_deletion_1(struct string_list *names,
+				      struct string_list *list)
+{
+	struct string_list_item *item;
+	const int hexsz = the_hash_algo->hexsz;
+
+	for_each_string_list_item(item, list) {
+		char *sha1;
+		size_t len = strlen(item->string);
+		if (len < hexsz)
+			continue;
+		sha1 = item->string + len - hexsz;
+		/*
+		 * Mark this pack for deletion, which ensures that this
+		 * pack won't be included in a MIDX (if `--write-midx`
+		 * was given) and that we will actually delete this pack
+		 * (if `-d` was given).
+		 */
+		if (!string_list_has_string(names, sha1))
+			item->util = (void*)(uintptr_t)((size_t)item->util | DELETE_PACK);
+	}
+}
+
+static void mark_packs_for_deletion(struct existing_packs *existing,
+				    struct string_list *names)
+
+{
+	mark_packs_for_deletion_1(names, &existing->non_kept_packs);
+}
+
 static void existing_packs_release(struct existing_packs *existing)
 {
 	string_list_clear(&existing->kept_packs, 0);
@@ -1142,24 +1172,8 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
 	}
 	/* End of pack replacement. */
 
-	if (delete_redundant && pack_everything & ALL_INTO_ONE) {
-		const int hexsz = the_hash_algo->hexsz;
-		for_each_string_list_item(item, &existing.non_kept_packs) {
-			char *sha1;
-			size_t len = strlen(item->string);
-			if (len < hexsz)
-				continue;
-			sha1 = item->string + len - hexsz;
-			/*
-			 * Mark this pack for deletion, which ensures that this
-			 * pack won't be included in a MIDX (if `--write-midx`
-			 * was given) and that we will actually delete this pack
-			 * (if `-d` was given).
-			 */
-			if (!string_list_has_string(&names, sha1))
-				item->util = (void*)(uintptr_t)((size_t)item->util | DELETE_PACK);
-		}
-	}
+	if (delete_redundant && pack_everything & ALL_INTO_ONE)
+		mark_packs_for_deletion(&existing, &names);
 
 	if (write_midx) {
 		struct string_list include = STRING_LIST_INIT_NODUP;
-- 
2.42.0.166.g68748eb9c8
Previous: Taylor BlauNext: Taylor Blau
Message 25 of 39 in “repack: refactor pack snapshot-ing logic”
  1. 0/8 repack: refactor pack snapshot-ing logicTaylor Blau, Sep 5, 2023
  2. 2/8 builtin/repack.c: extract marking packs for deletionTaylor Blau, Sep 5, 2023
  3. Jeff KingSep 7, 2023
  4. Taylor BlauSep 7, 2023
  5. 5/8 builtin/repack.c: extract `has_existing_non_kept_packs()`Taylor Blau, Sep 5, 2023
  6. Jeff KingSep 7, 2023
  7. 3/8 builtin/repack.c: extract redundant pack cleanup for --geometricTaylor Blau, Sep 5, 2023
  8. Jeff KingSep 7, 2023
  9. 1/8 builtin/repack.c: extract structure to store existing packsTaylor Blau, Sep 5, 2023
  10. Jeff KingSep 7, 2023
  11. 4/8 builtin/repack.c: extract redundant pack cleanup for existing packsTaylor Blau, Sep 5, 2023
  12. Jeff KingSep 7, 2023
  13. 6/8 builtin/repack.c: store existing cruft packs separatelyTaylor Blau, Sep 5, 2023
  14. Jeff KingSep 7, 2023
  15. 7/8 builtin/repack.c: drop `DELETE_PACK` macroTaylor Blau, Sep 5, 2023
  16. Junio C HamanoSep 6, 2023
  17. Taylor BlauSep 6, 2023
  18. Patrick SteinhardtSep 7, 2023
  19. Junio C HamanoSep 7, 2023
  20. Jeff KingSep 7, 2023
  21. 8/8 builtin/repack.c: extract common cruft pack loopTaylor Blau, Sep 5, 2023
  22. Jeff KingSep 7, 2023
  23. 0/8 repack: refactor pack snapshot-ing logicTaylor Blau, Sep 13, 2023
  24. 1/8 builtin/repack.c: extract structure to store existing packsTaylor Blau, Sep 13, 2023
  25. 2/8 builtin/repack.c: extract marking packs for deletionTaylor Blau, Sep 13, 2023
  26. 3/8 builtin/repack.c: extract redundant pack cleanup for --geometricTaylor Blau, Sep 13, 2023
  27. 4/8 builtin/repack.c: extract redundant pack cleanup for existing packsTaylor Blau, Sep 13, 2023
  28. 5/8 builtin/repack.c: extract `has_existing_non_kept_packs()`Taylor Blau, Sep 13, 2023
  29. Christian CouderSep 15, 2023
  30. 6/8 builtin/repack.c: store existing cruft packs separatelyTaylor Blau, Sep 13, 2023
  31. 7/8 builtin/repack.c: avoid directly inspecting "util"Taylor Blau, Sep 13, 2023
  32. 8/8 builtin/repack.c: extract common cruft pack loopTaylor Blau, Sep 13, 2023
  33. Junio C HamanoSep 13, 2023
  34. Jeff KingSep 14, 2023
  35. Patrick SteinhardtSep 14, 2023
  36. Christian CouderSep 14, 2023
  37. Taylor BlauSep 14, 2023
  38. Christian CouderSep 15, 2023
  39. Christian CouderSep 15, 2023

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.