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

[PATCH] repack: Add options to preserve and prune old pack files

From
JMJames Melvin <jmelvin@codeaurora.org>
Date
Mar 7, 2017, 16:40 UTC
Message-ID
<20170307164035.27866-1-jmelvin@codeaurora.org>

The new --preserve-oldpacks option moves old pack files into the preserved subdirectory instead of deleting them after repacking.

The new --prune-preserved option prunes old pack files from the preserved subdirectory after repacking, but before potentially moving the latest old packfiles to this subdirectory.

These options are designed to prevent stale file handle exceptions during git operations which can happen on users of NFS repos when repacking is done on them. The strategy is to preserve old pack files around until the next repack with the hopes that they will become unreferenced by then and not cause any exceptions to running processes when they are finally deleted (pruned).

Signed-off-by: James Melvin <jmelvin@codeaurora.org>
---
 Documentation/git-repack.txt |  9 +++++++++
 builtin/repack.c             | 38 ++++++++++++++++++++++++++++++++++++--
 2 files changed, 45 insertions(+), 2 deletions(-)
diff --git a/Documentation/git-repack.txt b/Documentation/git-repack.txt
index 26afe6ed5..0b19b761f 100644
--- a/Documentation/git-repack.txt
+++ b/Documentation/git-repack.txt
@@ -143,6 +143,15 @@ other objects in that pack they already have locally.
 	being removed. In addition, any unreachable loose objects will
 	be packed (and their loose counterparts removed).
 
+--preserve-oldpacks::
+	 Move old pack files into the preserved subdirectory instead
+	 of deleting them after repacking.
+
+--prune-preserved::
+	 Prune old pack files from the preserved subdirectory after
+	 repacking, but before potentially moving the latest old
+	 packfiles to this subdirectory
+
 Configuration
 -------------
 
diff --git a/builtin/repack.c b/builtin/repack.c
index 677bc7c81..f1a0c97f3 100644
--- a/builtin/repack.c
+++ b/builtin/repack.c
@@ -10,8 +10,10 @@
 
 static int delta_base_offset = 1;
 static int pack_kept_objects = -1;
+static int preserve_oldpacks = 0;
+static int prune_preserved = 0;
 static int write_bitmaps;
-static char *packdir, *packtmp;
+static char *packdir, *packtmp, *preservedir;
 
 static const char *const git_repack_usage[] = {
 	N_("git repack [<options>]"),
@@ -108,6 +110,27 @@ static void get_non_kept_pack_filenames(struct string_list *fname_list)
 	closedir(dir);
 }
 
+static void preserve_pack(const char *file_path, const char *file_name,  const char *file_ext)
+{
+	char *fname_old;
+
+	if (mkdir(preservedir, 0700) && errno != EEXIST)
+		error(_("failed to create preserve directory"));
+
+	fname_old = mkpathdup("%s/%s.old-%s", preservedir, file_name, ++file_ext);
+	rename(file_path, fname_old);
+
+	free(fname_old);
+}
+
+static void remove_preserved_dir(void) {
+	struct strbuf buf = STRBUF_INIT;
+
+	strbuf_addstr(&buf, preservedir);
+	remove_dir_recursively(&buf, 0);
+	strbuf_release(&buf);
+}
+
 static void remove_redundant_pack(const char *dir_name, const char *base_name)
 {
 	const char *exts[] = {".pack", ".idx", ".keep", ".bitmap"};
@@ -121,7 +144,10 @@ static void remove_redundant_pack(const char *dir_name, const char *base_name)
 	for (i = 0; i < ARRAY_SIZE(exts); i++) {
 		strbuf_setlen(&buf, plen);
 		strbuf_addstr(&buf, exts[i]);
-		unlink(buf.buf);
+		if (preserve_oldpacks)
+			preserve_pack(buf.buf, base_name, exts[i]);
+		else
+			unlink(buf.buf);
 	}
 	strbuf_release(&buf);
 }
@@ -194,6 +220,10 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
 				N_("maximum size of each packfile")),
 		OPT_BOOL(0, "pack-kept-objects", &pack_kept_objects,
 				N_("repack objects in packs marked with .keep")),
+		OPT_BOOL(0, "preserve-oldpacks", &preserve_oldpacks,
+				N_("move old pack files into the preserved subdirectory")),
+		OPT_BOOL(0, "prune-preserved", &prune_preserved,
+				N_("prune old pack files from the preserved subdirectory after repacking")),
 		OPT_END()
 	};
 
@@ -217,6 +247,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
 
 	packdir = mkpathdup("%s/pack", get_object_directory());
 	packtmp = mkpathdup("%s/.tmp-%d-pack", packdir, (int)getpid());
+	preservedir = mkpathdup("%s/preserved", packdir);
 
 	sigchain_push_common(remove_pack_on_signal);
 
@@ -404,6 +435,9 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
 
 	/* End of pack replacement. */
 
+	if (prune_preserved)
+		remove_preserved_dir();
+
 	if (delete_redundant) {
 		int opts = 0;
 		string_list_sort(&names);
-- 
2.12.0.190.gab6997d48.dirty
Next: Junio C Hamano
Message 1 of 5 in “repack: Add options to preserve and prune old pack files”
  1. repack: Add options to preserve and prune old pack filesJames Melvin, Mar 7, 2017
  2. Junio C HamanoMar 7, 2017
  3. jmelvin@codeaurora.orgMar 9, 2017
  4. Martin FickMar 9, 2017
  5. jmelvin@codeaurora.orgMar 10, 2017

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.