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

[RFC PATCH 3/3] builtin/replay.c: introduce `--write-pack`

From
Taylor Blau <me@ttaylorr.com>
Date
Nov 7, 2023, 18:23 UTC
Message-ID
<0b9ef904888d45289445638ae59817feab1dc813.1699381371.git.me@ttaylorr.com>
In-Reply-To
<cover.1699381371.git.me@ttaylorr.com>

Now that the prerequisites are in place, we can implement a `--write-pack` option for `git replay`, corresponding to the existing one in `git merge-tree`.

The changes are mostly limited to:
  - introducing a new option in the builtin
  - replacing the main object store with the temporary one
  - then repacking and migrating the temporary object store back into
    the main object store after the replay has completed

Along with tests and documentation to ensure that the new behavior matches our expectations.

Signed-off-by: Taylor Blau <me@ttaylorr.com>
---
 Documentation/git-replay.txt |  4 ++++
 builtin/replay.c             | 18 ++++++++++++++++++
 t/t3650-replay-basics.sh     | 37 ++++++++++++++++++++++++++++++++++++
 3 files changed, 59 insertions(+)
diff --git a/Documentation/git-replay.txt b/Documentation/git-replay.txt
index e7551aec54..f424c1a676 100644
--- a/Documentation/git-replay.txt
+++ b/Documentation/git-replay.txt
@@ -42,6 +42,10 @@ When `--advance` is specified, the update-ref command(s) in the output
 will update the branch passed as an argument to `--advance` to point at
 the new commits (in other words, this mimics a cherry-pick operation).
 
+--write-pack::
+	Write any new objects into a separate packfile instead of as
+	individual loose objects.
+
 <revision-range>::
 	Range of commits to replay. More than one <revision-range> can
 	be passed, but in `--advance <branch>` mode, they should have
diff --git a/builtin/replay.c b/builtin/replay.c
index c3d53ff0cd..72b7b7f43a 100644
--- a/builtin/replay.c
+++ b/builtin/replay.c
@@ -17,6 +17,7 @@
 #include "strmap.h"
 #include <oidset.h>
 #include <tree.h>
+#include "tmp-objdir.h"
 
 static const char *short_commit_name(struct commit *commit)
 {
@@ -272,6 +273,7 @@ int cmd_replay(int argc, const char **argv, const char *prefix)
 	struct commit *onto = NULL;
 	const char *onto_name = NULL;
 	int contained = 0;
+	int write_pack = 0;
 
 	struct rev_info revs;
 	struct commit *last_commit = NULL;
@@ -279,6 +281,7 @@ int cmd_replay(int argc, const char **argv, const char *prefix)
 	struct merge_options merge_opt;
 	struct merge_result result;
 	struct strset *update_refs = NULL;
+	struct tmp_objdir *tmp_objdir = NULL;
 	kh_oid_map_t *replayed_commits;
 	int i, ret = 0;
 
@@ -296,6 +299,8 @@ int cmd_replay(int argc, const char **argv, const char *prefix)
 			   N_("replay onto given commit")),
 		OPT_BOOL(0, "contained", &contained,
 			 N_("advance all branches contained in revision-range")),
+		OPT_BOOL(0, "write-pack", &write_pack,
+			 N_("write new objects to a pack instead of as loose")),
 		OPT_END()
 	};
 
@@ -352,8 +357,15 @@ int cmd_replay(int argc, const char **argv, const char *prefix)
 	init_merge_options(&merge_opt, the_repository);
 	memset(&result, 0, sizeof(result));
 	merge_opt.show_rename_progress = 0;
+	merge_opt.write_pack = write_pack;
 	last_commit = onto;
 	replayed_commits = kh_init_oid_map();
+
+	if (merge_opt.write_pack) {
+		tmp_objdir = tmp_objdir_create("replay");
+		tmp_objdir_replace_primary_odb(tmp_objdir, 0);
+	}
+
 	while ((commit = get_revision(&revs))) {
 		const struct name_decoration *decoration;
 		khint_t pos;
@@ -417,5 +429,11 @@ int cmd_replay(int argc, const char **argv, const char *prefix)
 	/* Return */
 	if (ret < 0)
 		exit(128);
+	if (ret && tmp_objdir) {
+		if (tmp_objdir_repack(tmp_objdir) < 0)
+			ret = 0;
+		else if (tmp_objdir_migrate(tmp_objdir) < 0)
+			ret = 0;
+	}
 	return ret ? 0 : 1;
 }
diff --git a/t/t3650-replay-basics.sh b/t/t3650-replay-basics.sh
index 389670262e..e7048748c2 100755
--- a/t/t3650-replay-basics.sh
+++ b/t/t3650-replay-basics.sh
@@ -67,6 +67,43 @@ test_expect_success 'using replay to rebase two branches, one on top of other' '
 	test_cmp expect result
 '
 
+packdir=.git/objects/pack
+
+test_expect_success 'using replay to rebase two branches, with --write-pack' '
+	# Remove the results of the previous rebase, ensuring that they
+	# are pruned from the object store.
+	git gc --prune=now &&
+	test_must_fail git cat-file -t "$(cut -d " " -f 3 expect)" &&
+
+	# Create an extra packfile to ensure that the tmp-objdir repack
+	# takes place outside of the main object store.
+	git checkout --detach &&
+	test_commit unreachable &&
+	git repack -d &&
+	git checkout main &&
+
+	find $packdir -type f -name "*.idx" | sort >packs.before &&
+	git replay --write-pack --onto main topic1..topic2 >result &&
+	find $packdir -type f -name "*.idx" | sort >packs.after &&
+
+	comm -13 packs.before packs.after >packs.new &&
+
+	# Ensure that we got a single new pack.
+	test_line_count = 1 result &&
+	test_line_count = 1 packs.new &&
+
+	# ... and that the rest of the results match our expeectations.
+	git log --format=%s $(cut -f 3 -d " " result) >actual &&
+	test_write_lines E D M L B A >expect &&
+	test_cmp expect actual &&
+
+	printf "update refs/heads/topic2 " >expect &&
+	printf "%s " $(cut -f 3 -d " " result) >>expect &&
+	git rev-parse topic2 >>expect &&
+
+	test_cmp expect result
+'
+
 test_expect_success 'using replay on bare repo to rebase two branches, one on top of other' '
 	git -C bare replay --onto main topic1..topic2 >result-bare &&
 	test_cmp expect result-bare
-- 
2.42.0.446.g0b9ef90488
Previous: Taylor BlauNext: Elijah Newren
Message 60 of 63 in “merge-ort: implement support for packing objects together”
  1. 0/7 merge-ort: implement support for packing objects togetherTaylor Blau, Oct 19, 2023
  2. 1/7 bulk-checkin: extract abstract `bulk_checkin_source`Taylor Blau, Oct 19, 2023
  3. Jeff KingOct 20, 2023
  4. Junio C HamanoOct 20, 2023
  5. 2/7 bulk-checkin: generify `stream_blob_to_pack()` for arbitrary typesTaylor Blau, Oct 19, 2023
  6. 3/7 bulk-checkin: refactor deflate routine to accept a `bulk_checkin_source`Taylor Blau, Oct 19, 2023
  7. 4/7 bulk-checkin: implement `SOURCE_INCORE` mode for `bulk_checkin_source`Taylor Blau, Oct 19, 2023
  8. Patrick SteinhardtOct 23, 2023
  9. Jeff KingOct 23, 2023
  10. Patrick SteinhardtOct 24, 2023
  11. Junio C HamanoOct 24, 2023
  12. 5/7 bulk-checkin: introduce `index_blob_bulk_checkin_incore()`Taylor Blau, Oct 19, 2023
  13. 6/7 bulk-checkin: introduce `index_tree_bulk_checkin_incore()`Taylor Blau, Oct 19, 2023
  14. 7/7 builtin/merge-tree.c: implement support for `--write-pack`Taylor Blau, Oct 19, 2023
  15. Junio C HamanoOct 19, 2023
  16. Jeff KingOct 20, 2023
  17. Junio C HamanoOct 20, 2023
  18. Patrick SteinhardtOct 23, 2023
  19. 0/5 merge-ort: implement support for packing objects togetherTaylor Blau, Oct 23, 2023
  20. 1/5 bulk-checkin: extract abstract `bulk_checkin_source`Taylor Blau, Oct 23, 2023
  21. Jeff KingOct 25, 2023
  22. Taylor BlauOct 25, 2023
  23. Junio C HamanoOct 27, 2023
  24. 2/5 bulk-checkin: generify `stream_blob_to_pack()` for arbitrary typesTaylor Blau, Oct 23, 2023
  25. 3/5 bulk-checkin: introduce `index_blob_bulk_checkin_incore()`Taylor Blau, Oct 23, 2023
  26. Patrick SteinhardtOct 25, 2023
  27. Taylor BlauOct 25, 2023
  28. Eric SunshineOct 25, 2023
  29. Patrick SteinhardtOct 26, 2023
  30. Elijah NewrenNov 11, 2023
  31. 4/5 bulk-checkin: introduce `index_tree_bulk_checkin_incore()`Taylor Blau, Oct 23, 2023
  32. 5/5 builtin/merge-tree.c: implement support for `--write-pack`Taylor Blau, Oct 23, 2023
  33. Patrick SteinhardtOct 25, 2023
  34. Taylor BlauOct 25, 2023
  35. Elijah NewrenNov 10, 2023
  36. Junio C HamanoNov 11, 2023
  37. Taylor BlauNov 11, 2023
  38. Taylor BlauNov 11, 2023
  39. Jeff KingNov 13, 2023
  40. Junio C HamanoNov 14, 2023
  41. Elijah NewrenNov 14, 2023
  42. Jeff KingNov 14, 2023
  43. Elijah NewrenNov 14, 2023
  44. Jeff KingNov 13, 2023
  45. Taylor BlauNov 13, 2023
  46. Elijah NewrenNov 14, 2023
  47. Jeff KingNov 14, 2023
  48. Jeff KingNov 14, 2023
  49. Junio C HamanoOct 23, 2023
  50. Johannes SchindelinNov 6, 2023
  51. Junio C HamanoNov 6, 2023
  52. Jeff KingNov 7, 2023
  53. Taylor BlauNov 7, 2023
  54. 0/3 replay: implement support for writing new objects to a packTaylor Blau, Nov 7, 2023
  55. 1/3 merge-ort.c: finalize ODB transactions after each stepTaylor Blau, Nov 7, 2023
  56. Elijah NewrenNov 11, 2023
  57. 2/3 tmp-objdir: introduce `tmp_objdir_repack()`Taylor Blau, Nov 7, 2023
  58. Patrick SteinhardtNov 8, 2023
  59. Taylor BlauNov 9, 2023
  60. 3/3 builtin/replay.c: introduce `--write-pack`Taylor Blau, Nov 7, 2023
  61. Elijah NewrenNov 11, 2023
  62. Elijah NewrenNov 11, 2023
  63. Patrick SteinhardtOct 25, 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.