run-command: offer to close the object store before running

Especially on Windows, where files cannot be deleted if _any_ process holds an open file handle to them, it is important to close the object store (releasing all handles to all `.pack` files) before running a command that might spawn a garbage collection. This scenario is so common that we frequently see the pattern of closing the object store before running auto maintenance or another Git command. Let's make this much more convenient by teaching the `run_command()` machinery a new flag to release the object store before spawning the process. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Sep 9, 2021 at 09:47 UTC 28d04e1ec19777bf6382d016b6e624d0ff4336cd
2 files changed +14
run-command.c
+5
@@ -8,6 +8,7 @@
8 #include "string-list.h"
9 #include "quote.h"
10 #include "config.h"
11 +#include "packfile.h"
12
13 void child_process_init(struct child_process *child)
14 {
@@ -740,6 +741,9 @@ fail_pipe:
741
742 fflush(NULL);
743
744 + if (cmd->close_object_store)
745 + close_object_store(the_repository->objects);
746 +
747 #ifndef GIT_WINDOWS_NATIVE
748 {
749 int notify_pipe[2];
@@ -1044,6 +1048,7 @@ int run_command_v_opt_cd_env_tr2(const char **argv, int opt, const char *dir,
1048 cmd.use_shell = opt & RUN_USING_SHELL ? 1 : 0;
1049 cmd.clean_on_exit = opt & RUN_CLEAN_ON_EXIT ? 1 : 0;
1050 cmd.wait_after_clean = opt & RUN_WAIT_AFTER_CLEAN ? 1 : 0;
1051 + cmd.close_object_store = opt & RUN_CLOSE_OBJECT_STORE ? 1 : 0;
1052 cmd.dir = dir;
1053 cmd.env = env;
1054 cmd.trace2_child_class = tr2_class;
run-command.h
+9
@@ -134,6 +134,14 @@ struct child_process {
134 */
135 unsigned use_shell:1;
136
137 + /**
138 + * Release any open file handles to the object store before running
139 + * the command; This is necessary e.g. when the spawned process may
140 + * want to repack because that would delete `.pack` files (and on
141 + * Windows, you cannot delete files that are still in use).
142 + */
143 + unsigned close_object_store:1;
144 +
145 unsigned stdout_to_stderr:1;
146 unsigned clean_on_exit:1;
147 unsigned wait_after_clean:1;
@@ -240,6 +248,7 @@ int run_auto_maintenance(int quiet);
248 #define RUN_USING_SHELL (1<<4)
249 #define RUN_CLEAN_ON_EXIT (1<<5)
250 #define RUN_WAIT_AFTER_CLEAN (1<<6)
251 +#define RUN_CLOSE_OBJECT_STORE (1<<7)
252
253 /**
254 * Convenience functions that encapsulate a sequence of