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

[PATCH] fetch: only run 'gc' once when fetching multiple remotes

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Jun 19, 2019, 09:46 UTC
Message-ID
<20190619094630.32557-1-pclouds@gmail.com>

In multiple remotes mode, git-fetch is launched for n-1 remotes and the last remote is handled by the current process. Each of these processes will in turn run 'gc' at the end.

This is not really a problem because even if multiple 'gc --auto' is run at the same time we still handle it correctly. It does show multiple "auto packing in the background" messages though. And we may waste some resources when gc actually runs because we still do some stuff before checking the lock and moving it to background.

So let's try to avoid that. We should only need one 'gc' run after all objects and references are added anyway. Add a new option --no-auto-gc that will be used by those n-1 processes. 'gc --auto' will always run on the main fetch process (*).

(*) even if we fetch remotes in parallel at some point in future, this
    should still be fine because we should "join" all those processes
    before this step.
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 Documentation/fetch-options.txt |  4 ++++
 builtin/fetch.c                 | 17 +++++++++++------
 t/t5514-fetch-multiple.sh       |  7 +++++--
 3 files changed, 20 insertions(+), 8 deletions(-)
diff --git a/Documentation/fetch-options.txt b/Documentation/fetch-options.txt
index 91c47752ec..592f391298 100644
--- a/Documentation/fetch-options.txt
+++ b/Documentation/fetch-options.txt
@@ -88,6 +88,10 @@ ifndef::git-pull[]
 	Allow several <repository> and <group> arguments to be
 	specified. No <refspec>s may be specified.
 
+--[no-]auto-gc::
+	Run `git gc --auto` at the end to perform garbage collection
+	if needed. This is enabled by default.
+
 -p::
 --prune::
 	Before fetching, remove any remote-tracking references that no
diff --git a/builtin/fetch.c b/builtin/fetch.c
index 4ba63d5ac6..6a3c507897 100644
--- a/builtin/fetch.c
+++ b/builtin/fetch.c
@@ -48,6 +48,7 @@ static int prune_tags = -1; /* unspecified */
 
 static int all, append, dry_run, force, keep, multiple, update_head_ok, verbosity, deepen_relative;
 static int progress = -1;
+static int enable_auto_gc = 1;
 static int tags = TAGS_DEFAULT, unshallow, update_shallow, deepen;
 static int max_children = 1;
 static enum transport_family family;
@@ -169,6 +170,8 @@ static struct option builtin_fetch_options[] = {
 	OPT_STRING_LIST(0, "negotiation-tip", &negotiation_tip, N_("revision"),
 			N_("report that we have only objects reachable from this object")),
 	OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
+	OPT_BOOL(0, "auto-gc", &enable_auto_gc,
+		 N_("run 'gc --auto' after fetching")),
 	OPT_END()
 };
 
@@ -1424,7 +1427,7 @@ static int fetch_multiple(struct string_list *list)
 			return errcode;
 	}
 
-	argv_array_pushl(&argv, "fetch", "--append", NULL);
+	argv_array_pushl(&argv, "fetch", "--append", "--no-auto-gc", NULL);
 	add_options_to_argv(&argv);
 
 	for (i = 0; i < list->nr; i++) {
@@ -1674,11 +1677,13 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
 
 	close_all_packs(the_repository->objects);
 
-	argv_array_pushl(&argv_gc_auto, "gc", "--auto", NULL);
-	if (verbosity < 0)
-		argv_array_push(&argv_gc_auto, "--quiet");
-	run_command_v_opt(argv_gc_auto.argv, RUN_GIT_CMD);
-	argv_array_clear(&argv_gc_auto);
+	if (enable_auto_gc) {
+		argv_array_pushl(&argv_gc_auto, "gc", "--auto", NULL);
+		if (verbosity < 0)
+			argv_array_push(&argv_gc_auto, "--quiet");
+		run_command_v_opt(argv_gc_auto.argv, RUN_GIT_CMD);
+		argv_array_clear(&argv_gc_auto);
+	}
 
 	return result;
 }
diff --git a/t/t5514-fetch-multiple.sh b/t/t5514-fetch-multiple.sh
index 0030c92e1a..5426d4b5ab 100755
--- a/t/t5514-fetch-multiple.sh
+++ b/t/t5514-fetch-multiple.sh
@@ -105,9 +105,12 @@ test_expect_success 'git fetch --multiple (two remotes)' '
 	 git remote rm origin &&
 	 git remote add one ../one &&
 	 git remote add two ../two &&
-	 git fetch --multiple one two &&
+	 GIT_TRACE=1 git fetch --multiple one two 2>trace &&
 	 git branch -r > output &&
-	 test_cmp ../expect output)
+	 test_cmp ../expect output &&
+	 grep "built-in: git gc" trace >gc &&
+	 test_line_count = 1 gc
+	)
 '
 
 test_expect_success 'git fetch --multiple (bad remote names)' '
-- 
2.22.0.rc0.322.g2b0371e29a
Next: Ævar Arnfjörð Bjarmason
Message 1 of 61 in “fetch: only run 'gc' once when fetching multiple remotes”
  1. fetch: only run 'gc' once when fetching multiple remotesNguyễn Thái Ngọc Duy, Jun 19, 2019
  2. gc: run more pre-detach operations under lockÆvar Arnfjörð Bjarmason, Jun 19, 2019
  3. Duy NguyenJun 19, 2019
  4. Ævar Arnfjörð BjarmasonJun 19, 2019
  5. Jeff KingJun 19, 2019
  6. Ævar Arnfjörð BjarmasonJun 19, 2019
  7. 0/6 Change <non-empty?> GIT_TEST_* variables to <boolean>Ævar Arnfjörð Bjarmason, Jun 19, 2019
  8. Junio C HamanoJun 20, 2019
  9. Ævar Arnfjörð BjarmasonJun 20, 2019
  10. Junio C HamanoJun 20, 2019
  11. 0/8 Change <non-empty?> GIT_TEST_* variables to <boolean>Ævar Arnfjörð Bjarmason, Jun 20, 2019
  12. 1/8 config tests: simplify include cycle testÆvar Arnfjörð Bjarmason, Jun 21, 2019
  13. 0/8 Change <non-empty?> GIT_TEST_* variables to <boolean>Ævar Arnfjörð Bjarmason, Jun 21, 2019
  14. 2/8 env--helper: new undocumented builtin wrapping git_env_*()Ævar Arnfjörð Bjarmason, Jun 21, 2019
  15. Junio C HamanoJun 21, 2019
  16. 3/8 config.c: refactor die_bad_number() to not call gettext() earlyÆvar Arnfjörð Bjarmason, Jun 21, 2019
  17. 4/8 t6040 test: stop using global "script" variableÆvar Arnfjörð Bjarmason, Jun 21, 2019
  18. 6/8 tests README: re-flow a previously changed paragraphÆvar Arnfjörð Bjarmason, Jun 21, 2019
  19. 5/8 tests: make GIT_TEST_GETTEXT_POISON a booleanÆvar Arnfjörð Bjarmason, Jun 21, 2019
  20. Junio C HamanoJun 24, 2019
  21. 7/8 tests: replace test_tristate with "git env--helper"Ævar Arnfjörð Bjarmason, Jun 21, 2019
  22. 1/2 t/lib-git-svn.sh: check GIT_TEST_SVN_HTTPD when running SVN HTTP testsSZEDER Gábor, Sep 6, 2019
  23. 2/2 ci: restore running httpd testsSZEDER Gábor, Sep 6, 2019
  24. Junio C HamanoSep 6, 2019
  25. Jeff KingSep 6, 2019
  26. SZEDER GáborSep 7, 2019
  27. 0/2 tests: catch non-bool GIT_TEST_* valuesSZEDER Gábor, Nov 22, 2019
  28. 1/2 tests: add 'test_bool_env' to catch non-bool GIT_TEST_* valuesSZEDER Gábor, Nov 22, 2019
  29. Jeff KingNov 25, 2019
  30. 2/2 t5608-clone-2gb.sh: turn GIT_TEST_CLONE_2GB into a boolSZEDER Gábor, Nov 22, 2019
  31. Jeff KingNov 25, 2019
  32. 8/8 tests: make GIT_TEST_FAIL_PREREQS a booleanÆvar Arnfjörð Bjarmason, Jun 21, 2019
  33. 1/8 config tests: simplify include cycle testÆvar Arnfjörð Bjarmason, Jun 20, 2019
  34. 2/8 env--helper: new undocumented builtin wrapping git_env_*()Ævar Arnfjörð Bjarmason, Jun 20, 2019
  35. Junio C HamanoJun 20, 2019
  36. Junio C HamanoJun 20, 2019
  37. Ævar Arnfjörð BjarmasonJun 21, 2019
  38. Junio C HamanoJun 21, 2019
  39. 3/8 config.c: refactor die_bad_number() to not call gettext() earlyÆvar Arnfjörð Bjarmason, Jun 20, 2019
  40. 4/8 t6040 test: stop using global "script" variableÆvar Arnfjörð Bjarmason, Jun 20, 2019
  41. 5/8 tests: make GIT_TEST_GETTEXT_POISON a booleanÆvar Arnfjörð Bjarmason, Jun 20, 2019
  42. 7/8 tests: replace test_tristate with "git env--helper"Ævar Arnfjörð Bjarmason, Jun 20, 2019
  43. 6/8 tests README: re-flow a previously changed paragraphÆvar Arnfjörð Bjarmason, Jun 20, 2019
  44. 8/8 tests: make GIT_TEST_FAIL_PREREQS a booleanÆvar Arnfjörð Bjarmason, Jun 20, 2019
  45. 1/6 env--helper: new undocumented builtin wrapping git_env_*()Ævar Arnfjörð Bjarmason, Jun 19, 2019
  46. Junio C HamanoJun 20, 2019
  47. 2/6 t6040 test: stop using global "script" variableÆvar Arnfjörð Bjarmason, Jun 19, 2019
  48. Junio C HamanoJun 20, 2019
  49. 3/6 tests: make GIT_TEST_GETTEXT_POISON a booleanÆvar Arnfjörð Bjarmason, Jun 19, 2019
  50. Junio C HamanoJun 20, 2019
  51. 5/6 tests: replace test_tristate with "git env--helper"Ævar Arnfjörð Bjarmason, Jun 19, 2019
  52. 4/6 tests README: re-flow a previously changed paragraphÆvar Arnfjörð Bjarmason, Jun 19, 2019
  53. 6/6 tests: make GIT_TEST_FAIL_PREREQS a booleanÆvar Arnfjörð Bjarmason, Jun 19, 2019
  54. Duy NguyenJun 20, 2019
  55. Ævar Arnfjörð BjarmasonJun 20, 2019
  56. Jeff KingJun 20, 2019
  57. Junio C HamanoJun 20, 2019
  58. Jeff KingJun 19, 2019
  59. Jeff KingJun 19, 2019
  60. Duy NguyenJun 20, 2019
  61. Jeff KingJun 20, 2019

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.