{"thread":{"id":"54001","subject":"Re: [PATCH 8/9] maintenance: auto-size incremental-repack batch","startedAt":"2020-08-06T17:09:32Z","lastAt":"2020-09-25T18:43:15Z","messageCount":66,"participants":["Son Luong Ngoc","Derrick Stolee via GitGitGadget","Junio C Hamano via GitGitGadget","Derrick Stolee","Emily Shaffer","Junio C Hamano","Jonathan Nieder","Jonathan Tan"],"isPatch":true,"patchVersion":1,"patchTotal":9},"messages":[{"id":"403052","messageId":"6FA8F54A-C92D-497B-895F-AC6E8287AACD@gmail.com","threadId":"54001","inReplyTo":"c3487fb8e3e72949080f51f341655f37b5f2f03f.1596731425.git.gitgitgadget@gmail.com","subject":"Re: [PATCH 8/9] maintenance: auto-size incremental-repack batch","fromName":"Son Luong Ngoc","fromEmail":"sluongng@gmail.com","sentAt":"2020-08-06T17:02:30Z","receivedAt":"2020-08-06T17:09:32Z","isPatch":true,"sender":{"key":"sluongng@gmail.com","avatar":"https://avatars.githubusercontent.com/u/26684313?v=4"},"body":"Hi Derrick,\n\n> On Aug 6, 2020, at 18:30, Derrick Stolee via GitGitGadget <gitgitgadget@gmail.com> wrote:\n> \n> From: Derrick Stolee <dstolee@microsoft.com>\n> \n> When repacking during the 'incremental-repack' task, we use the\n> --batch-size option in 'git multi-pack-index repack'. The initial setting\n> used --batch-size=0 to repack everything into a single pack-file. This is\n> not sustainable for a large repository. The amount of work required is\n> also likely to use too many system resources for a background job.\n> \n> Update the 'incremental-repack' task by dynamically computing a\n> --batch-size option based on the current pack-file structure.\n> \n> The dynamic default size is computed with this idea in mind for a client\n> repository that was cloned from a very large remote: there is likely one\n> \"big\" pack-file that was created at clone time. Thus, do not try\n> repacking it as it is likely packed efficiently by the server.\n> \n> Instead, we select the second-largest pack-file, and create a batch size\n> that is one larger than that pack-file. If there are three or more\n> pack-files, then this guarantees that at least two will be combined into\n> a new pack-file.\n\nI have been using this strategy with git-care.sh [1] with large success.\nHowever it worth to note that there are still edge case where I observed that\npack count keep increasing because using '--batch-size=<second-biggest-pack>+1'\ndid not resulted in any repacking.\nIn one case, I have observed a local copy went up to 160+ packs without being able\nto repack.\n\nI have been considering whether a strategy such as falling back to the '(3rd biggest\npack size) + 1' and 4th and 5th and so on... when midx repack call resulted in no-op,\nas that was how I fixed my repo when the edge case happen.\n\nSuch strategy would require a way to detect midx repack to signal when no-op happen,\nso something like 'git multi-pack-index repack --batch-size=123456 --exit-code' would\nbe much desirable.\n\n> \n> Of course, this means that the second-largest pack-file size is likely\n> to grow over time and may eventually surpass the initially-cloned\n> pack-file. Recall that the pack-file batch is selected in a greedy\n> manner: the packs are considered from oldest to newest and are selected\n> if they have size smaller than the batch size until the total selected\n> size is larger than the batch size. Thus, that oldest \"clone\" pack will\n> be first to repack after the new data creates a pack larger than that.\n> \n> We also want to place some limits on how large these pack-files become,\n> in order to bound the amount of time spent repacking. A maximum\n> batch-size of two gigabytes means that large repositories will never be\n> packed into a single pack-file using this job, but also that repack is\n> rather expensive. This is a trade-off that is valuable to have if the\n> maintenance is being run automatically or in the background. Users who\n> truly want to optimize for space and performance (and are willing to pay\n> the upfront cost of a full repack) can use the 'gc' task to do so.\n> \n> Create a test for this two gigabyte limit by creating an EXPENSIVE test\n> that generates two pack-files of roughly 2.5 gigabytes in size, then\n> performs an incremental repack. Check that the --batch-size argument in\n> the subcommand uses the hard-coded maximum.\n> \n> Helped-by: Chris Torek <chris.torek@gmail.com>\n> Reported-by: Son Luong Ngoc <sluongng@gmail.com>\n> Signed-off-by: Derrick Stolee <dstolee@microsoft.com>\n\nGenerally, I have found working with '--batch-size' to be a bit unpredictable.\nI wonder if we could tweak the behavior somewhat so that its more consistent\nto use and test?\n\nThanks a lot for making this happen.\nHope this patch would make it in stable soon\n\nCheers,\nSon Luong.\n\n[1]: https://github.com/sluongng/git-care"},{"id":"403069","messageId":"c3487fb8e3e72949080f51f341655f37b5f2f03f.1596731425.git.gitgitgadget@gmail.com","threadId":"54001","inReplyTo":"pull.696.git.1596731424.gitgitgadget@gmail.com","subject":"[PATCH 8/9] maintenance: auto-size incremental-repack batch","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2020-08-06T16:30:23Z","receivedAt":"2020-08-06T17:52:26Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"From: Derrick Stolee <dstolee@microsoft.com>\n\nWhen repacking during the 'incremental-repack' task, we use the\n--batch-size option in 'git multi-pack-index repack'. The initial setting\nused --batch-size=0 to repack everything into a single pack-file. This is\nnot sustainable for a large repository. The amount of work required is\nalso likely to use too many system resources for a background job.\n\nUpdate the 'incremental-repack' task by dynamically computing a\n--batch-size option based on the current pack-file structure.\n\nThe dynamic default size is computed with this idea in mind for a client\nrepository that was cloned from a very large remote: there is likely one\n\"big\" pack-file that was created at clone time. Thus, do not try\nrepacking it as it is likely packed efficiently by the server.\n\nInstead, we select the second-largest pack-file, and create a batch size\nthat is one larger than that pack-file. If there are three or more\npack-files, then this guarantees that at least two will be combined into\na new pack-file.\n\nOf course, this means that the second-largest pack-file size is likely\nto grow over time and may eventually surpass the initially-cloned\npack-file. Recall that the pack-file batch is selected in a greedy\nmanner: the packs are considered from oldest to newest and are selected\nif they have size smaller than the batch size until the total selected\nsize is larger than the batch size. Thus, that oldest \"clone\" pack will\nbe first to repack after the new data creates a pack larger than that.\n\nWe also want to place some limits on how large these pack-files become,\nin order to bound the amount of time spent repacking. A maximum\nbatch-size of two gigabytes means that large repositories will never be\npacked into a single pack-file using this job, but also that repack is\nrather expensive. This is a trade-off that is valuable to have if the\nmaintenance is being run automatically or in the background. Users who\ntruly want to optimize for space and performance (and are willing to pay\nthe upfront cost of a full repack) can use the 'gc' task to do so.\n\nCreate a test for this two gigabyte limit by creating an EXPENSIVE test\nthat generates two pack-files of roughly 2.5 gigabytes in size, then\nperforms an incremental repack. Check that the --batch-size argument in\nthe subcommand uses the hard-coded maximum.\n\nHelped-by: Chris Torek <chris.torek@gmail.com>\nReported-by: Son Luong Ngoc <sluongng@gmail.com>\nSigned-off-by: Derrick Stolee <dstolee@microsoft.com>\n---\n builtin/gc.c           | 43 +++++++++++++++++++++++++++++++++++++++++-\n t/t7900-maintenance.sh | 36 +++++++++++++++++++++++++++++++++--\n 2 files changed, 76 insertions(+), 3 deletions(-)\n\ndiff --git a/builtin/gc.c b/builtin/gc.c\nindex 35c6d7ce82..c09bc1381c 100644\n--- a/builtin/gc.c\n+++ b/builtin/gc.c\n@@ -1094,6 +1094,46 @@ static int multi_pack_index_expire(struct maintenance_opts *opts)\n \treturn 0;\n }\n \n+#define TWO_GIGABYTES (INT32_MAX)\n+\n+static off_t get_auto_pack_size(void)\n+{\n+\t/*\n+\t * The \"auto\" value is special: we optimize for\n+\t * one large pack-file (i.e. from a clone) and\n+\t * expect the rest to be small and they can be\n+\t * repacked quickly.\n+\t *\n+\t * The strategy we select here is to select a\n+\t * size that is one more than the second largest\n+\t * pack-file. This ensures that we will repack\n+\t * at least two packs if there are three or more\n+\t * packs.\n+\t */\n+\toff_t max_size = 0;\n+\toff_t second_largest_size = 0;\n+\toff_t result_size;\n+\tstruct packed_git *p;\n+\tstruct repository *r = the_repository;\n+\n+\treprepare_packed_git(r);\n+\tfor (p = get_all_packs(r); p; p = p->next) {\n+\t\tif (p->pack_size > max_size) {\n+\t\t\tsecond_largest_size = max_size;\n+\t\t\tmax_size = p->pack_size;\n+\t\t} else if (p->pack_size > second_largest_size)\n+\t\t\tsecond_largest_size = p->pack_size;\n+\t}\n+\n+\tresult_size = second_largest_size + 1;\n+\n+\t/* But limit ourselves to a batch size of 2g */\n+\tif (result_size > TWO_GIGABYTES)\n+\t\tresult_size = TWO_GIGABYTES;\n+\n+\treturn result_size;\n+}\n+\n static int multi_pack_index_repack(struct maintenance_opts *opts)\n {\n \tstruct child_process child = CHILD_PROCESS_INIT;\n@@ -1104,7 +1144,8 @@ static int multi_pack_index_repack(struct maintenance_opts *opts)\n \tif (opts->quiet)\n \t\tstrvec_push(&child.args, \"--no-progress\");\n \n-\tstrvec_push(&child.args, \"--batch-size=0\");\n+\tstrvec_pushf(&child.args, \"--batch-size=%\"PRIuMAX,\n+\t\t\t\t  (uintmax_t)get_auto_pack_size());\n \n \tclose_object_store(the_repository->objects);\n \ndiff --git a/t/t7900-maintenance.sh b/t/t7900-maintenance.sh\nindex be19ac7623..1c5f44f2b3 100755\n--- a/t/t7900-maintenance.sh\n+++ b/t/t7900-maintenance.sh\n@@ -179,10 +179,42 @@ test_expect_success 'incremental-repack task' '\n \ttest_line_count = 4 packs-between &&\n \n \t# the job deletes the two old packs, and does not write\n-\t# a new one because only one pack remains.\n+\t# a new one because the batch size is not high enough to\n+\t# pack the largest pack-file.\n \tgit maintenance run --task=incremental-repack &&\n \tls .git/objects/pack/*.pack >packs-after &&\n-\ttest_line_count = 1 packs-after\n+\ttest_line_count = 2 packs-after\n+'\n+\n+test_expect_success EXPENSIVE 'incremental-repack 2g limit' '\n+\tfor i in $(test_seq 1 5)\n+\tdo\n+\t\ttest-tool genrandom foo$i $((512 * 1024 * 1024 + 1)) >>big ||\n+\t\treturn 1\n+\tdone &&\n+\tgit add big &&\n+\tgit commit -m \"Add big file (1)\" &&\n+\n+\t# ensure any possible loose objects are in a pack-file\n+\tgit maintenance run --task=loose-objects &&\n+\n+\trm big &&\n+\tfor i in $(test_seq 6 10)\n+\tdo\n+\t\ttest-tool genrandom foo$i $((512 * 1024 * 1024 + 1)) >>big ||\n+\t\treturn 1\n+\tdone &&\n+\tgit add big &&\n+\tgit commit -m \"Add big file (2)\" &&\n+\n+\t# ensure any possible loose objects are in a pack-file\n+\tgit maintenance run --task=loose-objects &&\n+\n+\t# Now run the incremental-repack task and check the batch-size\n+\tGIT_TRACE2_EVENT=\"$(pwd)/run-2g.txt\" git maintenance run \\\n+\t\t--task=incremental-repack 2>/dev/null &&\n+\ttest_subcommand git multi-pack-index repack \\\n+\t\t --no-progress --batch-size=2147483647 <run-2g.txt\n '\n \n test_done\n-- \ngitgitgadget\n\n"},{"id":"403070","messageId":"aba087f663de382463585db83fe0530cded0fa94.1596731425.git.gitgitgadget@gmail.com","threadId":"54001","inReplyTo":"pull.696.git.1596731424.gitgitgadget@gmail.com","subject":"[PATCH 6/9] midx: use start_delayed_progress()","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2020-08-06T16:30:21Z","receivedAt":"2020-08-06T17:52:51Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"From: Derrick Stolee <dstolee@microsoft.com>\n\nNow that the multi-pack-index may be written as part of auto maintenance\nat the end of a command, reduce the progress output when the operations\nare quick. Use start_delayed_progress() instead of start_progress().\n\nUpdate t5319-multi-pack-index.sh to use GIT_PROGRESS_DELAY=0 now that\nthe progress indicators are conditional.\n\nSigned-off-by: Derrick Stolee <dstolee@microsoft.com>\n---\n midx.c                      | 10 +++++-----\n t/t5319-multi-pack-index.sh | 14 +++++++-------\n 2 files changed, 12 insertions(+), 12 deletions(-)\n\ndiff --git a/midx.c b/midx.c\nindex ef499cf504..aa37d5da86 100644\n--- a/midx.c\n+++ b/midx.c\n@@ -832,7 +832,7 @@ static int write_midx_internal(const char *object_dir, struct multi_pack_index *\n \n \tpacks.pack_paths_checked = 0;\n \tif (flags & MIDX_PROGRESS)\n-\t\tpacks.progress = start_progress(_(\"Adding packfiles to multi-pack-index\"), 0);\n+\t\tpacks.progress = start_delayed_progress(_(\"Adding packfiles to multi-pack-index\"), 0);\n \telse\n \t\tpacks.progress = NULL;\n \n@@ -969,7 +969,7 @@ static int write_midx_internal(const char *object_dir, struct multi_pack_index *\n \t}\n \n \tif (flags & MIDX_PROGRESS)\n-\t\tprogress = start_progress(_(\"Writing chunks to multi-pack-index\"),\n+\t\tprogress = start_delayed_progress(_(\"Writing chunks to multi-pack-index\"),\n \t\t\t\t\t  num_chunks);\n \tfor (i = 0; i < num_chunks; i++) {\n \t\tif (written != chunk_offsets[i])\n@@ -1104,7 +1104,7 @@ int verify_midx_file(struct repository *r, const char *object_dir, unsigned flag\n \t\treturn 0;\n \n \tif (flags & MIDX_PROGRESS)\n-\t\tprogress = start_progress(_(\"Looking for referenced packfiles\"),\n+\t\tprogress = start_delayed_progress(_(\"Looking for referenced packfiles\"),\n \t\t\t\t\t  m->num_packs);\n \tfor (i = 0; i < m->num_packs; i++) {\n \t\tif (prepare_midx_pack(r, m, i))\n@@ -1225,7 +1225,7 @@ int expire_midx_packs(struct repository *r, const char *object_dir, unsigned fla\n \tcount = xcalloc(m->num_packs, sizeof(uint32_t));\n \n \tif (flags & MIDX_PROGRESS)\n-\t\tprogress = start_progress(_(\"Counting referenced objects\"),\n+\t\tprogress = start_delayed_progress(_(\"Counting referenced objects\"),\n \t\t\t\t\t  m->num_objects);\n \tfor (i = 0; i < m->num_objects; i++) {\n \t\tint pack_int_id = nth_midxed_pack_int_id(m, i);\n@@ -1235,7 +1235,7 @@ int expire_midx_packs(struct repository *r, const char *object_dir, unsigned fla\n \tstop_progress(&progress);\n \n \tif (flags & MIDX_PROGRESS)\n-\t\tprogress = start_progress(_(\"Finding and deleting unreferenced packfiles\"),\n+\t\tprogress = start_delayed_progress(_(\"Finding and deleting unreferenced packfiles\"),\n \t\t\t\t\t  m->num_packs);\n \tfor (i = 0; i < m->num_packs; i++) {\n \t\tchar *pack_name;\ndiff --git a/t/t5319-multi-pack-index.sh b/t/t5319-multi-pack-index.sh\nindex 7214cab36c..12f41dfc18 100755\n--- a/t/t5319-multi-pack-index.sh\n+++ b/t/t5319-multi-pack-index.sh\n@@ -172,12 +172,12 @@ test_expect_success 'write progress off for redirected stderr' '\n '\n \n test_expect_success 'write force progress on for stderr' '\n-\tgit multi-pack-index --object-dir=$objdir --progress write 2>err &&\n+\tGIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir --progress write 2>err &&\n \ttest_file_not_empty err\n '\n \n test_expect_success 'write with the --no-progress option' '\n-\tgit multi-pack-index --object-dir=$objdir --no-progress write 2>err &&\n+\tGIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir --no-progress write 2>err &&\n \ttest_line_count = 0 err\n '\n \n@@ -334,17 +334,17 @@ test_expect_success 'git-fsck incorrect offset' '\n '\n \n test_expect_success 'repack progress off for redirected stderr' '\n-\tgit multi-pack-index --object-dir=$objdir repack 2>err &&\n+\tGIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir repack 2>err &&\n \ttest_line_count = 0 err\n '\n \n test_expect_success 'repack force progress on for stderr' '\n-\tgit multi-pack-index --object-dir=$objdir --progress repack 2>err &&\n+\tGIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir --progress repack 2>err &&\n \ttest_file_not_empty err\n '\n \n test_expect_success 'repack with the --no-progress option' '\n-\tgit multi-pack-index --object-dir=$objdir --no-progress repack 2>err &&\n+\tGIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir --no-progress repack 2>err &&\n \ttest_line_count = 0 err\n '\n \n@@ -488,7 +488,7 @@ test_expect_success 'expire progress off for redirected stderr' '\n test_expect_success 'expire force progress on for stderr' '\n \t(\n \t\tcd dup &&\n-\t\tgit multi-pack-index --progress expire 2>err &&\n+\t\tGIT_PROGRESS_DELAY=0 git multi-pack-index --progress expire 2>err &&\n \t\ttest_file_not_empty err\n \t)\n '\n@@ -496,7 +496,7 @@ test_expect_success 'expire force progress on for stderr' '\n test_expect_success 'expire with the --no-progress option' '\n \t(\n \t\tcd dup &&\n-\t\tgit multi-pack-index --no-progress expire 2>err &&\n+\t\tGIT_PROGRESS_DELAY=0 git multi-pack-index --no-progress expire 2>err &&\n \t\ttest_line_count = 0 err\n \t)\n '\n-- \ngitgitgadget\n\n"},{"id":"403071","messageId":"407c123c519f473f303c5d0316e1b5e67d6b94ad.1596731425.git.gitgitgadget@gmail.com","threadId":"54001","inReplyTo":"pull.696.git.1596731424.gitgitgadget@gmail.com","subject":"[PATCH 9/9] maintenance: add incremental-repack auto condition","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2020-08-06T16:30:24Z","receivedAt":"2020-08-06T17:53:02Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"From: Derrick Stolee <dstolee@microsoft.com>\n\nThe incremental-repack task updates the multi-pack-index by deleting pack-\nfiles that have been replaced with new packs, then repacking a batch of\nsmall pack-files into a larger pack-file. This incremental repack is faster\nthan rewriting all object data, but is slower than some other\nmaintenance activities.\n\nThe 'maintenance.incremental-repack.auto' config option specifies how many\npack-files should exist outside of the multi-pack-index before running\nthe step. These pack-files could be created by 'git fetch' commands or\nby the loose-objects task. The default value is 10.\n\nSetting the option to zero disables the task with the '--auto' option,\nand a negative value makes the task run every time.\n\nSigned-off-by: Derrick Stolee <dstolee@microsoft.com>\n---\n Documentation/config/maintenance.txt |  9 ++++++++\n builtin/gc.c                         | 31 +++++++++++++++++++++++++++\n t/t7900-maintenance.sh               | 32 ++++++++++++++++++++++++++++\n 3 files changed, 72 insertions(+)\n\ndiff --git a/Documentation/config/maintenance.txt b/Documentation/config/maintenance.txt\nindex a9442dd260..22229e7174 100644\n--- a/Documentation/config/maintenance.txt\n+++ b/Documentation/config/maintenance.txt\n@@ -21,3 +21,12 @@ maintenance.loose-objects.auto::\n \tpositive value implies the command should run when the number of\n \tloose objects is at least the value of `maintenance.loose-objects.auto`.\n \tThe default value is 100.\n+\n+maintenance.incremental-repack.auto::\n+\tThis integer config option controls how often the `incremental-repack`\n+\ttask should be run as part of `git maintenance run --auto`. If zero,\n+\tthen the `incremental-repack` task will not run with the `--auto`\n+\toption. A negative value will force the task to run every time.\n+\tOtherwise, a positive value implies the command should run when the\n+\tnumber of pack-files not in the multi-pack-index is at least the value\n+\tof `maintenance.incremental-repack.auto`. The default value is 10.\ndiff --git a/builtin/gc.c b/builtin/gc.c\nindex c09bc1381c..dbf8080d3b 100644\n--- a/builtin/gc.c\n+++ b/builtin/gc.c\n@@ -31,6 +31,7 @@\n #include \"refs.h\"\n #include \"remote.h\"\n #include \"midx.h\"\n+#include \"object-store.h\"\n \n #define FAILED_RUN \"failed to run %s\"\n \n@@ -1030,6 +1031,35 @@ static int maintenance_task_loose_objects(struct maintenance_opts *opts)\n \treturn prune_packed(opts) || pack_loose(opts);\n }\n \n+static int incremental_repack_auto_condition(void)\n+{\n+\tstruct packed_git *p;\n+\tint enabled;\n+\tint incremental_repack_auto_limit = 10;\n+\tint count = 0;\n+\n+\tif (git_config_get_bool(\"core.multiPackIndex\", &enabled) ||\n+\t    !enabled)\n+\t\treturn 0;\n+\n+\tgit_config_get_int(\"maintenance.incremental-repack.auto\",\n+\t\t\t   &incremental_repack_auto_limit);\n+\n+\tif (!incremental_repack_auto_limit)\n+\t\treturn 0;\n+\tif (incremental_repack_auto_limit < 0)\n+\t\treturn 1;\n+\n+\tfor (p = get_packed_git(the_repository);\n+\t     count < incremental_repack_auto_limit && p;\n+\t     p = p->next) {\n+\t\tif (!p->multi_pack_index)\n+\t\t\tcount++;\n+\t}\n+\n+\treturn count >= incremental_repack_auto_limit;\n+}\n+\n static int multi_pack_index_write(struct maintenance_opts *opts)\n {\n \tstruct child_process child = CHILD_PROCESS_INIT;\n@@ -1220,6 +1250,7 @@ static struct maintenance_task tasks[] = {\n \t[TASK_INCREMENTAL_REPACK] = {\n \t\t\"incremental-repack\",\n \t\tmaintenance_task_incremental_repack,\n+\t\tincremental_repack_auto_condition,\n \t},\n \t[TASK_GC] = {\n \t\t\"gc\",\ndiff --git a/t/t7900-maintenance.sh b/t/t7900-maintenance.sh\nindex 1c5f44f2b3..e1e802e543 100755\n--- a/t/t7900-maintenance.sh\n+++ b/t/t7900-maintenance.sh\n@@ -187,6 +187,7 @@ test_expect_success 'incremental-repack task' '\n '\n \n test_expect_success EXPENSIVE 'incremental-repack 2g limit' '\n+\n \tfor i in $(test_seq 1 5)\n \tdo\n \t\ttest-tool genrandom foo$i $((512 * 1024 * 1024 + 1)) >>big ||\n@@ -217,4 +218,35 @@ test_expect_success EXPENSIVE 'incremental-repack 2g limit' '\n \t\t --no-progress --batch-size=2147483647 <run-2g.txt\n '\n \n+test_expect_success 'maintenance.incremental-repack.auto' '\n+\tgit repack -adk &&\n+\tgit config core.multiPackIndex true &&\n+\tgit multi-pack-index write &&\n+\tGIT_TRACE2_EVENT=\"$(pwd)/midx-init.txt\" git \\\n+\t\t-c maintenance.incremental-repack.auto=1 \\\n+\t\tmaintenance run --auto --task=incremental-repack 2>/dev/null &&\n+\ttest_subcommand ! git multi-pack-index write --no-progress <midx-init.txt &&\n+\tfor i in 1 2\n+\tdo\n+\t\ttest_commit A-$i &&\n+\t\tgit pack-objects --revs .git/objects/pack/pack <<-\\EOF &&\n+\t\tHEAD\n+\t\t^HEAD~1\n+\t\tEOF\n+\t\tGIT_TRACE2_EVENT=$(pwd)/trace-A-$i git \\\n+\t\t\t-c maintenance.incremental-repack.auto=2 \\\n+\t\t\tmaintenance run --auto --task=incremental-repack 2>/dev/null &&\n+\t\ttest_subcommand ! git multi-pack-index write --no-progress <trace-A-$i &&\n+\t\ttest_commit B-$i &&\n+\t\tgit pack-objects --revs .git/objects/pack/pack <<-\\EOF &&\n+\t\tHEAD\n+\t\t^HEAD~1\n+\t\tEOF\n+\t\tGIT_TRACE2_EVENT=$(pwd)/trace-B-$i git \\\n+\t\t\t-c maintenance.incremental-repack.auto=2 \\\n+\t\t\tmaintenance run --auto --task=incremental-repack 2>/dev/null &&\n+\t\ttest_subcommand git multi-pack-index write --no-progress <trace-B-$i || return 1\n+\tdone\n+'\n+\n test_done\n-- \ngitgitgadget\n"},{"id":"403072","messageId":"68727c555beecee7e86a89ed4a0c4d95f50430eb.1596731425.git.gitgitgadget@gmail.com","threadId":"54001","inReplyTo":"pull.696.git.1596731424.gitgitgadget@gmail.com","subject":"[PATCH 7/9] maintenance: add incremental-repack task","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2020-08-06T16:30:22Z","receivedAt":"2020-08-06T17:53:06Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"From: Derrick Stolee <dstolee@microsoft.com>\n\nThe previous change cleaned up loose objects using the\n'loose-objects' that can be run safely in the background. Add a\nsimilar job that performs similar cleanups for pack-files.\n\nOne issue with running 'git repack' is that it is designed to\nrepack all pack-files into a single pack-file. While this is the\nmost space-efficient way to store object data, it is not time or\nmemory efficient. This becomes extremely important if the repo is\nso large that a user struggles to store two copies of the pack on\ntheir disk.\n\nInstead, perform an \"incremental\" repack by collecting a few small\npack-files into a new pack-file. The multi-pack-index facilitates\nthis process ever since 'git multi-pack-index expire' was added in\n19575c7 (multi-pack-index: implement 'expire' subcommand,\n2019-06-10) and 'git multi-pack-index repack' was added in ce1e4a1\n(midx: implement midx_repack(), 2019-06-10).\n\nThe 'incremental-repack' task runs the following steps:\n\n1. 'git multi-pack-index write' creates a multi-pack-index file if\n   one did not exist, and otherwise will update the multi-pack-index\n   with any new pack-files that appeared since the last write. This\n   is particularly relevant with the background fetch job.\n\n   When the multi-pack-index sees two copies of the same object, it\n   stores the offset data into the newer pack-file. This means that\n   some old pack-files could become \"unreferenced\" which I will use\n   to mean \"a pack-file that is in the pack-file list of the\n   multi-pack-index but none of the objects in the multi-pack-index\n   reference a location inside that pack-file.\"\n\n2. 'git multi-pack-index expire' deletes any unreferenced pack-files\n   and updaes the multi-pack-index to drop those pack-files from the\n   list. This is safe to do as concurrent Git processes will see the\n   multi-pack-index and not open those packs when looking for object\n   contents. (Similar to the 'loose-objects' job, there are some Git\n   commands that open pack-files regardless of the multi-pack-index,\n   but they are rarely used. Further, a user that self-selects to\n   use background operations would likely refrain from using those\n   commands.)\n\n3. 'git multi-pack-index repack --bacth-size=<size>' collects a set\n   of pack-files that are listed in the multi-pack-index and creates\n   a new pack-file containing the objects whose offsets are listed\n   by the multi-pack-index to be in those objects. The set of pack-\n   files is selected greedily by sorting the pack-files by modified\n   time and adding a pack-file to the set if its \"expected size\" is\n   smaller than the batch size until the total expected size of the\n   selected pack-files is at least the batch size. The \"expected\n   size\" is calculated by taking the size of the pack-file divided\n   by the number of objects in the pack-file and multiplied by the\n   number of objects from the multi-pack-index with offset in that\n   pack-file. The expected size approximates how much data from that\n   pack-file will contribute to the resulting pack-file size. The\n   intention is that the resulting pack-file will be close in size\n   to the provided batch size.\n\n   The next run of the incremental-repack task will delete these\n   repacked pack-files during the 'expire' step.\n\n   In this version, the batch size is set to \"0\" which ignores the\n   size restrictions when selecting the pack-files. It instead\n   selects all pack-files and repacks all packed objects into a\n   single pack-file. This will be updated in the next change, but\n   it requires doing some calculations that are better isolated to\n   a separate change.\n\nEach of the above steps update the multi-pack-index file. After\neach step, we verify the new multi-pack-index. If the new\nmulti-pack-index is corrupt, then delete the multi-pack-index,\nrewrite it from scratch, and stop doing the later steps of the\njob. This is intended to be an extra-safe check without leaving\na repo with many pack-files without a multi-pack-index.\n\nThese steps are based on a similar background maintenance step in\nScalar (and VFS for Git) [1]. This was incredibly effective for\nusers of the Windows OS repository. After using the same VFS for Git\nrepository for over a year, some users had _thousands_ of pack-files\nthat combined to up to 250 GB of data. We noticed a few users were\nrunning into the open file descriptor limits (due in part to a bug\nin the multi-pack-index fixed by af96fe3 (midx: add packs to\npacked_git linked list, 2019-04-29).\n\nThese pack-files were mostly small since they contained the commits\nand trees that were pushed to the origin in a given hour. The GVFS\nprotocol includes a \"prefetch\" step that asks for pre-computed pack-\nfiles containing commits and trees by timestamp. These pack-files\nwere grouped into \"daily\" pack-files once a day for up to 30 days.\nIf a user did not request prefetch packs for over 30 days, then they\nwould get the entire history of commits and trees in a new, large\npack-file. This led to a large number of pack-files that had poor\ndelta compression.\n\nBy running this pack-file maintenance step once per day, these repos\nwith thousands of packs spanning 200+ GB dropped to dozens of pack-\nfiles spanning 30-50 GB. This was done all without removing objects\nfrom the system and using a constant batch size of two gigabytes.\nOnce the work was done to reduce the pack-files to small sizes, the\nbatch size of two gigabytes means that not every run triggers a\nrepack operation, so the following run will not expire a pack-file.\nThis has kept these repos in a \"clean\" state.\n\n[1] https://github.com/microsoft/scalar/blob/master/Scalar.Common/Maintenance/PackfileMaintenanceStep.cs\n\nSigned-off-by: Derrick Stolee <dstolee@microsoft.com>\n---\n Documentation/git-maintenance.txt |  15 ++++\n builtin/gc.c                      | 113 ++++++++++++++++++++++++++++++\n midx.c                            |   2 +-\n midx.h                            |   1 +\n t/t5319-multi-pack-index.sh       |   1 +\n t/t7900-maintenance.sh            |  38 ++++++++++\n 6 files changed, 169 insertions(+), 1 deletion(-)\n\ndiff --git a/Documentation/git-maintenance.txt b/Documentation/git-maintenance.txt\nindex 898aff4726..1bd105918f 100644\n--- a/Documentation/git-maintenance.txt\n+++ b/Documentation/git-maintenance.txt\n@@ -91,6 +91,21 @@ loose-objects::\n \tthousand objects to prevent the job from taking too long on a\n \trepository with many loose objects.\n \n+incremental-repack::\n+\tThe `incremental-repack` job repacks the object directory\n+\tusing the `multi-pack-index` feature. In order to prevent race\n+\tconditions with concurrent Git commands, it follows a two-step\n+\tprocess. First, it deletes any pack-files included in the\n+\t`multi-pack-index` where none of the objects in the\n+\t`multi-pack-index` reference those pack-files; this only happens\n+\tif all objects in the pack-file are also stored in a newer\n+\tpack-file. Second, it selects a group of pack-files whose \"expected\n+\tsize\" is below the batch size until the group has total expected\n+\tsize at least the batch size; see the `--batch-size` option for\n+\tthe `repack` subcommand in linkgit:git-multi-pack-index[1]. The\n+\tdefault batch-size is zero, which is a special case that attempts\n+\tto repack all pack-files into a single pack-file.\n+\n OPTIONS\n -------\n --auto::\ndiff --git a/builtin/gc.c b/builtin/gc.c\nindex 23329b5652..35c6d7ce82 100644\n--- a/builtin/gc.c\n+++ b/builtin/gc.c\n@@ -30,6 +30,7 @@\n #include \"promisor-remote.h\"\n #include \"refs.h\"\n #include \"remote.h\"\n+#include \"midx.h\"\n \n #define FAILED_RUN \"failed to run %s\"\n \n@@ -1029,6 +1030,113 @@ static int maintenance_task_loose_objects(struct maintenance_opts *opts)\n \treturn prune_packed(opts) || pack_loose(opts);\n }\n \n+static int multi_pack_index_write(struct maintenance_opts *opts)\n+{\n+\tstruct child_process child = CHILD_PROCESS_INIT;\n+\n+\tchild.git_cmd = 1;\n+\tstrvec_pushl(&child.args, \"multi-pack-index\", \"write\", NULL);\n+\n+\tif (opts->quiet)\n+\t\tstrvec_push(&child.args, \"--no-progress\");\n+\n+\tif (run_command(&child))\n+\t\treturn error(_(\"failed to write multi-pack-index\"));\n+\n+\treturn 0;\n+}\n+\n+static int rewrite_multi_pack_index(struct maintenance_opts *opts)\n+{\n+\tstruct repository *r = the_repository;\n+\tchar *midx_name = get_midx_filename(r->objects->odb->path);\n+\n+\tunlink(midx_name);\n+\tfree(midx_name);\n+\n+\treturn multi_pack_index_write(opts);\n+}\n+\n+static int multi_pack_index_verify(struct maintenance_opts *opts,\n+\t\t\t\t   const char *message)\n+{\n+\tstruct child_process child = CHILD_PROCESS_INIT;\n+\n+\tchild.git_cmd = 1;\n+\tstrvec_pushl(&child.args, \"multi-pack-index\", \"verify\", NULL);\n+\n+\tif (opts->quiet)\n+\t\tstrvec_push(&child.args, \"--no-progress\");\n+\n+\tif (run_command(&child)) {\n+\t\twarning(_(\"'git multi-pack-index verify' failed %s\"), message);\n+\t\treturn 1;\n+\t}\n+\n+\treturn 0;\n+}\n+\n+static int multi_pack_index_expire(struct maintenance_opts *opts)\n+{\n+\tstruct child_process child = CHILD_PROCESS_INIT;\n+\n+\tchild.git_cmd = 1;\n+\tstrvec_pushl(&child.args, \"multi-pack-index\", \"expire\", NULL);\n+\n+\tif (opts->quiet)\n+\t\tstrvec_push(&child.args, \"--no-progress\");\n+\n+\tclose_object_store(the_repository->objects);\n+\n+\tif (run_command(&child))\n+\t\treturn error(_(\"'git multi-pack-index expire' failed\"));\n+\n+\treturn 0;\n+}\n+\n+static int multi_pack_index_repack(struct maintenance_opts *opts)\n+{\n+\tstruct child_process child = CHILD_PROCESS_INIT;\n+\n+\tchild.git_cmd = 1;\n+\tstrvec_pushl(&child.args, \"multi-pack-index\", \"repack\", NULL);\n+\n+\tif (opts->quiet)\n+\t\tstrvec_push(&child.args, \"--no-progress\");\n+\n+\tstrvec_push(&child.args, \"--batch-size=0\");\n+\n+\tclose_object_store(the_repository->objects);\n+\n+\tif (run_command(&child))\n+\t\treturn error(_(\"'git multi-pack-index repack' failed\"));\n+\n+\treturn 0;\n+}\n+\n+static int maintenance_task_incremental_repack(struct maintenance_opts *opts)\n+{\n+\tprepare_repo_settings(the_repository);\n+\tif (!the_repository->settings.core_multi_pack_index) {\n+\t\twarning(_(\"skipping incremental-repack task because core.multiPackIndex is disabled\"));\n+\t\treturn 0;\n+\t}\n+\n+\tif (multi_pack_index_write(opts))\n+\t\treturn 1;\n+\tif (multi_pack_index_verify(opts, \"after initial write\"))\n+\t\treturn rewrite_multi_pack_index(opts);\n+\tif (multi_pack_index_expire(opts))\n+\t\treturn 1;\n+\tif (multi_pack_index_verify(opts, \"after expire step\"))\n+\t\treturn !!rewrite_multi_pack_index(opts);\n+\tif (multi_pack_index_repack(opts))\n+\t\treturn 1;\n+\tif (multi_pack_index_verify(opts, \"after repack step\"))\n+\t\treturn !!rewrite_multi_pack_index(opts);\n+\treturn 0;\n+}\n+\n typedef int maintenance_task_fn(struct maintenance_opts *opts);\n \n /*\n@@ -1050,6 +1158,7 @@ struct maintenance_task {\n enum maintenance_task_label {\n \tTASK_PREFETCH,\n \tTASK_LOOSE_OBJECTS,\n+\tTASK_INCREMENTAL_REPACK,\n \tTASK_GC,\n \tTASK_COMMIT_GRAPH,\n \n@@ -1067,6 +1176,10 @@ static struct maintenance_task tasks[] = {\n \t\tmaintenance_task_loose_objects,\n \t\tloose_object_auto_condition,\n \t},\n+\t[TASK_INCREMENTAL_REPACK] = {\n+\t\t\"incremental-repack\",\n+\t\tmaintenance_task_incremental_repack,\n+\t},\n \t[TASK_GC] = {\n \t\t\"gc\",\n \t\tmaintenance_task_gc,\ndiff --git a/midx.c b/midx.c\nindex aa37d5da86..66d7053d83 100644\n--- a/midx.c\n+++ b/midx.c\n@@ -37,7 +37,7 @@\n \n #define PACK_EXPIRED UINT_MAX\n \n-static char *get_midx_filename(const char *object_dir)\n+char *get_midx_filename(const char *object_dir)\n {\n \treturn xstrfmt(\"%s/pack/multi-pack-index\", object_dir);\n }\ndiff --git a/midx.h b/midx.h\nindex b18cf53bc4..baeecc70c9 100644\n--- a/midx.h\n+++ b/midx.h\n@@ -37,6 +37,7 @@ struct multi_pack_index {\n \n #define MIDX_PROGRESS     (1 << 0)\n \n+char *get_midx_filename(const char *object_dir);\n struct multi_pack_index *load_multi_pack_index(const char *object_dir, int local);\n int prepare_midx_pack(struct repository *r, struct multi_pack_index *m, uint32_t pack_int_id);\n int bsearch_midx(const struct object_id *oid, struct multi_pack_index *m, uint32_t *result);\ndiff --git a/t/t5319-multi-pack-index.sh b/t/t5319-multi-pack-index.sh\nindex 12f41dfc18..26f224b0e3 100755\n--- a/t/t5319-multi-pack-index.sh\n+++ b/t/t5319-multi-pack-index.sh\n@@ -3,6 +3,7 @@\n test_description='multi-pack-indexes'\n . ./test-lib.sh\n \n+GIT_TEST_MULTI_PACK_INDEX=0\n objdir=.git/objects\n \n midx_read_expect () {\ndiff --git a/t/t7900-maintenance.sh b/t/t7900-maintenance.sh\nindex 71ac686c09..be19ac7623 100755\n--- a/t/t7900-maintenance.sh\n+++ b/t/t7900-maintenance.sh\n@@ -5,6 +5,7 @@ test_description='git maintenance builtin'\n . ./test-lib.sh\n \n GIT_TEST_COMMIT_GRAPH=0\n+GIT_TEST_MULTI_PACK_INDEX=0\n \n test_expect_success 'help text' '\n \ttest_expect_code 129 git maintenance -h 2>err &&\n@@ -147,4 +148,41 @@ test_expect_success 'maintenance.loose-objects.auto' '\n \tdone\n '\n \n+test_expect_success 'incremental-repack task' '\n+\tpackDir=.git/objects/pack &&\n+\tfor i in $(test_seq 1 5)\n+\tdo\n+\t\ttest_commit $i || return 1\n+\tdone &&\n+\n+\t# Create three disjoint pack-files with size BIG, small, small.\n+\techo HEAD~2 | git pack-objects --revs $packDir/test-1 &&\n+\ttest_tick &&\n+\tgit pack-objects --revs $packDir/test-2 <<-\\EOF &&\n+\tHEAD~1\n+\t^HEAD~2\n+\tEOF\n+\ttest_tick &&\n+\tgit pack-objects --revs $packDir/test-3 <<-\\EOF &&\n+\tHEAD\n+\t^HEAD~1\n+\tEOF\n+\trm -f $packDir/pack-* &&\n+\trm -f $packDir/loose-* &&\n+\tls $packDir/*.pack >packs-before &&\n+\ttest_line_count = 3 packs-before &&\n+\n+\t# the job repacks the two into a new pack, but does not\n+\t# delete the old ones.\n+\tgit maintenance run --task=incremental-repack &&\n+\tls $packDir/*.pack >packs-between &&\n+\ttest_line_count = 4 packs-between &&\n+\n+\t# the job deletes the two old packs, and does not write\n+\t# a new one because only one pack remains.\n+\tgit maintenance run --task=incremental-repack &&\n+\tls .git/objects/pack/*.pack >packs-after &&\n+\ttest_line_count = 1 packs-after\n+'\n+\n test_done\n-- \ngitgitgadget\n\n"},{"id":"403073","messageId":"37e59b1a8de378c5d7f23cb3f5630be7990bc3ff.1596731425.git.gitgitgadget@gmail.com","threadId":"54001","inReplyTo":"pull.696.git.1596731424.gitgitgadget@gmail.com","subject":"[PATCH 5/9] midx: enable core.multiPackIndex by default","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2020-08-06T16:30:20Z","receivedAt":"2020-08-06T17:53:17Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"From: Derrick Stolee <dstolee@microsoft.com>\n\nThe core.multiPackIndex setting has been around since c4d25228ebb\n(config: create core.multiPackIndex setting, 2018-07-12), but has been\ndisabled by default. If a user wishes to use the multi-pack-index\nfeature, then they must enable this config and run 'git multi-pack-index\nwrite'.\n\nThe multi-pack-index feature is relatively stable now, so make the\nconfig option true by default. For users that do not use a\nmulti-pack-index, the only extra cost will be a file lookup to see if a\nmulti-pack-index file exists (once per process, per object directory).\n\nAlso, this config option will be referenced by an upcoming\n\"incremental-repack\" task in the maintenance builtin, so move the config\noption into the repository settings struct. Note that if\nGIT_TEST_MULTI_PACK_INDEX=1, then we want to ignore the config option\nand treat core.multiPackIndex as enabled.\n\nSigned-off-by: Derrick Stolee <dstolee@microsoft.com>\n---\n Documentation/config/core.txt |  4 ++--\n midx.c                        | 11 +++--------\n repo-settings.c               |  6 ++++++\n repository.h                  |  2 ++\n 4 files changed, 13 insertions(+), 10 deletions(-)\n\ndiff --git a/Documentation/config/core.txt b/Documentation/config/core.txt\nindex 74619a9c03..86c91d5381 100644\n--- a/Documentation/config/core.txt\n+++ b/Documentation/config/core.txt\n@@ -606,8 +606,8 @@ core.useReplaceRefs::\n \n core.multiPackIndex::\n \tUse the multi-pack-index file to track multiple packfiles using a\n-\tsingle index. See link:technical/multi-pack-index.html[the\n-\tmulti-pack-index design document].\n+\tsingle index. See linkgit:git-multi-pack-index[1] for more\n+\tinformation. Defaults to true.\n \n core.sparseCheckout::\n \tEnable \"sparse checkout\" feature. See linkgit:git-sparse-checkout[1]\ndiff --git a/midx.c b/midx.c\nindex a5fb797ede..ef499cf504 100644\n--- a/midx.c\n+++ b/midx.c\n@@ -10,6 +10,7 @@\n #include \"progress.h\"\n #include \"trace2.h\"\n #include \"run-command.h\"\n+#include \"repository.h\"\n \n #define MIDX_SIGNATURE 0x4d494458 /* \"MIDX\" */\n #define MIDX_VERSION 1\n@@ -384,15 +385,9 @@ int prepare_multi_pack_index_one(struct repository *r, const char *object_dir, i\n {\n \tstruct multi_pack_index *m;\n \tstruct multi_pack_index *m_search;\n-\tint config_value;\n-\tstatic int env_value = -1;\n \n-\tif (env_value < 0)\n-\t\tenv_value = git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0);\n-\n-\tif (!env_value &&\n-\t    (repo_config_get_bool(r, \"core.multipackindex\", &config_value) ||\n-\t    !config_value))\n+\tprepare_repo_settings(r);\n+\tif (!r->settings.core_multi_pack_index)\n \t\treturn 0;\n \n \tfor (m_search = r->objects->multi_pack_index; m_search; m_search = m_search->next)\ndiff --git a/repo-settings.c b/repo-settings.c\nindex 0918408b34..5bd2c22726 100644\n--- a/repo-settings.c\n+++ b/repo-settings.c\n@@ -1,6 +1,7 @@\n #include \"cache.h\"\n #include \"config.h\"\n #include \"repository.h\"\n+#include \"midx.h\"\n \n #define UPDATE_DEFAULT_BOOL(s,v) do { if (s == -1) { s = v; } } while(0)\n \n@@ -47,6 +48,11 @@ void prepare_repo_settings(struct repository *r)\n \t\tr->settings.pack_use_sparse = value;\n \tUPDATE_DEFAULT_BOOL(r->settings.pack_use_sparse, 1);\n \n+\tvalue = git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0);\n+\tif (value || !repo_config_get_bool(r, \"core.multipackindex\", &value))\n+\t\tr->settings.core_multi_pack_index = value;\n+\tUPDATE_DEFAULT_BOOL(r->settings.core_multi_pack_index, 1);\n+\n \tif (!repo_config_get_bool(r, \"feature.manyfiles\", &value) && value) {\n \t\tUPDATE_DEFAULT_BOOL(r->settings.index_version, 4);\n \t\tUPDATE_DEFAULT_BOOL(r->settings.core_untracked_cache, UNTRACKED_CACHE_WRITE);\ndiff --git a/repository.h b/repository.h\nindex 3c1f7d54bd..3901ce0b65 100644\n--- a/repository.h\n+++ b/repository.h\n@@ -37,6 +37,8 @@ struct repo_settings {\n \n \tint pack_use_sparse;\n \tenum fetch_negotiation_setting fetch_negotiation_algorithm;\n+\n+\tint core_multi_pack_index;\n };\n \n struct repository {\n-- \ngitgitgadget\n\n"},{"id":"403074","messageId":"e787403ea7e95dfeb30a0760fea4c56f64895d1e.1596731425.git.gitgitgadget@gmail.com","threadId":"54001","inReplyTo":"pull.696.git.1596731424.gitgitgadget@gmail.com","subject":"[PATCH 4/9] maintenance: create auto condition for loose-objects","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2020-08-06T16:30:19Z","receivedAt":"2020-08-06T17:53:25Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"From: Derrick Stolee <dstolee@microsoft.com>\n\nThe loose-objects task deletes loose objects that already exist in a\npack-file, then place the remaining loose objects into a new pack-file.\nIf this step runs all the time, then we risk creating pack-files with\nvery few objects with every 'git commit' process. To prevent\noverwhelming the packs directory with small pack-files, place a minimum\nnumber of objects to justify the task.\n\nThe 'maintenance.loose-objects.auto' config option specifies a minimum\nnumber of loose objects to justify the task to run under the '--auto'\noption. This defaults to 100 loose objects. Setting the value to zero\nwill prevent the step from running under '--auto' while a negative value\nwill force it to run every time.\n\nSigned-off-by: Derrick Stolee <dstolee@microsoft.com>\n---\n Documentation/config/maintenance.txt |  9 +++++++++\n builtin/gc.c                         | 30 ++++++++++++++++++++++++++++\n t/t7900-maintenance.sh               | 25 +++++++++++++++++++++++\n 3 files changed, 64 insertions(+)\n\ndiff --git a/Documentation/config/maintenance.txt b/Documentation/config/maintenance.txt\nindex 9bd69b9df3..a9442dd260 100644\n--- a/Documentation/config/maintenance.txt\n+++ b/Documentation/config/maintenance.txt\n@@ -12,3 +12,12 @@ maintenance.commit-graph.auto::\n \treachable commits that are not in the commit-graph file is at least\n \tthe value of `maintenance.commit-graph.auto`. The default value is\n \t100.\n+\n+maintenance.loose-objects.auto::\n+\tThis integer config option controls how often the `loose-objects` task\n+\tshould be run as part of `git maintenance run --auto`. If zero, then\n+\tthe `loose-objects` task will not run with the `--auto` option. A\n+\tnegative value will force the task to run every time. Otherwise, a\n+\tpositive value implies the command should run when the number of\n+\tloose objects is at least the value of `maintenance.loose-objects.auto`.\n+\tThe default value is 100.\ndiff --git a/builtin/gc.c b/builtin/gc.c\nindex 60261d2647..23329b5652 100644\n--- a/builtin/gc.c\n+++ b/builtin/gc.c\n@@ -927,6 +927,35 @@ struct write_loose_object_data {\n \tint batch_size;\n };\n \n+static int loose_object_auto_limit = 100;\n+\n+static int loose_object_count(const struct object_id *oid,\n+\t\t\t       const char *path,\n+\t\t\t       void *data)\n+{\n+\tint *count = (int*)data;\n+\tif (++(*count) >= loose_object_auto_limit)\n+\t\treturn 1;\n+\treturn 0;\n+}\n+\n+static int loose_object_auto_condition(void)\n+{\n+\tint count = 0;\n+\n+\tgit_config_get_int(\"maintenance.loose-objects.auto\",\n+\t\t\t   &loose_object_auto_limit);\n+\n+\tif (!loose_object_auto_limit)\n+\t\treturn 0;\n+\tif (loose_object_auto_limit < 0)\n+\t\treturn 1;\n+\n+\treturn for_each_loose_file_in_objdir(the_repository->objects->odb->path,\n+\t\t\t\t\t     loose_object_count,\n+\t\t\t\t\t     NULL, NULL, &count);\n+}\n+\n static int bail_on_loose(const struct object_id *oid,\n \t\t\t const char *path,\n \t\t\t void *data)\n@@ -1036,6 +1065,7 @@ static struct maintenance_task tasks[] = {\n \t[TASK_LOOSE_OBJECTS] = {\n \t\t\"loose-objects\",\n \t\tmaintenance_task_loose_objects,\n+\t\tloose_object_auto_condition,\n \t},\n \t[TASK_GC] = {\n \t\t\"gc\",\ndiff --git a/t/t7900-maintenance.sh b/t/t7900-maintenance.sh\nindex 8d54f93a10..71ac686c09 100755\n--- a/t/t7900-maintenance.sh\n+++ b/t/t7900-maintenance.sh\n@@ -122,4 +122,29 @@ test_expect_success 'loose-objects task' '\n \ttest_cmp packs-between packs-after\n '\n \n+test_expect_success 'maintenance.loose-objects.auto' '\n+\tgit repack -adk &&\n+\tGIT_TRACE2_EVENT=\"$(pwd)/trace-lo1.txt\" \\\n+\t\tgit -c maintenance.loose-objects.auto=1 maintenance \\\n+\t\trun --auto --task=loose-objects 2>/dev/null &&\n+\ttest_subcommand ! git prune-packed --quiet <trace-lo1.txt &&\n+\tfor i in 1 2\n+\tdo\n+\t\tprintf data-A-$i | git hash-object -t blob --stdin -w &&\n+\t\tGIT_TRACE2_EVENT=\"$(pwd)/trace-loA-$i\" \\\n+\t\t\tgit -c maintenance.loose-objects.auto=2 \\\n+\t\t\tmaintenance run --auto --task=loose-objects 2>/dev/null &&\n+\t\ttest_subcommand ! git prune-packed --quiet <trace-loA-$i &&\n+\t\tprintf data-B-$i | git hash-object -t blob --stdin -w &&\n+\t\tGIT_TRACE2_EVENT=\"$(pwd)/trace-loB-$i\" \\\n+\t\t\tgit -c maintenance.loose-objects.auto=2 \\\n+\t\t\tmaintenance run --auto --task=loose-objects 2>/dev/null &&\n+\t\ttest_subcommand git prune-packed --quiet <trace-loB-$i &&\n+\t\tGIT_TRACE2_EVENT=\"$(pwd)/trace-loC-$i\" \\\n+\t\t\tgit -c maintenance.loose-objects.auto=2 \\\n+\t\t\tmaintenance run --auto --task=loose-objects 2>/dev/null &&\n+\t\ttest_subcommand git prune-packed --quiet <trace-loC-$i || return 1\n+\tdone\n+'\n+\n test_done\n-- \ngitgitgadget\n\n"},{"id":"403075","messageId":"621375a3c99014c48568660bde062b7330d5a662.1596731425.git.gitgitgadget@gmail.com","threadId":"54001","inReplyTo":"pull.696.git.1596731424.gitgitgadget@gmail.com","subject":"[PATCH 3/9] maintenance: add loose-objects task","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2020-08-06T16:30:18Z","receivedAt":"2020-08-06T17:53:25Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"From: Derrick Stolee <dstolee@microsoft.com>\n\nOne goal of background maintenance jobs is to allow a user to\ndisable auto-gc (gc.auto=0) but keep their repository in a clean\nstate. Without any cleanup, loose objects will clutter the object\ndatabase and slow operations. In addition, the loose objects will\ntake up extra space because they are not stored with deltas against\nsimilar objects.\n\nCreate a 'loose-objects' task for the 'git maintenance run' command.\nThis helps clean up loose objects without disrupting concurrent Git\ncommands using the following sequence of events:\n\n1. Run 'git prune-packed' to delete any loose objects that exist\n   in a pack-file. Concurrent commands will prefer the packed\n   version of the object to the loose version. (Of course, there\n   are exceptions for commands that specifically care about the\n   location of an object. These are rare for a user to run on\n   purpose, and we hope a user that has selected background\n   maintenance will not be trying to do foreground maintenance.)\n\n2. Run 'git pack-objects' on a batch of loose objects. These\n   objects are grouped by scanning the loose object directories in\n   lexicographic order until listing all loose objects -or-\n   reaching 50,000 objects. This is more than enough if the loose\n   objects are created only by a user doing normal development.\n   We noticed users with _millions_ of loose objects because VFS\n   for Git downloads blobs on-demand when a file read operation\n   requires populating a virtual file. This has potential of\n   happening in partial clones if someone runs 'git grep' or\n   otherwise evades the batch-download feature for requesting\n   promisor objects.\n\nThis step is based on a similar step in Scalar [1] and VFS for Git.\n[1] https://github.com/microsoft/scalar/blob/master/Scalar.Common/Maintenance/LooseObjectsStep.cs\n\nSigned-off-by: Derrick Stolee <dstolee@microsoft.com>\n---\n Documentation/git-maintenance.txt | 11 ++++\n builtin/gc.c                      | 97 +++++++++++++++++++++++++++++++\n t/t7900-maintenance.sh            | 39 +++++++++++++\n 3 files changed, 147 insertions(+)\n\ndiff --git a/Documentation/git-maintenance.txt b/Documentation/git-maintenance.txt\nindex bb0d5eded4..898aff4726 100644\n--- a/Documentation/git-maintenance.txt\n+++ b/Documentation/git-maintenance.txt\n@@ -80,6 +80,17 @@ gc::\n \tIt can also be disruptive in some situations, as it deletes stale\n \tdata.\n \n+loose-objects::\n+\tThe `loose-objects` job cleans up loose objects and places them into\n+\tpack-files. In order to prevent race conditions with concurrent Git\n+\tcommands, it follows a two-step process. First, it deletes any loose\n+\tobjects that already exist in a pack-file; concurrent Git processes\n+\twill examine the pack-file for the object data instead of the loose\n+\tobject. Second, it creates a new pack-file (starting with \"loose-\")\n+\tcontaining a batch of loose objects. The batch size is limited to 50\n+\tthousand objects to prevent the job from taking too long on a\n+\trepository with many loose objects.\n+\n OPTIONS\n -------\n --auto::\ndiff --git a/builtin/gc.c b/builtin/gc.c\nindex 942b7ea535..60261d2647 100644\n--- a/builtin/gc.c\n+++ b/builtin/gc.c\n@@ -908,6 +908,98 @@ static int maintenance_task_gc(struct maintenance_opts *opts)\n \treturn run_command(&child);\n }\n \n+static int prune_packed(struct maintenance_opts *opts)\n+{\n+\tstruct child_process child = CHILD_PROCESS_INIT;\n+\n+\tchild.git_cmd = 1;\n+\tstrvec_push(&child.args, \"prune-packed\");\n+\n+\tif (opts->quiet)\n+\t\tstrvec_push(&child.args, \"--quiet\");\n+\n+\treturn !!run_command(&child);\n+}\n+\n+struct write_loose_object_data {\n+\tFILE *in;\n+\tint count;\n+\tint batch_size;\n+};\n+\n+static int bail_on_loose(const struct object_id *oid,\n+\t\t\t const char *path,\n+\t\t\t void *data)\n+{\n+\treturn 1;\n+}\n+\n+static int write_loose_object_to_stdin(const struct object_id *oid,\n+\t\t\t\t       const char *path,\n+\t\t\t\t       void *data)\n+{\n+\tstruct write_loose_object_data *d = (struct write_loose_object_data *)data;\n+\n+\tfprintf(d->in, \"%s\\n\", oid_to_hex(oid));\n+\n+\treturn ++(d->count) > d->batch_size;\n+}\n+\n+static int pack_loose(struct maintenance_opts *opts)\n+{\n+\tstruct repository *r = the_repository;\n+\tint result = 0;\n+\tstruct write_loose_object_data data;\n+\tstruct child_process pack_proc = CHILD_PROCESS_INIT;\n+\n+\t/*\n+\t * Do not start pack-objects process\n+\t * if there are no loose objects.\n+\t */\n+\tif (!for_each_loose_file_in_objdir(r->objects->odb->path,\n+\t\t\t\t\t   bail_on_loose,\n+\t\t\t\t\t   NULL, NULL, NULL))\n+\t\treturn 0;\n+\n+\tpack_proc.git_cmd = 1;\n+\n+\tstrvec_push(&pack_proc.args, \"pack-objects\");\n+\tif (opts->quiet)\n+\t\tstrvec_push(&pack_proc.args, \"--quiet\");\n+\tstrvec_pushf(&pack_proc.args, \"%s/pack/loose\", r->objects->odb->path);\n+\n+\tpack_proc.in = -1;\n+\n+\tif (start_command(&pack_proc)) {\n+\t\terror(_(\"failed to start 'git pack-objects' process\"));\n+\t\treturn 1;\n+\t}\n+\n+\tdata.in = xfdopen(pack_proc.in, \"w\");\n+\tdata.count = 0;\n+\tdata.batch_size = 50000;\n+\n+\tfor_each_loose_file_in_objdir(r->objects->odb->path,\n+\t\t\t\t      write_loose_object_to_stdin,\n+\t\t\t\t      NULL,\n+\t\t\t\t      NULL,\n+\t\t\t\t      &data);\n+\n+\tfclose(data.in);\n+\n+\tif (finish_command(&pack_proc)) {\n+\t\terror(_(\"failed to finish 'git pack-objects' process\"));\n+\t\tresult = 1;\n+\t}\n+\n+\treturn result;\n+}\n+\n+static int maintenance_task_loose_objects(struct maintenance_opts *opts)\n+{\n+\treturn prune_packed(opts) || pack_loose(opts);\n+}\n+\n typedef int maintenance_task_fn(struct maintenance_opts *opts);\n \n /*\n@@ -928,6 +1020,7 @@ struct maintenance_task {\n \n enum maintenance_task_label {\n \tTASK_PREFETCH,\n+\tTASK_LOOSE_OBJECTS,\n \tTASK_GC,\n \tTASK_COMMIT_GRAPH,\n \n@@ -940,6 +1033,10 @@ static struct maintenance_task tasks[] = {\n \t\t\"prefetch\",\n \t\tmaintenance_task_prefetch,\n \t},\n+\t[TASK_LOOSE_OBJECTS] = {\n+\t\t\"loose-objects\",\n+\t\tmaintenance_task_loose_objects,\n+\t},\n \t[TASK_GC] = {\n \t\t\"gc\",\n \t\tmaintenance_task_gc,\ndiff --git a/t/t7900-maintenance.sh b/t/t7900-maintenance.sh\nindex 3dd99ef660..8d54f93a10 100755\n--- a/t/t7900-maintenance.sh\n+++ b/t/t7900-maintenance.sh\n@@ -83,4 +83,43 @@ test_expect_success 'prefetch multiple remotes' '\n \tgit log prefetch/remote2/two\n '\n \n+test_expect_success 'loose-objects task' '\n+\t# Repack everything so we know the state of the object dir\n+\tgit repack -adk &&\n+\n+\t# Hack to stop maintenance from running during \"git commit\"\n+\techo in use >.git/objects/maintenance.lock &&\n+\n+\t# Assuming that \"git commit\" creates at least one loose object\n+\ttest_commit create-loose-object &&\n+\trm .git/objects/maintenance.lock &&\n+\n+\tls .git/objects >obj-dir-before &&\n+\ttest_file_not_empty obj-dir-before &&\n+\tls .git/objects/pack/*.pack >packs-before &&\n+\ttest_line_count = 1 packs-before &&\n+\n+\t# The first run creates a pack-file\n+\t# but does not delete loose objects.\n+\tgit maintenance run --task=loose-objects &&\n+\tls .git/objects >obj-dir-between &&\n+\ttest_cmp obj-dir-before obj-dir-between &&\n+\tls .git/objects/pack/*.pack >packs-between &&\n+\ttest_line_count = 2 packs-between &&\n+\tls .git/objects/pack/loose-*.pack >loose-packs &&\n+\ttest_line_count = 1 loose-packs &&\n+\n+\t# The second run deletes loose objects\n+\t# but does not create a pack-file.\n+\tgit maintenance run --task=loose-objects &&\n+\tls .git/objects >obj-dir-after &&\n+\tcat >expect <<-\\EOF &&\n+\tinfo\n+\tpack\n+\tEOF\n+\ttest_cmp expect obj-dir-after &&\n+\tls .git/objects/pack/*.pack >packs-after &&\n+\ttest_cmp packs-between packs-after\n+'\n+\n test_done\n-- \ngitgitgadget\n\n"},{"id":"403076","messageId":"85118ed5f19468d5051dd1579e35cae3c3114d24.1596731425.git.gitgitgadget@gmail.com","threadId":"54001","inReplyTo":"pull.696.git.1596731424.gitgitgadget@gmail.com","subject":"[PATCH 2/9] maintenance: add prefetch task","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2020-08-06T16:30:17Z","receivedAt":"2020-08-06T17:53:32Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"From: Derrick Stolee <dstolee@microsoft.com>\n\nWhen working with very large repositories, an incremental 'git fetch'\ncommand can download a large amount of data. If there are many other\nusers pushing to a common repo, then this data can rival the initial\npack-file size of a 'git clone' of a medium-size repo.\n\nUsers may want to keep the data on their local repos as close as\npossible to the data on the remote repos by fetching periodically in\nthe background. This can break up a large daily fetch into several\nsmaller hourly fetches.\n\nThe task is called \"prefetch\" because it is work done in advance\nof a foreground fetch to make that 'git fetch' command much faster.\n\nHowever, if we simply ran 'git fetch <remote>' in the background,\nthen the user running a foregroudn 'git fetch <remote>' would lose\nsome important feedback when a new branch appears or an existing\nbranch updates. This is especially true if a remote branch is\nforce-updated and this isn't noticed by the user because it occurred\nin the background. Further, the functionality of 'git push\n--force-with-lease' becomes suspect.\n\nWhen running 'git fetch <remote> <options>' in the background, use\nthe following options for careful updating:\n\n1. --no-tags prevents getting a new tag when a user wants to see\n   the new tags appear in their foreground fetches.\n\n2. --refmap= removes the configured refspec which usually updates\n   refs/remotes/<remote>/* with the refs advertised by the remote.\n   While this looks confusing, this was documented and tested by\n   b40a50264ac (fetch: document and test --refmap=\"\", 2020-01-21),\n   including this sentence in the documentation:\n\n\tProviding an empty `<refspec>` to the `--refmap` option\n\tcauses Git to ignore the configured refspecs and rely\n\tentirely on the refspecs supplied as command-line arguments.\n\n3. By adding a new refspec \"+refs/heads/*:refs/prefetch/<remote>/*\"\n   we can ensure that we actually load the new values somewhere in\n   our refspace while not updating refs/heads or refs/remotes. By\n   storing these refs here, the commit-graph job will update the\n   commit-graph with the commits from these hidden refs.\n\n4. --prune will delete the refs/prefetch/<remote> refs that no\n   longer appear on the remote.\n\n5. --no-write-fetch-head prevents updating FETCH_HEAD.\n\nWe've been using this step as a critical background job in Scalar\n[1] (and VFS for Git). This solved a pain point that was showing up\nin user reports: fetching was a pain! Users do not like waiting to\ndownload the data that was created while they were away from their\nmachines. After implementing background fetch, the foreground fetch\ncommands sped up significantly because they mostly just update refs\nand download a small amount of new data. The effect is especially\ndramatic when paried with --no-show-forced-udpates (through\nfetch.showForcedUpdates=false).\n\n[1] https://github.com/microsoft/scalar/blob/master/Scalar.Common/Maintenance/FetchStep.cs\n\nSigned-off-by: Derrick Stolee <dstolee@microsoft.com>\n---\n Documentation/git-maintenance.txt | 15 +++++++++\n builtin/gc.c                      | 52 +++++++++++++++++++++++++++++++\n t/t7900-maintenance.sh            | 25 +++++++++++++++\n 3 files changed, 92 insertions(+)\n\ndiff --git a/Documentation/git-maintenance.txt b/Documentation/git-maintenance.txt\nindex e1a2a8902c..bb0d5eded4 100644\n--- a/Documentation/git-maintenance.txt\n+++ b/Documentation/git-maintenance.txt\n@@ -57,6 +57,21 @@ since it will not expire `.graph` files that were in the previous\n `commit-graph-chain` file. They will be deleted by a later run based on\n the expiration delay.\n \n+prefetch::\n+\tThe `prefetch` task updates the object directory with the latest\n+\tobjects from all registered remotes. For each remote, a `git fetch`\n+\tcommand is run. The refmap is custom to avoid updating local or remote\n+\tbranches (those in `refs/heads` or `refs/remotes`). Instead, the\n+\tremote refs are stored in `refs/prefetch/<remote>/`. Also, tags are\n+\tnot updated.\n++\n+This is done to avoid disrupting the remote-tracking branches. The end users\n+expect these refs to stay unmoved unless they initiate a fetch.  With prefetch\n+task, however, the objects necessary to complete a later real fetch would\n+already be obtained, so the real fetch would go faster.  In the ideal case,\n+it will just become an update to bunch of remote-tracking branches without\n+any object transfer.\n+\n gc::\n \tCleanup unnecessary files and optimize the local repository. \"GC\"\n \tstands for \"garbage collection,\" but this task performs many\ndiff --git a/builtin/gc.c b/builtin/gc.c\nindex 8162bca974..942b7ea535 100644\n--- a/builtin/gc.c\n+++ b/builtin/gc.c\n@@ -29,6 +29,7 @@\n #include \"tree.h\"\n #include \"promisor-remote.h\"\n #include \"refs.h\"\n+#include \"remote.h\"\n \n #define FAILED_RUN \"failed to run %s\"\n \n@@ -843,6 +844,52 @@ static int maintenance_task_commit_graph(struct maintenance_opts *opts)\n \treturn 1;\n }\n \n+static int fetch_remote(const char *remote, struct maintenance_opts *opts)\n+{\n+\tstruct child_process child = CHILD_PROCESS_INIT;\n+\n+\tchild.git_cmd = 1;\n+\tstrvec_pushl(&child.args, \"fetch\", remote, \"--prune\", \"--no-tags\",\n+\t\t     \"--no-write-fetch-head\", \"--refmap=\", NULL);\n+\n+\tif (opts->quiet)\n+\t\tstrvec_push(&child.args, \"--quiet\");\n+\n+\tstrvec_pushf(&child.args, \"+refs/heads/*:refs/prefetch/%s/*\", remote);\n+\n+\treturn !!run_command(&child);\n+}\n+\n+static int fill_each_remote(struct remote *remote, void *cbdata)\n+{\n+\tstruct string_list *remotes = (struct string_list *)cbdata;\n+\n+\tstring_list_append(remotes, remote->name);\n+\treturn 0;\n+}\n+\n+static int maintenance_task_prefetch(struct maintenance_opts *opts)\n+{\n+\tint result = 0;\n+\tstruct string_list_item *item;\n+\tstruct string_list remotes = STRING_LIST_INIT_DUP;\n+\n+\tif (for_each_remote(fill_each_remote, &remotes)) {\n+\t\terror(_(\"failed to fill remotes\"));\n+\t\tresult = 1;\n+\t\tgoto cleanup;\n+\t}\n+\n+\tfor (item = remotes.items;\n+\t     item && item < remotes.items + remotes.nr;\n+\t     item++)\n+\t\tresult |= fetch_remote(item->string, opts);\n+\n+cleanup:\n+\tstring_list_clear(&remotes, 0);\n+\treturn result;\n+}\n+\n static int maintenance_task_gc(struct maintenance_opts *opts)\n {\n \tstruct child_process child = CHILD_PROCESS_INIT;\n@@ -880,6 +927,7 @@ struct maintenance_task {\n };\n \n enum maintenance_task_label {\n+\tTASK_PREFETCH,\n \tTASK_GC,\n \tTASK_COMMIT_GRAPH,\n \n@@ -888,6 +936,10 @@ enum maintenance_task_label {\n };\n \n static struct maintenance_task tasks[] = {\n+\t[TASK_PREFETCH] = {\n+\t\t\"prefetch\",\n+\t\tmaintenance_task_prefetch,\n+\t},\n \t[TASK_GC] = {\n \t\t\"gc\",\n \t\tmaintenance_task_gc,\ndiff --git a/t/t7900-maintenance.sh b/t/t7900-maintenance.sh\nindex 406dc7c303..3dd99ef660 100755\n--- a/t/t7900-maintenance.sh\n+++ b/t/t7900-maintenance.sh\n@@ -58,4 +58,29 @@ test_expect_success 'run --task duplicate' '\n \ttest_i18ngrep \"cannot be selected multiple times\" err\n '\n \n+test_expect_success 'run --task=prefetch with no remotes' '\n+\tgit maintenance run --task=prefetch 2>err &&\n+\ttest_must_be_empty err\n+'\n+\n+test_expect_success 'prefetch multiple remotes' '\n+\tgit clone . clone1 &&\n+\tgit clone . clone2 &&\n+\tgit remote add remote1 \"file://$(pwd)/clone1\" &&\n+\tgit remote add remote2 \"file://$(pwd)/clone2\" &&\n+\tgit -C clone1 switch -c one &&\n+\tgit -C clone2 switch -c two &&\n+\ttest_commit -C clone1 one &&\n+\ttest_commit -C clone2 two &&\n+\tGIT_TRACE2_EVENT=\"$(pwd)/run-prefetch.txt\" git maintenance run --task=prefetch 2>/dev/null &&\n+\tfetchargs=\"--prune --no-tags --no-write-fetch-head --refmap= --quiet\" &&\n+\ttest_subcommand git fetch remote1 $fetchargs +refs/heads/\\\\*:refs/prefetch/remote1/\\\\* <run-prefetch.txt &&\n+\ttest_subcommand git fetch remote2 $fetchargs +refs/heads/\\\\*:refs/prefetch/remote2/\\\\* <run-prefetch.txt &&\n+\ttest_path_is_missing .git/refs/remotes &&\n+\ttest_cmp clone1/.git/refs/heads/one .git/refs/prefetch/remote1/one &&\n+\ttest_cmp clone2/.git/refs/heads/two .git/refs/prefetch/remote2/two &&\n+\tgit log prefetch/remote1/one &&\n+\tgit log prefetch/remote2/two\n+'\n+\n test_done\n-- \ngitgitgadget\n\n"},{"id":"403077","messageId":"pull.696.git.1596731424.gitgitgadget@gmail.com","threadId":"54001","inReplyTo":null,"subject":"[PATCH 0/9] Maintenance II: prefetch, loose-objects, incremental-repack tasks","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2020-08-06T16:30:15Z","receivedAt":"2020-08-06T17:53:35Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"This series is based on part I [2].\n\nThis patch series contains 9 patches that were going to be part of v4 of\nds/maintenance [1], but the discussion has gotten really long. To help, I'm\nsplitting out the portions that create and test the 'maintenance' builtin\nfrom the additional tasks (prefetch, loose-objects, incremental-repack) that\ncan be brought in later.\n\n[1] \nhttps://lore.kernel.org/git/pull.671.git.1594131695.gitgitgadget@gmail.com/\n[2] \nhttps://lore.kernel.org/git/pull.695.git.1596728921.gitgitgadget@gmail.com/\n\nAs detailed in [2], the 'git maintenance run' subcommand will run certain\ntasks based on config options or the --task= arguments. The --auto option\nindicates to the task to only run based on some internal check that there\nhas been \"enough\" change in that domain to merit the work. In the case of\nthe 'gc' task, this also reduces the amount of work done. \n\nThe new maintenance tasks in this series are:\n\n * 'loose-objects' : prune packed loose objects, then create a new pack from\n   a batch of loose objects.\n * 'pack-files' : expire redundant packs from the multi-pack-index, then\n   repack using the multi-pack-index's incremental repack strategy.\n * 'prefetch' : fetch from each remote, storing the refs in 'refs/prefetch/\n   /'.\n\nThese tasks are all disabled by default, but can be enabled with config\noptions or run explicitly using \"git maintenance run --task=\". \n\nSince [2] replaced the 'git gc --auto' calls with 'git maintenance run\n--auto' at the end of some Git commands, users could replace the 'gc' task\nwith these lighter-weight changes for foreground maintenance.\n\nThe 'git maintenance' builtin has a 'run' subcommand so it can be extended\nlater with subcommands that manage background maintenance, such as 'start'\nor 'stop'. These are not the subject of this series, as it is important to\nfocus on the maintenance activities themselves. I have a WIP series for this\navailable at [3].\n\n[3] https://github.com/gitgitgadget/git/pull/680\n\nUPDATES since v3 of [1]\n=======================\n\n * The biggest change here is the use of \"test_subcommand\", based on\n   Jonathan Nieder's approach. This requires having the exact command-line\n   figured out, which now requires spelling out all --no- [quiet%7Cprogress] \n   options. I also added a bunch of \"2>/dev/null\" checks because of the\n   isatty(2) calls. Without that, the behavior will change depending on\n   whether the test is run with -x/-v or without.\n   \n   \n * The 0x7FFF/0x7FFFFFFF constant problem is fixed with an EXPENSIVE test\n   that verifies it.\n   \n   \n * The option parsing has changed to use a local struct and pass that struct\n   to the helper methods. This is instead of having a global singleton.\n   \n   \n\nThanks, -Stolee\n\nHere is the range-diff from the v3 of [1].\n\n 1:  12fe73bb72 <  -:  ---------- maintenance: create basic maintenance runner\n 2:  6e533e43d7 <  -:  ---------- maintenance: add --quiet option\n 3:  c4674fc211 <  -:  ---------- maintenance: replace run_auto_gc()\n 4:  b9332c1318 <  -:  ---------- maintenance: initialize task array\n 5:  a4d9836bed <  -:  ---------- maintenance: add commit-graph task\n 6:  dafb0d9bbc <  -:  ---------- maintenance: add --task option\n 7:  1b00524da3 <  -:  ---------- maintenance: take a lock on the objects directory\n 8:  0e94e04dcd =  1:  83401c5200 fetch: optionally allow disabling FETCH_HEAD update\n 9:  9e38ade15c !  2:  85118ed5f1 maintenance: add prefetch task\n    @@ Documentation/git-maintenance.txt: since it will not expire `.graph` files that\n\n      ## builtin/gc.c ##\n     @@\n    - #include \"blob.h\"\n      #include \"tree.h\"\n      #include \"promisor-remote.h\"\n    + #include \"refs.h\"\n     +#include \"remote.h\"\n\n      #define FAILED_RUN \"failed to run %s\"\n\n    -@@ builtin/gc.c: static int maintenance_task_commit_graph(void)\n    +@@ builtin/gc.c: static int maintenance_task_commit_graph(struct maintenance_opts *opts)\n          return 1;\n      }\n\n    -+static int fetch_remote(const char *remote)\n    ++static int fetch_remote(const char *remote, struct maintenance_opts *opts)\n     +{\n     +    struct child_process child = CHILD_PROCESS_INIT;\n     +\n    @@ builtin/gc.c: static int maintenance_task_commit_graph(void)\n     +    strvec_pushl(&child.args, \"fetch\", remote, \"--prune\", \"--no-tags\",\n     +             \"--no-write-fetch-head\", \"--refmap=\", NULL);\n     +\n    -+    strvec_pushf(&child.args, \"+refs/heads/*:refs/prefetch/%s/*\", remote);\n    -+\n    -+    if (opts.quiet)\n    ++    if (opts->quiet)\n     +        strvec_push(&child.args, \"--quiet\");\n     +\n    ++    strvec_pushf(&child.args, \"+refs/heads/*:refs/prefetch/%s/*\", remote);\n    ++\n     +    return !!run_command(&child);\n     +}\n     +\n    @@ builtin/gc.c: static int maintenance_task_commit_graph(void)\n     +    return 0;\n     +}\n     +\n    -+static int maintenance_task_prefetch(void)\n    ++static int maintenance_task_prefetch(struct maintenance_opts *opts)\n     +{\n     +    int result = 0;\n     +    struct string_list_item *item;\n    @@ builtin/gc.c: static int maintenance_task_commit_graph(void)\n     +    for (item = remotes.items;\n     +         item && item < remotes.items + remotes.nr;\n     +         item++)\n    -+        result |= fetch_remote(item->string);\n    ++        result |= fetch_remote(item->string, opts);\n     +\n     +cleanup:\n     +    string_list_clear(&remotes, 0);\n     +    return result;\n     +}\n     +\n    - static int maintenance_task_gc(void)\n    + static int maintenance_task_gc(struct maintenance_opts *opts)\n      {\n          struct child_process child = CHILD_PROCESS_INIT;\n     @@ builtin/gc.c: struct maintenance_task {\n    @@ t/t7900-maintenance.sh: test_expect_success 'run --task duplicate' '\n     +    git -C clone2 switch -c two &&\n     +    test_commit -C clone1 one &&\n     +    test_commit -C clone2 two &&\n    -+    GIT_TRACE2_EVENT=\"$(pwd)/run-prefetch.txt\" git maintenance run --task=prefetch &&\n    -+    grep \",\\\"fetch\\\",\\\"remote1\\\"\" run-prefetch.txt &&\n    -+    grep \",\\\"fetch\\\",\\\"remote2\\\"\" run-prefetch.txt &&\n    ++    GIT_TRACE2_EVENT=\"$(pwd)/run-prefetch.txt\" git maintenance run --task=prefetch 2>/dev/null &&\n    ++    fetchargs=\"--prune --no-tags --no-write-fetch-head --refmap= --quiet\" &&\n    ++    test_subcommand git fetch remote1 $fetchargs +refs/heads/\\\\*:refs/prefetch/remote1/\\\\* <run-prefetch.txt &&\n    ++    test_subcommand git fetch remote2 $fetchargs +refs/heads/\\\\*:refs/prefetch/remote2/\\\\* <run-prefetch.txt &&\n     +    test_path_is_missing .git/refs/remotes &&\n     +    test_cmp clone1/.git/refs/heads/one .git/refs/prefetch/remote1/one &&\n     +    test_cmp clone2/.git/refs/heads/two .git/refs/prefetch/remote2/two &&\n10:  0128fdfd1a !  3:  621375a3c9 maintenance: add loose-objects task\n    @@ Documentation/git-maintenance.txt: gc::\n      --auto::\n\n      ## builtin/gc.c ##\n    -@@ builtin/gc.c: static int maintenance_task_gc(void)\n    +@@ builtin/gc.c: static int maintenance_task_gc(struct maintenance_opts *opts)\n          return run_command(&child);\n      }\n\n    -+static int prune_packed(void)\n    ++static int prune_packed(struct maintenance_opts *opts)\n     +{\n     +    struct child_process child = CHILD_PROCESS_INIT;\n     +\n     +    child.git_cmd = 1;\n     +    strvec_push(&child.args, \"prune-packed\");\n     +\n    -+    if (opts.quiet)\n    ++    if (opts->quiet)\n     +        strvec_push(&child.args, \"--quiet\");\n     +\n     +    return !!run_command(&child);\n    @@ builtin/gc.c: static int maintenance_task_gc(void)\n     +    return ++(d->count) > d->batch_size;\n     +}\n     +\n    -+static int pack_loose(void)\n    ++static int pack_loose(struct maintenance_opts *opts)\n     +{\n     +    struct repository *r = the_repository;\n     +    int result = 0;\n    @@ builtin/gc.c: static int maintenance_task_gc(void)\n     +    pack_proc.git_cmd = 1;\n     +\n     +    strvec_push(&pack_proc.args, \"pack-objects\");\n    -+    if (opts.quiet)\n    ++    if (opts->quiet)\n     +        strvec_push(&pack_proc.args, \"--quiet\");\n     +    strvec_pushf(&pack_proc.args, \"%s/pack/loose\", r->objects->odb->path);\n     +\n    @@ builtin/gc.c: static int maintenance_task_gc(void)\n     +    return result;\n     +}\n     +\n    -+static int maintenance_task_loose_objects(void)\n    ++static int maintenance_task_loose_objects(struct maintenance_opts *opts)\n     +{\n    -+    return prune_packed() || pack_loose();\n    ++    return prune_packed(opts) || pack_loose(opts);\n     +}\n     +\n    - typedef int maintenance_task_fn(void);\n    + typedef int maintenance_task_fn(struct maintenance_opts *opts);\n\n    - struct maintenance_task {\n    + /*\n     @@ builtin/gc.c: struct maintenance_task {\n\n      enum maintenance_task_label {\n17:  6ac3a58f2f !  4:  e787403ea7 maintenance: create auto condition for loose-objects\n    @@ builtin/gc.c: static struct maintenance_task tasks[] = {\n              maintenance_task_loose_objects,\n     +        loose_object_auto_condition,\n          },\n    -     [TASK_INCREMENTAL_REPACK] = {\n    -         \"incremental-repack\",\n    +     [TASK_GC] = {\n    +         \"gc\",\n\n      ## t/t7900-maintenance.sh ##\n     @@ t/t7900-maintenance.sh: test_expect_success 'loose-objects task' '\n    @@ t/t7900-maintenance.sh: test_expect_success 'loose-objects task' '\n     +    git repack -adk &&\n     +    GIT_TRACE2_EVENT=\"$(pwd)/trace-lo1.txt\" \\\n     +        git -c maintenance.loose-objects.auto=1 maintenance \\\n    -+        run --auto --task=loose-objects &&\n    -+    ! grep \"\\\"prune-packed\\\"\" trace-lo1.txt &&\n    ++        run --auto --task=loose-objects 2>/dev/null &&\n    ++    test_subcommand ! git prune-packed --quiet <trace-lo1.txt &&\n     +    for i in 1 2\n     +    do\n     +        printf data-A-$i | git hash-object -t blob --stdin -w &&\n     +        GIT_TRACE2_EVENT=\"$(pwd)/trace-loA-$i\" \\\n     +            git -c maintenance.loose-objects.auto=2 \\\n    -+            maintenance run --auto --task=loose-objects &&\n    -+        ! grep \"\\\"prune-packed\\\"\" trace-loA-$i &&\n    ++            maintenance run --auto --task=loose-objects 2>/dev/null &&\n    ++        test_subcommand ! git prune-packed --quiet <trace-loA-$i &&\n     +        printf data-B-$i | git hash-object -t blob --stdin -w &&\n     +        GIT_TRACE2_EVENT=\"$(pwd)/trace-loB-$i\" \\\n     +            git -c maintenance.loose-objects.auto=2 \\\n    -+            maintenance run --auto --task=loose-objects &&\n    -+        grep \"\\\"prune-packed\\\"\" trace-loB-$i &&\n    ++            maintenance run --auto --task=loose-objects 2>/dev/null &&\n    ++        test_subcommand git prune-packed --quiet <trace-loB-$i &&\n     +        GIT_TRACE2_EVENT=\"$(pwd)/trace-loC-$i\" \\\n     +            git -c maintenance.loose-objects.auto=2 \\\n    -+            maintenance run --auto --task=loose-objects &&\n    -+        grep \"\\\"prune-packed\\\"\" trace-loC-$i || return 1\n    ++            maintenance run --auto --task=loose-objects 2>/dev/null &&\n    ++        test_subcommand git prune-packed --quiet <trace-loC-$i || return 1\n     +    done\n     +'\n     +\n    - test_expect_success 'incremental-repack task' '\n    -     packDir=.git/objects/pack &&\n    -     for i in $(test_seq 1 5)\n    + test_done\n11:  c2baf6e119 =  5:  37e59b1a8d midx: enable core.multiPackIndex by default\n19:  9b4cef7635 =  6:  aba087f663 midx: use start_delayed_progress()\n12:  00f47c4848 !  7:  68727c555b maintenance: add incremental-repack task\n    @@ Documentation/git-maintenance.txt: loose-objects::\n\n      ## builtin/gc.c ##\n     @@\n    - #include \"tree.h\"\n      #include \"promisor-remote.h\"\n    + #include \"refs.h\"\n      #include \"remote.h\"\n     +#include \"midx.h\"\n\n      #define FAILED_RUN \"failed to run %s\"\n\n    -@@ builtin/gc.c: static int maintenance_task_loose_objects(void)\n    -     return prune_packed() || pack_loose();\n    +@@ builtin/gc.c: static int maintenance_task_loose_objects(struct maintenance_opts *opts)\n    +     return prune_packed(opts) || pack_loose(opts);\n      }\n\n    -+static int multi_pack_index_write(void)\n    ++static int multi_pack_index_write(struct maintenance_opts *opts)\n     +{\n     +    struct child_process child = CHILD_PROCESS_INIT;\n     +\n     +    child.git_cmd = 1;\n     +    strvec_pushl(&child.args, \"multi-pack-index\", \"write\", NULL);\n     +\n    -+    if (opts.quiet)\n    ++    if (opts->quiet)\n     +        strvec_push(&child.args, \"--no-progress\");\n     +\n     +    if (run_command(&child))\n    @@ builtin/gc.c: static int maintenance_task_loose_objects(void)\n     +    return 0;\n     +}\n     +\n    -+static int rewrite_multi_pack_index(void)\n    ++static int rewrite_multi_pack_index(struct maintenance_opts *opts)\n     +{\n     +    struct repository *r = the_repository;\n     +    char *midx_name = get_midx_filename(r->objects->odb->path);\n    @@ builtin/gc.c: static int maintenance_task_loose_objects(void)\n     +    unlink(midx_name);\n     +    free(midx_name);\n     +\n    -+    return multi_pack_index_write();\n    ++    return multi_pack_index_write(opts);\n     +}\n     +\n    -+static int multi_pack_index_verify(const char *message)\n    ++static int multi_pack_index_verify(struct maintenance_opts *opts,\n    ++                   const char *message)\n     +{\n     +    struct child_process child = CHILD_PROCESS_INIT;\n     +\n     +    child.git_cmd = 1;\n     +    strvec_pushl(&child.args, \"multi-pack-index\", \"verify\", NULL);\n     +\n    -+    if (opts.quiet)\n    ++    if (opts->quiet)\n     +        strvec_push(&child.args, \"--no-progress\");\n     +\n     +    if (run_command(&child)) {\n    @@ builtin/gc.c: static int maintenance_task_loose_objects(void)\n     +    return 0;\n     +}\n     +\n    -+static int multi_pack_index_expire(void)\n    ++static int multi_pack_index_expire(struct maintenance_opts *opts)\n     +{\n     +    struct child_process child = CHILD_PROCESS_INIT;\n     +\n     +    child.git_cmd = 1;\n     +    strvec_pushl(&child.args, \"multi-pack-index\", \"expire\", NULL);\n     +\n    -+    if (opts.quiet)\n    ++    if (opts->quiet)\n     +        strvec_push(&child.args, \"--no-progress\");\n     +\n     +    close_object_store(the_repository->objects);\n    @@ builtin/gc.c: static int maintenance_task_loose_objects(void)\n     +    return 0;\n     +}\n     +\n    -+static int multi_pack_index_repack(void)\n    ++static int multi_pack_index_repack(struct maintenance_opts *opts)\n     +{\n     +    struct child_process child = CHILD_PROCESS_INIT;\n     +\n     +    child.git_cmd = 1;\n     +    strvec_pushl(&child.args, \"multi-pack-index\", \"repack\", NULL);\n     +\n    -+    if (opts.quiet)\n    ++    if (opts->quiet)\n     +        strvec_push(&child.args, \"--no-progress\");\n     +\n     +    strvec_push(&child.args, \"--batch-size=0\");\n    @@ builtin/gc.c: static int maintenance_task_loose_objects(void)\n     +    return 0;\n     +}\n     +\n    -+static int maintenance_task_incremental_repack(void)\n    ++static int maintenance_task_incremental_repack(struct maintenance_opts *opts)\n     +{\n     +    prepare_repo_settings(the_repository);\n     +    if (!the_repository->settings.core_multi_pack_index) {\n    @@ builtin/gc.c: static int maintenance_task_loose_objects(void)\n     +        return 0;\n     +    }\n     +\n    -+    if (multi_pack_index_write())\n    ++    if (multi_pack_index_write(opts))\n     +        return 1;\n    -+    if (multi_pack_index_verify(\"after initial write\"))\n    -+        return rewrite_multi_pack_index();\n    -+    if (multi_pack_index_expire())\n    ++    if (multi_pack_index_verify(opts, \"after initial write\"))\n    ++        return rewrite_multi_pack_index(opts);\n    ++    if (multi_pack_index_expire(opts))\n     +        return 1;\n    -+    if (multi_pack_index_verify(\"after expire step\"))\n    -+        return !!rewrite_multi_pack_index();\n    -+    if (multi_pack_index_repack())\n    ++    if (multi_pack_index_verify(opts, \"after expire step\"))\n    ++        return !!rewrite_multi_pack_index(opts);\n    ++    if (multi_pack_index_repack(opts))\n     +        return 1;\n    -+    if (multi_pack_index_verify(\"after repack step\"))\n    -+        return !!rewrite_multi_pack_index();\n    ++    if (multi_pack_index_verify(opts, \"after repack step\"))\n    ++        return !!rewrite_multi_pack_index(opts);\n     +    return 0;\n     +}\n     +\n    - typedef int maintenance_task_fn(void);\n    + typedef int maintenance_task_fn(struct maintenance_opts *opts);\n\n    - struct maintenance_task {\n    + /*\n     @@ builtin/gc.c: struct maintenance_task {\n      enum maintenance_task_label {\n          TASK_PREFETCH,\n    @@ builtin/gc.c: struct maintenance_task {\n          TASK_COMMIT_GRAPH,\n\n     @@ builtin/gc.c: static struct maintenance_task tasks[] = {\n    -         \"loose-objects\",\n              maintenance_task_loose_objects,\n    +         loose_object_auto_condition,\n          },\n     +    [TASK_INCREMENTAL_REPACK] = {\n     +        \"incremental-repack\",\n    @@ t/t7900-maintenance.sh: test_description='git maintenance builtin'\n\n      test_expect_success 'help text' '\n          test_expect_code 129 git maintenance -h 2>err &&\n    -@@ t/t7900-maintenance.sh: test_expect_success 'loose-objects task' '\n    -     test_cmp packs-between packs-after\n    +@@ t/t7900-maintenance.sh: test_expect_success 'maintenance.loose-objects.auto' '\n    +     done\n      '\n\n     +test_expect_success 'incremental-repack task' '\n13:  ef2a231956 !  8:  c3487fb8e3 maintenance: auto-size incremental-repack batch\n    @@ Commit message\n         truly want to optimize for space and performance (and are willing to pay\n         the upfront cost of a full repack) can use the 'gc' task to do so.\n\n    +    Create a test for this two gigabyte limit by creating an EXPENSIVE test\n    +    that generates two pack-files of roughly 2.5 gigabytes in size, then\n    +    performs an incremental repack. Check that the --batch-size argument in\n    +    the subcommand uses the hard-coded maximum.\n    +\n    +    Helped-by: Chris Torek <chris.torek@gmail.com>\n         Reported-by: Son Luong Ngoc <sluongng@gmail.com>\n         Signed-off-by: Derrick Stolee <dstolee@microsoft.com>\n\n      ## builtin/gc.c ##\n    -@@ builtin/gc.c: static int multi_pack_index_expire(void)\n    +@@ builtin/gc.c: static int multi_pack_index_expire(struct maintenance_opts *opts)\n          return 0;\n      }\n\n    -+#define TWO_GIGABYTES (0x7FFF)\n    ++#define TWO_GIGABYTES (INT32_MAX)\n     +\n     +static off_t get_auto_pack_size(void)\n     +{\n    @@ builtin/gc.c: static int multi_pack_index_expire(void)\n     +    return result_size;\n     +}\n     +\n    - static int multi_pack_index_repack(void)\n    + static int multi_pack_index_repack(struct maintenance_opts *opts)\n      {\n          struct child_process child = CHILD_PROCESS_INIT;\n    -@@ builtin/gc.c: static int multi_pack_index_repack(void)\n    -     if (opts.quiet)\n    +@@ builtin/gc.c: static int multi_pack_index_repack(struct maintenance_opts *opts)\n    +     if (opts->quiet)\n              strvec_push(&child.args, \"--no-progress\");\n\n     -    strvec_push(&child.args, \"--batch-size=0\");\n    @@ t/t7900-maintenance.sh: test_expect_success 'incremental-repack task' '\n          ls .git/objects/pack/*.pack >packs-after &&\n     -    test_line_count = 1 packs-after\n     +    test_line_count = 2 packs-after\n    ++'\n    ++\n    ++test_expect_success EXPENSIVE 'incremental-repack 2g limit' '\n    ++    for i in $(test_seq 1 5)\n    ++    do\n    ++        test-tool genrandom foo$i $((512 * 1024 * 1024 + 1)) >>big ||\n    ++        return 1\n    ++    done &&\n    ++    git add big &&\n    ++    git commit -m \"Add big file (1)\" &&\n    ++\n    ++    # ensure any possible loose objects are in a pack-file\n    ++    git maintenance run --task=loose-objects &&\n    ++\n    ++    rm big &&\n    ++    for i in $(test_seq 6 10)\n    ++    do\n    ++        test-tool genrandom foo$i $((512 * 1024 * 1024 + 1)) >>big ||\n    ++        return 1\n    ++    done &&\n    ++    git add big &&\n    ++    git commit -m \"Add big file (2)\" &&\n    ++\n    ++    # ensure any possible loose objects are in a pack-file\n    ++    git maintenance run --task=loose-objects &&\n    ++\n    ++    # Now run the incremental-repack task and check the batch-size\n    ++    GIT_TRACE2_EVENT=\"$(pwd)/run-2g.txt\" git maintenance run \\\n    ++        --task=incremental-repack 2>/dev/null &&\n    ++    test_subcommand git multi-pack-index repack \\\n    ++         --no-progress --batch-size=2147483647 <run-2g.txt\n      '\n\n      test_done\n14:  99840c4b8f <  -:  ---------- maintenance: create maintenance.<task>.enabled config\n15:  a087c63572 <  -:  ---------- maintenance: use pointers to check --auto\n16:  ef3a854508 <  -:  ---------- maintenance: add auto condition for commit-graph task\n18:  801b262d1c !  9:  407c123c51 maintenance: add incremental-repack auto condition\n    @@ Documentation/config/maintenance.txt: maintenance.loose-objects.auto::\n\n      ## builtin/gc.c ##\n     @@\n    + #include \"refs.h\"\n      #include \"remote.h\"\n      #include \"midx.h\"\n    - #include \"refs.h\"\n     +#include \"object-store.h\"\n\n      #define FAILED_RUN \"failed to run %s\"\n\n    -@@ builtin/gc.c: static int maintenance_task_loose_objects(void)\n    -     return prune_packed() || pack_loose();\n    +@@ builtin/gc.c: static int maintenance_task_loose_objects(struct maintenance_opts *opts)\n    +     return prune_packed(opts) || pack_loose(opts);\n      }\n\n     +static int incremental_repack_auto_condition(void)\n    @@ builtin/gc.c: static int maintenance_task_loose_objects(void)\n     +    return count >= incremental_repack_auto_limit;\n     +}\n     +\n    - static int multi_pack_index_write(void)\n    + static int multi_pack_index_write(struct maintenance_opts *opts)\n      {\n          struct child_process child = CHILD_PROCESS_INIT;\n     @@ builtin/gc.c: static struct maintenance_task tasks[] = {\n    @@ builtin/gc.c: static struct maintenance_task tasks[] = {\n\n      ## t/t7900-maintenance.sh ##\n     @@ t/t7900-maintenance.sh: test_expect_success 'incremental-repack task' '\n    -     test_line_count = 2 packs-after\n    + '\n    + \n    + test_expect_success EXPENSIVE 'incremental-repack 2g limit' '\n    ++\n    +     for i in $(test_seq 1 5)\n    +     do\n    +         test-tool genrandom foo$i $((512 * 1024 * 1024 + 1)) >>big ||\n    +@@ t/t7900-maintenance.sh: test_expect_success EXPENSIVE 'incremental-repack 2g limit' '\n    +          --no-progress --batch-size=2147483647 <run-2g.txt\n      '\n\n     +test_expect_success 'maintenance.incremental-repack.auto' '\n     +    git repack -adk &&\n     +    git config core.multiPackIndex true &&\n     +    git multi-pack-index write &&\n    -+    GIT_TRACE2_EVENT=1 git -c maintenance.incremental-repack.auto=1 \\\n    -+        maintenance run --auto --task=incremental-repack >out &&\n    -+    ! grep \"\\\"multi-pack-index\\\"\" out &&\n    ++    GIT_TRACE2_EVENT=\"$(pwd)/midx-init.txt\" git \\\n    ++        -c maintenance.incremental-repack.auto=1 \\\n    ++        maintenance run --auto --task=incremental-repack 2>/dev/null &&\n    ++    test_subcommand ! git multi-pack-index write --no-progress <midx-init.txt &&\n     +    for i in 1 2\n     +    do\n     +        test_commit A-$i &&\n    @@ t/t7900-maintenance.sh: test_expect_success 'incremental-repack task' '\n     +        EOF\n     +        GIT_TRACE2_EVENT=$(pwd)/trace-A-$i git \\\n     +            -c maintenance.incremental-repack.auto=2 \\\n    -+            maintenance run --auto --task=incremental-repack &&\n    -+        ! grep \"\\\"multi-pack-index\\\"\" trace-A-$i &&\n    ++            maintenance run --auto --task=incremental-repack 2>/dev/null &&\n    ++        test_subcommand ! git multi-pack-index write --no-progress <trace-A-$i &&\n     +        test_commit B-$i &&\n     +        git pack-objects --revs .git/objects/pack/pack <<-\\EOF &&\n     +        HEAD\n    @@ t/t7900-maintenance.sh: test_expect_success 'incremental-repack task' '\n     +        EOF\n     +        GIT_TRACE2_EVENT=$(pwd)/trace-B-$i git \\\n     +            -c maintenance.incremental-repack.auto=2 \\\n    -+            maintenance run --auto --task=incremental-repack >out &&\n    -+        grep \"\\\"multi-pack-index\\\"\" trace-B-$i >/dev/null || return 1\n    ++            maintenance run --auto --task=incremental-repack 2>/dev/null &&\n    ++        test_subcommand git multi-pack-index write --no-progress <trace-B-$i || return 1\n     +    done\n     +'\n     +\n20:  39eb83ad1e <  -:  ---------- maintenance: add trace2 regions for task execution\n\nDerrick Stolee (8):\n  maintenance: add prefetch task\n  maintenance: add loose-objects task\n  maintenance: create auto condition for loose-objects\n  midx: enable core.multiPackIndex by default\n  midx: use start_delayed_progress()\n  maintenance: add incremental-repack task\n  maintenance: auto-size incremental-repack batch\n  maintenance: add incremental-repack auto condition\n\nJunio C Hamano (1):\n  fetch: optionally allow disabling FETCH_HEAD update\n\n Documentation/config/core.txt        |   4 +-\n Documentation/config/fetch.txt       |   7 +\n Documentation/config/maintenance.txt |  18 ++\n Documentation/fetch-options.txt      |  10 +\n Documentation/git-maintenance.txt    |  41 +++\n builtin/fetch.c                      |  19 +-\n builtin/gc.c                         | 364 +++++++++++++++++++++++++++\n builtin/pull.c                       |   3 +-\n midx.c                               |  23 +-\n midx.h                               |   1 +\n repo-settings.c                      |   6 +\n repository.h                         |   2 +\n t/t5319-multi-pack-index.sh          |  15 +-\n t/t5510-fetch.sh                     |  39 ++-\n t/t5521-pull-options.sh              |  16 ++\n t/t7900-maintenance.sh               | 191 ++++++++++++++\n 16 files changed, 730 insertions(+), 29 deletions(-)\n\n\nbase-commit: a5d19148460decaf08e0e6293e996d42ff3f2d32\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-696%2Fderrickstolee%2Fmaintenance%2Fgc-v1\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-696/derrickstolee/maintenance/gc-v1\nPull-Request: https://github.com/gitgitgadget/git/pull/696\n-- \ngitgitgadget\n"},{"id":"403078","messageId":"83401c52002716084b9c53a77c9d57b6009f84e2.1596731424.git.gitgitgadget@gmail.com","threadId":"54001","inReplyTo":"pull.696.git.1596731424.gitgitgadget@gmail.com","subject":"[PATCH 1/9] fetch: optionally allow disabling FETCH_HEAD update","fromName":"Junio C Hamano via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2020-08-06T16:30:16Z","receivedAt":"2020-08-06T17:53:40Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"From: Junio C Hamano <gitster@pobox.com>\n\nIf you run fetch but record the result in remote-tracking branches,\nand either if you do nothing with the fetched refs (e.g. you are\nmerely mirroring) or if you always work from the remote-tracking\nrefs (e.g. you fetch and then merge origin/branchname separately),\nyou can get away with having no FETCH_HEAD at all.\n\nTeach \"git fetch\" a command line option \"--[no-]write-fetch-head\"\nand \"fetch.writeFetchHEAD\" configuration variable.  Without either,\nthe default is to write FETCH_HEAD, and the usual rule that the\ncommand line option defeats configured default applies.\n\nNote that under \"--dry-run\" mode, FETCH_HEAD is never written;\notherwise you'd see list of objects in the file that you do not\nactually have.  Passing `--write-fetch-head` does not force `git\nfetch` to write the file.\n\nAlso note that this option is explicitly passed when \"git pull\"\ninternally invokes \"git fetch\", so that those who configured their\n\"git fetch\" not to write FETCH_HEAD would not be able to break the\ncooperation between these two commands.  \"git pull\" must see what\n\"git fetch\" got recorded in FETCH_HEAD to work correctly.\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Derrick Stolee <dstolee@microsoft.com>\n---\n Documentation/config/fetch.txt  |  7 ++++++\n Documentation/fetch-options.txt | 10 +++++++++\n builtin/fetch.c                 | 19 +++++++++++++---\n builtin/pull.c                  |  3 ++-\n t/t5510-fetch.sh                | 39 +++++++++++++++++++++++++++++++--\n t/t5521-pull-options.sh         | 16 ++++++++++++++\n 6 files changed, 88 insertions(+), 6 deletions(-)\n\ndiff --git a/Documentation/config/fetch.txt b/Documentation/config/fetch.txt\nindex b20394038d..0aaa05e8c0 100644\n--- a/Documentation/config/fetch.txt\n+++ b/Documentation/config/fetch.txt\n@@ -91,3 +91,10 @@ fetch.writeCommitGraph::\n \tmerge and the write may take longer. Having an updated commit-graph\n \tfile helps performance of many Git commands, including `git merge-base`,\n \t`git push -f`, and `git log --graph`. Defaults to false.\n+\n+fetch.writeFetchHEAD::\n+\tSetting it to false tells `git fetch` not to write the list\n+\tof remote refs fetched in the `FETCH_HEAD` file directly\n+\tunder `$GIT_DIR`.  Can be countermanded from the command\n+\tline with the `--[no-]write-fetch-head` option.  Defaults to\n+\ttrue.\ndiff --git a/Documentation/fetch-options.txt b/Documentation/fetch-options.txt\nindex 495bc8ab5a..6972ad2522 100644\n--- a/Documentation/fetch-options.txt\n+++ b/Documentation/fetch-options.txt\n@@ -64,6 +64,16 @@ documented in linkgit:git-config[1].\n --dry-run::\n \tShow what would be done, without making any changes.\n \n+ifndef::git-pull[]\n+--[no-]write-fetch-head::\n+\tWrite the list of remote refs fetched in the `FETCH_HEAD`\n+\tfile directly under `$GIT_DIR`.  This is the default unless\n+\tthe configuration variable `fetch.writeFetchHEAD` is set to\n+\tfalse.  Passing `--no-write-fetch-head` from the command\n+\tline tells Git not to write the file.  Under `--dry-run`\n+\toption, the file is never written.\n+endif::git-pull[]\n+\n -f::\n --force::\n \tWhen 'git fetch' is used with `<src>:<dst>` refspec it may\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex c8b9366d3c..42ca774ad1 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -56,6 +56,7 @@ static int prune_tags = -1; /* unspecified */\n #define PRUNE_TAGS_BY_DEFAULT 0 /* do we prune tags by default? */\n \n static int all, append, dry_run, force, keep, multiple, update_head_ok;\n+static int write_fetch_head = 1;\n static int verbosity, deepen_relative, set_upstream;\n static int progress = -1;\n static int enable_auto_gc = 1;\n@@ -118,6 +119,10 @@ static int git_fetch_config(const char *k, const char *v, void *cb)\n \t\treturn 0;\n \t}\n \n+\tif (!strcmp(k, \"fetch.writefetchhead\")) {\n+\t\twrite_fetch_head = git_config_bool(k, v);\n+\t\treturn 0;\n+\t}\n \treturn git_default_config(k, v, cb);\n }\n \n@@ -162,6 +167,8 @@ static struct option builtin_fetch_options[] = {\n \t\t    PARSE_OPT_OPTARG, option_fetch_parse_recurse_submodules),\n \tOPT_BOOL(0, \"dry-run\", &dry_run,\n \t\t N_(\"dry run\")),\n+\tOPT_BOOL(0, \"write-fetch-head\", &write_fetch_head,\n+\t\t N_(\"write fetched references to the FETCH_HEAD file\")),\n \tOPT_BOOL('k', \"keep\", &keep, N_(\"keep downloaded pack\")),\n \tOPT_BOOL('u', \"update-head-ok\", &update_head_ok,\n \t\t    N_(\"allow updating of HEAD ref\")),\n@@ -895,7 +902,9 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,\n \tconst char *what, *kind;\n \tstruct ref *rm;\n \tchar *url;\n-\tconst char *filename = dry_run ? \"/dev/null\" : git_path_fetch_head(the_repository);\n+\tconst char *filename = (!write_fetch_head\n+\t\t\t\t? \"/dev/null\"\n+\t\t\t\t: git_path_fetch_head(the_repository));\n \tint want_status;\n \tint summary_width = transport_summary_width(ref_map);\n \n@@ -1329,7 +1338,7 @@ static int do_fetch(struct transport *transport,\n \t}\n \n \t/* if not appending, truncate FETCH_HEAD */\n-\tif (!append && !dry_run) {\n+\tif (!append && write_fetch_head) {\n \t\tretcode = truncate_fetch_head();\n \t\tif (retcode)\n \t\t\tgoto cleanup;\n@@ -1596,7 +1605,7 @@ static int fetch_multiple(struct string_list *list, int max_children)\n \tint i, result = 0;\n \tstruct strvec argv = STRVEC_INIT;\n \n-\tif (!append && !dry_run) {\n+\tif (!append && write_fetch_head) {\n \t\tint errcode = truncate_fetch_head();\n \t\tif (errcode)\n \t\t\treturn errcode;\n@@ -1797,6 +1806,10 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)\n \tif (depth || deepen_since || deepen_not.nr)\n \t\tdeepen = 1;\n \n+\t/* FETCH_HEAD never gets updated in --dry-run mode */\n+\tif (dry_run)\n+\t\twrite_fetch_head = 0;\n+\n \tif (all) {\n \t\tif (argc == 1)\n \t\t\tdie(_(\"fetch --all does not take a repository argument\"));\ndiff --git a/builtin/pull.c b/builtin/pull.c\nindex 015f6ded0b..5ef3434e1f 100644\n--- a/builtin/pull.c\n+++ b/builtin/pull.c\n@@ -527,7 +527,8 @@ static int run_fetch(const char *repo, const char **refspecs)\n \tstruct strvec args = STRVEC_INIT;\n \tint ret;\n \n-\tstrvec_pushl(&args, \"fetch\", \"--update-head-ok\", NULL);\n+\tstrvec_pushl(&args, \"fetch\", \"--update-head-ok\",\n+\t\t     \"--write-fetch-head\", NULL);\n \n \t/* Shared options */\n \targv_push_verbosity(&args);\ndiff --git a/t/t5510-fetch.sh b/t/t5510-fetch.sh\nindex 9850ecde5d..31c91d0ed2 100755\n--- a/t/t5510-fetch.sh\n+++ b/t/t5510-fetch.sh\n@@ -539,13 +539,48 @@ test_expect_success 'fetch into the current branch with --update-head-ok' '\n \n '\n \n-test_expect_success 'fetch --dry-run' '\n-\n+test_expect_success 'fetch --dry-run does not touch FETCH_HEAD' '\n \trm -f .git/FETCH_HEAD &&\n \tgit fetch --dry-run . &&\n \t! test -f .git/FETCH_HEAD\n '\n \n+test_expect_success '--no-write-fetch-head does not touch FETCH_HEAD' '\n+\trm -f .git/FETCH_HEAD &&\n+\tgit fetch --no-write-fetch-head . &&\n+\t! test -f .git/FETCH_HEAD\n+'\n+\n+test_expect_success '--write-fetch-head gets defeated by --dry-run' '\n+\trm -f .git/FETCH_HEAD &&\n+\tgit fetch --dry-run --write-fetch-head . &&\n+\t! test -f .git/FETCH_HEAD\n+'\n+\n+test_expect_success 'fetch.writeFetchHEAD and FETCH_HEAD' '\n+\trm -f .git/FETCH_HEAD &&\n+\tgit -c fetch.writeFetchHEAD=no fetch . &&\n+\t! test -f .git/FETCH_HEAD\n+'\n+\n+test_expect_success 'fetch.writeFetchHEAD gets defeated by --dry-run' '\n+\trm -f .git/FETCH_HEAD &&\n+\tgit -c fetch.writeFetchHEAD=yes fetch --dry-run . &&\n+\t! test -f .git/FETCH_HEAD\n+'\n+\n+test_expect_success 'fetch.writeFetchHEAD and --no-write-fetch-head' '\n+\trm -f .git/FETCH_HEAD &&\n+\tgit -c fetch.writeFetchHEAD=yes fetch --no-write-fetch-head . &&\n+\t! test -f .git/FETCH_HEAD\n+'\n+\n+test_expect_success 'fetch.writeFetchHEAD and --write-fetch-head' '\n+\trm -f .git/FETCH_HEAD &&\n+\tgit -c fetch.writeFetchHEAD=no fetch --write-fetch-head . &&\n+\ttest -f .git/FETCH_HEAD\n+'\n+\n test_expect_success \"should be able to fetch with duplicate refspecs\" '\n \tmkdir dups &&\n \t(\ndiff --git a/t/t5521-pull-options.sh b/t/t5521-pull-options.sh\nindex 159afa7ac8..1acae3b9a4 100755\n--- a/t/t5521-pull-options.sh\n+++ b/t/t5521-pull-options.sh\n@@ -77,6 +77,7 @@ test_expect_success 'git pull -q -v --no-rebase' '\n \ttest_must_be_empty out &&\n \ttest -s err)\n '\n+\n test_expect_success 'git pull --cleanup errors early on invalid argument' '\n \tmkdir clonedcleanup &&\n \t(cd clonedcleanup && git init &&\n@@ -85,6 +86,21 @@ test_expect_success 'git pull --cleanup errors early on invalid argument' '\n \ttest -s err)\n '\n \n+test_expect_success 'git pull --no-write-fetch-head fails' '\n+\tmkdir clonedwfh &&\n+\t(cd clonedwfh && git init &&\n+\ttest_must_fail git pull --no-write-fetch-head \"../parent\" >out 2>err &&\n+\ttest_must_be_empty out &&\n+\ttest_i18ngrep \"no-write-fetch-head\" err)\n+'\n+\n+test_expect_success 'git pull succeeds with fetch.writeFetchHEAD=false' '\n+\tmkdir clonedwfhconfig &&\n+\t(cd clonedwfhconfig && git init &&\n+\tgit config fetch.writeFetchHEAD false &&\n+\tgit pull \"../parent\" >out 2>err &&\n+\tgrep FETCH_HEAD err)\n+'\n \n test_expect_success 'git pull --force' '\n \tmkdir clonedoldstyle &&\n-- \ngitgitgadget\n\n"},{"id":"403084","messageId":"eb7fd1da-51fd-60a3-89ba-9d1f0a493698@gmail.com","threadId":"54001","inReplyTo":"6FA8F54A-C92D-497B-895F-AC6E8287AACD@gmail.com","subject":"Re: [PATCH 8/9] maintenance: auto-size incremental-repack batch","fromName":"Derrick Stolee","fromEmail":"stolee@gmail.com","sentAt":"2020-08-06T18:13:26Z","receivedAt":"2020-08-06T18:28:35Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"On 8/6/2020 1:02 PM, Son Luong Ngoc wrote:\n> Hi Derrick,\n> \n>> On Aug 6, 2020, at 18:30, Derrick Stolee via GitGitGadget <gitgitgadget@gmail.com> wrote:\n>>\n>> From: Derrick Stolee <dstolee@microsoft.com>\n>>\n>> When repacking during the 'incremental-repack' task, we use the\n>> --batch-size option in 'git multi-pack-index repack'. The initial setting\n>> used --batch-size=0 to repack everything into a single pack-file. This is\n>> not sustainable for a large repository. The amount of work required is\n>> also likely to use too many system resources for a background job.\n>>\n>> Update the 'incremental-repack' task by dynamically computing a\n>> --batch-size option based on the current pack-file structure.\n>>\n>> The dynamic default size is computed with this idea in mind for a client\n>> repository that was cloned from a very large remote: there is likely one\n>> \"big\" pack-file that was created at clone time. Thus, do not try\n>> repacking it as it is likely packed efficiently by the server.\n>>\n>> Instead, we select the second-largest pack-file, and create a batch size\n>> that is one larger than that pack-file. If there are three or more\n>> pack-files, then this guarantees that at least two will be combined into\n>> a new pack-file.\n> \n> I have been using this strategy with git-care.sh [1] with large success.\n> However it worth to note that there are still edge case where I observed that\n> pack count keep increasing because using '--batch-size=<second-biggest-pack>+1'\n> did not resulted in any repacking.\n> In one case, I have observed a local copy went up to 160+ packs without being able\n> to repack.\n> \n> I have been considering whether a strategy such as falling back to the '(3rd biggest\n> pack size) + 1' and 4th and 5th and so on... when midx repack call resulted in no-op,\n> as that was how I fixed my repo when the edge case happen.\n> \n> Such strategy would require a way to detect midx repack to signal when no-op happen,\n> so something like 'git multi-pack-index repack --batch-size=123456 --exit-code' would\n> be much desirable.\n> \n>>\n>> Of course, this means that the second-largest pack-file size is likely\n>> to grow over time and may eventually surpass the initially-cloned\n>> pack-file. Recall that the pack-file batch is selected in a greedy\n>> manner: the packs are considered from oldest to newest and are selected\n>> if they have size smaller than the batch size until the total selected\n>> size is larger than the batch size. Thus, that oldest \"clone\" pack will\n>> be first to repack after the new data creates a pack larger than that.\n>>\n>> We also want to place some limits on how large these pack-files become,\n>> in order to bound the amount of time spent repacking. A maximum\n>> batch-size of two gigabytes means that large repositories will never be\n>> packed into a single pack-file using this job, but also that repack is\n>> rather expensive. This is a trade-off that is valuable to have if the\n>> maintenance is being run automatically or in the background. Users who\n>> truly want to optimize for space and performance (and are willing to pay\n>> the upfront cost of a full repack) can use the 'gc' task to do so.\n>>\n>> Create a test for this two gigabyte limit by creating an EXPENSIVE test\n>> that generates two pack-files of roughly 2.5 gigabytes in size, then\n>> performs an incremental repack. Check that the --batch-size argument in\n>> the subcommand uses the hard-coded maximum.\n>>\n>> Helped-by: Chris Torek <chris.torek@gmail.com>\n>> Reported-by: Son Luong Ngoc <sluongng@gmail.com>\n>> Signed-off-by: Derrick Stolee <dstolee@microsoft.com>\n> \n> Generally, I have found working with '--batch-size' to be a bit unpredictable.\n> I wonder if we could tweak the behavior somewhat so that its more consistent\n> to use and test?\n\nThanks for continuing to test with this model. My experience has been limited\nto the --batch-size=2g option on repos that typically have >15gb of commit\nand tree data in their pack directory.\n\nOne bit of unpredictability that we've seen is that the --batch-size uses\nthe \"expected size\" of a pack-file to select if it should be repacked. This\nonly tracks the objects that are referenced in that pack, so when we have\nnew packs that were \"un-thinned\" by duplicating delta-bases, those affect\nthe expected size of a repack.\n\nHere is a great reason to have this series be split. While part I stabilizes,\nwe can take the time to re-evaluate this strategy. It might require updating\nthe multi-pack-index builtin itself.\n\nOne thing to think about is to focus on the different possible sizes of a\nrepository. If the entire pack-directory is small, then we might as well\nrepack everything. (What should the limit be? 2gb? configurable?) In the\ncase of a larger directory, should we just use the --batch-size logic with\nthat limit, or should we create a new option for which packs to repack?\n\nFor example, the recent simulation [1] of downloading fetch packs and\nrunning this maintenance did see extra space that could be recovered with\nthe current logic. However, what if we could specify \"repack everything\nexcept the largest pack\" exactly? That would do exactly what this task\nis _intending_, as long as that resulting pack-file does not exceed the\n2gb maximum by too much.\n\n[1] https://lore.kernel.org/git/d50fbb33-9be3-1c48-2277-8bf894df734f@gmail.com/\n\nI will think more on this. I'm open to alternate strategies.\n\nThanks,\n-Stolee\n"},{"id":"403517","messageId":"20200812231021.GG2965447@google.com","threadId":"54001","inReplyTo":"83401c52002716084b9c53a77c9d57b6009f84e2.1596731424.git.gitgitgadget@gmail.com","subject":"Re: [PATCH 1/9] fetch: optionally allow disabling FETCH_HEAD update","fromName":"Emily Shaffer","fromEmail":"emilyshaffer@google.com","sentAt":"2020-08-12T23:10:21Z","receivedAt":"2020-08-12T23:10:29Z","isPatch":true,"sender":{"key":"nasamuffin@google.com","avatar":"https://avatars.githubusercontent.com/u/1606826?v=4"},"body":"[Note: like last time, this was the subject of our team's review club\nthis week, so I'll try to cite individual comments. -Emily]\n\nOn Thu, Aug 06, 2020 at 04:30:16PM +0000, Junio C Hamano via GitGitGadget wrote:\n> \n> +fetch.writeFetchHEAD::\n> +\tSetting it to false tells `git fetch` not to write the list\n> +\tof remote refs fetched in the `FETCH_HEAD` file directly\n> +\tunder `$GIT_DIR`.  Can be countermanded from the command\n> +\tline with the `--[no-]write-fetch-head` option.  Defaults to\n> +\ttrue.\n\n[jrnieder] Is providing a config option creating more trouble than\nbenefit? Is this a burden on script authors that they need to check the\nconfig state, when instead they could just pass the flag? Or rather,\nbecause of the config, the script author has to pass the flag explicitly\nanyways.\n[emily] removing the config also clears up the confusion around 'git pull' +\n'fetch.writeFetchHEAD' I commented on later.\n\n> @@ -895,7 +902,9 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,\n>  \tconst char *what, *kind;\n>  \tstruct ref *rm;\n>  \tchar *url;\n> -\tconst char *filename = dry_run ? \"/dev/null\" : git_path_fetch_head(the_repository);\n> +\tconst char *filename = (!write_fetch_head\n> +\t\t\t\t? \"/dev/null\"\n> +\t\t\t\t: git_path_fetch_head(the_repository));\n\n[emily] Huh. so what does the dry_run codepath look like now? It looks\nlike it's been superseded by write_fetch_head but the option parse\ndoesn't tell dry_run to update write_fetch_head instead. (Found the\nanswer later, see below)\n\n> @@ -1797,6 +1806,10 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)\n>  \tif (depth || deepen_since || deepen_not.nr)\n>  \t\tdeepen = 1;\n>  \n> +\t/* FETCH_HEAD never gets updated in --dry-run mode */\n> +\tif (dry_run)\n> +\t\twrite_fetch_head = 0;\n\n[emily] Aha, here is where dry_run informs write_fetch_head. I wonder if\nthere's a way to instead tell the options struct that --dry-run ~=\n--write-fetch-head?\n\n> +test_expect_success '--write-fetch-head gets defeated by --dry-run' '\n> +\trm -f .git/FETCH_HEAD &&\n> +\tgit fetch --dry-run --write-fetch-head . &&\n> +\t! test -f .git/FETCH_HEAD\n> +'\n\n[emily] Would it make more sense to make these args mutually exclusive?\n[jrnieder] If someone specifies both, then they probably want to say\n\"show me what I would write to FETCH_HEAD but don't actually do that\" -\nwhich isn't info that we print anyways, right now.\n\n> +\n> +test_expect_success 'fetch.writeFetchHEAD and FETCH_HEAD' '\n> +\trm -f .git/FETCH_HEAD &&\n> +\tgit -c fetch.writeFetchHEAD=no fetch . &&\n> +\t! test -f .git/FETCH_HEAD\n> +'\n> +\n> +test_expect_success 'fetch.writeFetchHEAD gets defeated by --dry-run' '\n> +\trm -f .git/FETCH_HEAD &&\n> +\tgit -c fetch.writeFetchHEAD=yes fetch --dry-run . &&\n> +\t! test -f .git/FETCH_HEAD\n> +'\n> +\n> +test_expect_success 'fetch.writeFetchHEAD and --no-write-fetch-head' '\n> +\trm -f .git/FETCH_HEAD &&\n> +\tgit -c fetch.writeFetchHEAD=yes fetch --no-write-fetch-head . &&\n> +\t! test -f .git/FETCH_HEAD\n> +'\n> +\n> +test_expect_success 'fetch.writeFetchHEAD and --write-fetch-head' '\n> +\trm -f .git/FETCH_HEAD &&\n> +\tgit -c fetch.writeFetchHEAD=no fetch --write-fetch-head . &&\n> +\ttest -f .git/FETCH_HEAD\n> +'\n[jrnieder] Thanks for being thorough about these.\n\n> +test_expect_success 'git pull --no-write-fetch-head fails' '\n> +\tmkdir clonedwfh &&\n> +\t(cd clonedwfh && git init &&\n> +\ttest_must_fail git pull --no-write-fetch-head \"../parent\" >out 2>err &&\n> +\ttest_must_be_empty out &&\n> +\ttest_i18ngrep \"no-write-fetch-head\" err)\n\nShould this be shown as a usage error instead of a die()?\n\n> +'\n> +\n> +test_expect_success 'git pull succeeds with fetch.writeFetchHEAD=false' '\n> +\tmkdir clonedwfhconfig &&\n> +\t(cd clonedwfhconfig && git init &&\n> +\tgit config fetch.writeFetchHEAD false &&\n> +\tgit pull \"../parent\" >out 2>err &&\n> +\tgrep FETCH_HEAD err)\n\n[emily] I guess it's a little surprising that my config is being overridden\nwithout any warning... although pull can't possibly work if FETCH_HEAD\nisn't updated. I am conflicted about this use case..\n[jrnieder] This is another argument to drop the config from this patch,\nunless we are thinking of changing the default behavior of\n--[no-]write-fetch-head.\n\n"},{"id":"403518","messageId":"20200812231036.GH2965447@google.com","threadId":"54001","inReplyTo":"85118ed5f19468d5051dd1579e35cae3c3114d24.1596731425.git.gitgitgadget@gmail.com","subject":"Re: [PATCH 2/9] maintenance: add prefetch task","fromName":"Emily Shaffer","fromEmail":"emilyshaffer@google.com","sentAt":"2020-08-12T23:10:36Z","receivedAt":"2020-08-12T23:10:43Z","isPatch":true,"sender":{"key":"nasamuffin@google.com","avatar":"https://avatars.githubusercontent.com/u/1606826?v=4"},"body":"On Thu, Aug 06, 2020 at 04:30:17PM +0000, Derrick Stolee via GitGitGadget wrote:\n> \n> 3. By adding a new refspec \"+refs/heads/*:refs/prefetch/<remote>/*\"\n>    we can ensure that we actually load the new values somewhere in\n>    our refspace while not updating refs/heads or refs/remotes. By\n>    storing these refs here, the commit-graph job will update the\n>    commit-graph with the commits from these hidden refs.\n\n[emily] How does the content of refs/prefetch/* get used?\n[jrnieder] How does the content of refs/prefetch/* get cleaned up?\n[jonathantan] refs/prefetch/* will get used in negotiation so it is\nuseful to keep these.\n> \n> 4. --prune will delete the refs/prefetch/<remote> refs that no\n>    longer appear on the remote.\n[jrnieder] this is what cleans up refs/prefetch/* later :)\n\n> +static int fetch_remote(const char *remote, struct maintenance_opts *opts)\n> +{\n> +\tstruct child_process child = CHILD_PROCESS_INIT;\n> +\n> +\tchild.git_cmd = 1;\n> +\tstrvec_pushl(&child.args, \"fetch\", remote, \"--prune\", \"--no-tags\",\n> +\t\t     \"--no-write-fetch-head\", \"--refmap=\", NULL);\n[jonathantan] It would be good to pass --recurse-submodules=no.\n[jrnieder] Since we are specifying the refmap here, if we were to\nrecurse into submodules, we'd have to be careful about making sure\nrefmap gets propagated correctly.\n\n> +static int fill_each_remote(struct remote *remote, void *cbdata)\n[jrnieder] Since this is a callback that happens for each remote, maybe\nthis should be named to indicate it only fills one remote at a time\ninstead. Nit :)\n> +{\n> +\tstruct string_list *remotes = (struct string_list *)cbdata;\n> +\n> +\tstring_list_append(remotes, remote->name);\n> +\treturn 0;\n> +}\n> +\n> +static int maintenance_task_prefetch(struct maintenance_opts *opts)\n> +{\n> +\tint result = 0;\n> +\tstruct string_list_item *item;\n> +\tstruct string_list remotes = STRING_LIST_INIT_DUP;\n> +\n> +\tif (for_each_remote(fill_each_remote, &remotes)) {\n> +\t\terror(_(\"failed to fill remotes\"));\n> +\t\tresult = 1;\n> +\t\tgoto cleanup;\n> +\t}\n> +\n> +\tfor (item = remotes.items;\n> +\t     item && item < remotes.items + remotes.nr;\n> +\t     item++)\n\nIs there a reason not to use for_each_string_list_item() instead? This\nwould be more brief and also easier to read (less thinking about what\nthe loop is doing).\n\n> +\t\tresult |= fetch_remote(item->string, opts);\n> +\n> +cleanup:\n> +\tstring_list_clear(&remotes, 0);\n> +\treturn result;\n> +}\n> +\n\n>  enum maintenance_task_label {\n> +\tTASK_PREFETCH,\n[jrnieder] Nit: Is there a sort order for these? Should we establish an order\nearly on (e.g. alphabetical)?\n>  \tTASK_GC,\n>  \tTASK_COMMIT_GRAPH,\n\n> +test_expect_success 'prefetch multiple remotes' '\n> +\tgit clone . clone1 &&\n> +\tgit clone . clone2 &&\n> +\tgit remote add remote1 \"file://$(pwd)/clone1\" &&\n> +\tgit remote add remote2 \"file://$(pwd)/clone2\" &&\n> +\tgit -C clone1 switch -c one &&\n> +\tgit -C clone2 switch -c two &&\n> +\ttest_commit -C clone1 one &&\n> +\ttest_commit -C clone2 two &&\n> +\tGIT_TRACE2_EVENT=\"$(pwd)/run-prefetch.txt\" git maintenance run --task=prefetch 2>/dev/null &&\n> +\tfetchargs=\"--prune --no-tags --no-write-fetch-head --refmap= --quiet\" &&\n> +\ttest_subcommand git fetch remote1 $fetchargs +refs/heads/\\\\*:refs/prefetch/remote1/\\\\* <run-prefetch.txt &&\n[jrnieder] In practice, why were all the \\\\ needed? Trying to figure out\nwhere Git is using a shell that would need the * escaped and finding it\nhard to reason about.\n> +\ttest_subcommand git fetch remote2 $fetchargs +refs/heads/\\\\*:refs/prefetch/remote2/\\\\* <run-prefetch.txt &&\n> +\ttest_path_is_missing .git/refs/remotes &&\n> +\ttest_cmp clone1/.git/refs/heads/one .git/refs/prefetch/remote1/one &&\n> +\ttest_cmp clone2/.git/refs/heads/two .git/refs/prefetch/remote2/two &&\n[jrnieder] Should we use test_cmp_rev instead to make compatible with\npacked-refs?\n> +\tgit log prefetch/remote1/one &&\n> +\tgit log prefetch/remote2/two\n[jonathantan] Why do we use 'git log' to check? I'm a little confused\nabout what's going on; if you just want to check that the refs are\npresent you could use 'git rev-parse' instead?\n"},{"id":"403519","messageId":"20200812231053.GI2965447@google.com","threadId":"54001","inReplyTo":"621375a3c99014c48568660bde062b7330d5a662.1596731425.git.gitgitgadget@gmail.com","subject":"Re: [PATCH 3/9] maintenance: add loose-objects task","fromName":"Emily Shaffer","fromEmail":"emilyshaffer@google.com","sentAt":"2020-08-12T23:10:53Z","receivedAt":"2020-08-12T23:11:00Z","isPatch":true,"sender":{"key":"nasamuffin@google.com","avatar":"https://avatars.githubusercontent.com/u/1606826?v=4"},"body":"On Thu, Aug 06, 2020 at 04:30:18PM +0000, Derrick Stolee via GitGitGadget wrote:\n\n> diff --git a/Documentation/git-maintenance.txt b/Documentation/git-maintenance.txt\n> index bb0d5eded4..898aff4726 100644\n> --- a/Documentation/git-maintenance.txt\n> +++ b/Documentation/git-maintenance.txt\n> @@ -80,6 +80,17 @@ gc::\n>  \tIt can also be disruptive in some situations, as it deletes stale\n>  \tdata.\n>  \n> +loose-objects::\n> +\tThe `loose-objects` job cleans up loose objects and places them into\n> +\tpack-files. In order to prevent race conditions with concurrent Git\n> +\tcommands, it follows a two-step process. First, it deletes any loose\n> +\tobjects that already exist in a pack-file; concurrent Git processes\n> +\twill examine the pack-file for the object data instead of the loose\n> +\tobject. Second, it creates a new pack-file (starting with \"loose-\")\n> +\tcontaining a batch of loose objects. The batch size is limited to 50\n> +\tthousand objects to prevent the job from taking too long on a\n> +\trepository with many loose objects.\n\n[emily] What's not said is what happens to loose-* packfile. Does this\nget repacked and disappear, is it treated differently, etc?\n[jonathantan] This is treated the same as any other pack. Is there a\nreason to call it loose-*?\n[jrnieder] It seems like unreachable objects will get stuck going in and\nout of this pack, right? unreachable loose obj -> loose-*.pack ->\nrepack, unreachable becomes loose -> repeat\n\n> +static int prune_packed(struct maintenance_opts *opts)\n> +{\n> +\tstruct child_process child = CHILD_PROCESS_INIT;\n> +\n> +\tchild.git_cmd = 1;\n> +\tstrvec_push(&child.args, \"prune-packed\");\n> +\n> +\tif (opts->quiet)\n> +\t\tstrvec_push(&child.args, \"--quiet\");\n> +\n> +\treturn !!run_command(&child);\n[emily] Why not report the error code here to the caller? Is there a\npath to notify user of errors that require user intervention?\n[jrnieder] Some errors might be expected and some might not, so should\nwe handle them differently?\n[emily] Probably makes more sense to revisit this in the far future when\nwe teach 'git maintenance' to send us emails and flashing lights when\nour jobs fail, instead of worrying about it now :)\n[jrnieder] Imagine if we got exit 127 because the usage we are using is\nwrong - in that case, for example, we would want to BUG()\n\n> +static int write_loose_object_to_stdin(const struct object_id *oid,\n> +\t\t\t\t       const char *path,\n> +\t\t\t\t       void *data)\n> +{\n> +\tstruct write_loose_object_data *d = (struct write_loose_object_data *)data;\n[jrnieder] Since we are enlightened C developers, not C++ developers,\nyou can skip the explicit cast.\n\n> +++ b/t/t7900-maintenance.sh\n> @@ -83,4 +83,43 @@ test_expect_success 'prefetch multiple remotes' '\n>  \tgit log prefetch/remote2/two\n>  '\n>  \n> +test_expect_success 'loose-objects task' '\n[jrnieder] Unrelated to this change, this test makes me sad that we\ndon't have better low-level test helpers to enable this kind of testing.\nMakes me wish for something better :)\n> +\t# Repack everything so we know the state of the object dir\n> +\tgit repack -adk &&\n> +\n> +\t# Hack to stop maintenance from running during \"git commit\"\n> +\techo in use >.git/objects/maintenance.lock &&\n> +\n> +\t# Assuming that \"git commit\" creates at least one loose object\n> +\ttest_commit create-loose-object &&\n> +\trm .git/objects/maintenance.lock &&\n> +\n> +\tls .git/objects >obj-dir-before &&\n> +\ttest_file_not_empty obj-dir-before &&\n> +\tls .git/objects/pack/*.pack >packs-before &&\n> +\ttest_line_count = 1 packs-before &&\n> +\n> +\t# The first run creates a pack-file\n> +\t# but does not delete loose objects.\n> +\tgit maintenance run --task=loose-objects &&\n> +\tls .git/objects >obj-dir-between &&\n> +\ttest_cmp obj-dir-before obj-dir-between &&\n> +\tls .git/objects/pack/*.pack >packs-between &&\n> +\ttest_line_count = 2 packs-between &&\n> +\tls .git/objects/pack/loose-*.pack >loose-packs &&\n> +\ttest_line_count = 1 loose-packs &&\n> +\n> +\t# The second run deletes loose objects\n> +\t# but does not create a pack-file.\n> +\tgit maintenance run --task=loose-objects &&\n> +\tls .git/objects >obj-dir-after &&\n> +\tcat >expect <<-\\EOF &&\n> +\tinfo\n> +\tpack\n> +\tEOF\n> +\ttest_cmp expect obj-dir-after &&\n> +\tls .git/objects/pack/*.pack >packs-after &&\n> +\ttest_cmp packs-between packs-after\n> +'\n> +\n\n"},{"id":"403521","messageId":"xmqqo8nf4dkb.fsf@gitster.c.googlers.com","threadId":"54001","inReplyTo":"20200812231021.GG2965447@google.com","subject":"Re: [PATCH 1/9] fetch: optionally allow disabling FETCH_HEAD update","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2020-08-13T00:03:32Z","receivedAt":"2020-08-13T00:03:41Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Emily Shaffer <emilyshaffer@google.com> writes:\n\n>> +fetch.writeFetchHEAD::\n>> +\tSetting it to false tells `git fetch` not to write the list\n>> +\tof remote refs fetched in the `FETCH_HEAD` file directly\n>> +\tunder `$GIT_DIR`.  Can be countermanded from the command\n>> +\tline with the `--[no-]write-fetch-head` option.  Defaults to\n>> +\ttrue.\n>\n> [jrnieder] Is providing a config option creating more trouble than\n> benefit? Is this a burden on script authors that they need to check the\n> config state, when instead they could just pass the flag? Or rather,\n> because of the config, the script author has to pass the flag explicitly\n> anyways.\n> [emily] removing the config also clears up the confusion around 'git pull' +\n> 'fetch.writeFetchHEAD' I commented on later.\n\n[A meta comment, but I somehow find this format cumbersome to read\nand respond to.  Would it be possible to dedup and/or summarize\ncomments on a single point?]\n\nI do not think \"it becomes cumbersome to design interaction between\ncommand line option and configuration\" is a valid reason not to add\nconfiguration variable.  It would be a valid reason not to, if we\nhave a convincing argument why people won't pass the command line\noption very often, though, but you'd need to be prepared for a\npossible future in which somebody finds a good use case where a\nconfiguration is useful, which means you cannot forever depend on\nthe lack of configurability to avoid making a proper design of the\ninteraction between configuration and command line.\n\nAs the feature itself is primarily designed for scripts that want to\nalways disable writing of FETCH_HEAD, I can certainly understand a\nshort-term/sighted view of not wanting to add configuration, though.\n\n>> @@ -895,7 +902,9 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,\n>>  \tconst char *what, *kind;\n>>  \tstruct ref *rm;\n>>  \tchar *url;\n>> -\tconst char *filename = dry_run ? \"/dev/null\" : git_path_fetch_head(the_repository);\n>> +\tconst char *filename = (!write_fetch_head\n>> +\t\t\t\t? \"/dev/null\"\n>> +\t\t\t\t: git_path_fetch_head(the_repository));\n>\n> [emily] Huh. so what does the dry_run codepath look like now? It looks\n> like it's been superseded by write_fetch_head but the option parse\n> doesn't tell dry_run to update write_fetch_head instead. (Found the\n> answer later, see below)\n>\n>> @@ -1797,6 +1806,10 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)\n>>  \tif (depth || deepen_since || deepen_not.nr)\n>>  \t\tdeepen = 1;\n>>  \n>> +\t/* FETCH_HEAD never gets updated in --dry-run mode */\n>> +\tif (dry_run)\n>> +\t\twrite_fetch_head = 0;\n>\n> [emily] Aha, here is where dry_run informs write_fetch_head. I wonder if\n> there's a way to instead tell the options struct that --dry-run ~=\n> --write-fetch-head?\n\nI do not know with what meaning you used the symbol \"~=\".  Dry-run\ntells the command to operate differently in a few ways, and one of\nthe (smaller) ways is not to write the FETCH_HEAD file.  Did you\nmean \"contains\", \"includes\", etc.?\n\n>> +test_expect_success '--write-fetch-head gets defeated by --dry-run' '\n>> +\trm -f .git/FETCH_HEAD &&\n>> +\tgit fetch --dry-run --write-fetch-head . &&\n>> +\t! test -f .git/FETCH_HEAD\n>> +'\n>\n> [emily] Would it make more sense to make these args mutually exclusive?\n\nAt least, give a hint, if you cannot state in concrete words, why\nyou suspect it might make sense to do things differently.  What use\ncase do you think an alternative design would help better?  Without\nit, you can just get \"no\" and such an exchange would not be useful\nat all to improve the patch.\n\n> [jrnieder] If someone specifies both, then they probably want to say\n> \"show me what I would write to FETCH_HEAD but don't actually do that\" -\n> which isn't info that we print anyways, right now.\n\nDo you mean \"don't actually write but show it to standard output\ninstead\" or something?  If we were designing --dry-run from scratch\nback when there were no \"git fetch\" command, that would have made\ntons of sense, I think ;-) To me, the \"--write-fetch-head\" option\ntells it to \"write to the file\", and not \"write the info to unfixed\ndestination that is not specified by this option but derived by the\npresense of other options\".  While the \"don't download and show what\nwould be in FETCH_HEAD\" might be a good feature to add, combining\nthese two options may be a good way to invoke the feature.\n\n>> +test_expect_success 'git pull --no-write-fetch-head fails' '\n>> +\tmkdir clonedwfh &&\n>> +\t(cd clonedwfh && git init &&\n>> +\ttest_must_fail git pull --no-write-fetch-head \"../parent\" >out 2>err &&\n>> +\ttest_must_be_empty out &&\n>> +\ttest_i18ngrep \"no-write-fetch-head\" err)\n>\n> Should this be shown as a usage error instead of a die()?\n\nAs \"pull\" does and should not take \"--[no-]write-fetch-head\" as its\noption, I think the above command line deserves an usage error, just\nlike \"git pull --update-head-ok\" does (or \"git pull --no-such-option\"\nfor that matter).\n\n"},{"id":"403522","messageId":"20200813014532.GC104953@google.com","threadId":"54001","inReplyTo":"xmqqo8nf4dkb.fsf@gitster.c.googlers.com","subject":"Re: [PATCH 1/9] fetch: optionally allow disabling FETCH_HEAD update","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2020-08-13T01:45:32Z","receivedAt":"2020-08-13T01:45:37Z","isPatch":true,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"Junio C Hamano wrote:\n\n> As the feature itself is primarily designed for scripts that want to\n> always disable writing of FETCH_HEAD, I can certainly understand a\n> short-term/sighted view of not wanting to add configuration, though.\n\nYes, I think that since this feature is primarily designed for\nscripts, an option is more likely to be useful for them than a config\nsetting.  An option kicks in when the calling script requests it; a\nconfig setting can kick in even when they didn't intend to request it.\n\nMy opinion would change if we think that we're going to flip the\ndefault to --no-write-fetch-head some day, in which case a config\nsetting would be a good way to request a preview of the future.  But I\ndon't believe anyone's brought that up as a direction we want to\npursue.\n\n[...]\n>>            If someone specifies both, then they probably want to say\n>> \"show me what I would write to FETCH_HEAD but don't actually do that\" -\n>> which isn't info that we print anyways, right now.\n>\n> Do you mean \"don't actually write but show it to standard output\n> instead\" or something?\n\nMy take is that the behavior that the patch implements for --dry-run\n--write-fetch-head is correct and what a user would want: it acts *as\nthough* you passed --write-fetch-head (including producing the same\nconsole output), without producing mutations that the user might\nregret (such as updating FETCH_HEAD).\n\nThanks and hope that helps,\nJonathan\n"},{"id":"403524","messageId":"xmqqh7t740vu.fsf_-_@gitster.c.googlers.com","threadId":"54001","inReplyTo":"xmqqo8nf4dkb.fsf@gitster.c.googlers.com","subject":"[PATCH v3] fetch: optionally allow disabling FETCH_HEAD update","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2020-08-13T04:37:25Z","receivedAt":"2020-08-13T04:37:30Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"If you run fetch but record the result in remote-tracking branches,\nand either if you do nothing with the fetched refs (e.g. you are\nmerely mirroring) or if you always work from the remote-tracking\nrefs (e.g. you fetch and then merge origin/branchname separately),\nyou can get away with having no FETCH_HEAD at all.\n\nTeach \"git fetch\" a command line option \"--[no-]write-fetch-head\".\nThe default is to write FETCH_HEAD, and the option is primarily\nmeant to be used with the \"--no-\" prefix to override this default,\nbecause there is no matching fetch.writeFetchHEAD configuration\nvariable to flip the default to off (in which case, the positive\nform may become necessary to defeat it).\n\nNote that under \"--dry-run\" mode, FETCH_HEAD is never written;\notherwise you'd see list of objects in the file that you do not\nactually have.  Passing `--write-fetch-head` does not force `git\nfetch` to write the file.\n\nSigned-off-by: Derrick Stolee <dstolee@microsoft.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n\n * So, it becomes much smaller by punting the whole configuration\n   thing, as we do not need the extra code for config parsing and\n   there is no need for code to override the user configuration when\n   driving \"git fetch\" from \"git pull\".\n\n Documentation/fetch-options.txt |  9 +++++++++\n builtin/fetch.c                 | 15 ++++++++++++---\n t/t5510-fetch.sh                | 15 +++++++++++++--\n 3 files changed, 34 insertions(+), 5 deletions(-)\n\ndiff --git a/Documentation/fetch-options.txt b/Documentation/fetch-options.txt\nindex 495bc8ab5a..b65a758661 100644\n--- a/Documentation/fetch-options.txt\n+++ b/Documentation/fetch-options.txt\n@@ -64,6 +64,15 @@ documented in linkgit:git-config[1].\n --dry-run::\n \tShow what would be done, without making any changes.\n \n+ifndef::git-pull[]\n+--[no-]write-fetch-head::\n+\tWrite the list of remote refs fetched in the `FETCH_HEAD`\n+\tfile directly under `$GIT_DIR`.  This is the default.\n+\tPassing `--no-write-fetch-head` from the command line tells\n+\tGit not to write the file.  Under `--dry-run` option, the\n+\tfile is never written.\n+endif::git-pull[]\n+\n -f::\n --force::\n \tWhen 'git fetch' is used with `<src>:<dst>` refspec it may\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex c8b9366d3c..cb38e6f5ec 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -56,6 +56,7 @@ static int prune_tags = -1; /* unspecified */\n #define PRUNE_TAGS_BY_DEFAULT 0 /* do we prune tags by default? */\n \n static int all, append, dry_run, force, keep, multiple, update_head_ok;\n+static int write_fetch_head = 1;\n static int verbosity, deepen_relative, set_upstream;\n static int progress = -1;\n static int enable_auto_gc = 1;\n@@ -162,6 +163,8 @@ static struct option builtin_fetch_options[] = {\n \t\t    PARSE_OPT_OPTARG, option_fetch_parse_recurse_submodules),\n \tOPT_BOOL(0, \"dry-run\", &dry_run,\n \t\t N_(\"dry run\")),\n+\tOPT_BOOL(0, \"write-fetch-head\", &write_fetch_head,\n+\t\t N_(\"write fetched references to the FETCH_HEAD file\")),\n \tOPT_BOOL('k', \"keep\", &keep, N_(\"keep downloaded pack\")),\n \tOPT_BOOL('u', \"update-head-ok\", &update_head_ok,\n \t\t    N_(\"allow updating of HEAD ref\")),\n@@ -895,7 +898,9 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,\n \tconst char *what, *kind;\n \tstruct ref *rm;\n \tchar *url;\n-\tconst char *filename = dry_run ? \"/dev/null\" : git_path_fetch_head(the_repository);\n+\tconst char *filename = (!write_fetch_head\n+\t\t\t\t? \"/dev/null\"\n+\t\t\t\t: git_path_fetch_head(the_repository));\n \tint want_status;\n \tint summary_width = transport_summary_width(ref_map);\n \n@@ -1329,7 +1334,7 @@ static int do_fetch(struct transport *transport,\n \t}\n \n \t/* if not appending, truncate FETCH_HEAD */\n-\tif (!append && !dry_run) {\n+\tif (!append && write_fetch_head) {\n \t\tretcode = truncate_fetch_head();\n \t\tif (retcode)\n \t\t\tgoto cleanup;\n@@ -1596,7 +1601,7 @@ static int fetch_multiple(struct string_list *list, int max_children)\n \tint i, result = 0;\n \tstruct strvec argv = STRVEC_INIT;\n \n-\tif (!append && !dry_run) {\n+\tif (!append && write_fetch_head) {\n \t\tint errcode = truncate_fetch_head();\n \t\tif (errcode)\n \t\t\treturn errcode;\n@@ -1797,6 +1802,10 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)\n \tif (depth || deepen_since || deepen_not.nr)\n \t\tdeepen = 1;\n \n+\t/* FETCH_HEAD never gets updated in --dry-run mode */\n+\tif (dry_run)\n+\t\twrite_fetch_head = 0;\n+\n \tif (all) {\n \t\tif (argc == 1)\n \t\t\tdie(_(\"fetch --all does not take a repository argument\"));\ndiff --git a/t/t5510-fetch.sh b/t/t5510-fetch.sh\nindex 9850ecde5d..5bd1f953af 100755\n--- a/t/t5510-fetch.sh\n+++ b/t/t5510-fetch.sh\n@@ -539,13 +539,24 @@ test_expect_success 'fetch into the current branch with --update-head-ok' '\n \n '\n \n-test_expect_success 'fetch --dry-run' '\n-\n+test_expect_success 'fetch --dry-run does not touch FETCH_HEAD' '\n \trm -f .git/FETCH_HEAD &&\n \tgit fetch --dry-run . &&\n \t! test -f .git/FETCH_HEAD\n '\n \n+test_expect_success '--no-write-fetch-head does not touch FETCH_HEAD' '\n+\trm -f .git/FETCH_HEAD &&\n+\tgit fetch --no-write-fetch-head . &&\n+\t! test -f .git/FETCH_HEAD\n+'\n+\n+test_expect_success '--write-fetch-head gets defeated by --dry-run' '\n+\trm -f .git/FETCH_HEAD &&\n+\tgit fetch --dry-run --write-fetch-head . &&\n+\t! test -f .git/FETCH_HEAD\n+'\n+\n test_expect_success \"should be able to fetch with duplicate refspecs\" '\n \tmkdir dups &&\n \t(\n-- \n2.28.0-314-g35e3b9c6bd\n\n\n"},{"id":"403610","messageId":"8acc67e5-4b17-7fdc-0685-b5113e819010@gmail.com","threadId":"54001","inReplyTo":"xmqqh7t740vu.fsf_-_@gitster.c.googlers.com","subject":"Re: [PATCH v3] fetch: optionally allow disabling FETCH_HEAD update","fromName":"Derrick Stolee","fromEmail":"stolee@gmail.com","sentAt":"2020-08-14T01:13:14Z","receivedAt":"2020-08-14T01:13:21Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"On 8/13/2020 12:37 AM, Junio C Hamano wrote:\n> If you run fetch but record the result in remote-tracking branches,\n> and either if you do nothing with the fetched refs (e.g. you are\n> merely mirroring) or if you always work from the remote-tracking\n> refs (e.g. you fetch and then merge origin/branchname separately),\n> you can get away with having no FETCH_HEAD at all.\n> \n> Teach \"git fetch\" a command line option \"--[no-]write-fetch-head\".\n> The default is to write FETCH_HEAD, and the option is primarily\n> meant to be used with the \"--no-\" prefix to override this default,\n> because there is no matching fetch.writeFetchHEAD configuration\n> variable to flip the default to off (in which case, the positive\n> form may become necessary to defeat it).\n> \n> Note that under \"--dry-run\" mode, FETCH_HEAD is never written;\n> otherwise you'd see list of objects in the file that you do not\n> actually have.  Passing `--write-fetch-head` does not force `git\n> fetch` to write the file.\n> \n> Signed-off-by: Derrick Stolee <dstolee@microsoft.com>\n> Signed-off-by: Junio C Hamano <gitster@pobox.com>\n> ---\n> \n>  * So, it becomes much smaller by punting the whole configuration\n>    thing, as we do not need the extra code for config parsing and\n>    there is no need for code to override the user configuration when\n>    driving \"git fetch\" from \"git pull\".\n\nSounds good. I'll incorporate this patch into my next version,\nexcept it seems you dropped this test:\n\ndiff --git a/t/t5521-pull-options.sh b/t/t5521-pull-options.sh\nindex 159afa7ac8..db1a381cd9 100755\n--- a/t/t5521-pull-options.sh\n+++ b/t/t5521-pull-options.sh\n@@ -85,6 +85,13 @@ test_expect_success 'git pull --cleanup errors early on invalid argument' '\n        test -s err)\n '\n \n+test_expect_success 'git pull --no-write-fetch-head fails' '\n+       mkdir clonedwfh &&\n+       (cd clonedwfh && git init &&\n+       test_expect_code 129 git pull --no-write-fetch-head \"../parent\" >out 2>err &&\n+       test_must_be_empty out &&\n+       test_i18ngrep \"no-write-fetch-head\" err)\n+'\n \n test_expect_success 'git pull --force' '\n        mkdir clonedoldstyle &&\n\nwhich I changed \"test_must_fail\" to \"test_expect_code 129\" to\ndemonstrate that this is a usage error, not just any error.\n\nThanks,\n-Stolee\n"},{"id":"403611","messageId":"e96b2eec-0746-1b00-4b8b-ce737cdce967@gmail.com","threadId":"54001","inReplyTo":"20200812231036.GH2965447@google.com","subject":"Re: [PATCH 2/9] maintenance: add prefetch task","fromName":"Derrick Stolee","fromEmail":"stolee@gmail.com","sentAt":"2020-08-14T01:28:33Z","receivedAt":"2020-08-14T01:28:43Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"On 8/12/2020 7:10 PM, Emily Shaffer wrote:\n> On Thu, Aug 06, 2020 at 04:30:17PM +0000, Derrick Stolee via GitGitGadget wrote:\n>>\n>> 3. By adding a new refspec \"+refs/heads/*:refs/prefetch/<remote>/*\"\n>>    we can ensure that we actually load the new values somewhere in\n>>    our refspace while not updating refs/heads or refs/remotes. By\n>>    storing these refs here, the commit-graph job will update the\n>>    commit-graph with the commits from these hidden refs.\n> \n> [emily] How does the content of refs/prefetch/* get used?\n> [jrnieder] How does the content of refs/prefetch/* get cleaned up?\n> [jonathantan] refs/prefetch/* will get used in negotiation so it is\n> useful to keep these.\n>>\n>> 4. --prune will delete the refs/prefetch/<remote> refs that no\n>>    longer appear on the remote.\n> [jrnieder] this is what cleans up refs/prefetch/* later :)\n> \n>> +static int fetch_remote(const char *remote, struct maintenance_opts *opts)\n>> +{\n>> +\tstruct child_process child = CHILD_PROCESS_INIT;\n>> +\n>> +\tchild.git_cmd = 1;\n>> +\tstrvec_pushl(&child.args, \"fetch\", remote, \"--prune\", \"--no-tags\",\n>> +\t\t     \"--no-write-fetch-head\", \"--refmap=\", NULL);\n> [jonathantan] It would be good to pass --recurse-submodules=no.\n> [jrnieder] Since we are specifying the refmap here, if we were to\n> recurse into submodules, we'd have to be careful about making sure\n> refmap gets propagated correctly.\n\nGood point.\n\n>> +static int fill_each_remote(struct remote *remote, void *cbdata)\n> [jrnieder] Since this is a callback that happens for each remote, maybe\n> this should be named to indicate it only fills one remote at a time\n> instead. Nit :)\n\nSure. append_remote() is better.\n\n>> +{\n>> +\tstruct string_list *remotes = (struct string_list *)cbdata;\n>> +\n>> +\tstring_list_append(remotes, remote->name);\n>> +\treturn 0;\n>> +}\n>> +\n>> +static int maintenance_task_prefetch(struct maintenance_opts *opts)\n>> +{\n>> +\tint result = 0;\n>> +\tstruct string_list_item *item;\n>> +\tstruct string_list remotes = STRING_LIST_INIT_DUP;\n>> +\n>> +\tif (for_each_remote(fill_each_remote, &remotes)) {\n>> +\t\terror(_(\"failed to fill remotes\"));\n>> +\t\tresult = 1;\n>> +\t\tgoto cleanup;\n>> +\t}\n>> +\n>> +\tfor (item = remotes.items;\n>> +\t     item && item < remotes.items + remotes.nr;\n>> +\t     item++)\n> \n> Is there a reason not to use for_each_string_list_item() instead? This\n> would be more brief and also easier to read (less thinking about what\n> the loop is doing).\n\nI just keep forgetting about that macro. Thanks.\n\n>> +\t\tresult |= fetch_remote(item->string, opts);\n>> +\n>> +cleanup:\n>> +\tstring_list_clear(&remotes, 0);\n>> +\treturn result;\n>> +}\n>> +\n> \n>>  enum maintenance_task_label {\n>> +\tTASK_PREFETCH,\n> [jrnieder] Nit: Is there a sort order for these? Should we establish an order\n> early on (e.g. alphabetical)?\n\nThere _is_ an order, and maybe that should be up for debate.\nThis one is really about fetching new refs before (possibly)\ndeleting unreachable data or doing a full repack in the 'gc'\ntask. The rest of the tasks are after the 'gc' task (in an\nunimportant order) because they are likely to be no-ops if the\n'gc' task does work.\n\nOf course, users can customize this order using the\n--task=<task> options.\n\n>>  \tTASK_GC,\n>>  \tTASK_COMMIT_GRAPH,\n> \n>> +test_expect_success 'prefetch multiple remotes' '\n>> +\tgit clone . clone1 &&\n>> +\tgit clone . clone2 &&\n>> +\tgit remote add remote1 \"file://$(pwd)/clone1\" &&\n>> +\tgit remote add remote2 \"file://$(pwd)/clone2\" &&\n>> +\tgit -C clone1 switch -c one &&\n>> +\tgit -C clone2 switch -c two &&\n>> +\ttest_commit -C clone1 one &&\n>> +\ttest_commit -C clone2 two &&\n>> +\tGIT_TRACE2_EVENT=\"$(pwd)/run-prefetch.txt\" git maintenance run --task=prefetch 2>/dev/null &&\n>> +\tfetchargs=\"--prune --no-tags --no-write-fetch-head --refmap= --quiet\" &&\n>> +\ttest_subcommand git fetch remote1 $fetchargs +refs/heads/\\\\*:refs/prefetch/remote1/\\\\* <run-prefetch.txt &&\n\n> [jrnieder] In practice, why were all the \\\\ needed? Trying to figure out\n> where Git is using a shell that would need the * escaped and finding it\n> hard to reason about.\n\nIt's the 'grep' way down inside test_subcommand that will\nfail if it interprets these '*' incorrectly.\n\n>> +\ttest_subcommand git fetch remote2 $fetchargs +refs/heads/\\\\*:refs/prefetch/remote2/\\\\* <run-prefetch.txt &&\n>> +\ttest_path_is_missing .git/refs/remotes &&\n>> +\ttest_cmp clone1/.git/refs/heads/one .git/refs/prefetch/remote1/one &&\n>> +\ttest_cmp clone2/.git/refs/heads/two .git/refs/prefetch/remote2/two &&\n\n> [jrnieder] Should we use test_cmp_rev instead to make compatible with\n> packed-refs?\n\nWhile that would be good, these are refs across two .git directories.\n\nI suppose that I could fetch normally and check refs/remotes/remote1/one\nversus refs/prefetch/remote1/one.\n\n>> +\tgit log prefetch/remote1/one &&\n>> +\tgit log prefetch/remote2/two\n\n> [jonathantan] Why do we use 'git log' to check? I'm a little confused\n> about what's going on; if you just want to check that the refs are\n> present you could use 'git rev-parse' instead?\n\nThe main point is to demonstrate that we have the actual objects\nin the current repository, not just refs storing object IDs that\ndon't actually exist in the object database. Does rev-parse check\nthat the object exists? (A quick check in my local repository seems\nto say no.)\n\nReorganizing the test to work with test_cmp_rev results in the\nfollowing:\n\ntest_expect_success 'prefetch multiple remotes' '\n\tgit clone . clone1 &&\n\tgit clone . clone2 &&\n\tgit remote add remote1 \"file://$(pwd)/clone1\" &&\n\tgit remote add remote2 \"file://$(pwd)/clone2\" &&\n\tgit -C clone1 switch -c one &&\n\tgit -C clone2 switch -c two &&\n\ttest_commit -C clone1 one &&\n\ttest_commit -C clone2 two &&\n\tGIT_TRACE2_EVENT=\"$(pwd)/run-prefetch.txt\" git maintenance run --task=prefetch 2>/dev/null &&\n\tfetchargs=\"--prune --no-tags --no-write-fetch-head --recurse-submodules=no --refmap= --quiet\" &&\n\ttest_subcommand git fetch remote1 $fetchargs +refs/heads/\\\\*:refs/prefetch/remote1/\\\\* <run-prefetch.txt &&\n\ttest_subcommand git fetch remote2 $fetchargs +refs/heads/\\\\*:refs/prefetch/remote2/\\\\* <run-prefetch.txt &&\n\ttest_path_is_missing .git/refs/remotes &&\n\tgit log prefetch/remote1/one &&\n\tgit log prefetch/remote2/two &&\n\tgit fetch --all &&\n\ttest_cmp_rev refs/remotes/remote1/one refs/prefetch/remote1/one &&\n\ttest_cmp_rev refs/remotes/remote2/two refs/prefetch/remote2/two\n'\n\nThanks,\n-Stolee\n"},{"id":"403612","messageId":"xmqqtux6yptz.fsf@gitster.c.googlers.com","threadId":"54001","inReplyTo":"8acc67e5-4b17-7fdc-0685-b5113e819010@gmail.com","subject":"Re: [PATCH v3] fetch: optionally allow disabling FETCH_HEAD update","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2020-08-14T01:32:40Z","receivedAt":"2020-08-14T01:32:50Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Derrick Stolee <stolee@gmail.com> writes:\n\n>>  * So, it becomes much smaller by punting the whole configuration\n>>    thing, as we do not need the extra code for config parsing and\n>>    there is no need for code to override the user configuration when\n>>    driving \"git fetch\" from \"git pull\".\n>\n> Sounds good. I'll incorporate this patch into my next version,\n> except it seems you dropped this test:\n\nYup.  \n\nAs we are not touching \"pull\" at all, the test I removed would make\nas much sense as a hypothetical one where its \"git pull\" is replaced\nwith \"git cat-file\" or any other unrelated subcommand, and that is\nwhy I removed it.  But I am OK if you resurrect it---everybody thinks\n\"fetch\" and \"pull\" are closely related after all.\n\nThanks.\n\n\n\n> diff --git a/t/t5521-pull-options.sh b/t/t5521-pull-options.sh\n> index 159afa7ac8..db1a381cd9 100755\n> --- a/t/t5521-pull-options.sh\n> +++ b/t/t5521-pull-options.sh\n> @@ -85,6 +85,13 @@ test_expect_success 'git pull --cleanup errors early on invalid argument' '\n>         test -s err)\n>  '\n>  \n> +test_expect_success 'git pull --no-write-fetch-head fails' '\n> +       mkdir clonedwfh &&\n> +       (cd clonedwfh && git init &&\n> +       test_expect_code 129 git pull --no-write-fetch-head \"../parent\" >out 2>err &&\n> +       test_must_be_empty out &&\n> +       test_i18ngrep \"no-write-fetch-head\" err)\n> +'\n>  \n>  test_expect_success 'git pull --force' '\n>         mkdir clonedoldstyle &&\n>\n> which I changed \"test_must_fail\" to \"test_expect_code 129\" to\n> demonstrate that this is a usage error, not just any error.\n>\n> Thanks,\n> -Stolee\n"},{"id":"403613","messageId":"0fd9d838-657f-448a-f09b-6e431aebdf69@gmail.com","threadId":"54001","inReplyTo":"20200812231053.GI2965447@google.com","subject":"Re: [PATCH 3/9] maintenance: add loose-objects task","fromName":"Derrick Stolee","fromEmail":"stolee@gmail.com","sentAt":"2020-08-14T01:46:37Z","receivedAt":"2020-08-14T01:46:44Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"On 8/12/2020 7:10 PM, Emily Shaffer wrote:\n> On Thu, Aug 06, 2020 at 04:30:18PM +0000, Derrick Stolee via GitGitGadget wrote:\n> \n>> diff --git a/Documentation/git-maintenance.txt b/Documentation/git-maintenance.txt\n>> index bb0d5eded4..898aff4726 100644\n>> --- a/Documentation/git-maintenance.txt\n>> +++ b/Documentation/git-maintenance.txt\n>> @@ -80,6 +80,17 @@ gc::\n>>  \tIt can also be disruptive in some situations, as it deletes stale\n>>  \tdata.\n>>  \n>> +loose-objects::\n>> +\tThe `loose-objects` job cleans up loose objects and places them into\n>> +\tpack-files. In order to prevent race conditions with concurrent Git\n>> +\tcommands, it follows a two-step process. First, it deletes any loose\n>> +\tobjects that already exist in a pack-file; concurrent Git processes\n>> +\twill examine the pack-file for the object data instead of the loose\n>> +\tobject. Second, it creates a new pack-file (starting with \"loose-\")\n>> +\tcontaining a batch of loose objects. The batch size is limited to 50\n>> +\tthousand objects to prevent the job from taking too long on a\n>> +\trepository with many loose objects.\n> \n> [emily] What's not said is what happens to loose-* packfile. Does this\n> get repacked and disappear, is it treated differently, etc?\n> [jonathantan] This is treated the same as any other pack. Is there a\n> reason to call it loose-*?\n\nAuditing, mostly. When working with repositories that have\nmultiple pack-files as a rule, it is helpful to know what \"kind\"\nof pack-file they are. I almost added a \"repacked-\" prefix to\nthe packs produced by 'git multi-pack-index repack'. It helps\nto distinguish \"reprocessed\" data from \"I got this from a 'clone'\nor 'fetch'.\"\n\nOf course, as these pack-files are repacked later, the prefixes\nwill get dropped. The name does give us some help when performing\ndiagnostics on a misbehaving repository. A pack directory filled\nwith many \"loose-*.pack\" files indicates that the 'loose-objects'\nstep was run many times without either 'gc' or 'incremental-repack'\ncoalescing those packs with delta compression.\n\n> [jrnieder] It seems like unreachable objects will get stuck going in and\n> out of this pack, right? unreachable loose obj -> loose-*.pack ->\n> repack, unreachable becomes loose -> repeat\n\nThis is a good point. This task would probably interact poorly with\nthe 'gc' task in that regard. They are not intended to both be\nenabled at the same time, but it might be good to point that out\ndirectly in the documentation.\n\nHow does this sound?\n\n The `gc` task writes unreachable objects as loose objects to\n be cleaned up by a later step only if they are not re-added to\n a pack-file; for this reason it is not advisable to enable both\n the `loose-objects` and `gc` tasks at the same time.\n\n>> +static int prune_packed(struct maintenance_opts *opts)\n>> +{\n>> +\tstruct child_process child = CHILD_PROCESS_INIT;\n>> +\n>> +\tchild.git_cmd = 1;\n>> +\tstrvec_push(&child.args, \"prune-packed\");\n>> +\n>> +\tif (opts->quiet)\n>> +\t\tstrvec_push(&child.args, \"--quiet\");\n>> +\n>> +\treturn !!run_command(&child);\n\n> [emily] Why not report the error code here to the caller? Is there a\n> path to notify user of errors that require user intervention?\n> [jrnieder] Some errors might be expected and some might not, so should\n> we handle them differently?\n> [emily] Probably makes more sense to revisit this in the far future when\n> we teach 'git maintenance' to send us emails and flashing lights when\n> our jobs fail, instead of worrying about it now :)\n> [jrnieder] Imagine if we got exit 127 because the usage we are using is\n> wrong - in that case, for example, we would want to BUG()\n\nI was previously returning the exact error code, but received\nfeedback that these internal methods should have simpler return\nvalues [1].\n\n[1] https://lore.kernel.org/git/xmqq4kpyq8wh.fsf@gitster.c.googlers.com/\n\n>> +static int write_loose_object_to_stdin(const struct object_id *oid,\n>> +\t\t\t\t       const char *path,\n>> +\t\t\t\t       void *data)\n>> +{\n>> +\tstruct write_loose_object_data *d = (struct write_loose_object_data *)data;\n\n> [jrnieder] Since we are enlightened C developers, not C++ developers,\n> you can skip the explicit cast.\n\nHm. I definitely _could_ but don't really want to. Perhaps I'm just\nbeing pedantic, but I was just talking to someone today off-list that\nthey were disappointed that they couldn't compile Git in a C++ compiler\nmainly because of these kinds of casting issues.\n\nI was surprised the word \"cast\" does not appear anywhere in the\nCodingGuidelines, because if this is a convention we want to follow,\nthen I have definitely been violating it for a *while*.\n \n>> +++ b/t/t7900-maintenance.sh\n>> @@ -83,4 +83,43 @@ test_expect_success 'prefetch multiple remotes' '\n>>  \tgit log prefetch/remote2/two\n>>  '\n>>  \n>> +test_expect_success 'loose-objects task' '\n\n> [jrnieder] Unrelated to this change, this test makes me sad that we\n> don't have better low-level test helpers to enable this kind of testing.> Makes me wish for something better :)\n\nDo you mean you wish we had helpers for getting a repo\ninto a certain state of its object database? Or somehow\nmocking the object database in-memory to provide more\nunit-level testing? I don't understand what \"something\nbetter\" looks like.\n\n>> +\t# Repack everything so we know the state of the object dir\n>> +\tgit repack -adk &&\n>> +\n>> +\t# Hack to stop maintenance from running during \"git commit\"\n>> +\techo in use >.git/objects/maintenance.lock &&\n>> +\n>> +\t# Assuming that \"git commit\" creates at least one loose object\n>> +\ttest_commit create-loose-object &&\n>> +\trm .git/objects/maintenance.lock &&\n>> +\n>> +\tls .git/objects >obj-dir-before &&\n>> +\ttest_file_not_empty obj-dir-before &&\n>> +\tls .git/objects/pack/*.pack >packs-before &&\n>> +\ttest_line_count = 1 packs-before &&\n>> +\n>> +\t# The first run creates a pack-file\n>> +\t# but does not delete loose objects.\n>> +\tgit maintenance run --task=loose-objects &&\n>> +\tls .git/objects >obj-dir-between &&\n>> +\ttest_cmp obj-dir-before obj-dir-between &&\n>> +\tls .git/objects/pack/*.pack >packs-between &&\n>> +\ttest_line_count = 2 packs-between &&\n>> +\tls .git/objects/pack/loose-*.pack >loose-packs &&\n>> +\ttest_line_count = 1 loose-packs &&\n>> +\n>> +\t# The second run deletes loose objects\n>> +\t# but does not create a pack-file.\n>> +\tgit maintenance run --task=loose-objects &&\n>> +\tls .git/objects >obj-dir-after &&\n>> +\tcat >expect <<-\\EOF &&\n>> +\tinfo\n>> +\tpack\n>> +\tEOF\n>> +\ttest_cmp expect obj-dir-after &&\n>> +\tls .git/objects/pack/*.pack >packs-after &&\n>> +\ttest_cmp packs-between packs-after\n>> +'\n>> +\n> \n\n"},{"id":"403946","messageId":"pull.696.v2.git.1597760730.gitgitgadget@gmail.com","threadId":"54001","inReplyTo":"pull.696.git.1596731424.gitgitgadget@gmail.com","subject":"[PATCH v2 0/9] Maintenance II: prefetch, loose-objects, incremental-repack tasks","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2020-08-18T14:25:21Z","receivedAt":"2020-08-18T14:25:47Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"This series is based on part I [2].\n\nThis patch series contains 9 patches that were going to be part of v4 of\nds/maintenance [1], but the discussion has gotten really long. To help, I'm\nsplitting out the portions that create and test the 'maintenance' builtin\nfrom the additional tasks (prefetch, loose-objects, incremental-repack) that\ncan be brought in later.\n\n[1] \nhttps://lore.kernel.org/git/pull.671.git.1594131695.gitgitgadget@gmail.com/\n[2] \nhttps://lore.kernel.org/git/pull.695.git.1596728921.gitgitgadget@gmail.com/\n\nAs detailed in [2], the 'git maintenance run' subcommand will run certain\ntasks based on config options or the --task= arguments. The --auto option\nindicates to the task to only run based on some internal check that there\nhas been \"enough\" change in that domain to merit the work. In the case of\nthe 'gc' task, this also reduces the amount of work done. \n\nThe new maintenance tasks in this series are:\n\n * 'loose-objects' : prune packed loose objects, then create a new pack from\n   a batch of loose objects.\n * 'pack-files' : expire redundant packs from the multi-pack-index, then\n   repack using the multi-pack-index's incremental repack strategy.\n * 'prefetch' : fetch from each remote, storing the refs in 'refs/prefetch/\n   /'.\n\nThese tasks are all disabled by default, but can be enabled with config\noptions or run explicitly using \"git maintenance run --task=\". \n\nSince [2] replaced the 'git gc --auto' calls with 'git maintenance run\n--auto' at the end of some Git commands, users could replace the 'gc' task\nwith these lighter-weight changes for foreground maintenance.\n\nThe 'git maintenance' builtin has a 'run' subcommand so it can be extended\nlater with subcommands that manage background maintenance, such as 'start'\nor 'stop'. These are not the subject of this series, as it is important to\nfocus on the maintenance activities themselves. I have a WIP series for this\navailable at [3].\n\n[3] https://github.com/gitgitgadget/git/pull/680\n\nUpdates since v1 (of this series)\n=================================\n\n * PATCH 1 (\"fetch: optionally allow disabling FETCH_HEAD update\") was\n   rewritten on-list. Getting a version out with this patch is the main\n   reason for rolling a v2. (That, and Part I is re-rolled with a v2 and I\n   want to make sure this series applies cleanly.)\n   \n   \n * The 'prefetch' and 'loose-objects' tasks had some review, but my proposed\n   changes were not acked, so they may need another review.\n   \n   \n\nUPDATES since v3 of [1]\n=======================\n\n * The biggest change here is the use of \"test_subcommand\", based on\n   Jonathan Nieder's approach. This requires having the exact command-line\n   figured out, which now requires spelling out all --no- [quiet%7Cprogress] \n   options. I also added a bunch of \"2>/dev/null\" checks because of the\n   isatty(2) calls. Without that, the behavior will change depending on\n   whether the test is run with -x/-v or without.\n   \n   \n * The 0x7FFF/0x7FFFFFFF constant problem is fixed with an EXPENSIVE test\n   that verifies it.\n   \n   \n * The option parsing has changed to use a local struct and pass that struct\n   to the helper methods. This is instead of having a global singleton.\n   \n   \n\nThanks, -Stolee\n\nDerrick Stolee (8):\n  maintenance: add prefetch task\n  maintenance: add loose-objects task\n  maintenance: create auto condition for loose-objects\n  midx: enable core.multiPackIndex by default\n  midx: use start_delayed_progress()\n  maintenance: add incremental-repack task\n  maintenance: auto-size incremental-repack batch\n  maintenance: add incremental-repack auto condition\n\nJunio C Hamano (1):\n  fetch: optionally allow disabling FETCH_HEAD update\n\n Documentation/config/core.txt        |   4 +-\n Documentation/config/maintenance.txt |  18 ++\n Documentation/fetch-options.txt      |   9 +\n Documentation/git-maintenance.txt    |  45 ++++\n builtin/fetch.c                      |  15 +-\n builtin/gc.c                         | 363 +++++++++++++++++++++++++++\n midx.c                               |  23 +-\n midx.h                               |   1 +\n repo-settings.c                      |   6 +\n repository.h                         |   2 +\n t/t5319-multi-pack-index.sh          |  15 +-\n t/t5510-fetch.sh                     |  15 +-\n t/t5521-pull-options.sh              |   7 +\n t/t7900-maintenance.sh               | 191 ++++++++++++++\n 14 files changed, 686 insertions(+), 28 deletions(-)\n\n\nbase-commit: 46fbe161aa154de9406117ee916b1bd5f549905c\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-696%2Fderrickstolee%2Fmaintenance%2Fgc-v2\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-696/derrickstolee/maintenance/gc-v2\nPull-Request: https://github.com/gitgitgadget/git/pull/696\n\nRange-diff vs v1:\n\n  1:  83401c5200 !  1:  f3bc0b2d92 fetch: optionally allow disabling FETCH_HEAD update\n     @@ Commit message\n          refs (e.g. you fetch and then merge origin/branchname separately),\n          you can get away with having no FETCH_HEAD at all.\n      \n     -    Teach \"git fetch\" a command line option \"--[no-]write-fetch-head\"\n     -    and \"fetch.writeFetchHEAD\" configuration variable.  Without either,\n     -    the default is to write FETCH_HEAD, and the usual rule that the\n     -    command line option defeats configured default applies.\n     +    Teach \"git fetch\" a command line option \"--[no-]write-fetch-head\".\n     +    The default is to write FETCH_HEAD, and the option is primarily\n     +    meant to be used with the \"--no-\" prefix to override this default,\n     +    because there is no matching fetch.writeFetchHEAD configuration\n     +    variable to flip the default to off (in which case, the positive\n     +    form may become necessary to defeat it).\n      \n          Note that under \"--dry-run\" mode, FETCH_HEAD is never written;\n          otherwise you'd see list of objects in the file that you do not\n          actually have.  Passing `--write-fetch-head` does not force `git\n          fetch` to write the file.\n      \n     -    Also note that this option is explicitly passed when \"git pull\"\n     -    internally invokes \"git fetch\", so that those who configured their\n     -    \"git fetch\" not to write FETCH_HEAD would not be able to break the\n     -    cooperation between these two commands.  \"git pull\" must see what\n     -    \"git fetch\" got recorded in FETCH_HEAD to work correctly.\n     -\n     -    Signed-off-by: Junio C Hamano <gitster@pobox.com>\n          Signed-off-by: Derrick Stolee <dstolee@microsoft.com>\n     -\n     - ## Documentation/config/fetch.txt ##\n     -@@ Documentation/config/fetch.txt: fetch.writeCommitGraph::\n     - \tmerge and the write may take longer. Having an updated commit-graph\n     - \tfile helps performance of many Git commands, including `git merge-base`,\n     - \t`git push -f`, and `git log --graph`. Defaults to false.\n     -+\n     -+fetch.writeFetchHEAD::\n     -+\tSetting it to false tells `git fetch` not to write the list\n     -+\tof remote refs fetched in the `FETCH_HEAD` file directly\n     -+\tunder `$GIT_DIR`.  Can be countermanded from the command\n     -+\tline with the `--[no-]write-fetch-head` option.  Defaults to\n     -+\ttrue.\n     +    Signed-off-by: Junio C Hamano <gitster@pobox.com>\n      \n       ## Documentation/fetch-options.txt ##\n      @@ Documentation/fetch-options.txt: documented in linkgit:git-config[1].\n     @@ Documentation/fetch-options.txt: documented in linkgit:git-config[1].\n      +ifndef::git-pull[]\n      +--[no-]write-fetch-head::\n      +\tWrite the list of remote refs fetched in the `FETCH_HEAD`\n     -+\tfile directly under `$GIT_DIR`.  This is the default unless\n     -+\tthe configuration variable `fetch.writeFetchHEAD` is set to\n     -+\tfalse.  Passing `--no-write-fetch-head` from the command\n     -+\tline tells Git not to write the file.  Under `--dry-run`\n     -+\toption, the file is never written.\n     ++\tfile directly under `$GIT_DIR`.  This is the default.\n     ++\tPassing `--no-write-fetch-head` from the command line tells\n     ++\tGit not to write the file.  Under `--dry-run` option, the\n     ++\tfile is never written.\n      +endif::git-pull[]\n      +\n       -f::\n     @@ builtin/fetch.c: static int prune_tags = -1; /* unspecified */\n       static int verbosity, deepen_relative, set_upstream;\n       static int progress = -1;\n       static int enable_auto_gc = 1;\n     -@@ builtin/fetch.c: static int git_fetch_config(const char *k, const char *v, void *cb)\n     - \t\treturn 0;\n     - \t}\n     - \n     -+\tif (!strcmp(k, \"fetch.writefetchhead\")) {\n     -+\t\twrite_fetch_head = git_config_bool(k, v);\n     -+\t\treturn 0;\n     -+\t}\n     - \treturn git_default_config(k, v, cb);\n     - }\n     - \n      @@ builtin/fetch.c: static struct option builtin_fetch_options[] = {\n       \t\t    PARSE_OPT_OPTARG, option_fetch_parse_recurse_submodules),\n       \tOPT_BOOL(0, \"dry-run\", &dry_run,\n     @@ builtin/fetch.c: int cmd_fetch(int argc, const char **argv, const char *prefix)\n       \t\tif (argc == 1)\n       \t\t\tdie(_(\"fetch --all does not take a repository argument\"));\n      \n     - ## builtin/pull.c ##\n     -@@ builtin/pull.c: static int run_fetch(const char *repo, const char **refspecs)\n     - \tstruct strvec args = STRVEC_INIT;\n     - \tint ret;\n     - \n     --\tstrvec_pushl(&args, \"fetch\", \"--update-head-ok\", NULL);\n     -+\tstrvec_pushl(&args, \"fetch\", \"--update-head-ok\",\n     -+\t\t     \"--write-fetch-head\", NULL);\n     - \n     - \t/* Shared options */\n     - \targv_push_verbosity(&args);\n     -\n       ## t/t5510-fetch.sh ##\n      @@ t/t5510-fetch.sh: test_expect_success 'fetch into the current branch with --update-head-ok' '\n       \n     @@ t/t5510-fetch.sh: test_expect_success 'fetch into the current branch with --upda\n      +\tgit fetch --dry-run --write-fetch-head . &&\n      +\t! test -f .git/FETCH_HEAD\n      +'\n     -+\n     -+test_expect_success 'fetch.writeFetchHEAD and FETCH_HEAD' '\n     -+\trm -f .git/FETCH_HEAD &&\n     -+\tgit -c fetch.writeFetchHEAD=no fetch . &&\n     -+\t! test -f .git/FETCH_HEAD\n     -+'\n     -+\n     -+test_expect_success 'fetch.writeFetchHEAD gets defeated by --dry-run' '\n     -+\trm -f .git/FETCH_HEAD &&\n     -+\tgit -c fetch.writeFetchHEAD=yes fetch --dry-run . &&\n     -+\t! test -f .git/FETCH_HEAD\n     -+'\n     -+\n     -+test_expect_success 'fetch.writeFetchHEAD and --no-write-fetch-head' '\n     -+\trm -f .git/FETCH_HEAD &&\n     -+\tgit -c fetch.writeFetchHEAD=yes fetch --no-write-fetch-head . &&\n     -+\t! test -f .git/FETCH_HEAD\n     -+'\n     -+\n     -+test_expect_success 'fetch.writeFetchHEAD and --write-fetch-head' '\n     -+\trm -f .git/FETCH_HEAD &&\n     -+\tgit -c fetch.writeFetchHEAD=no fetch --write-fetch-head . &&\n     -+\ttest -f .git/FETCH_HEAD\n     -+'\n      +\n       test_expect_success \"should be able to fetch with duplicate refspecs\" '\n       \tmkdir dups &&\n       \t(\n      \n       ## t/t5521-pull-options.sh ##\n     -@@ t/t5521-pull-options.sh: test_expect_success 'git pull -q -v --no-rebase' '\n     - \ttest_must_be_empty out &&\n     - \ttest -s err)\n     - '\n     -+\n     - test_expect_success 'git pull --cleanup errors early on invalid argument' '\n     - \tmkdir clonedcleanup &&\n     - \t(cd clonedcleanup && git init &&\n      @@ t/t5521-pull-options.sh: test_expect_success 'git pull --cleanup errors early on invalid argument' '\n       \ttest -s err)\n       '\n     @@ t/t5521-pull-options.sh: test_expect_success 'git pull --cleanup errors early on\n      +test_expect_success 'git pull --no-write-fetch-head fails' '\n      +\tmkdir clonedwfh &&\n      +\t(cd clonedwfh && git init &&\n     -+\ttest_must_fail git pull --no-write-fetch-head \"../parent\" >out 2>err &&\n     ++\ttest_expect_code 129 git pull --no-write-fetch-head \"../parent\" >out 2>err &&\n      +\ttest_must_be_empty out &&\n      +\ttest_i18ngrep \"no-write-fetch-head\" err)\n     -+'\n     -+\n     -+test_expect_success 'git pull succeeds with fetch.writeFetchHEAD=false' '\n     -+\tmkdir clonedwfhconfig &&\n     -+\t(cd clonedwfhconfig && git init &&\n     -+\tgit config fetch.writeFetchHEAD false &&\n     -+\tgit pull \"../parent\" >out 2>err &&\n     -+\tgrep FETCH_HEAD err)\n      +'\n       \n       test_expect_success 'git pull --force' '\n  2:  85118ed5f1 !  2:  8779c6c20d maintenance: add prefetch task\n     @@ Documentation/git-maintenance.txt: since it will not expire `.graph` files that\n      +any object transfer.\n      +\n       gc::\n     - \tCleanup unnecessary files and optimize the local repository. \"GC\"\n     + \tClean up unnecessary files and optimize the local repository. \"GC\"\n       \tstands for \"garbage collection,\" but this task performs many\n      \n       ## builtin/gc.c ##\n     @@ builtin/gc.c: static int maintenance_task_commit_graph(struct maintenance_opts *\n      +\n      +\tchild.git_cmd = 1;\n      +\tstrvec_pushl(&child.args, \"fetch\", remote, \"--prune\", \"--no-tags\",\n     -+\t\t     \"--no-write-fetch-head\", \"--refmap=\", NULL);\n     ++\t\t     \"--no-write-fetch-head\", \"--recurse-submodules=no\",\n     ++\t\t     \"--refmap=\", NULL);\n      +\n      +\tif (opts->quiet)\n      +\t\tstrvec_push(&child.args, \"--quiet\");\n     @@ builtin/gc.c: static int maintenance_task_commit_graph(struct maintenance_opts *\n      +\treturn !!run_command(&child);\n      +}\n      +\n     -+static int fill_each_remote(struct remote *remote, void *cbdata)\n     ++static int append_remote(struct remote *remote, void *cbdata)\n      +{\n      +\tstruct string_list *remotes = (struct string_list *)cbdata;\n      +\n     @@ builtin/gc.c: static int maintenance_task_commit_graph(struct maintenance_opts *\n      +\tstruct string_list_item *item;\n      +\tstruct string_list remotes = STRING_LIST_INIT_DUP;\n      +\n     -+\tif (for_each_remote(fill_each_remote, &remotes)) {\n     ++\tif (for_each_remote(append_remote, &remotes)) {\n      +\t\terror(_(\"failed to fill remotes\"));\n      +\t\tresult = 1;\n      +\t\tgoto cleanup;\n      +\t}\n      +\n     -+\tfor (item = remotes.items;\n     -+\t     item && item < remotes.items + remotes.nr;\n     -+\t     item++)\n     ++\tfor_each_string_list_item(item, &remotes)\n      +\t\tresult |= fetch_remote(item->string, opts);\n      +\n      +cleanup:\n     @@ t/t7900-maintenance.sh: test_expect_success 'run --task duplicate' '\n      +\ttest_commit -C clone1 one &&\n      +\ttest_commit -C clone2 two &&\n      +\tGIT_TRACE2_EVENT=\"$(pwd)/run-prefetch.txt\" git maintenance run --task=prefetch 2>/dev/null &&\n     -+\tfetchargs=\"--prune --no-tags --no-write-fetch-head --refmap= --quiet\" &&\n     ++\tfetchargs=\"--prune --no-tags --no-write-fetch-head --recurse-submodules=no --refmap= --quiet\" &&\n      +\ttest_subcommand git fetch remote1 $fetchargs +refs/heads/\\\\*:refs/prefetch/remote1/\\\\* <run-prefetch.txt &&\n      +\ttest_subcommand git fetch remote2 $fetchargs +refs/heads/\\\\*:refs/prefetch/remote2/\\\\* <run-prefetch.txt &&\n      +\ttest_path_is_missing .git/refs/remotes &&\n     -+\ttest_cmp clone1/.git/refs/heads/one .git/refs/prefetch/remote1/one &&\n     -+\ttest_cmp clone2/.git/refs/heads/two .git/refs/prefetch/remote2/two &&\n      +\tgit log prefetch/remote1/one &&\n     -+\tgit log prefetch/remote2/two\n     ++\tgit log prefetch/remote2/two &&\n     ++\tgit fetch --all &&\n     ++\ttest_cmp_rev refs/remotes/remote1/one refs/prefetch/remote1/one &&\n     ++\ttest_cmp_rev refs/remotes/remote2/two refs/prefetch/remote2/two\n      +'\n      +\n       test_done\n  3:  621375a3c9 !  3:  4fa9d298b9 maintenance: add loose-objects task\n     @@ Commit message\n      \n       ## Documentation/git-maintenance.txt ##\n      @@ Documentation/git-maintenance.txt: gc::\n     - \tIt can also be disruptive in some situations, as it deletes stale\n     - \tdata.\n     + \tbe disruptive in some situations, as it deletes stale data. See\n     + \tlinkgit:git-gc[1] for more details on garbage collection in Git.\n       \n      +loose-objects::\n      +\tThe `loose-objects` job cleans up loose objects and places them into\n     @@ Documentation/git-maintenance.txt: gc::\n      +\tobject. Second, it creates a new pack-file (starting with \"loose-\")\n      +\tcontaining a batch of loose objects. The batch size is limited to 50\n      +\tthousand objects to prevent the job from taking too long on a\n     -+\trepository with many loose objects.\n     ++\trepository with many loose objects. The `gc` task writes unreachable\n     ++\tobjects as loose objects to be cleaned up by a later step only if\n     ++\tthey are not re-added to a pack-file; for this reason it is not\n     ++\tadvisable to enable both the `loose-objects` and `gc` tasks at the\n     ++\tsame time.\n      +\n       OPTIONS\n       -------\n     @@ builtin/gc.c: static struct maintenance_task tasks[] = {\n      \n       ## t/t7900-maintenance.sh ##\n      @@ t/t7900-maintenance.sh: test_expect_success 'prefetch multiple remotes' '\n     - \tgit log prefetch/remote2/two\n     + \ttest_cmp_rev refs/remotes/remote2/two refs/prefetch/remote2/two\n       '\n       \n      +test_expect_success 'loose-objects task' '\n  4:  e787403ea7 =  4:  3432bc3167 maintenance: create auto condition for loose-objects\n  5:  37e59b1a8d =  5:  0ee2434bdb midx: enable core.multiPackIndex by default\n  6:  aba087f663 =  6:  e157ea8dd7 midx: use start_delayed_progress()\n  7:  68727c555b !  7:  a8d956dad6 maintenance: add incremental-repack task\n     @@ Commit message\n      \n       ## Documentation/git-maintenance.txt ##\n      @@ Documentation/git-maintenance.txt: loose-objects::\n     - \tthousand objects to prevent the job from taking too long on a\n     - \trepository with many loose objects.\n     + \tadvisable to enable both the `loose-objects` and `gc` tasks at the\n     + \tsame time.\n       \n      +incremental-repack::\n      +\tThe `incremental-repack` job repacks the object directory\n  8:  c3487fb8e3 =  8:  f0e7276755 maintenance: auto-size incremental-repack batch\n  9:  407c123c51 !  9:  5659a23ad5 maintenance: add incremental-repack auto condition\n     @@ builtin/gc.c: static struct maintenance_task tasks[] = {\n       \t\t\"gc\",\n      \n       ## t/t7900-maintenance.sh ##\n     -@@ t/t7900-maintenance.sh: test_expect_success 'incremental-repack task' '\n     - '\n     - \n     - test_expect_success EXPENSIVE 'incremental-repack 2g limit' '\n     -+\n     - \tfor i in $(test_seq 1 5)\n     - \tdo\n     - \t\ttest-tool genrandom foo$i $((512 * 1024 * 1024 + 1)) >>big ||\n      @@ t/t7900-maintenance.sh: test_expect_success EXPENSIVE 'incremental-repack 2g limit' '\n       \t\t --no-progress --batch-size=2147483647 <run-2g.txt\n       '\n\n-- \ngitgitgadget\n"},{"id":"403947","messageId":"f3bc0b2d921a88e4a19f80645cfac5c5039a0450.1597760730.git.gitgitgadget@gmail.com","threadId":"54001","inReplyTo":"pull.696.v2.git.1597760730.gitgitgadget@gmail.com","subject":"[PATCH v2 1/9] fetch: optionally allow disabling FETCH_HEAD update","fromName":"Junio C Hamano via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2020-08-18T14:25:22Z","receivedAt":"2020-08-18T14:25:49Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"From: Junio C Hamano <gitster@pobox.com>\n\nIf you run fetch but record the result in remote-tracking branches,\nand either if you do nothing with the fetched refs (e.g. you are\nmerely mirroring) or if you always work from the remote-tracking\nrefs (e.g. you fetch and then merge origin/branchname separately),\nyou can get away with having no FETCH_HEAD at all.\n\nTeach \"git fetch\" a command line option \"--[no-]write-fetch-head\".\nThe default is to write FETCH_HEAD, and the option is primarily\nmeant to be used with the \"--no-\" prefix to override this default,\nbecause there is no matching fetch.writeFetchHEAD configuration\nvariable to flip the default to off (in which case, the positive\nform may become necessary to defeat it).\n\nNote that under \"--dry-run\" mode, FETCH_HEAD is never written;\notherwise you'd see list of objects in the file that you do not\nactually have.  Passing `--write-fetch-head` does not force `git\nfetch` to write the file.\n\nSigned-off-by: Derrick Stolee <dstolee@microsoft.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n Documentation/fetch-options.txt |  9 +++++++++\n builtin/fetch.c                 | 15 ++++++++++++---\n t/t5510-fetch.sh                | 15 +++++++++++++--\n t/t5521-pull-options.sh         |  7 +++++++\n 4 files changed, 41 insertions(+), 5 deletions(-)\n\ndiff --git a/Documentation/fetch-options.txt b/Documentation/fetch-options.txt\nindex 495bc8ab5a..b65a758661 100644\n--- a/Documentation/fetch-options.txt\n+++ b/Documentation/fetch-options.txt\n@@ -64,6 +64,15 @@ documented in linkgit:git-config[1].\n --dry-run::\n \tShow what would be done, without making any changes.\n \n+ifndef::git-pull[]\n+--[no-]write-fetch-head::\n+\tWrite the list of remote refs fetched in the `FETCH_HEAD`\n+\tfile directly under `$GIT_DIR`.  This is the default.\n+\tPassing `--no-write-fetch-head` from the command line tells\n+\tGit not to write the file.  Under `--dry-run` option, the\n+\tfile is never written.\n+endif::git-pull[]\n+\n -f::\n --force::\n \tWhen 'git fetch' is used with `<src>:<dst>` refspec it may\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex c8b9366d3c..cb38e6f5ec 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -56,6 +56,7 @@ static int prune_tags = -1; /* unspecified */\n #define PRUNE_TAGS_BY_DEFAULT 0 /* do we prune tags by default? */\n \n static int all, append, dry_run, force, keep, multiple, update_head_ok;\n+static int write_fetch_head = 1;\n static int verbosity, deepen_relative, set_upstream;\n static int progress = -1;\n static int enable_auto_gc = 1;\n@@ -162,6 +163,8 @@ static struct option builtin_fetch_options[] = {\n \t\t    PARSE_OPT_OPTARG, option_fetch_parse_recurse_submodules),\n \tOPT_BOOL(0, \"dry-run\", &dry_run,\n \t\t N_(\"dry run\")),\n+\tOPT_BOOL(0, \"write-fetch-head\", &write_fetch_head,\n+\t\t N_(\"write fetched references to the FETCH_HEAD file\")),\n \tOPT_BOOL('k', \"keep\", &keep, N_(\"keep downloaded pack\")),\n \tOPT_BOOL('u', \"update-head-ok\", &update_head_ok,\n \t\t    N_(\"allow updating of HEAD ref\")),\n@@ -895,7 +898,9 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,\n \tconst char *what, *kind;\n \tstruct ref *rm;\n \tchar *url;\n-\tconst char *filename = dry_run ? \"/dev/null\" : git_path_fetch_head(the_repository);\n+\tconst char *filename = (!write_fetch_head\n+\t\t\t\t? \"/dev/null\"\n+\t\t\t\t: git_path_fetch_head(the_repository));\n \tint want_status;\n \tint summary_width = transport_summary_width(ref_map);\n \n@@ -1329,7 +1334,7 @@ static int do_fetch(struct transport *transport,\n \t}\n \n \t/* if not appending, truncate FETCH_HEAD */\n-\tif (!append && !dry_run) {\n+\tif (!append && write_fetch_head) {\n \t\tretcode = truncate_fetch_head();\n \t\tif (retcode)\n \t\t\tgoto cleanup;\n@@ -1596,7 +1601,7 @@ static int fetch_multiple(struct string_list *list, int max_children)\n \tint i, result = 0;\n \tstruct strvec argv = STRVEC_INIT;\n \n-\tif (!append && !dry_run) {\n+\tif (!append && write_fetch_head) {\n \t\tint errcode = truncate_fetch_head();\n \t\tif (errcode)\n \t\t\treturn errcode;\n@@ -1797,6 +1802,10 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)\n \tif (depth || deepen_since || deepen_not.nr)\n \t\tdeepen = 1;\n \n+\t/* FETCH_HEAD never gets updated in --dry-run mode */\n+\tif (dry_run)\n+\t\twrite_fetch_head = 0;\n+\n \tif (all) {\n \t\tif (argc == 1)\n \t\t\tdie(_(\"fetch --all does not take a repository argument\"));\ndiff --git a/t/t5510-fetch.sh b/t/t5510-fetch.sh\nindex 9850ecde5d..5bd1f953af 100755\n--- a/t/t5510-fetch.sh\n+++ b/t/t5510-fetch.sh\n@@ -539,13 +539,24 @@ test_expect_success 'fetch into the current branch with --update-head-ok' '\n \n '\n \n-test_expect_success 'fetch --dry-run' '\n-\n+test_expect_success 'fetch --dry-run does not touch FETCH_HEAD' '\n \trm -f .git/FETCH_HEAD &&\n \tgit fetch --dry-run . &&\n \t! test -f .git/FETCH_HEAD\n '\n \n+test_expect_success '--no-write-fetch-head does not touch FETCH_HEAD' '\n+\trm -f .git/FETCH_HEAD &&\n+\tgit fetch --no-write-fetch-head . &&\n+\t! test -f .git/FETCH_HEAD\n+'\n+\n+test_expect_success '--write-fetch-head gets defeated by --dry-run' '\n+\trm -f .git/FETCH_HEAD &&\n+\tgit fetch --dry-run --write-fetch-head . &&\n+\t! test -f .git/FETCH_HEAD\n+'\n+\n test_expect_success \"should be able to fetch with duplicate refspecs\" '\n \tmkdir dups &&\n \t(\ndiff --git a/t/t5521-pull-options.sh b/t/t5521-pull-options.sh\nindex 159afa7ac8..db1a381cd9 100755\n--- a/t/t5521-pull-options.sh\n+++ b/t/t5521-pull-options.sh\n@@ -85,6 +85,13 @@ test_expect_success 'git pull --cleanup errors early on invalid argument' '\n \ttest -s err)\n '\n \n+test_expect_success 'git pull --no-write-fetch-head fails' '\n+\tmkdir clonedwfh &&\n+\t(cd clonedwfh && git init &&\n+\ttest_expect_code 129 git pull --no-write-fetch-head \"../parent\" >out 2>err &&\n+\ttest_must_be_empty out &&\n+\ttest_i18ngrep \"no-write-fetch-head\" err)\n+'\n \n test_expect_success 'git pull --force' '\n \tmkdir clonedoldstyle &&\n-- \ngitgitgadget\n\n"},{"id":"403948","messageId":"4fa9d298b98fa8b83108d9d9c58aef0eab934ee5.1597760730.git.gitgitgadget@gmail.com","threadId":"54001","inReplyTo":"pull.696.v2.git.1597760730.gitgitgadget@gmail.com","subject":"[PATCH v2 3/9] maintenance: add loose-objects task","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2020-08-18T14:25:24Z","receivedAt":"2020-08-18T14:25:52Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"From: Derrick Stolee <dstolee@microsoft.com>\n\nOne goal of background maintenance jobs is to allow a user to\ndisable auto-gc (gc.auto=0) but keep their repository in a clean\nstate. Without any cleanup, loose objects will clutter the object\ndatabase and slow operations. In addition, the loose objects will\ntake up extra space because they are not stored with deltas against\nsimilar objects.\n\nCreate a 'loose-objects' task for the 'git maintenance run' command.\nThis helps clean up loose objects without disrupting concurrent Git\ncommands using the following sequence of events:\n\n1. Run 'git prune-packed' to delete any loose objects that exist\n   in a pack-file. Concurrent commands will prefer the packed\n   version of the object to the loose version. (Of course, there\n   are exceptions for commands that specifically care about the\n   location of an object. These are rare for a user to run on\n   purpose, and we hope a user that has selected background\n   maintenance will not be trying to do foreground maintenance.)\n\n2. Run 'git pack-objects' on a batch of loose objects. These\n   objects are grouped by scanning the loose object directories in\n   lexicographic order until listing all loose objects -or-\n   reaching 50,000 objects. This is more than enough if the loose\n   objects are created only by a user doing normal development.\n   We noticed users with _millions_ of loose objects because VFS\n   for Git downloads blobs on-demand when a file read operation\n   requires populating a virtual file. This has potential of\n   happening in partial clones if someone runs 'git grep' or\n   otherwise evades the batch-download feature for requesting\n   promisor objects.\n\nThis step is based on a similar step in Scalar [1] and VFS for Git.\n[1] https://github.com/microsoft/scalar/blob/master/Scalar.Common/Maintenance/LooseObjectsStep.cs\n\nSigned-off-by: Derrick Stolee <dstolee@microsoft.com>\n---\n Documentation/git-maintenance.txt | 15 +++++\n builtin/gc.c                      | 97 +++++++++++++++++++++++++++++++\n t/t7900-maintenance.sh            | 39 +++++++++++++\n 3 files changed, 151 insertions(+)\n\ndiff --git a/Documentation/git-maintenance.txt b/Documentation/git-maintenance.txt\nindex e82799ccff..85205b5bb9 100644\n--- a/Documentation/git-maintenance.txt\n+++ b/Documentation/git-maintenance.txt\n@@ -73,6 +73,21 @@ gc::\n \tbe disruptive in some situations, as it deletes stale data. See\n \tlinkgit:git-gc[1] for more details on garbage collection in Git.\n \n+loose-objects::\n+\tThe `loose-objects` job cleans up loose objects and places them into\n+\tpack-files. In order to prevent race conditions with concurrent Git\n+\tcommands, it follows a two-step process. First, it deletes any loose\n+\tobjects that already exist in a pack-file; concurrent Git processes\n+\twill examine the pack-file for the object data instead of the loose\n+\tobject. Second, it creates a new pack-file (starting with \"loose-\")\n+\tcontaining a batch of loose objects. The batch size is limited to 50\n+\tthousand objects to prevent the job from taking too long on a\n+\trepository with many loose objects. The `gc` task writes unreachable\n+\tobjects as loose objects to be cleaned up by a later step only if\n+\tthey are not re-added to a pack-file; for this reason it is not\n+\tadvisable to enable both the `loose-objects` and `gc` tasks at the\n+\tsame time.\n+\n OPTIONS\n -------\n --auto::\ndiff --git a/builtin/gc.c b/builtin/gc.c\nindex 2ac08cc740..39ea590b5a 100644\n--- a/builtin/gc.c\n+++ b/builtin/gc.c\n@@ -907,6 +907,98 @@ static int maintenance_task_gc(struct maintenance_opts *opts)\n \treturn run_command(&child);\n }\n \n+static int prune_packed(struct maintenance_opts *opts)\n+{\n+\tstruct child_process child = CHILD_PROCESS_INIT;\n+\n+\tchild.git_cmd = 1;\n+\tstrvec_push(&child.args, \"prune-packed\");\n+\n+\tif (opts->quiet)\n+\t\tstrvec_push(&child.args, \"--quiet\");\n+\n+\treturn !!run_command(&child);\n+}\n+\n+struct write_loose_object_data {\n+\tFILE *in;\n+\tint count;\n+\tint batch_size;\n+};\n+\n+static int bail_on_loose(const struct object_id *oid,\n+\t\t\t const char *path,\n+\t\t\t void *data)\n+{\n+\treturn 1;\n+}\n+\n+static int write_loose_object_to_stdin(const struct object_id *oid,\n+\t\t\t\t       const char *path,\n+\t\t\t\t       void *data)\n+{\n+\tstruct write_loose_object_data *d = (struct write_loose_object_data *)data;\n+\n+\tfprintf(d->in, \"%s\\n\", oid_to_hex(oid));\n+\n+\treturn ++(d->count) > d->batch_size;\n+}\n+\n+static int pack_loose(struct maintenance_opts *opts)\n+{\n+\tstruct repository *r = the_repository;\n+\tint result = 0;\n+\tstruct write_loose_object_data data;\n+\tstruct child_process pack_proc = CHILD_PROCESS_INIT;\n+\n+\t/*\n+\t * Do not start pack-objects process\n+\t * if there are no loose objects.\n+\t */\n+\tif (!for_each_loose_file_in_objdir(r->objects->odb->path,\n+\t\t\t\t\t   bail_on_loose,\n+\t\t\t\t\t   NULL, NULL, NULL))\n+\t\treturn 0;\n+\n+\tpack_proc.git_cmd = 1;\n+\n+\tstrvec_push(&pack_proc.args, \"pack-objects\");\n+\tif (opts->quiet)\n+\t\tstrvec_push(&pack_proc.args, \"--quiet\");\n+\tstrvec_pushf(&pack_proc.args, \"%s/pack/loose\", r->objects->odb->path);\n+\n+\tpack_proc.in = -1;\n+\n+\tif (start_command(&pack_proc)) {\n+\t\terror(_(\"failed to start 'git pack-objects' process\"));\n+\t\treturn 1;\n+\t}\n+\n+\tdata.in = xfdopen(pack_proc.in, \"w\");\n+\tdata.count = 0;\n+\tdata.batch_size = 50000;\n+\n+\tfor_each_loose_file_in_objdir(r->objects->odb->path,\n+\t\t\t\t      write_loose_object_to_stdin,\n+\t\t\t\t      NULL,\n+\t\t\t\t      NULL,\n+\t\t\t\t      &data);\n+\n+\tfclose(data.in);\n+\n+\tif (finish_command(&pack_proc)) {\n+\t\terror(_(\"failed to finish 'git pack-objects' process\"));\n+\t\tresult = 1;\n+\t}\n+\n+\treturn result;\n+}\n+\n+static int maintenance_task_loose_objects(struct maintenance_opts *opts)\n+{\n+\treturn prune_packed(opts) || pack_loose(opts);\n+}\n+\n typedef int maintenance_task_fn(struct maintenance_opts *opts);\n \n /*\n@@ -927,6 +1019,7 @@ struct maintenance_task {\n \n enum maintenance_task_label {\n \tTASK_PREFETCH,\n+\tTASK_LOOSE_OBJECTS,\n \tTASK_GC,\n \tTASK_COMMIT_GRAPH,\n \n@@ -939,6 +1032,10 @@ static struct maintenance_task tasks[] = {\n \t\t\"prefetch\",\n \t\tmaintenance_task_prefetch,\n \t},\n+\t[TASK_LOOSE_OBJECTS] = {\n+\t\t\"loose-objects\",\n+\t\tmaintenance_task_loose_objects,\n+\t},\n \t[TASK_GC] = {\n \t\t\"gc\",\n \t\tmaintenance_task_gc,\ndiff --git a/t/t7900-maintenance.sh b/t/t7900-maintenance.sh\nindex 0bade09c43..2e9e369786 100755\n--- a/t/t7900-maintenance.sh\n+++ b/t/t7900-maintenance.sh\n@@ -86,4 +86,43 @@ test_expect_success 'prefetch multiple remotes' '\n \ttest_cmp_rev refs/remotes/remote2/two refs/prefetch/remote2/two\n '\n \n+test_expect_success 'loose-objects task' '\n+\t# Repack everything so we know the state of the object dir\n+\tgit repack -adk &&\n+\n+\t# Hack to stop maintenance from running during \"git commit\"\n+\techo in use >.git/objects/maintenance.lock &&\n+\n+\t# Assuming that \"git commit\" creates at least one loose object\n+\ttest_commit create-loose-object &&\n+\trm .git/objects/maintenance.lock &&\n+\n+\tls .git/objects >obj-dir-before &&\n+\ttest_file_not_empty obj-dir-before &&\n+\tls .git/objects/pack/*.pack >packs-before &&\n+\ttest_line_count = 1 packs-before &&\n+\n+\t# The first run creates a pack-file\n+\t# but does not delete loose objects.\n+\tgit maintenance run --task=loose-objects &&\n+\tls .git/objects >obj-dir-between &&\n+\ttest_cmp obj-dir-before obj-dir-between &&\n+\tls .git/objects/pack/*.pack >packs-between &&\n+\ttest_line_count = 2 packs-between &&\n+\tls .git/objects/pack/loose-*.pack >loose-packs &&\n+\ttest_line_count = 1 loose-packs &&\n+\n+\t# The second run deletes loose objects\n+\t# but does not create a pack-file.\n+\tgit maintenance run --task=loose-objects &&\n+\tls .git/objects >obj-dir-after &&\n+\tcat >expect <<-\\EOF &&\n+\tinfo\n+\tpack\n+\tEOF\n+\ttest_cmp expect obj-dir-after &&\n+\tls .git/objects/pack/*.pack >packs-after &&\n+\ttest_cmp packs-between packs-after\n+'\n+\n test_done\n-- \ngitgitgadget\n\n"},{"id":"403949","messageId":"0ee2434bdbabc6f3ed6e80402aa3a19bf9626fc6.1597760730.git.gitgitgadget@gmail.com","threadId":"54001","inReplyTo":"pull.696.v2.git.1597760730.gitgitgadget@gmail.com","subject":"[PATCH v2 5/9] midx: enable core.multiPackIndex by default","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2020-08-18T14:25:26Z","receivedAt":"2020-08-18T14:25:55Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"From: Derrick Stolee <dstolee@microsoft.com>\n\nThe core.multiPackIndex setting has been around since c4d25228ebb\n(config: create core.multiPackIndex setting, 2018-07-12), but has been\ndisabled by default. If a user wishes to use the multi-pack-index\nfeature, then they must enable this config and run 'git multi-pack-index\nwrite'.\n\nThe multi-pack-index feature is relatively stable now, so make the\nconfig option true by default. For users that do not use a\nmulti-pack-index, the only extra cost will be a file lookup to see if a\nmulti-pack-index file exists (once per process, per object directory).\n\nAlso, this config option will be referenced by an upcoming\n\"incremental-repack\" task in the maintenance builtin, so move the config\noption into the repository settings struct. Note that if\nGIT_TEST_MULTI_PACK_INDEX=1, then we want to ignore the config option\nand treat core.multiPackIndex as enabled.\n\nSigned-off-by: Derrick Stolee <dstolee@microsoft.com>\n---\n Documentation/config/core.txt |  4 ++--\n midx.c                        | 11 +++--------\n repo-settings.c               |  6 ++++++\n repository.h                  |  2 ++\n 4 files changed, 13 insertions(+), 10 deletions(-)\n\ndiff --git a/Documentation/config/core.txt b/Documentation/config/core.txt\nindex 74619a9c03..86c91d5381 100644\n--- a/Documentation/config/core.txt\n+++ b/Documentation/config/core.txt\n@@ -606,8 +606,8 @@ core.useReplaceRefs::\n \n core.multiPackIndex::\n \tUse the multi-pack-index file to track multiple packfiles using a\n-\tsingle index. See link:technical/multi-pack-index.html[the\n-\tmulti-pack-index design document].\n+\tsingle index. See linkgit:git-multi-pack-index[1] for more\n+\tinformation. Defaults to true.\n \n core.sparseCheckout::\n \tEnable \"sparse checkout\" feature. See linkgit:git-sparse-checkout[1]\ndiff --git a/midx.c b/midx.c\nindex a5fb797ede..ef499cf504 100644\n--- a/midx.c\n+++ b/midx.c\n@@ -10,6 +10,7 @@\n #include \"progress.h\"\n #include \"trace2.h\"\n #include \"run-command.h\"\n+#include \"repository.h\"\n \n #define MIDX_SIGNATURE 0x4d494458 /* \"MIDX\" */\n #define MIDX_VERSION 1\n@@ -384,15 +385,9 @@ int prepare_multi_pack_index_one(struct repository *r, const char *object_dir, i\n {\n \tstruct multi_pack_index *m;\n \tstruct multi_pack_index *m_search;\n-\tint config_value;\n-\tstatic int env_value = -1;\n \n-\tif (env_value < 0)\n-\t\tenv_value = git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0);\n-\n-\tif (!env_value &&\n-\t    (repo_config_get_bool(r, \"core.multipackindex\", &config_value) ||\n-\t    !config_value))\n+\tprepare_repo_settings(r);\n+\tif (!r->settings.core_multi_pack_index)\n \t\treturn 0;\n \n \tfor (m_search = r->objects->multi_pack_index; m_search; m_search = m_search->next)\ndiff --git a/repo-settings.c b/repo-settings.c\nindex 0918408b34..5bd2c22726 100644\n--- a/repo-settings.c\n+++ b/repo-settings.c\n@@ -1,6 +1,7 @@\n #include \"cache.h\"\n #include \"config.h\"\n #include \"repository.h\"\n+#include \"midx.h\"\n \n #define UPDATE_DEFAULT_BOOL(s,v) do { if (s == -1) { s = v; } } while(0)\n \n@@ -47,6 +48,11 @@ void prepare_repo_settings(struct repository *r)\n \t\tr->settings.pack_use_sparse = value;\n \tUPDATE_DEFAULT_BOOL(r->settings.pack_use_sparse, 1);\n \n+\tvalue = git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0);\n+\tif (value || !repo_config_get_bool(r, \"core.multipackindex\", &value))\n+\t\tr->settings.core_multi_pack_index = value;\n+\tUPDATE_DEFAULT_BOOL(r->settings.core_multi_pack_index, 1);\n+\n \tif (!repo_config_get_bool(r, \"feature.manyfiles\", &value) && value) {\n \t\tUPDATE_DEFAULT_BOOL(r->settings.index_version, 4);\n \t\tUPDATE_DEFAULT_BOOL(r->settings.core_untracked_cache, UNTRACKED_CACHE_WRITE);\ndiff --git a/repository.h b/repository.h\nindex 3c1f7d54bd..3901ce0b65 100644\n--- a/repository.h\n+++ b/repository.h\n@@ -37,6 +37,8 @@ struct repo_settings {\n \n \tint pack_use_sparse;\n \tenum fetch_negotiation_setting fetch_negotiation_algorithm;\n+\n+\tint core_multi_pack_index;\n };\n \n struct repository {\n-- \ngitgitgadget\n\n"},{"id":"403950","messageId":"8779c6c20d7e25e13189074dbd57a86b49ec56e9.1597760730.git.gitgitgadget@gmail.com","threadId":"54001","inReplyTo":"pull.696.v2.git.1597760730.gitgitgadget@gmail.com","subject":"[PATCH v2 2/9] maintenance: add prefetch task","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2020-08-18T14:25:23Z","receivedAt":"2020-08-18T14:26:05Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"From: Derrick Stolee <dstolee@microsoft.com>\n\nWhen working with very large repositories, an incremental 'git fetch'\ncommand can download a large amount of data. If there are many other\nusers pushing to a common repo, then this data can rival the initial\npack-file size of a 'git clone' of a medium-size repo.\n\nUsers may want to keep the data on their local repos as close as\npossible to the data on the remote repos by fetching periodically in\nthe background. This can break up a large daily fetch into several\nsmaller hourly fetches.\n\nThe task is called \"prefetch\" because it is work done in advance\nof a foreground fetch to make that 'git fetch' command much faster.\n\nHowever, if we simply ran 'git fetch <remote>' in the background,\nthen the user running a foregroudn 'git fetch <remote>' would lose\nsome important feedback when a new branch appears or an existing\nbranch updates. This is especially true if a remote branch is\nforce-updated and this isn't noticed by the user because it occurred\nin the background. Further, the functionality of 'git push\n--force-with-lease' becomes suspect.\n\nWhen running 'git fetch <remote> <options>' in the background, use\nthe following options for careful updating:\n\n1. --no-tags prevents getting a new tag when a user wants to see\n   the new tags appear in their foreground fetches.\n\n2. --refmap= removes the configured refspec which usually updates\n   refs/remotes/<remote>/* with the refs advertised by the remote.\n   While this looks confusing, this was documented and tested by\n   b40a50264ac (fetch: document and test --refmap=\"\", 2020-01-21),\n   including this sentence in the documentation:\n\n\tProviding an empty `<refspec>` to the `--refmap` option\n\tcauses Git to ignore the configured refspecs and rely\n\tentirely on the refspecs supplied as command-line arguments.\n\n3. By adding a new refspec \"+refs/heads/*:refs/prefetch/<remote>/*\"\n   we can ensure that we actually load the new values somewhere in\n   our refspace while not updating refs/heads or refs/remotes. By\n   storing these refs here, the commit-graph job will update the\n   commit-graph with the commits from these hidden refs.\n\n4. --prune will delete the refs/prefetch/<remote> refs that no\n   longer appear on the remote.\n\n5. --no-write-fetch-head prevents updating FETCH_HEAD.\n\nWe've been using this step as a critical background job in Scalar\n[1] (and VFS for Git). This solved a pain point that was showing up\nin user reports: fetching was a pain! Users do not like waiting to\ndownload the data that was created while they were away from their\nmachines. After implementing background fetch, the foreground fetch\ncommands sped up significantly because they mostly just update refs\nand download a small amount of new data. The effect is especially\ndramatic when paried with --no-show-forced-udpates (through\nfetch.showForcedUpdates=false).\n\n[1] https://github.com/microsoft/scalar/blob/master/Scalar.Common/Maintenance/FetchStep.cs\n\nSigned-off-by: Derrick Stolee <dstolee@microsoft.com>\n---\n Documentation/git-maintenance.txt | 15 +++++++++\n builtin/gc.c                      | 51 +++++++++++++++++++++++++++++++\n t/t7900-maintenance.sh            | 26 ++++++++++++++++\n 3 files changed, 92 insertions(+)\n\ndiff --git a/Documentation/git-maintenance.txt b/Documentation/git-maintenance.txt\nindex 9af08c644f..e82799ccff 100644\n--- a/Documentation/git-maintenance.txt\n+++ b/Documentation/git-maintenance.txt\n@@ -50,6 +50,21 @@ since it will not expire `.graph` files that were in the previous\n `commit-graph-chain` file. They will be deleted by a later run based on\n the expiration delay.\n \n+prefetch::\n+\tThe `prefetch` task updates the object directory with the latest\n+\tobjects from all registered remotes. For each remote, a `git fetch`\n+\tcommand is run. The refmap is custom to avoid updating local or remote\n+\tbranches (those in `refs/heads` or `refs/remotes`). Instead, the\n+\tremote refs are stored in `refs/prefetch/<remote>/`. Also, tags are\n+\tnot updated.\n++\n+This is done to avoid disrupting the remote-tracking branches. The end users\n+expect these refs to stay unmoved unless they initiate a fetch.  With prefetch\n+task, however, the objects necessary to complete a later real fetch would\n+already be obtained, so the real fetch would go faster.  In the ideal case,\n+it will just become an update to bunch of remote-tracking branches without\n+any object transfer.\n+\n gc::\n \tClean up unnecessary files and optimize the local repository. \"GC\"\n \tstands for \"garbage collection,\" but this task performs many\ndiff --git a/builtin/gc.c b/builtin/gc.c\nindex 3fdb08655c..2ac08cc740 100644\n--- a/builtin/gc.c\n+++ b/builtin/gc.c\n@@ -29,6 +29,7 @@\n #include \"tree.h\"\n #include \"promisor-remote.h\"\n #include \"refs.h\"\n+#include \"remote.h\"\n \n #define FAILED_RUN \"failed to run %s\"\n \n@@ -843,6 +844,51 @@ static int maintenance_task_commit_graph(struct maintenance_opts *opts)\n \treturn 1;\n }\n \n+static int fetch_remote(const char *remote, struct maintenance_opts *opts)\n+{\n+\tstruct child_process child = CHILD_PROCESS_INIT;\n+\n+\tchild.git_cmd = 1;\n+\tstrvec_pushl(&child.args, \"fetch\", remote, \"--prune\", \"--no-tags\",\n+\t\t     \"--no-write-fetch-head\", \"--recurse-submodules=no\",\n+\t\t     \"--refmap=\", NULL);\n+\n+\tif (opts->quiet)\n+\t\tstrvec_push(&child.args, \"--quiet\");\n+\n+\tstrvec_pushf(&child.args, \"+refs/heads/*:refs/prefetch/%s/*\", remote);\n+\n+\treturn !!run_command(&child);\n+}\n+\n+static int append_remote(struct remote *remote, void *cbdata)\n+{\n+\tstruct string_list *remotes = (struct string_list *)cbdata;\n+\n+\tstring_list_append(remotes, remote->name);\n+\treturn 0;\n+}\n+\n+static int maintenance_task_prefetch(struct maintenance_opts *opts)\n+{\n+\tint result = 0;\n+\tstruct string_list_item *item;\n+\tstruct string_list remotes = STRING_LIST_INIT_DUP;\n+\n+\tif (for_each_remote(append_remote, &remotes)) {\n+\t\terror(_(\"failed to fill remotes\"));\n+\t\tresult = 1;\n+\t\tgoto cleanup;\n+\t}\n+\n+\tfor_each_string_list_item(item, &remotes)\n+\t\tresult |= fetch_remote(item->string, opts);\n+\n+cleanup:\n+\tstring_list_clear(&remotes, 0);\n+\treturn result;\n+}\n+\n static int maintenance_task_gc(struct maintenance_opts *opts)\n {\n \tstruct child_process child = CHILD_PROCESS_INIT;\n@@ -880,6 +926,7 @@ struct maintenance_task {\n };\n \n enum maintenance_task_label {\n+\tTASK_PREFETCH,\n \tTASK_GC,\n \tTASK_COMMIT_GRAPH,\n \n@@ -888,6 +935,10 @@ enum maintenance_task_label {\n };\n \n static struct maintenance_task tasks[] = {\n+\t[TASK_PREFETCH] = {\n+\t\t\"prefetch\",\n+\t\tmaintenance_task_prefetch,\n+\t},\n \t[TASK_GC] = {\n \t\t\"gc\",\n \t\tmaintenance_task_gc,\ndiff --git a/t/t7900-maintenance.sh b/t/t7900-maintenance.sh\nindex 4f6a04ddb1..0bade09c43 100755\n--- a/t/t7900-maintenance.sh\n+++ b/t/t7900-maintenance.sh\n@@ -60,4 +60,30 @@ test_expect_success 'run --task duplicate' '\n \ttest_i18ngrep \"cannot be selected multiple times\" err\n '\n \n+test_expect_success 'run --task=prefetch with no remotes' '\n+\tgit maintenance run --task=prefetch 2>err &&\n+\ttest_must_be_empty err\n+'\n+\n+test_expect_success 'prefetch multiple remotes' '\n+\tgit clone . clone1 &&\n+\tgit clone . clone2 &&\n+\tgit remote add remote1 \"file://$(pwd)/clone1\" &&\n+\tgit remote add remote2 \"file://$(pwd)/clone2\" &&\n+\tgit -C clone1 switch -c one &&\n+\tgit -C clone2 switch -c two &&\n+\ttest_commit -C clone1 one &&\n+\ttest_commit -C clone2 two &&\n+\tGIT_TRACE2_EVENT=\"$(pwd)/run-prefetch.txt\" git maintenance run --task=prefetch 2>/dev/null &&\n+\tfetchargs=\"--prune --no-tags --no-write-fetch-head --recurse-submodules=no --refmap= --quiet\" &&\n+\ttest_subcommand git fetch remote1 $fetchargs +refs/heads/\\\\*:refs/prefetch/remote1/\\\\* <run-prefetch.txt &&\n+\ttest_subcommand git fetch remote2 $fetchargs +refs/heads/\\\\*:refs/prefetch/remote2/\\\\* <run-prefetch.txt &&\n+\ttest_path_is_missing .git/refs/remotes &&\n+\tgit log prefetch/remote1/one &&\n+\tgit log prefetch/remote2/two &&\n+\tgit fetch --all &&\n+\ttest_cmp_rev refs/remotes/remote1/one refs/prefetch/remote1/one &&\n+\ttest_cmp_rev refs/remotes/remote2/two refs/prefetch/remote2/two\n+'\n+\n test_done\n-- \ngitgitgadget\n\n"},{"id":"403951","messageId":"f0e727675584c60046a0fdd7c63a57ec1cbaf150.1597760730.git.gitgitgadget@gmail.com","threadId":"54001","inReplyTo":"pull.696.v2.git.1597760730.gitgitgadget@gmail.com","subject":"[PATCH v2 8/9] maintenance: auto-size incremental-repack batch","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2020-08-18T14:25:29Z","receivedAt":"2020-08-18T14:26:07Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"From: Derrick Stolee <dstolee@microsoft.com>\n\nWhen repacking during the 'incremental-repack' task, we use the\n--batch-size option in 'git multi-pack-index repack'. The initial setting\nused --batch-size=0 to repack everything into a single pack-file. This is\nnot sustainable for a large repository. The amount of work required is\nalso likely to use too many system resources for a background job.\n\nUpdate the 'incremental-repack' task by dynamically computing a\n--batch-size option based on the current pack-file structure.\n\nThe dynamic default size is computed with this idea in mind for a client\nrepository that was cloned from a very large remote: there is likely one\n\"big\" pack-file that was created at clone time. Thus, do not try\nrepacking it as it is likely packed efficiently by the server.\n\nInstead, we select the second-largest pack-file, and create a batch size\nthat is one larger than that pack-file. If there are three or more\npack-files, then this guarantees that at least two will be combined into\na new pack-file.\n\nOf course, this means that the second-largest pack-file size is likely\nto grow over time and may eventually surpass the initially-cloned\npack-file. Recall that the pack-file batch is selected in a greedy\nmanner: the packs are considered from oldest to newest and are selected\nif they have size smaller than the batch size until the total selected\nsize is larger than the batch size. Thus, that oldest \"clone\" pack will\nbe first to repack after the new data creates a pack larger than that.\n\nWe also want to place some limits on how large these pack-files become,\nin order to bound the amount of time spent repacking. A maximum\nbatch-size of two gigabytes means that large repositories will never be\npacked into a single pack-file using this job, but also that repack is\nrather expensive. This is a trade-off that is valuable to have if the\nmaintenance is being run automatically or in the background. Users who\ntruly want to optimize for space and performance (and are willing to pay\nthe upfront cost of a full repack) can use the 'gc' task to do so.\n\nCreate a test for this two gigabyte limit by creating an EXPENSIVE test\nthat generates two pack-files of roughly 2.5 gigabytes in size, then\nperforms an incremental repack. Check that the --batch-size argument in\nthe subcommand uses the hard-coded maximum.\n\nHelped-by: Chris Torek <chris.torek@gmail.com>\nReported-by: Son Luong Ngoc <sluongng@gmail.com>\nSigned-off-by: Derrick Stolee <dstolee@microsoft.com>\n---\n builtin/gc.c           | 43 +++++++++++++++++++++++++++++++++++++++++-\n t/t7900-maintenance.sh | 36 +++++++++++++++++++++++++++++++++--\n 2 files changed, 76 insertions(+), 3 deletions(-)\n\ndiff --git a/builtin/gc.c b/builtin/gc.c\nindex 9aabef44a8..d2f3e27e54 100644\n--- a/builtin/gc.c\n+++ b/builtin/gc.c\n@@ -1093,6 +1093,46 @@ static int multi_pack_index_expire(struct maintenance_opts *opts)\n \treturn 0;\n }\n \n+#define TWO_GIGABYTES (INT32_MAX)\n+\n+static off_t get_auto_pack_size(void)\n+{\n+\t/*\n+\t * The \"auto\" value is special: we optimize for\n+\t * one large pack-file (i.e. from a clone) and\n+\t * expect the rest to be small and they can be\n+\t * repacked quickly.\n+\t *\n+\t * The strategy we select here is to select a\n+\t * size that is one more than the second largest\n+\t * pack-file. This ensures that we will repack\n+\t * at least two packs if there are three or more\n+\t * packs.\n+\t */\n+\toff_t max_size = 0;\n+\toff_t second_largest_size = 0;\n+\toff_t result_size;\n+\tstruct packed_git *p;\n+\tstruct repository *r = the_repository;\n+\n+\treprepare_packed_git(r);\n+\tfor (p = get_all_packs(r); p; p = p->next) {\n+\t\tif (p->pack_size > max_size) {\n+\t\t\tsecond_largest_size = max_size;\n+\t\t\tmax_size = p->pack_size;\n+\t\t} else if (p->pack_size > second_largest_size)\n+\t\t\tsecond_largest_size = p->pack_size;\n+\t}\n+\n+\tresult_size = second_largest_size + 1;\n+\n+\t/* But limit ourselves to a batch size of 2g */\n+\tif (result_size > TWO_GIGABYTES)\n+\t\tresult_size = TWO_GIGABYTES;\n+\n+\treturn result_size;\n+}\n+\n static int multi_pack_index_repack(struct maintenance_opts *opts)\n {\n \tstruct child_process child = CHILD_PROCESS_INIT;\n@@ -1103,7 +1143,8 @@ static int multi_pack_index_repack(struct maintenance_opts *opts)\n \tif (opts->quiet)\n \t\tstrvec_push(&child.args, \"--no-progress\");\n \n-\tstrvec_push(&child.args, \"--batch-size=0\");\n+\tstrvec_pushf(&child.args, \"--batch-size=%\"PRIuMAX,\n+\t\t\t\t  (uintmax_t)get_auto_pack_size());\n \n \tclose_object_store(the_repository->objects);\n \ndiff --git a/t/t7900-maintenance.sh b/t/t7900-maintenance.sh\nindex dde28cf837..5c08afc19a 100755\n--- a/t/t7900-maintenance.sh\n+++ b/t/t7900-maintenance.sh\n@@ -182,10 +182,42 @@ test_expect_success 'incremental-repack task' '\n \ttest_line_count = 4 packs-between &&\n \n \t# the job deletes the two old packs, and does not write\n-\t# a new one because only one pack remains.\n+\t# a new one because the batch size is not high enough to\n+\t# pack the largest pack-file.\n \tgit maintenance run --task=incremental-repack &&\n \tls .git/objects/pack/*.pack >packs-after &&\n-\ttest_line_count = 1 packs-after\n+\ttest_line_count = 2 packs-after\n+'\n+\n+test_expect_success EXPENSIVE 'incremental-repack 2g limit' '\n+\tfor i in $(test_seq 1 5)\n+\tdo\n+\t\ttest-tool genrandom foo$i $((512 * 1024 * 1024 + 1)) >>big ||\n+\t\treturn 1\n+\tdone &&\n+\tgit add big &&\n+\tgit commit -m \"Add big file (1)\" &&\n+\n+\t# ensure any possible loose objects are in a pack-file\n+\tgit maintenance run --task=loose-objects &&\n+\n+\trm big &&\n+\tfor i in $(test_seq 6 10)\n+\tdo\n+\t\ttest-tool genrandom foo$i $((512 * 1024 * 1024 + 1)) >>big ||\n+\t\treturn 1\n+\tdone &&\n+\tgit add big &&\n+\tgit commit -m \"Add big file (2)\" &&\n+\n+\t# ensure any possible loose objects are in a pack-file\n+\tgit maintenance run --task=loose-objects &&\n+\n+\t# Now run the incremental-repack task and check the batch-size\n+\tGIT_TRACE2_EVENT=\"$(pwd)/run-2g.txt\" git maintenance run \\\n+\t\t--task=incremental-repack 2>/dev/null &&\n+\ttest_subcommand git multi-pack-index repack \\\n+\t\t --no-progress --batch-size=2147483647 <run-2g.txt\n '\n \n test_done\n-- \ngitgitgadget\n\n"},{"id":"403952","messageId":"5659a23ad578fe58f2f6d6287e59d39b7c8ae9b7.1597760730.git.gitgitgadget@gmail.com","threadId":"54001","inReplyTo":"pull.696.v2.git.1597760730.gitgitgadget@gmail.com","subject":"[PATCH v2 9/9] maintenance: add incremental-repack auto condition","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2020-08-18T14:25:30Z","receivedAt":"2020-08-18T14:26:08Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"From: Derrick Stolee <dstolee@microsoft.com>\n\nThe incremental-repack task updates the multi-pack-index by deleting pack-\nfiles that have been replaced with new packs, then repacking a batch of\nsmall pack-files into a larger pack-file. This incremental repack is faster\nthan rewriting all object data, but is slower than some other\nmaintenance activities.\n\nThe 'maintenance.incremental-repack.auto' config option specifies how many\npack-files should exist outside of the multi-pack-index before running\nthe step. These pack-files could be created by 'git fetch' commands or\nby the loose-objects task. The default value is 10.\n\nSetting the option to zero disables the task with the '--auto' option,\nand a negative value makes the task run every time.\n\nSigned-off-by: Derrick Stolee <dstolee@microsoft.com>\n---\n Documentation/config/maintenance.txt |  9 ++++++++\n builtin/gc.c                         | 31 ++++++++++++++++++++++++++++\n t/t7900-maintenance.sh               | 31 ++++++++++++++++++++++++++++\n 3 files changed, 71 insertions(+)\n\ndiff --git a/Documentation/config/maintenance.txt b/Documentation/config/maintenance.txt\nindex a9442dd260..22229e7174 100644\n--- a/Documentation/config/maintenance.txt\n+++ b/Documentation/config/maintenance.txt\n@@ -21,3 +21,12 @@ maintenance.loose-objects.auto::\n \tpositive value implies the command should run when the number of\n \tloose objects is at least the value of `maintenance.loose-objects.auto`.\n \tThe default value is 100.\n+\n+maintenance.incremental-repack.auto::\n+\tThis integer config option controls how often the `incremental-repack`\n+\ttask should be run as part of `git maintenance run --auto`. If zero,\n+\tthen the `incremental-repack` task will not run with the `--auto`\n+\toption. A negative value will force the task to run every time.\n+\tOtherwise, a positive value implies the command should run when the\n+\tnumber of pack-files not in the multi-pack-index is at least the value\n+\tof `maintenance.incremental-repack.auto`. The default value is 10.\ndiff --git a/builtin/gc.c b/builtin/gc.c\nindex d2f3e27e54..7fa0e2fe97 100644\n--- a/builtin/gc.c\n+++ b/builtin/gc.c\n@@ -31,6 +31,7 @@\n #include \"refs.h\"\n #include \"remote.h\"\n #include \"midx.h\"\n+#include \"object-store.h\"\n \n #define FAILED_RUN \"failed to run %s\"\n \n@@ -1029,6 +1030,35 @@ static int maintenance_task_loose_objects(struct maintenance_opts *opts)\n \treturn prune_packed(opts) || pack_loose(opts);\n }\n \n+static int incremental_repack_auto_condition(void)\n+{\n+\tstruct packed_git *p;\n+\tint enabled;\n+\tint incremental_repack_auto_limit = 10;\n+\tint count = 0;\n+\n+\tif (git_config_get_bool(\"core.multiPackIndex\", &enabled) ||\n+\t    !enabled)\n+\t\treturn 0;\n+\n+\tgit_config_get_int(\"maintenance.incremental-repack.auto\",\n+\t\t\t   &incremental_repack_auto_limit);\n+\n+\tif (!incremental_repack_auto_limit)\n+\t\treturn 0;\n+\tif (incremental_repack_auto_limit < 0)\n+\t\treturn 1;\n+\n+\tfor (p = get_packed_git(the_repository);\n+\t     count < incremental_repack_auto_limit && p;\n+\t     p = p->next) {\n+\t\tif (!p->multi_pack_index)\n+\t\t\tcount++;\n+\t}\n+\n+\treturn count >= incremental_repack_auto_limit;\n+}\n+\n static int multi_pack_index_write(struct maintenance_opts *opts)\n {\n \tstruct child_process child = CHILD_PROCESS_INIT;\n@@ -1219,6 +1249,7 @@ static struct maintenance_task tasks[] = {\n \t[TASK_INCREMENTAL_REPACK] = {\n \t\t\"incremental-repack\",\n \t\tmaintenance_task_incremental_repack,\n+\t\tincremental_repack_auto_condition,\n \t},\n \t[TASK_GC] = {\n \t\t\"gc\",\ndiff --git a/t/t7900-maintenance.sh b/t/t7900-maintenance.sh\nindex 5c08afc19a..6f878b0141 100755\n--- a/t/t7900-maintenance.sh\n+++ b/t/t7900-maintenance.sh\n@@ -220,4 +220,35 @@ test_expect_success EXPENSIVE 'incremental-repack 2g limit' '\n \t\t --no-progress --batch-size=2147483647 <run-2g.txt\n '\n \n+test_expect_success 'maintenance.incremental-repack.auto' '\n+\tgit repack -adk &&\n+\tgit config core.multiPackIndex true &&\n+\tgit multi-pack-index write &&\n+\tGIT_TRACE2_EVENT=\"$(pwd)/midx-init.txt\" git \\\n+\t\t-c maintenance.incremental-repack.auto=1 \\\n+\t\tmaintenance run --auto --task=incremental-repack 2>/dev/null &&\n+\ttest_subcommand ! git multi-pack-index write --no-progress <midx-init.txt &&\n+\tfor i in 1 2\n+\tdo\n+\t\ttest_commit A-$i &&\n+\t\tgit pack-objects --revs .git/objects/pack/pack <<-\\EOF &&\n+\t\tHEAD\n+\t\t^HEAD~1\n+\t\tEOF\n+\t\tGIT_TRACE2_EVENT=$(pwd)/trace-A-$i git \\\n+\t\t\t-c maintenance.incremental-repack.auto=2 \\\n+\t\t\tmaintenance run --auto --task=incremental-repack 2>/dev/null &&\n+\t\ttest_subcommand ! git multi-pack-index write --no-progress <trace-A-$i &&\n+\t\ttest_commit B-$i &&\n+\t\tgit pack-objects --revs .git/objects/pack/pack <<-\\EOF &&\n+\t\tHEAD\n+\t\t^HEAD~1\n+\t\tEOF\n+\t\tGIT_TRACE2_EVENT=$(pwd)/trace-B-$i git \\\n+\t\t\t-c maintenance.incremental-repack.auto=2 \\\n+\t\t\tmaintenance run --auto --task=incremental-repack 2>/dev/null &&\n+\t\ttest_subcommand git multi-pack-index write --no-progress <trace-B-$i || return 1\n+\tdone\n+'\n+\n test_done\n-- \ngitgitgadget\n"},{"id":"403953","messageId":"a8d956dad6b3a81d0f1b63cbd48f36a5e1195ae8.1597760730.git.gitgitgadget@gmail.com","threadId":"54001","inReplyTo":"pull.696.v2.git.1597760730.gitgitgadget@gmail.com","subject":"[PATCH v2 7/9] maintenance: add incremental-repack task","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2020-08-18T14:25:28Z","receivedAt":"2020-08-18T14:26:08Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"From: Derrick Stolee <dstolee@microsoft.com>\n\nThe previous change cleaned up loose objects using the\n'loose-objects' that can be run safely in the background. Add a\nsimilar job that performs similar cleanups for pack-files.\n\nOne issue with running 'git repack' is that it is designed to\nrepack all pack-files into a single pack-file. While this is the\nmost space-efficient way to store object data, it is not time or\nmemory efficient. This becomes extremely important if the repo is\nso large that a user struggles to store two copies of the pack on\ntheir disk.\n\nInstead, perform an \"incremental\" repack by collecting a few small\npack-files into a new pack-file. The multi-pack-index facilitates\nthis process ever since 'git multi-pack-index expire' was added in\n19575c7 (multi-pack-index: implement 'expire' subcommand,\n2019-06-10) and 'git multi-pack-index repack' was added in ce1e4a1\n(midx: implement midx_repack(), 2019-06-10).\n\nThe 'incremental-repack' task runs the following steps:\n\n1. 'git multi-pack-index write' creates a multi-pack-index file if\n   one did not exist, and otherwise will update the multi-pack-index\n   with any new pack-files that appeared since the last write. This\n   is particularly relevant with the background fetch job.\n\n   When the multi-pack-index sees two copies of the same object, it\n   stores the offset data into the newer pack-file. This means that\n   some old pack-files could become \"unreferenced\" which I will use\n   to mean \"a pack-file that is in the pack-file list of the\n   multi-pack-index but none of the objects in the multi-pack-index\n   reference a location inside that pack-file.\"\n\n2. 'git multi-pack-index expire' deletes any unreferenced pack-files\n   and updaes the multi-pack-index to drop those pack-files from the\n   list. This is safe to do as concurrent Git processes will see the\n   multi-pack-index and not open those packs when looking for object\n   contents. (Similar to the 'loose-objects' job, there are some Git\n   commands that open pack-files regardless of the multi-pack-index,\n   but they are rarely used. Further, a user that self-selects to\n   use background operations would likely refrain from using those\n   commands.)\n\n3. 'git multi-pack-index repack --bacth-size=<size>' collects a set\n   of pack-files that are listed in the multi-pack-index and creates\n   a new pack-file containing the objects whose offsets are listed\n   by the multi-pack-index to be in those objects. The set of pack-\n   files is selected greedily by sorting the pack-files by modified\n   time and adding a pack-file to the set if its \"expected size\" is\n   smaller than the batch size until the total expected size of the\n   selected pack-files is at least the batch size. The \"expected\n   size\" is calculated by taking the size of the pack-file divided\n   by the number of objects in the pack-file and multiplied by the\n   number of objects from the multi-pack-index with offset in that\n   pack-file. The expected size approximates how much data from that\n   pack-file will contribute to the resulting pack-file size. The\n   intention is that the resulting pack-file will be close in size\n   to the provided batch size.\n\n   The next run of the incremental-repack task will delete these\n   repacked pack-files during the 'expire' step.\n\n   In this version, the batch size is set to \"0\" which ignores the\n   size restrictions when selecting the pack-files. It instead\n   selects all pack-files and repacks all packed objects into a\n   single pack-file. This will be updated in the next change, but\n   it requires doing some calculations that are better isolated to\n   a separate change.\n\nEach of the above steps update the multi-pack-index file. After\neach step, we verify the new multi-pack-index. If the new\nmulti-pack-index is corrupt, then delete the multi-pack-index,\nrewrite it from scratch, and stop doing the later steps of the\njob. This is intended to be an extra-safe check without leaving\na repo with many pack-files without a multi-pack-index.\n\nThese steps are based on a similar background maintenance step in\nScalar (and VFS for Git) [1]. This was incredibly effective for\nusers of the Windows OS repository. After using the same VFS for Git\nrepository for over a year, some users had _thousands_ of pack-files\nthat combined to up to 250 GB of data. We noticed a few users were\nrunning into the open file descriptor limits (due in part to a bug\nin the multi-pack-index fixed by af96fe3 (midx: add packs to\npacked_git linked list, 2019-04-29).\n\nThese pack-files were mostly small since they contained the commits\nand trees that were pushed to the origin in a given hour. The GVFS\nprotocol includes a \"prefetch\" step that asks for pre-computed pack-\nfiles containing commits and trees by timestamp. These pack-files\nwere grouped into \"daily\" pack-files once a day for up to 30 days.\nIf a user did not request prefetch packs for over 30 days, then they\nwould get the entire history of commits and trees in a new, large\npack-file. This led to a large number of pack-files that had poor\ndelta compression.\n\nBy running this pack-file maintenance step once per day, these repos\nwith thousands of packs spanning 200+ GB dropped to dozens of pack-\nfiles spanning 30-50 GB. This was done all without removing objects\nfrom the system and using a constant batch size of two gigabytes.\nOnce the work was done to reduce the pack-files to small sizes, the\nbatch size of two gigabytes means that not every run triggers a\nrepack operation, so the following run will not expire a pack-file.\nThis has kept these repos in a \"clean\" state.\n\n[1] https://github.com/microsoft/scalar/blob/master/Scalar.Common/Maintenance/PackfileMaintenanceStep.cs\n\nSigned-off-by: Derrick Stolee <dstolee@microsoft.com>\n---\n Documentation/git-maintenance.txt |  15 ++++\n builtin/gc.c                      | 113 ++++++++++++++++++++++++++++++\n midx.c                            |   2 +-\n midx.h                            |   1 +\n t/t5319-multi-pack-index.sh       |   1 +\n t/t7900-maintenance.sh            |  38 ++++++++++\n 6 files changed, 169 insertions(+), 1 deletion(-)\n\ndiff --git a/Documentation/git-maintenance.txt b/Documentation/git-maintenance.txt\nindex 85205b5bb9..b0b73b7185 100644\n--- a/Documentation/git-maintenance.txt\n+++ b/Documentation/git-maintenance.txt\n@@ -88,6 +88,21 @@ loose-objects::\n \tadvisable to enable both the `loose-objects` and `gc` tasks at the\n \tsame time.\n \n+incremental-repack::\n+\tThe `incremental-repack` job repacks the object directory\n+\tusing the `multi-pack-index` feature. In order to prevent race\n+\tconditions with concurrent Git commands, it follows a two-step\n+\tprocess. First, it deletes any pack-files included in the\n+\t`multi-pack-index` where none of the objects in the\n+\t`multi-pack-index` reference those pack-files; this only happens\n+\tif all objects in the pack-file are also stored in a newer\n+\tpack-file. Second, it selects a group of pack-files whose \"expected\n+\tsize\" is below the batch size until the group has total expected\n+\tsize at least the batch size; see the `--batch-size` option for\n+\tthe `repack` subcommand in linkgit:git-multi-pack-index[1]. The\n+\tdefault batch-size is zero, which is a special case that attempts\n+\tto repack all pack-files into a single pack-file.\n+\n OPTIONS\n -------\n --auto::\ndiff --git a/builtin/gc.c b/builtin/gc.c\nindex 61d5752af5..9aabef44a8 100644\n--- a/builtin/gc.c\n+++ b/builtin/gc.c\n@@ -30,6 +30,7 @@\n #include \"promisor-remote.h\"\n #include \"refs.h\"\n #include \"remote.h\"\n+#include \"midx.h\"\n \n #define FAILED_RUN \"failed to run %s\"\n \n@@ -1028,6 +1029,113 @@ static int maintenance_task_loose_objects(struct maintenance_opts *opts)\n \treturn prune_packed(opts) || pack_loose(opts);\n }\n \n+static int multi_pack_index_write(struct maintenance_opts *opts)\n+{\n+\tstruct child_process child = CHILD_PROCESS_INIT;\n+\n+\tchild.git_cmd = 1;\n+\tstrvec_pushl(&child.args, \"multi-pack-index\", \"write\", NULL);\n+\n+\tif (opts->quiet)\n+\t\tstrvec_push(&child.args, \"--no-progress\");\n+\n+\tif (run_command(&child))\n+\t\treturn error(_(\"failed to write multi-pack-index\"));\n+\n+\treturn 0;\n+}\n+\n+static int rewrite_multi_pack_index(struct maintenance_opts *opts)\n+{\n+\tstruct repository *r = the_repository;\n+\tchar *midx_name = get_midx_filename(r->objects->odb->path);\n+\n+\tunlink(midx_name);\n+\tfree(midx_name);\n+\n+\treturn multi_pack_index_write(opts);\n+}\n+\n+static int multi_pack_index_verify(struct maintenance_opts *opts,\n+\t\t\t\t   const char *message)\n+{\n+\tstruct child_process child = CHILD_PROCESS_INIT;\n+\n+\tchild.git_cmd = 1;\n+\tstrvec_pushl(&child.args, \"multi-pack-index\", \"verify\", NULL);\n+\n+\tif (opts->quiet)\n+\t\tstrvec_push(&child.args, \"--no-progress\");\n+\n+\tif (run_command(&child)) {\n+\t\twarning(_(\"'git multi-pack-index verify' failed %s\"), message);\n+\t\treturn 1;\n+\t}\n+\n+\treturn 0;\n+}\n+\n+static int multi_pack_index_expire(struct maintenance_opts *opts)\n+{\n+\tstruct child_process child = CHILD_PROCESS_INIT;\n+\n+\tchild.git_cmd = 1;\n+\tstrvec_pushl(&child.args, \"multi-pack-index\", \"expire\", NULL);\n+\n+\tif (opts->quiet)\n+\t\tstrvec_push(&child.args, \"--no-progress\");\n+\n+\tclose_object_store(the_repository->objects);\n+\n+\tif (run_command(&child))\n+\t\treturn error(_(\"'git multi-pack-index expire' failed\"));\n+\n+\treturn 0;\n+}\n+\n+static int multi_pack_index_repack(struct maintenance_opts *opts)\n+{\n+\tstruct child_process child = CHILD_PROCESS_INIT;\n+\n+\tchild.git_cmd = 1;\n+\tstrvec_pushl(&child.args, \"multi-pack-index\", \"repack\", NULL);\n+\n+\tif (opts->quiet)\n+\t\tstrvec_push(&child.args, \"--no-progress\");\n+\n+\tstrvec_push(&child.args, \"--batch-size=0\");\n+\n+\tclose_object_store(the_repository->objects);\n+\n+\tif (run_command(&child))\n+\t\treturn error(_(\"'git multi-pack-index repack' failed\"));\n+\n+\treturn 0;\n+}\n+\n+static int maintenance_task_incremental_repack(struct maintenance_opts *opts)\n+{\n+\tprepare_repo_settings(the_repository);\n+\tif (!the_repository->settings.core_multi_pack_index) {\n+\t\twarning(_(\"skipping incremental-repack task because core.multiPackIndex is disabled\"));\n+\t\treturn 0;\n+\t}\n+\n+\tif (multi_pack_index_write(opts))\n+\t\treturn 1;\n+\tif (multi_pack_index_verify(opts, \"after initial write\"))\n+\t\treturn rewrite_multi_pack_index(opts);\n+\tif (multi_pack_index_expire(opts))\n+\t\treturn 1;\n+\tif (multi_pack_index_verify(opts, \"after expire step\"))\n+\t\treturn !!rewrite_multi_pack_index(opts);\n+\tif (multi_pack_index_repack(opts))\n+\t\treturn 1;\n+\tif (multi_pack_index_verify(opts, \"after repack step\"))\n+\t\treturn !!rewrite_multi_pack_index(opts);\n+\treturn 0;\n+}\n+\n typedef int maintenance_task_fn(struct maintenance_opts *opts);\n \n /*\n@@ -1049,6 +1157,7 @@ struct maintenance_task {\n enum maintenance_task_label {\n \tTASK_PREFETCH,\n \tTASK_LOOSE_OBJECTS,\n+\tTASK_INCREMENTAL_REPACK,\n \tTASK_GC,\n \tTASK_COMMIT_GRAPH,\n \n@@ -1066,6 +1175,10 @@ static struct maintenance_task tasks[] = {\n \t\tmaintenance_task_loose_objects,\n \t\tloose_object_auto_condition,\n \t},\n+\t[TASK_INCREMENTAL_REPACK] = {\n+\t\t\"incremental-repack\",\n+\t\tmaintenance_task_incremental_repack,\n+\t},\n \t[TASK_GC] = {\n \t\t\"gc\",\n \t\tmaintenance_task_gc,\ndiff --git a/midx.c b/midx.c\nindex aa37d5da86..66d7053d83 100644\n--- a/midx.c\n+++ b/midx.c\n@@ -37,7 +37,7 @@\n \n #define PACK_EXPIRED UINT_MAX\n \n-static char *get_midx_filename(const char *object_dir)\n+char *get_midx_filename(const char *object_dir)\n {\n \treturn xstrfmt(\"%s/pack/multi-pack-index\", object_dir);\n }\ndiff --git a/midx.h b/midx.h\nindex b18cf53bc4..baeecc70c9 100644\n--- a/midx.h\n+++ b/midx.h\n@@ -37,6 +37,7 @@ struct multi_pack_index {\n \n #define MIDX_PROGRESS     (1 << 0)\n \n+char *get_midx_filename(const char *object_dir);\n struct multi_pack_index *load_multi_pack_index(const char *object_dir, int local);\n int prepare_midx_pack(struct repository *r, struct multi_pack_index *m, uint32_t pack_int_id);\n int bsearch_midx(const struct object_id *oid, struct multi_pack_index *m, uint32_t *result);\ndiff --git a/t/t5319-multi-pack-index.sh b/t/t5319-multi-pack-index.sh\nindex 12f41dfc18..26f224b0e3 100755\n--- a/t/t5319-multi-pack-index.sh\n+++ b/t/t5319-multi-pack-index.sh\n@@ -3,6 +3,7 @@\n test_description='multi-pack-indexes'\n . ./test-lib.sh\n \n+GIT_TEST_MULTI_PACK_INDEX=0\n objdir=.git/objects\n \n midx_read_expect () {\ndiff --git a/t/t7900-maintenance.sh b/t/t7900-maintenance.sh\nindex efda1cf69b..dde28cf837 100755\n--- a/t/t7900-maintenance.sh\n+++ b/t/t7900-maintenance.sh\n@@ -5,6 +5,7 @@ test_description='git maintenance builtin'\n . ./test-lib.sh\n \n GIT_TEST_COMMIT_GRAPH=0\n+GIT_TEST_MULTI_PACK_INDEX=0\n \n test_expect_success 'help text' '\n \ttest_expect_code 129 git maintenance -h 2>err &&\n@@ -150,4 +151,41 @@ test_expect_success 'maintenance.loose-objects.auto' '\n \tdone\n '\n \n+test_expect_success 'incremental-repack task' '\n+\tpackDir=.git/objects/pack &&\n+\tfor i in $(test_seq 1 5)\n+\tdo\n+\t\ttest_commit $i || return 1\n+\tdone &&\n+\n+\t# Create three disjoint pack-files with size BIG, small, small.\n+\techo HEAD~2 | git pack-objects --revs $packDir/test-1 &&\n+\ttest_tick &&\n+\tgit pack-objects --revs $packDir/test-2 <<-\\EOF &&\n+\tHEAD~1\n+\t^HEAD~2\n+\tEOF\n+\ttest_tick &&\n+\tgit pack-objects --revs $packDir/test-3 <<-\\EOF &&\n+\tHEAD\n+\t^HEAD~1\n+\tEOF\n+\trm -f $packDir/pack-* &&\n+\trm -f $packDir/loose-* &&\n+\tls $packDir/*.pack >packs-before &&\n+\ttest_line_count = 3 packs-before &&\n+\n+\t# the job repacks the two into a new pack, but does not\n+\t# delete the old ones.\n+\tgit maintenance run --task=incremental-repack &&\n+\tls $packDir/*.pack >packs-between &&\n+\ttest_line_count = 4 packs-between &&\n+\n+\t# the job deletes the two old packs, and does not write\n+\t# a new one because only one pack remains.\n+\tgit maintenance run --task=incremental-repack &&\n+\tls .git/objects/pack/*.pack >packs-after &&\n+\ttest_line_count = 1 packs-after\n+'\n+\n test_done\n-- \ngitgitgadget\n\n"},{"id":"403954","messageId":"e157ea8dd7f16281d358a080b3424918d3572a36.1597760730.git.gitgitgadget@gmail.com","threadId":"54001","inReplyTo":"pull.696.v2.git.1597760730.gitgitgadget@gmail.com","subject":"[PATCH v2 6/9] midx: use start_delayed_progress()","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2020-08-18T14:25:27Z","receivedAt":"2020-08-18T14:26:19Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"From: Derrick Stolee <dstolee@microsoft.com>\n\nNow that the multi-pack-index may be written as part of auto maintenance\nat the end of a command, reduce the progress output when the operations\nare quick. Use start_delayed_progress() instead of start_progress().\n\nUpdate t5319-multi-pack-index.sh to use GIT_PROGRESS_DELAY=0 now that\nthe progress indicators are conditional.\n\nSigned-off-by: Derrick Stolee <dstolee@microsoft.com>\n---\n midx.c                      | 10 +++++-----\n t/t5319-multi-pack-index.sh | 14 +++++++-------\n 2 files changed, 12 insertions(+), 12 deletions(-)\n\ndiff --git a/midx.c b/midx.c\nindex ef499cf504..aa37d5da86 100644\n--- a/midx.c\n+++ b/midx.c\n@@ -832,7 +832,7 @@ static int write_midx_internal(const char *object_dir, struct multi_pack_index *\n \n \tpacks.pack_paths_checked = 0;\n \tif (flags & MIDX_PROGRESS)\n-\t\tpacks.progress = start_progress(_(\"Adding packfiles to multi-pack-index\"), 0);\n+\t\tpacks.progress = start_delayed_progress(_(\"Adding packfiles to multi-pack-index\"), 0);\n \telse\n \t\tpacks.progress = NULL;\n \n@@ -969,7 +969,7 @@ static int write_midx_internal(const char *object_dir, struct multi_pack_index *\n \t}\n \n \tif (flags & MIDX_PROGRESS)\n-\t\tprogress = start_progress(_(\"Writing chunks to multi-pack-index\"),\n+\t\tprogress = start_delayed_progress(_(\"Writing chunks to multi-pack-index\"),\n \t\t\t\t\t  num_chunks);\n \tfor (i = 0; i < num_chunks; i++) {\n \t\tif (written != chunk_offsets[i])\n@@ -1104,7 +1104,7 @@ int verify_midx_file(struct repository *r, const char *object_dir, unsigned flag\n \t\treturn 0;\n \n \tif (flags & MIDX_PROGRESS)\n-\t\tprogress = start_progress(_(\"Looking for referenced packfiles\"),\n+\t\tprogress = start_delayed_progress(_(\"Looking for referenced packfiles\"),\n \t\t\t\t\t  m->num_packs);\n \tfor (i = 0; i < m->num_packs; i++) {\n \t\tif (prepare_midx_pack(r, m, i))\n@@ -1225,7 +1225,7 @@ int expire_midx_packs(struct repository *r, const char *object_dir, unsigned fla\n \tcount = xcalloc(m->num_packs, sizeof(uint32_t));\n \n \tif (flags & MIDX_PROGRESS)\n-\t\tprogress = start_progress(_(\"Counting referenced objects\"),\n+\t\tprogress = start_delayed_progress(_(\"Counting referenced objects\"),\n \t\t\t\t\t  m->num_objects);\n \tfor (i = 0; i < m->num_objects; i++) {\n \t\tint pack_int_id = nth_midxed_pack_int_id(m, i);\n@@ -1235,7 +1235,7 @@ int expire_midx_packs(struct repository *r, const char *object_dir, unsigned fla\n \tstop_progress(&progress);\n \n \tif (flags & MIDX_PROGRESS)\n-\t\tprogress = start_progress(_(\"Finding and deleting unreferenced packfiles\"),\n+\t\tprogress = start_delayed_progress(_(\"Finding and deleting unreferenced packfiles\"),\n \t\t\t\t\t  m->num_packs);\n \tfor (i = 0; i < m->num_packs; i++) {\n \t\tchar *pack_name;\ndiff --git a/t/t5319-multi-pack-index.sh b/t/t5319-multi-pack-index.sh\nindex 7214cab36c..12f41dfc18 100755\n--- a/t/t5319-multi-pack-index.sh\n+++ b/t/t5319-multi-pack-index.sh\n@@ -172,12 +172,12 @@ test_expect_success 'write progress off for redirected stderr' '\n '\n \n test_expect_success 'write force progress on for stderr' '\n-\tgit multi-pack-index --object-dir=$objdir --progress write 2>err &&\n+\tGIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir --progress write 2>err &&\n \ttest_file_not_empty err\n '\n \n test_expect_success 'write with the --no-progress option' '\n-\tgit multi-pack-index --object-dir=$objdir --no-progress write 2>err &&\n+\tGIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir --no-progress write 2>err &&\n \ttest_line_count = 0 err\n '\n \n@@ -334,17 +334,17 @@ test_expect_success 'git-fsck incorrect offset' '\n '\n \n test_expect_success 'repack progress off for redirected stderr' '\n-\tgit multi-pack-index --object-dir=$objdir repack 2>err &&\n+\tGIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir repack 2>err &&\n \ttest_line_count = 0 err\n '\n \n test_expect_success 'repack force progress on for stderr' '\n-\tgit multi-pack-index --object-dir=$objdir --progress repack 2>err &&\n+\tGIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir --progress repack 2>err &&\n \ttest_file_not_empty err\n '\n \n test_expect_success 'repack with the --no-progress option' '\n-\tgit multi-pack-index --object-dir=$objdir --no-progress repack 2>err &&\n+\tGIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir --no-progress repack 2>err &&\n \ttest_line_count = 0 err\n '\n \n@@ -488,7 +488,7 @@ test_expect_success 'expire progress off for redirected stderr' '\n test_expect_success 'expire force progress on for stderr' '\n \t(\n \t\tcd dup &&\n-\t\tgit multi-pack-index --progress expire 2>err &&\n+\t\tGIT_PROGRESS_DELAY=0 git multi-pack-index --progress expire 2>err &&\n \t\ttest_file_not_empty err\n \t)\n '\n@@ -496,7 +496,7 @@ test_expect_success 'expire force progress on for stderr' '\n test_expect_success 'expire with the --no-progress option' '\n \t(\n \t\tcd dup &&\n-\t\tgit multi-pack-index --no-progress expire 2>err &&\n+\t\tGIT_PROGRESS_DELAY=0 git multi-pack-index --no-progress expire 2>err &&\n \t\ttest_line_count = 0 err\n \t)\n '\n-- \ngitgitgadget\n\n"},{"id":"403955","messageId":"3432bc3167b2250ce4d02f8b81950c3b12b524d4.1597760730.git.gitgitgadget@gmail.com","threadId":"54001","inReplyTo":"pull.696.v2.git.1597760730.gitgitgadget@gmail.com","subject":"[PATCH v2 4/9] maintenance: create auto condition for loose-objects","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2020-08-18T14:25:25Z","receivedAt":"2020-08-18T14:26:20Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"From: Derrick Stolee <dstolee@microsoft.com>\n\nThe loose-objects task deletes loose objects that already exist in a\npack-file, then place the remaining loose objects into a new pack-file.\nIf this step runs all the time, then we risk creating pack-files with\nvery few objects with every 'git commit' process. To prevent\noverwhelming the packs directory with small pack-files, place a minimum\nnumber of objects to justify the task.\n\nThe 'maintenance.loose-objects.auto' config option specifies a minimum\nnumber of loose objects to justify the task to run under the '--auto'\noption. This defaults to 100 loose objects. Setting the value to zero\nwill prevent the step from running under '--auto' while a negative value\nwill force it to run every time.\n\nSigned-off-by: Derrick Stolee <dstolee@microsoft.com>\n---\n Documentation/config/maintenance.txt |  9 +++++++++\n builtin/gc.c                         | 30 ++++++++++++++++++++++++++++\n t/t7900-maintenance.sh               | 25 +++++++++++++++++++++++\n 3 files changed, 64 insertions(+)\n\ndiff --git a/Documentation/config/maintenance.txt b/Documentation/config/maintenance.txt\nindex 9bd69b9df3..a9442dd260 100644\n--- a/Documentation/config/maintenance.txt\n+++ b/Documentation/config/maintenance.txt\n@@ -12,3 +12,12 @@ maintenance.commit-graph.auto::\n \treachable commits that are not in the commit-graph file is at least\n \tthe value of `maintenance.commit-graph.auto`. The default value is\n \t100.\n+\n+maintenance.loose-objects.auto::\n+\tThis integer config option controls how often the `loose-objects` task\n+\tshould be run as part of `git maintenance run --auto`. If zero, then\n+\tthe `loose-objects` task will not run with the `--auto` option. A\n+\tnegative value will force the task to run every time. Otherwise, a\n+\tpositive value implies the command should run when the number of\n+\tloose objects is at least the value of `maintenance.loose-objects.auto`.\n+\tThe default value is 100.\ndiff --git a/builtin/gc.c b/builtin/gc.c\nindex 39ea590b5a..61d5752af5 100644\n--- a/builtin/gc.c\n+++ b/builtin/gc.c\n@@ -926,6 +926,35 @@ struct write_loose_object_data {\n \tint batch_size;\n };\n \n+static int loose_object_auto_limit = 100;\n+\n+static int loose_object_count(const struct object_id *oid,\n+\t\t\t       const char *path,\n+\t\t\t       void *data)\n+{\n+\tint *count = (int*)data;\n+\tif (++(*count) >= loose_object_auto_limit)\n+\t\treturn 1;\n+\treturn 0;\n+}\n+\n+static int loose_object_auto_condition(void)\n+{\n+\tint count = 0;\n+\n+\tgit_config_get_int(\"maintenance.loose-objects.auto\",\n+\t\t\t   &loose_object_auto_limit);\n+\n+\tif (!loose_object_auto_limit)\n+\t\treturn 0;\n+\tif (loose_object_auto_limit < 0)\n+\t\treturn 1;\n+\n+\treturn for_each_loose_file_in_objdir(the_repository->objects->odb->path,\n+\t\t\t\t\t     loose_object_count,\n+\t\t\t\t\t     NULL, NULL, &count);\n+}\n+\n static int bail_on_loose(const struct object_id *oid,\n \t\t\t const char *path,\n \t\t\t void *data)\n@@ -1035,6 +1064,7 @@ static struct maintenance_task tasks[] = {\n \t[TASK_LOOSE_OBJECTS] = {\n \t\t\"loose-objects\",\n \t\tmaintenance_task_loose_objects,\n+\t\tloose_object_auto_condition,\n \t},\n \t[TASK_GC] = {\n \t\t\"gc\",\ndiff --git a/t/t7900-maintenance.sh b/t/t7900-maintenance.sh\nindex 2e9e369786..efda1cf69b 100755\n--- a/t/t7900-maintenance.sh\n+++ b/t/t7900-maintenance.sh\n@@ -125,4 +125,29 @@ test_expect_success 'loose-objects task' '\n \ttest_cmp packs-between packs-after\n '\n \n+test_expect_success 'maintenance.loose-objects.auto' '\n+\tgit repack -adk &&\n+\tGIT_TRACE2_EVENT=\"$(pwd)/trace-lo1.txt\" \\\n+\t\tgit -c maintenance.loose-objects.auto=1 maintenance \\\n+\t\trun --auto --task=loose-objects 2>/dev/null &&\n+\ttest_subcommand ! git prune-packed --quiet <trace-lo1.txt &&\n+\tfor i in 1 2\n+\tdo\n+\t\tprintf data-A-$i | git hash-object -t blob --stdin -w &&\n+\t\tGIT_TRACE2_EVENT=\"$(pwd)/trace-loA-$i\" \\\n+\t\t\tgit -c maintenance.loose-objects.auto=2 \\\n+\t\t\tmaintenance run --auto --task=loose-objects 2>/dev/null &&\n+\t\ttest_subcommand ! git prune-packed --quiet <trace-loA-$i &&\n+\t\tprintf data-B-$i | git hash-object -t blob --stdin -w &&\n+\t\tGIT_TRACE2_EVENT=\"$(pwd)/trace-loB-$i\" \\\n+\t\t\tgit -c maintenance.loose-objects.auto=2 \\\n+\t\t\tmaintenance run --auto --task=loose-objects 2>/dev/null &&\n+\t\ttest_subcommand git prune-packed --quiet <trace-loB-$i &&\n+\t\tGIT_TRACE2_EVENT=\"$(pwd)/trace-loC-$i\" \\\n+\t\t\tgit -c maintenance.loose-objects.auto=2 \\\n+\t\t\tmaintenance run --auto --task=loose-objects 2>/dev/null &&\n+\t\ttest_subcommand git prune-packed --quiet <trace-loC-$i || return 1\n+\tdone\n+'\n+\n test_done\n-- \ngitgitgadget\n\n"},{"id":"404452","messageId":"pull.696.v3.git.1598380599.gitgitgadget@gmail.com","threadId":"54001","inReplyTo":"pull.696.v2.git.1597760730.gitgitgadget@gmail.com","subject":"[PATCH v3 0/8] Maintenance II: prefetch, loose-objects, incremental-repack tasks","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2020-08-25T18:36:31Z","receivedAt":"2020-08-25T18:36:44Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"This series is based on v3 of part I (ds/maintenance-part-1) [2].\n\nThis patch series contains 9 patches that were going to be part of v4 of\nds/maintenance [1], but the discussion has gotten really long. To help, I'm\nsplitting out the portions that create and test the 'maintenance' builtin\nfrom the additional tasks (prefetch, loose-objects, incremental-repack) that\ncan be brought in later.\n\n[1] \nhttps://lore.kernel.org/git/pull.671.git.1594131695.gitgitgadget@gmail.com/\n[2] \nhttps://lore.kernel.org/git/pull.695.v3.git.1598380426.gitgitgadget@gmail.com/\n\nAs detailed in [2], the 'git maintenance run' subcommand will run certain\ntasks based on config options or the --task= arguments. The --auto option\nindicates to the task to only run based on some internal check that there\nhas been \"enough\" change in that domain to merit the work. In the case of\nthe 'gc' task, this also reduces the amount of work done. \n\nThe new maintenance tasks in this series are:\n\n * 'loose-objects' : prune packed loose objects, then create a new pack from\n   a batch of loose objects.\n * 'pack-files' : expire redundant packs from the multi-pack-index, then\n   repack using the multi-pack-index's incremental repack strategy.\n * 'prefetch' : fetch from each remote, storing the refs in 'refs/prefetch/\n   /'.\n\nThese tasks are all disabled by default, but can be enabled with config\noptions or run explicitly using \"git maintenance run --task=\". \n\nSince [2] replaced the 'git gc --auto' calls with 'git maintenance run\n--auto' at the end of some Git commands, users could replace the 'gc' task\nwith these lighter-weight changes for foreground maintenance.\n\nThe 'git maintenance' builtin has a 'run' subcommand so it can be extended\nlater with subcommands that manage background maintenance, such as 'start'\nor 'stop'. These are not the subject of this series, as it is important to\nfocus on the maintenance activities themselves. I have an RFC series for\nthis available at [3].\n\n[3] \nhttps://lore.kernel.org/git/pull.680.git.1597857408.gitgitgadget@gmail.com/\n\nUpdates since v2\n================\n\n * Dropped \"fetch: optionally allow disabling FETCH_HEAD update\"\n   \n   \n * A lot of fallout from the change in the option parsing in v3 of\n   Maintenance II.\n   \n   \n * Dropped the \"verify, and delete and rewrite on failure\" logic from the\n   incremental-repack task. This might be added again later after it can be\n   tested more thoroughly.\n   \n   \n\nUpdates since v1 (of this series)\n=================================\n\n * PATCH 1 (\"fetch: optionally allow disabling FETCH_HEAD update\") was\n   rewritten on-list. Getting a version out with this patch is the main\n   reason for rolling a v2. (That, and Part I is re-rolled with a v2 and I\n   want to make sure this series applies cleanly.)\n   \n   \n * The 'prefetch' and 'loose-objects' tasks had some review, but my proposed\n   changes were not acked, so they may need another review.\n   \n   \n\nUPDATES since v3 of [1]\n=======================\n\n * The biggest change here is the use of \"test_subcommand\", based on\n   Jonathan Nieder's approach. This requires having the exact command-line\n   figured out, which now requires spelling out all --no- [quiet%7Cprogress] \n   options. I also added a bunch of \"2>/dev/null\" checks because of the\n   isatty(2) calls. Without that, the behavior will change depending on\n   whether the test is run with -x/-v or without.\n   \n   \n * The 0x7FFF/0x7FFFFFFF constant problem is fixed with an EXPENSIVE test\n   that verifies it.\n   \n   \n * The option parsing has changed to use a local struct and pass that struct\n   to the helper methods. This is instead of having a global singleton.\n   \n   \n\nThanks, -Stolee\n\nDerrick Stolee (8):\n  maintenance: add prefetch task\n  maintenance: add loose-objects task\n  maintenance: create auto condition for loose-objects\n  midx: enable core.multiPackIndex by default\n  midx: use start_delayed_progress()\n  maintenance: add incremental-repack task\n  maintenance: auto-size incremental-repack batch\n  maintenance: add incremental-repack auto condition\n\n Documentation/config/core.txt        |   4 +-\n Documentation/config/maintenance.txt |  18 ++\n Documentation/git-maintenance.txt    |  45 ++++\n builtin/gc.c                         | 327 +++++++++++++++++++++++++++\n midx.c                               |  23 +-\n midx.h                               |   1 +\n repo-settings.c                      |   6 +\n repository.h                         |   2 +\n t/t5319-multi-pack-index.sh          |  15 +-\n t/t7900-maintenance.sh               | 191 ++++++++++++++++\n 10 files changed, 609 insertions(+), 23 deletions(-)\n\n\nbase-commit: 652a8eac57d04a51820c7a5b45031b50c5188e7b\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-696%2Fderrickstolee%2Fmaintenance%2Fgc-v3\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-696/derrickstolee/maintenance/gc-v3\nPull-Request: https://github.com/gitgitgadget/git/pull/696\n\nRange-diff vs v2:\n\n  1:  f3bc0b2d92 <  -:  ---------- fetch: optionally allow disabling FETCH_HEAD update\n  2:  8779c6c20d !  1:  da64c51a81 maintenance: add prefetch task\n     @@ Commit message\n          Signed-off-by: Derrick Stolee <dstolee@microsoft.com>\n      \n       ## Documentation/git-maintenance.txt ##\n     -@@ Documentation/git-maintenance.txt: since it will not expire `.graph` files that were in the previous\n     - `commit-graph-chain` file. They will be deleted by a later run based on\n     - the expiration delay.\n     +@@ Documentation/git-maintenance.txt: commit-graph::\n     + \t`commit-graph-chain` file. They will be deleted by a later run based\n     + \ton the expiration delay.\n       \n      +prefetch::\n      +\tThe `prefetch` task updates the object directory with the latest\n     @@ builtin/gc.c\n       \n       #define FAILED_RUN \"failed to run %s\"\n       \n     -@@ builtin/gc.c: static int maintenance_task_commit_graph(struct maintenance_opts *opts)\n     - \treturn 1;\n     +@@ builtin/gc.c: static int maintenance_task_commit_graph(struct maintenance_run_opts *opts)\n     + \treturn 0;\n       }\n       \n     -+static int fetch_remote(const char *remote, struct maintenance_opts *opts)\n     ++static int fetch_remote(const char *remote, struct maintenance_run_opts *opts)\n      +{\n      +\tstruct child_process child = CHILD_PROCESS_INIT;\n      +\n     @@ builtin/gc.c: static int maintenance_task_commit_graph(struct maintenance_opts *\n      +\treturn 0;\n      +}\n      +\n     -+static int maintenance_task_prefetch(struct maintenance_opts *opts)\n     ++static int maintenance_task_prefetch(struct maintenance_run_opts *opts)\n      +{\n      +\tint result = 0;\n      +\tstruct string_list_item *item;\n     @@ builtin/gc.c: static int maintenance_task_commit_graph(struct maintenance_opts *\n      +\treturn result;\n      +}\n      +\n     - static int maintenance_task_gc(struct maintenance_opts *opts)\n     + static int maintenance_task_gc(struct maintenance_run_opts *opts)\n       {\n       \tstruct child_process child = CHILD_PROCESS_INIT;\n      @@ builtin/gc.c: struct maintenance_task {\n  3:  4fa9d298b9 !  2:  75e846456b maintenance: add loose-objects task\n     @@ Documentation/git-maintenance.txt: gc::\n       --auto::\n      \n       ## builtin/gc.c ##\n     -@@ builtin/gc.c: static int maintenance_task_gc(struct maintenance_opts *opts)\n     +@@ builtin/gc.c: static int maintenance_task_gc(struct maintenance_run_opts *opts)\n       \treturn run_command(&child);\n       }\n       \n     -+static int prune_packed(struct maintenance_opts *opts)\n     ++static int prune_packed(struct maintenance_run_opts *opts)\n      +{\n      +\tstruct child_process child = CHILD_PROCESS_INIT;\n      +\n     @@ builtin/gc.c: static int maintenance_task_gc(struct maintenance_opts *opts)\n      +\treturn ++(d->count) > d->batch_size;\n      +}\n      +\n     -+static int pack_loose(struct maintenance_opts *opts)\n     ++static int pack_loose(struct maintenance_run_opts *opts)\n      +{\n      +\tstruct repository *r = the_repository;\n      +\tint result = 0;\n     @@ builtin/gc.c: static int maintenance_task_gc(struct maintenance_opts *opts)\n      +\treturn result;\n      +}\n      +\n     -+static int maintenance_task_loose_objects(struct maintenance_opts *opts)\n     ++static int maintenance_task_loose_objects(struct maintenance_run_opts *opts)\n      +{\n      +\treturn prune_packed(opts) || pack_loose(opts);\n      +}\n      +\n     - typedef int maintenance_task_fn(struct maintenance_opts *opts);\n     + typedef int maintenance_task_fn(struct maintenance_run_opts *opts);\n       \n       /*\n      @@ builtin/gc.c: struct maintenance_task {\n  4:  3432bc3167 =  3:  d6e382c43e maintenance: create auto condition for loose-objects\n  5:  0ee2434bdb =  4:  d0f2ec70d9 midx: enable core.multiPackIndex by default\n  6:  e157ea8dd7 =  5:  2cd3c803d9 midx: use start_delayed_progress()\n  7:  a8d956dad6 !  6:  0dd26bb584 maintenance: add incremental-repack task\n     @@ Commit message\n             it requires doing some calculations that are better isolated to\n             a separate change.\n      \n     -    Each of the above steps update the multi-pack-index file. After\n     -    each step, we verify the new multi-pack-index. If the new\n     -    multi-pack-index is corrupt, then delete the multi-pack-index,\n     -    rewrite it from scratch, and stop doing the later steps of the\n     -    job. This is intended to be an extra-safe check without leaving\n     -    a repo with many pack-files without a multi-pack-index.\n     -\n          These steps are based on a similar background maintenance step in\n          Scalar (and VFS for Git) [1]. This was incredibly effective for\n          users of the Windows OS repository. After using the same VFS for Git\n     @@ builtin/gc.c\n       \n       #define FAILED_RUN \"failed to run %s\"\n       \n     -@@ builtin/gc.c: static int maintenance_task_loose_objects(struct maintenance_opts *opts)\n     +@@ builtin/gc.c: static int maintenance_task_loose_objects(struct maintenance_run_opts *opts)\n       \treturn prune_packed(opts) || pack_loose(opts);\n       }\n       \n     -+static int multi_pack_index_write(struct maintenance_opts *opts)\n     ++static int multi_pack_index_write(struct maintenance_run_opts *opts)\n      +{\n      +\tstruct child_process child = CHILD_PROCESS_INIT;\n      +\n     @@ builtin/gc.c: static int maintenance_task_loose_objects(struct maintenance_opts\n      +\treturn 0;\n      +}\n      +\n     -+static int rewrite_multi_pack_index(struct maintenance_opts *opts)\n     -+{\n     -+\tstruct repository *r = the_repository;\n     -+\tchar *midx_name = get_midx_filename(r->objects->odb->path);\n     -+\n     -+\tunlink(midx_name);\n     -+\tfree(midx_name);\n     -+\n     -+\treturn multi_pack_index_write(opts);\n     -+}\n     -+\n     -+static int multi_pack_index_verify(struct maintenance_opts *opts,\n     -+\t\t\t\t   const char *message)\n     -+{\n     -+\tstruct child_process child = CHILD_PROCESS_INIT;\n     -+\n     -+\tchild.git_cmd = 1;\n     -+\tstrvec_pushl(&child.args, \"multi-pack-index\", \"verify\", NULL);\n     -+\n     -+\tif (opts->quiet)\n     -+\t\tstrvec_push(&child.args, \"--no-progress\");\n     -+\n     -+\tif (run_command(&child)) {\n     -+\t\twarning(_(\"'git multi-pack-index verify' failed %s\"), message);\n     -+\t\treturn 1;\n     -+\t}\n     -+\n     -+\treturn 0;\n     -+}\n     -+\n     -+static int multi_pack_index_expire(struct maintenance_opts *opts)\n     ++static int multi_pack_index_expire(struct maintenance_run_opts *opts)\n      +{\n      +\tstruct child_process child = CHILD_PROCESS_INIT;\n      +\n     @@ builtin/gc.c: static int maintenance_task_loose_objects(struct maintenance_opts\n      +\treturn 0;\n      +}\n      +\n     -+static int multi_pack_index_repack(struct maintenance_opts *opts)\n     ++static int multi_pack_index_repack(struct maintenance_run_opts *opts)\n      +{\n      +\tstruct child_process child = CHILD_PROCESS_INIT;\n      +\n     @@ builtin/gc.c: static int maintenance_task_loose_objects(struct maintenance_opts\n      +\treturn 0;\n      +}\n      +\n     -+static int maintenance_task_incremental_repack(struct maintenance_opts *opts)\n     ++static int maintenance_task_incremental_repack(struct maintenance_run_opts *opts)\n      +{\n      +\tprepare_repo_settings(the_repository);\n      +\tif (!the_repository->settings.core_multi_pack_index) {\n     @@ builtin/gc.c: static int maintenance_task_loose_objects(struct maintenance_opts\n      +\n      +\tif (multi_pack_index_write(opts))\n      +\t\treturn 1;\n     -+\tif (multi_pack_index_verify(opts, \"after initial write\"))\n     -+\t\treturn rewrite_multi_pack_index(opts);\n      +\tif (multi_pack_index_expire(opts))\n      +\t\treturn 1;\n     -+\tif (multi_pack_index_verify(opts, \"after expire step\"))\n     -+\t\treturn !!rewrite_multi_pack_index(opts);\n      +\tif (multi_pack_index_repack(opts))\n      +\t\treturn 1;\n     -+\tif (multi_pack_index_verify(opts, \"after repack step\"))\n     -+\t\treturn !!rewrite_multi_pack_index(opts);\n      +\treturn 0;\n      +}\n      +\n     - typedef int maintenance_task_fn(struct maintenance_opts *opts);\n     + typedef int maintenance_task_fn(struct maintenance_run_opts *opts);\n       \n       /*\n      @@ builtin/gc.c: struct maintenance_task {\n  8:  f0e7276755 !  7:  f3b25a9927 maintenance: auto-size incremental-repack batch\n     @@ Commit message\n          Signed-off-by: Derrick Stolee <dstolee@microsoft.com>\n      \n       ## builtin/gc.c ##\n     -@@ builtin/gc.c: static int multi_pack_index_expire(struct maintenance_opts *opts)\n     +@@ builtin/gc.c: static int multi_pack_index_expire(struct maintenance_run_opts *opts)\n       \treturn 0;\n       }\n       \n     @@ builtin/gc.c: static int multi_pack_index_expire(struct maintenance_opts *opts)\n      +\treturn result_size;\n      +}\n      +\n     - static int multi_pack_index_repack(struct maintenance_opts *opts)\n     + static int multi_pack_index_repack(struct maintenance_run_opts *opts)\n       {\n       \tstruct child_process child = CHILD_PROCESS_INIT;\n     -@@ builtin/gc.c: static int multi_pack_index_repack(struct maintenance_opts *opts)\n     +@@ builtin/gc.c: static int multi_pack_index_repack(struct maintenance_run_opts *opts)\n       \tif (opts->quiet)\n       \t\tstrvec_push(&child.args, \"--no-progress\");\n       \n  9:  5659a23ad5 !  8:  e9bb32f53a maintenance: add incremental-repack auto condition\n     @@ builtin/gc.c\n       \n       #define FAILED_RUN \"failed to run %s\"\n       \n     -@@ builtin/gc.c: static int maintenance_task_loose_objects(struct maintenance_opts *opts)\n     +@@ builtin/gc.c: static int maintenance_task_loose_objects(struct maintenance_run_opts *opts)\n       \treturn prune_packed(opts) || pack_loose(opts);\n       }\n       \n     @@ builtin/gc.c: static int maintenance_task_loose_objects(struct maintenance_opts\n      +\treturn count >= incremental_repack_auto_limit;\n      +}\n      +\n     - static int multi_pack_index_write(struct maintenance_opts *opts)\n     + static int multi_pack_index_write(struct maintenance_run_opts *opts)\n       {\n       \tstruct child_process child = CHILD_PROCESS_INIT;\n      @@ builtin/gc.c: static struct maintenance_task tasks[] = {\n\n-- \ngitgitgadget\n"},{"id":"404453","messageId":"da64c51a8182ec13aeed8f0157079fb29a09ee85.1598380599.git.gitgitgadget@gmail.com","threadId":"54001","inReplyTo":"pull.696.v3.git.1598380599.gitgitgadget@gmail.com","subject":"[PATCH v3 1/8] maintenance: add prefetch task","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2020-08-25T18:36:32Z","receivedAt":"2020-08-25T18:36:45Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"From: Derrick Stolee <dstolee@microsoft.com>\n\nWhen working with very large repositories, an incremental 'git fetch'\ncommand can download a large amount of data. If there are many other\nusers pushing to a common repo, then this data can rival the initial\npack-file size of a 'git clone' of a medium-size repo.\n\nUsers may want to keep the data on their local repos as close as\npossible to the data on the remote repos by fetching periodically in\nthe background. This can break up a large daily fetch into several\nsmaller hourly fetches.\n\nThe task is called \"prefetch\" because it is work done in advance\nof a foreground fetch to make that 'git fetch' command much faster.\n\nHowever, if we simply ran 'git fetch <remote>' in the background,\nthen the user running a foregroudn 'git fetch <remote>' would lose\nsome important feedback when a new branch appears or an existing\nbranch updates. This is especially true if a remote branch is\nforce-updated and this isn't noticed by the user because it occurred\nin the background. Further, the functionality of 'git push\n--force-with-lease' becomes suspect.\n\nWhen running 'git fetch <remote> <options>' in the background, use\nthe following options for careful updating:\n\n1. --no-tags prevents getting a new tag when a user wants to see\n   the new tags appear in their foreground fetches.\n\n2. --refmap= removes the configured refspec which usually updates\n   refs/remotes/<remote>/* with the refs advertised by the remote.\n   While this looks confusing, this was documented and tested by\n   b40a50264ac (fetch: document and test --refmap=\"\", 2020-01-21),\n   including this sentence in the documentation:\n\n\tProviding an empty `<refspec>` to the `--refmap` option\n\tcauses Git to ignore the configured refspecs and rely\n\tentirely on the refspecs supplied as command-line arguments.\n\n3. By adding a new refspec \"+refs/heads/*:refs/prefetch/<remote>/*\"\n   we can ensure that we actually load the new values somewhere in\n   our refspace while not updating refs/heads or refs/remotes. By\n   storing these refs here, the commit-graph job will update the\n   commit-graph with the commits from these hidden refs.\n\n4. --prune will delete the refs/prefetch/<remote> refs that no\n   longer appear on the remote.\n\n5. --no-write-fetch-head prevents updating FETCH_HEAD.\n\nWe've been using this step as a critical background job in Scalar\n[1] (and VFS for Git). This solved a pain point that was showing up\nin user reports: fetching was a pain! Users do not like waiting to\ndownload the data that was created while they were away from their\nmachines. After implementing background fetch, the foreground fetch\ncommands sped up significantly because they mostly just update refs\nand download a small amount of new data. The effect is especially\ndramatic when paried with --no-show-forced-udpates (through\nfetch.showForcedUpdates=false).\n\n[1] https://github.com/microsoft/scalar/blob/master/Scalar.Common/Maintenance/FetchStep.cs\n\nSigned-off-by: Derrick Stolee <dstolee@microsoft.com>\n---\n Documentation/git-maintenance.txt | 15 +++++++++\n builtin/gc.c                      | 51 +++++++++++++++++++++++++++++++\n t/t7900-maintenance.sh            | 26 ++++++++++++++++\n 3 files changed, 92 insertions(+)\n\ndiff --git a/Documentation/git-maintenance.txt b/Documentation/git-maintenance.txt\nindex 6abcb8255a..12668fccf7 100644\n--- a/Documentation/git-maintenance.txt\n+++ b/Documentation/git-maintenance.txt\n@@ -47,6 +47,21 @@ commit-graph::\n \t`commit-graph-chain` file. They will be deleted by a later run based\n \ton the expiration delay.\n \n+prefetch::\n+\tThe `prefetch` task updates the object directory with the latest\n+\tobjects from all registered remotes. For each remote, a `git fetch`\n+\tcommand is run. The refmap is custom to avoid updating local or remote\n+\tbranches (those in `refs/heads` or `refs/remotes`). Instead, the\n+\tremote refs are stored in `refs/prefetch/<remote>/`. Also, tags are\n+\tnot updated.\n++\n+This is done to avoid disrupting the remote-tracking branches. The end users\n+expect these refs to stay unmoved unless they initiate a fetch.  With prefetch\n+task, however, the objects necessary to complete a later real fetch would\n+already be obtained, so the real fetch would go faster.  In the ideal case,\n+it will just become an update to bunch of remote-tracking branches without\n+any object transfer.\n+\n gc::\n \tClean up unnecessary files and optimize the local repository. \"GC\"\n \tstands for \"garbage collection,\" but this task performs many\ndiff --git a/builtin/gc.c b/builtin/gc.c\nindex c3bcdc1167..eb9c4d6147 100644\n--- a/builtin/gc.c\n+++ b/builtin/gc.c\n@@ -29,6 +29,7 @@\n #include \"tree.h\"\n #include \"promisor-remote.h\"\n #include \"refs.h\"\n+#include \"remote.h\"\n \n #define FAILED_RUN \"failed to run %s\"\n \n@@ -816,6 +817,51 @@ static int maintenance_task_commit_graph(struct maintenance_run_opts *opts)\n \treturn 0;\n }\n \n+static int fetch_remote(const char *remote, struct maintenance_run_opts *opts)\n+{\n+\tstruct child_process child = CHILD_PROCESS_INIT;\n+\n+\tchild.git_cmd = 1;\n+\tstrvec_pushl(&child.args, \"fetch\", remote, \"--prune\", \"--no-tags\",\n+\t\t     \"--no-write-fetch-head\", \"--recurse-submodules=no\",\n+\t\t     \"--refmap=\", NULL);\n+\n+\tif (opts->quiet)\n+\t\tstrvec_push(&child.args, \"--quiet\");\n+\n+\tstrvec_pushf(&child.args, \"+refs/heads/*:refs/prefetch/%s/*\", remote);\n+\n+\treturn !!run_command(&child);\n+}\n+\n+static int append_remote(struct remote *remote, void *cbdata)\n+{\n+\tstruct string_list *remotes = (struct string_list *)cbdata;\n+\n+\tstring_list_append(remotes, remote->name);\n+\treturn 0;\n+}\n+\n+static int maintenance_task_prefetch(struct maintenance_run_opts *opts)\n+{\n+\tint result = 0;\n+\tstruct string_list_item *item;\n+\tstruct string_list remotes = STRING_LIST_INIT_DUP;\n+\n+\tif (for_each_remote(append_remote, &remotes)) {\n+\t\terror(_(\"failed to fill remotes\"));\n+\t\tresult = 1;\n+\t\tgoto cleanup;\n+\t}\n+\n+\tfor_each_string_list_item(item, &remotes)\n+\t\tresult |= fetch_remote(item->string, opts);\n+\n+cleanup:\n+\tstring_list_clear(&remotes, 0);\n+\treturn result;\n+}\n+\n static int maintenance_task_gc(struct maintenance_run_opts *opts)\n {\n \tstruct child_process child = CHILD_PROCESS_INIT;\n@@ -854,6 +900,7 @@ struct maintenance_task {\n };\n \n enum maintenance_task_label {\n+\tTASK_PREFETCH,\n \tTASK_GC,\n \tTASK_COMMIT_GRAPH,\n \n@@ -862,6 +909,10 @@ enum maintenance_task_label {\n };\n \n static struct maintenance_task tasks[] = {\n+\t[TASK_PREFETCH] = {\n+\t\t\"prefetch\",\n+\t\tmaintenance_task_prefetch,\n+\t},\n \t[TASK_GC] = {\n \t\t\"gc\",\n \t\tmaintenance_task_gc,\ndiff --git a/t/t7900-maintenance.sh b/t/t7900-maintenance.sh\nindex 4f6a04ddb1..0bade09c43 100755\n--- a/t/t7900-maintenance.sh\n+++ b/t/t7900-maintenance.sh\n@@ -60,4 +60,30 @@ test_expect_success 'run --task duplicate' '\n \ttest_i18ngrep \"cannot be selected multiple times\" err\n '\n \n+test_expect_success 'run --task=prefetch with no remotes' '\n+\tgit maintenance run --task=prefetch 2>err &&\n+\ttest_must_be_empty err\n+'\n+\n+test_expect_success 'prefetch multiple remotes' '\n+\tgit clone . clone1 &&\n+\tgit clone . clone2 &&\n+\tgit remote add remote1 \"file://$(pwd)/clone1\" &&\n+\tgit remote add remote2 \"file://$(pwd)/clone2\" &&\n+\tgit -C clone1 switch -c one &&\n+\tgit -C clone2 switch -c two &&\n+\ttest_commit -C clone1 one &&\n+\ttest_commit -C clone2 two &&\n+\tGIT_TRACE2_EVENT=\"$(pwd)/run-prefetch.txt\" git maintenance run --task=prefetch 2>/dev/null &&\n+\tfetchargs=\"--prune --no-tags --no-write-fetch-head --recurse-submodules=no --refmap= --quiet\" &&\n+\ttest_subcommand git fetch remote1 $fetchargs +refs/heads/\\\\*:refs/prefetch/remote1/\\\\* <run-prefetch.txt &&\n+\ttest_subcommand git fetch remote2 $fetchargs +refs/heads/\\\\*:refs/prefetch/remote2/\\\\* <run-prefetch.txt &&\n+\ttest_path_is_missing .git/refs/remotes &&\n+\tgit log prefetch/remote1/one &&\n+\tgit log prefetch/remote2/two &&\n+\tgit fetch --all &&\n+\ttest_cmp_rev refs/remotes/remote1/one refs/prefetch/remote1/one &&\n+\ttest_cmp_rev refs/remotes/remote2/two refs/prefetch/remote2/two\n+'\n+\n test_done\n-- \ngitgitgadget\n\n"},{"id":"404454","messageId":"75e846456b8ac4a3518ecc9051c927539dd7ff08.1598380599.git.gitgitgadget@gmail.com","threadId":"54001","inReplyTo":"pull.696.v3.git.1598380599.gitgitgadget@gmail.com","subject":"[PATCH v3 2/8] maintenance: add loose-objects task","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2020-08-25T18:36:33Z","receivedAt":"2020-08-25T18:36:49Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"From: Derrick Stolee <dstolee@microsoft.com>\n\nOne goal of background maintenance jobs is to allow a user to\ndisable auto-gc (gc.auto=0) but keep their repository in a clean\nstate. Without any cleanup, loose objects will clutter the object\ndatabase and slow operations. In addition, the loose objects will\ntake up extra space because they are not stored with deltas against\nsimilar objects.\n\nCreate a 'loose-objects' task for the 'git maintenance run' command.\nThis helps clean up loose objects without disrupting concurrent Git\ncommands using the following sequence of events:\n\n1. Run 'git prune-packed' to delete any loose objects that exist\n   in a pack-file. Concurrent commands will prefer the packed\n   version of the object to the loose version. (Of course, there\n   are exceptions for commands that specifically care about the\n   location of an object. These are rare for a user to run on\n   purpose, and we hope a user that has selected background\n   maintenance will not be trying to do foreground maintenance.)\n\n2. Run 'git pack-objects' on a batch of loose objects. These\n   objects are grouped by scanning the loose object directories in\n   lexicographic order until listing all loose objects -or-\n   reaching 50,000 objects. This is more than enough if the loose\n   objects are created only by a user doing normal development.\n   We noticed users with _millions_ of loose objects because VFS\n   for Git downloads blobs on-demand when a file read operation\n   requires populating a virtual file. This has potential of\n   happening in partial clones if someone runs 'git grep' or\n   otherwise evades the batch-download feature for requesting\n   promisor objects.\n\nThis step is based on a similar step in Scalar [1] and VFS for Git.\n[1] https://github.com/microsoft/scalar/blob/master/Scalar.Common/Maintenance/LooseObjectsStep.cs\n\nSigned-off-by: Derrick Stolee <dstolee@microsoft.com>\n---\n Documentation/git-maintenance.txt | 15 +++++\n builtin/gc.c                      | 97 +++++++++++++++++++++++++++++++\n t/t7900-maintenance.sh            | 39 +++++++++++++\n 3 files changed, 151 insertions(+)\n\ndiff --git a/Documentation/git-maintenance.txt b/Documentation/git-maintenance.txt\nindex 12668fccf7..fc95eb594f 100644\n--- a/Documentation/git-maintenance.txt\n+++ b/Documentation/git-maintenance.txt\n@@ -70,6 +70,21 @@ gc::\n \tbe disruptive in some situations, as it deletes stale data. See\n \tlinkgit:git-gc[1] for more details on garbage collection in Git.\n \n+loose-objects::\n+\tThe `loose-objects` job cleans up loose objects and places them into\n+\tpack-files. In order to prevent race conditions with concurrent Git\n+\tcommands, it follows a two-step process. First, it deletes any loose\n+\tobjects that already exist in a pack-file; concurrent Git processes\n+\twill examine the pack-file for the object data instead of the loose\n+\tobject. Second, it creates a new pack-file (starting with \"loose-\")\n+\tcontaining a batch of loose objects. The batch size is limited to 50\n+\tthousand objects to prevent the job from taking too long on a\n+\trepository with many loose objects. The `gc` task writes unreachable\n+\tobjects as loose objects to be cleaned up by a later step only if\n+\tthey are not re-added to a pack-file; for this reason it is not\n+\tadvisable to enable both the `loose-objects` and `gc` tasks at the\n+\tsame time.\n+\n OPTIONS\n -------\n --auto::\ndiff --git a/builtin/gc.c b/builtin/gc.c\nindex eb9c4d6147..248ccde3c3 100644\n--- a/builtin/gc.c\n+++ b/builtin/gc.c\n@@ -880,6 +880,98 @@ static int maintenance_task_gc(struct maintenance_run_opts *opts)\n \treturn run_command(&child);\n }\n \n+static int prune_packed(struct maintenance_run_opts *opts)\n+{\n+\tstruct child_process child = CHILD_PROCESS_INIT;\n+\n+\tchild.git_cmd = 1;\n+\tstrvec_push(&child.args, \"prune-packed\");\n+\n+\tif (opts->quiet)\n+\t\tstrvec_push(&child.args, \"--quiet\");\n+\n+\treturn !!run_command(&child);\n+}\n+\n+struct write_loose_object_data {\n+\tFILE *in;\n+\tint count;\n+\tint batch_size;\n+};\n+\n+static int bail_on_loose(const struct object_id *oid,\n+\t\t\t const char *path,\n+\t\t\t void *data)\n+{\n+\treturn 1;\n+}\n+\n+static int write_loose_object_to_stdin(const struct object_id *oid,\n+\t\t\t\t       const char *path,\n+\t\t\t\t       void *data)\n+{\n+\tstruct write_loose_object_data *d = (struct write_loose_object_data *)data;\n+\n+\tfprintf(d->in, \"%s\\n\", oid_to_hex(oid));\n+\n+\treturn ++(d->count) > d->batch_size;\n+}\n+\n+static int pack_loose(struct maintenance_run_opts *opts)\n+{\n+\tstruct repository *r = the_repository;\n+\tint result = 0;\n+\tstruct write_loose_object_data data;\n+\tstruct child_process pack_proc = CHILD_PROCESS_INIT;\n+\n+\t/*\n+\t * Do not start pack-objects process\n+\t * if there are no loose objects.\n+\t */\n+\tif (!for_each_loose_file_in_objdir(r->objects->odb->path,\n+\t\t\t\t\t   bail_on_loose,\n+\t\t\t\t\t   NULL, NULL, NULL))\n+\t\treturn 0;\n+\n+\tpack_proc.git_cmd = 1;\n+\n+\tstrvec_push(&pack_proc.args, \"pack-objects\");\n+\tif (opts->quiet)\n+\t\tstrvec_push(&pack_proc.args, \"--quiet\");\n+\tstrvec_pushf(&pack_proc.args, \"%s/pack/loose\", r->objects->odb->path);\n+\n+\tpack_proc.in = -1;\n+\n+\tif (start_command(&pack_proc)) {\n+\t\terror(_(\"failed to start 'git pack-objects' process\"));\n+\t\treturn 1;\n+\t}\n+\n+\tdata.in = xfdopen(pack_proc.in, \"w\");\n+\tdata.count = 0;\n+\tdata.batch_size = 50000;\n+\n+\tfor_each_loose_file_in_objdir(r->objects->odb->path,\n+\t\t\t\t      write_loose_object_to_stdin,\n+\t\t\t\t      NULL,\n+\t\t\t\t      NULL,\n+\t\t\t\t      &data);\n+\n+\tfclose(data.in);\n+\n+\tif (finish_command(&pack_proc)) {\n+\t\terror(_(\"failed to finish 'git pack-objects' process\"));\n+\t\tresult = 1;\n+\t}\n+\n+\treturn result;\n+}\n+\n+static int maintenance_task_loose_objects(struct maintenance_run_opts *opts)\n+{\n+\treturn prune_packed(opts) || pack_loose(opts);\n+}\n+\n typedef int maintenance_task_fn(struct maintenance_run_opts *opts);\n \n /*\n@@ -901,6 +993,7 @@ struct maintenance_task {\n \n enum maintenance_task_label {\n \tTASK_PREFETCH,\n+\tTASK_LOOSE_OBJECTS,\n \tTASK_GC,\n \tTASK_COMMIT_GRAPH,\n \n@@ -913,6 +1006,10 @@ static struct maintenance_task tasks[] = {\n \t\t\"prefetch\",\n \t\tmaintenance_task_prefetch,\n \t},\n+\t[TASK_LOOSE_OBJECTS] = {\n+\t\t\"loose-objects\",\n+\t\tmaintenance_task_loose_objects,\n+\t},\n \t[TASK_GC] = {\n \t\t\"gc\",\n \t\tmaintenance_task_gc,\ndiff --git a/t/t7900-maintenance.sh b/t/t7900-maintenance.sh\nindex 0bade09c43..2e9e369786 100755\n--- a/t/t7900-maintenance.sh\n+++ b/t/t7900-maintenance.sh\n@@ -86,4 +86,43 @@ test_expect_success 'prefetch multiple remotes' '\n \ttest_cmp_rev refs/remotes/remote2/two refs/prefetch/remote2/two\n '\n \n+test_expect_success 'loose-objects task' '\n+\t# Repack everything so we know the state of the object dir\n+\tgit repack -adk &&\n+\n+\t# Hack to stop maintenance from running during \"git commit\"\n+\techo in use >.git/objects/maintenance.lock &&\n+\n+\t# Assuming that \"git commit\" creates at least one loose object\n+\ttest_commit create-loose-object &&\n+\trm .git/objects/maintenance.lock &&\n+\n+\tls .git/objects >obj-dir-before &&\n+\ttest_file_not_empty obj-dir-before &&\n+\tls .git/objects/pack/*.pack >packs-before &&\n+\ttest_line_count = 1 packs-before &&\n+\n+\t# The first run creates a pack-file\n+\t# but does not delete loose objects.\n+\tgit maintenance run --task=loose-objects &&\n+\tls .git/objects >obj-dir-between &&\n+\ttest_cmp obj-dir-before obj-dir-between &&\n+\tls .git/objects/pack/*.pack >packs-between &&\n+\ttest_line_count = 2 packs-between &&\n+\tls .git/objects/pack/loose-*.pack >loose-packs &&\n+\ttest_line_count = 1 loose-packs &&\n+\n+\t# The second run deletes loose objects\n+\t# but does not create a pack-file.\n+\tgit maintenance run --task=loose-objects &&\n+\tls .git/objects >obj-dir-after &&\n+\tcat >expect <<-\\EOF &&\n+\tinfo\n+\tpack\n+\tEOF\n+\ttest_cmp expect obj-dir-after &&\n+\tls .git/objects/pack/*.pack >packs-after &&\n+\ttest_cmp packs-between packs-after\n+'\n+\n test_done\n-- \ngitgitgadget\n\n"},{"id":"404455","messageId":"d6e382c43effe063fb1137659f616d414ee52682.1598380599.git.gitgitgadget@gmail.com","threadId":"54001","inReplyTo":"pull.696.v3.git.1598380599.gitgitgadget@gmail.com","subject":"[PATCH v3 3/8] maintenance: create auto condition for loose-objects","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2020-08-25T18:36:34Z","receivedAt":"2020-08-25T18:36:57Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"From: Derrick Stolee <dstolee@microsoft.com>\n\nThe loose-objects task deletes loose objects that already exist in a\npack-file, then place the remaining loose objects into a new pack-file.\nIf this step runs all the time, then we risk creating pack-files with\nvery few objects with every 'git commit' process. To prevent\noverwhelming the packs directory with small pack-files, place a minimum\nnumber of objects to justify the task.\n\nThe 'maintenance.loose-objects.auto' config option specifies a minimum\nnumber of loose objects to justify the task to run under the '--auto'\noption. This defaults to 100 loose objects. Setting the value to zero\nwill prevent the step from running under '--auto' while a negative value\nwill force it to run every time.\n\nSigned-off-by: Derrick Stolee <dstolee@microsoft.com>\n---\n Documentation/config/maintenance.txt |  9 +++++++++\n builtin/gc.c                         | 30 ++++++++++++++++++++++++++++\n t/t7900-maintenance.sh               | 25 +++++++++++++++++++++++\n 3 files changed, 64 insertions(+)\n\ndiff --git a/Documentation/config/maintenance.txt b/Documentation/config/maintenance.txt\nindex 7cc6700d57..c31613be62 100644\n--- a/Documentation/config/maintenance.txt\n+++ b/Documentation/config/maintenance.txt\n@@ -14,3 +14,12 @@ maintenance.commit-graph.auto::\n \treachable commits that are not in the commit-graph file is at least\n \tthe value of `maintenance.commit-graph.auto`. The default value is\n \t100.\n+\n+maintenance.loose-objects.auto::\n+\tThis integer config option controls how often the `loose-objects` task\n+\tshould be run as part of `git maintenance run --auto`. If zero, then\n+\tthe `loose-objects` task will not run with the `--auto` option. A\n+\tnegative value will force the task to run every time. Otherwise, a\n+\tpositive value implies the command should run when the number of\n+\tloose objects is at least the value of `maintenance.loose-objects.auto`.\n+\tThe default value is 100.\ndiff --git a/builtin/gc.c b/builtin/gc.c\nindex 248ccde3c3..25245bcc10 100644\n--- a/builtin/gc.c\n+++ b/builtin/gc.c\n@@ -899,6 +899,35 @@ struct write_loose_object_data {\n \tint batch_size;\n };\n \n+static int loose_object_auto_limit = 100;\n+\n+static int loose_object_count(const struct object_id *oid,\n+\t\t\t       const char *path,\n+\t\t\t       void *data)\n+{\n+\tint *count = (int*)data;\n+\tif (++(*count) >= loose_object_auto_limit)\n+\t\treturn 1;\n+\treturn 0;\n+}\n+\n+static int loose_object_auto_condition(void)\n+{\n+\tint count = 0;\n+\n+\tgit_config_get_int(\"maintenance.loose-objects.auto\",\n+\t\t\t   &loose_object_auto_limit);\n+\n+\tif (!loose_object_auto_limit)\n+\t\treturn 0;\n+\tif (loose_object_auto_limit < 0)\n+\t\treturn 1;\n+\n+\treturn for_each_loose_file_in_objdir(the_repository->objects->odb->path,\n+\t\t\t\t\t     loose_object_count,\n+\t\t\t\t\t     NULL, NULL, &count);\n+}\n+\n static int bail_on_loose(const struct object_id *oid,\n \t\t\t const char *path,\n \t\t\t void *data)\n@@ -1009,6 +1038,7 @@ static struct maintenance_task tasks[] = {\n \t[TASK_LOOSE_OBJECTS] = {\n \t\t\"loose-objects\",\n \t\tmaintenance_task_loose_objects,\n+\t\tloose_object_auto_condition,\n \t},\n \t[TASK_GC] = {\n \t\t\"gc\",\ndiff --git a/t/t7900-maintenance.sh b/t/t7900-maintenance.sh\nindex 2e9e369786..efda1cf69b 100755\n--- a/t/t7900-maintenance.sh\n+++ b/t/t7900-maintenance.sh\n@@ -125,4 +125,29 @@ test_expect_success 'loose-objects task' '\n \ttest_cmp packs-between packs-after\n '\n \n+test_expect_success 'maintenance.loose-objects.auto' '\n+\tgit repack -adk &&\n+\tGIT_TRACE2_EVENT=\"$(pwd)/trace-lo1.txt\" \\\n+\t\tgit -c maintenance.loose-objects.auto=1 maintenance \\\n+\t\trun --auto --task=loose-objects 2>/dev/null &&\n+\ttest_subcommand ! git prune-packed --quiet <trace-lo1.txt &&\n+\tfor i in 1 2\n+\tdo\n+\t\tprintf data-A-$i | git hash-object -t blob --stdin -w &&\n+\t\tGIT_TRACE2_EVENT=\"$(pwd)/trace-loA-$i\" \\\n+\t\t\tgit -c maintenance.loose-objects.auto=2 \\\n+\t\t\tmaintenance run --auto --task=loose-objects 2>/dev/null &&\n+\t\ttest_subcommand ! git prune-packed --quiet <trace-loA-$i &&\n+\t\tprintf data-B-$i | git hash-object -t blob --stdin -w &&\n+\t\tGIT_TRACE2_EVENT=\"$(pwd)/trace-loB-$i\" \\\n+\t\t\tgit -c maintenance.loose-objects.auto=2 \\\n+\t\t\tmaintenance run --auto --task=loose-objects 2>/dev/null &&\n+\t\ttest_subcommand git prune-packed --quiet <trace-loB-$i &&\n+\t\tGIT_TRACE2_EVENT=\"$(pwd)/trace-loC-$i\" \\\n+\t\t\tgit -c maintenance.loose-objects.auto=2 \\\n+\t\t\tmaintenance run --auto --task=loose-objects 2>/dev/null &&\n+\t\ttest_subcommand git prune-packed --quiet <trace-loC-$i || return 1\n+\tdone\n+'\n+\n test_done\n-- \ngitgitgadget\n\n"},{"id":"404456","messageId":"d0f2ec70d9dc0b4901704d48bf5cbddbd6e50c01.1598380599.git.gitgitgadget@gmail.com","threadId":"54001","inReplyTo":"pull.696.v3.git.1598380599.gitgitgadget@gmail.com","subject":"[PATCH v3 4/8] midx: enable core.multiPackIndex by default","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2020-08-25T18:36:35Z","receivedAt":"2020-08-25T18:36:59Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"From: Derrick Stolee <dstolee@microsoft.com>\n\nThe core.multiPackIndex setting has been around since c4d25228ebb\n(config: create core.multiPackIndex setting, 2018-07-12), but has been\ndisabled by default. If a user wishes to use the multi-pack-index\nfeature, then they must enable this config and run 'git multi-pack-index\nwrite'.\n\nThe multi-pack-index feature is relatively stable now, so make the\nconfig option true by default. For users that do not use a\nmulti-pack-index, the only extra cost will be a file lookup to see if a\nmulti-pack-index file exists (once per process, per object directory).\n\nAlso, this config option will be referenced by an upcoming\n\"incremental-repack\" task in the maintenance builtin, so move the config\noption into the repository settings struct. Note that if\nGIT_TEST_MULTI_PACK_INDEX=1, then we want to ignore the config option\nand treat core.multiPackIndex as enabled.\n\nSigned-off-by: Derrick Stolee <dstolee@microsoft.com>\n---\n Documentation/config/core.txt |  4 ++--\n midx.c                        | 11 +++--------\n repo-settings.c               |  6 ++++++\n repository.h                  |  2 ++\n 4 files changed, 13 insertions(+), 10 deletions(-)\n\ndiff --git a/Documentation/config/core.txt b/Documentation/config/core.txt\nindex 74619a9c03..86c91d5381 100644\n--- a/Documentation/config/core.txt\n+++ b/Documentation/config/core.txt\n@@ -606,8 +606,8 @@ core.useReplaceRefs::\n \n core.multiPackIndex::\n \tUse the multi-pack-index file to track multiple packfiles using a\n-\tsingle index. See link:technical/multi-pack-index.html[the\n-\tmulti-pack-index design document].\n+\tsingle index. See linkgit:git-multi-pack-index[1] for more\n+\tinformation. Defaults to true.\n \n core.sparseCheckout::\n \tEnable \"sparse checkout\" feature. See linkgit:git-sparse-checkout[1]\ndiff --git a/midx.c b/midx.c\nindex a5fb797ede..ef499cf504 100644\n--- a/midx.c\n+++ b/midx.c\n@@ -10,6 +10,7 @@\n #include \"progress.h\"\n #include \"trace2.h\"\n #include \"run-command.h\"\n+#include \"repository.h\"\n \n #define MIDX_SIGNATURE 0x4d494458 /* \"MIDX\" */\n #define MIDX_VERSION 1\n@@ -384,15 +385,9 @@ int prepare_multi_pack_index_one(struct repository *r, const char *object_dir, i\n {\n \tstruct multi_pack_index *m;\n \tstruct multi_pack_index *m_search;\n-\tint config_value;\n-\tstatic int env_value = -1;\n \n-\tif (env_value < 0)\n-\t\tenv_value = git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0);\n-\n-\tif (!env_value &&\n-\t    (repo_config_get_bool(r, \"core.multipackindex\", &config_value) ||\n-\t    !config_value))\n+\tprepare_repo_settings(r);\n+\tif (!r->settings.core_multi_pack_index)\n \t\treturn 0;\n \n \tfor (m_search = r->objects->multi_pack_index; m_search; m_search = m_search->next)\ndiff --git a/repo-settings.c b/repo-settings.c\nindex 0918408b34..5bd2c22726 100644\n--- a/repo-settings.c\n+++ b/repo-settings.c\n@@ -1,6 +1,7 @@\n #include \"cache.h\"\n #include \"config.h\"\n #include \"repository.h\"\n+#include \"midx.h\"\n \n #define UPDATE_DEFAULT_BOOL(s,v) do { if (s == -1) { s = v; } } while(0)\n \n@@ -47,6 +48,11 @@ void prepare_repo_settings(struct repository *r)\n \t\tr->settings.pack_use_sparse = value;\n \tUPDATE_DEFAULT_BOOL(r->settings.pack_use_sparse, 1);\n \n+\tvalue = git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0);\n+\tif (value || !repo_config_get_bool(r, \"core.multipackindex\", &value))\n+\t\tr->settings.core_multi_pack_index = value;\n+\tUPDATE_DEFAULT_BOOL(r->settings.core_multi_pack_index, 1);\n+\n \tif (!repo_config_get_bool(r, \"feature.manyfiles\", &value) && value) {\n \t\tUPDATE_DEFAULT_BOOL(r->settings.index_version, 4);\n \t\tUPDATE_DEFAULT_BOOL(r->settings.core_untracked_cache, UNTRACKED_CACHE_WRITE);\ndiff --git a/repository.h b/repository.h\nindex 3c1f7d54bd..3901ce0b65 100644\n--- a/repository.h\n+++ b/repository.h\n@@ -37,6 +37,8 @@ struct repo_settings {\n \n \tint pack_use_sparse;\n \tenum fetch_negotiation_setting fetch_negotiation_algorithm;\n+\n+\tint core_multi_pack_index;\n };\n \n struct repository {\n-- \ngitgitgadget\n\n"},{"id":"404457","messageId":"2cd3c803d9b7227e645b26758605475e40bf98eb.1598380599.git.gitgitgadget@gmail.com","threadId":"54001","inReplyTo":"pull.696.v3.git.1598380599.gitgitgadget@gmail.com","subject":"[PATCH v3 5/8] midx: use start_delayed_progress()","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2020-08-25T18:36:36Z","receivedAt":"2020-08-25T18:37:01Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"From: Derrick Stolee <dstolee@microsoft.com>\n\nNow that the multi-pack-index may be written as part of auto maintenance\nat the end of a command, reduce the progress output when the operations\nare quick. Use start_delayed_progress() instead of start_progress().\n\nUpdate t5319-multi-pack-index.sh to use GIT_PROGRESS_DELAY=0 now that\nthe progress indicators are conditional.\n\nSigned-off-by: Derrick Stolee <dstolee@microsoft.com>\n---\n midx.c                      | 10 +++++-----\n t/t5319-multi-pack-index.sh | 14 +++++++-------\n 2 files changed, 12 insertions(+), 12 deletions(-)\n\ndiff --git a/midx.c b/midx.c\nindex ef499cf504..aa37d5da86 100644\n--- a/midx.c\n+++ b/midx.c\n@@ -832,7 +832,7 @@ static int write_midx_internal(const char *object_dir, struct multi_pack_index *\n \n \tpacks.pack_paths_checked = 0;\n \tif (flags & MIDX_PROGRESS)\n-\t\tpacks.progress = start_progress(_(\"Adding packfiles to multi-pack-index\"), 0);\n+\t\tpacks.progress = start_delayed_progress(_(\"Adding packfiles to multi-pack-index\"), 0);\n \telse\n \t\tpacks.progress = NULL;\n \n@@ -969,7 +969,7 @@ static int write_midx_internal(const char *object_dir, struct multi_pack_index *\n \t}\n \n \tif (flags & MIDX_PROGRESS)\n-\t\tprogress = start_progress(_(\"Writing chunks to multi-pack-index\"),\n+\t\tprogress = start_delayed_progress(_(\"Writing chunks to multi-pack-index\"),\n \t\t\t\t\t  num_chunks);\n \tfor (i = 0; i < num_chunks; i++) {\n \t\tif (written != chunk_offsets[i])\n@@ -1104,7 +1104,7 @@ int verify_midx_file(struct repository *r, const char *object_dir, unsigned flag\n \t\treturn 0;\n \n \tif (flags & MIDX_PROGRESS)\n-\t\tprogress = start_progress(_(\"Looking for referenced packfiles\"),\n+\t\tprogress = start_delayed_progress(_(\"Looking for referenced packfiles\"),\n \t\t\t\t\t  m->num_packs);\n \tfor (i = 0; i < m->num_packs; i++) {\n \t\tif (prepare_midx_pack(r, m, i))\n@@ -1225,7 +1225,7 @@ int expire_midx_packs(struct repository *r, const char *object_dir, unsigned fla\n \tcount = xcalloc(m->num_packs, sizeof(uint32_t));\n \n \tif (flags & MIDX_PROGRESS)\n-\t\tprogress = start_progress(_(\"Counting referenced objects\"),\n+\t\tprogress = start_delayed_progress(_(\"Counting referenced objects\"),\n \t\t\t\t\t  m->num_objects);\n \tfor (i = 0; i < m->num_objects; i++) {\n \t\tint pack_int_id = nth_midxed_pack_int_id(m, i);\n@@ -1235,7 +1235,7 @@ int expire_midx_packs(struct repository *r, const char *object_dir, unsigned fla\n \tstop_progress(&progress);\n \n \tif (flags & MIDX_PROGRESS)\n-\t\tprogress = start_progress(_(\"Finding and deleting unreferenced packfiles\"),\n+\t\tprogress = start_delayed_progress(_(\"Finding and deleting unreferenced packfiles\"),\n \t\t\t\t\t  m->num_packs);\n \tfor (i = 0; i < m->num_packs; i++) {\n \t\tchar *pack_name;\ndiff --git a/t/t5319-multi-pack-index.sh b/t/t5319-multi-pack-index.sh\nindex 7dfff0f8f4..ec87f616c6 100755\n--- a/t/t5319-multi-pack-index.sh\n+++ b/t/t5319-multi-pack-index.sh\n@@ -171,12 +171,12 @@ test_expect_success 'write progress off for redirected stderr' '\n '\n \n test_expect_success 'write force progress on for stderr' '\n-\tgit multi-pack-index --object-dir=$objdir --progress write 2>err &&\n+\tGIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir --progress write 2>err &&\n \ttest_file_not_empty err\n '\n \n test_expect_success 'write with the --no-progress option' '\n-\tgit multi-pack-index --object-dir=$objdir --no-progress write 2>err &&\n+\tGIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir --no-progress write 2>err &&\n \ttest_line_count = 0 err\n '\n \n@@ -333,17 +333,17 @@ test_expect_success 'git-fsck incorrect offset' '\n '\n \n test_expect_success 'repack progress off for redirected stderr' '\n-\tgit multi-pack-index --object-dir=$objdir repack 2>err &&\n+\tGIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir repack 2>err &&\n \ttest_line_count = 0 err\n '\n \n test_expect_success 'repack force progress on for stderr' '\n-\tgit multi-pack-index --object-dir=$objdir --progress repack 2>err &&\n+\tGIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir --progress repack 2>err &&\n \ttest_file_not_empty err\n '\n \n test_expect_success 'repack with the --no-progress option' '\n-\tgit multi-pack-index --object-dir=$objdir --no-progress repack 2>err &&\n+\tGIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir --no-progress repack 2>err &&\n \ttest_line_count = 0 err\n '\n \n@@ -487,7 +487,7 @@ test_expect_success 'expire progress off for redirected stderr' '\n test_expect_success 'expire force progress on for stderr' '\n \t(\n \t\tcd dup &&\n-\t\tgit multi-pack-index --progress expire 2>err &&\n+\t\tGIT_PROGRESS_DELAY=0 git multi-pack-index --progress expire 2>err &&\n \t\ttest_file_not_empty err\n \t)\n '\n@@ -495,7 +495,7 @@ test_expect_success 'expire force progress on for stderr' '\n test_expect_success 'expire with the --no-progress option' '\n \t(\n \t\tcd dup &&\n-\t\tgit multi-pack-index --no-progress expire 2>err &&\n+\t\tGIT_PROGRESS_DELAY=0 git multi-pack-index --no-progress expire 2>err &&\n \t\ttest_line_count = 0 err\n \t)\n '\n-- \ngitgitgadget\n\n"},{"id":"404458","messageId":"0dd26bb584bb7e8b9616eb32f7b1235462df77fa.1598380599.git.gitgitgadget@gmail.com","threadId":"54001","inReplyTo":"pull.696.v3.git.1598380599.gitgitgadget@gmail.com","subject":"[PATCH v3 6/8] maintenance: add incremental-repack task","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2020-08-25T18:36:37Z","receivedAt":"2020-08-25T18:37:03Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"From: Derrick Stolee <dstolee@microsoft.com>\n\nThe previous change cleaned up loose objects using the\n'loose-objects' that can be run safely in the background. Add a\nsimilar job that performs similar cleanups for pack-files.\n\nOne issue with running 'git repack' is that it is designed to\nrepack all pack-files into a single pack-file. While this is the\nmost space-efficient way to store object data, it is not time or\nmemory efficient. This becomes extremely important if the repo is\nso large that a user struggles to store two copies of the pack on\ntheir disk.\n\nInstead, perform an \"incremental\" repack by collecting a few small\npack-files into a new pack-file. The multi-pack-index facilitates\nthis process ever since 'git multi-pack-index expire' was added in\n19575c7 (multi-pack-index: implement 'expire' subcommand,\n2019-06-10) and 'git multi-pack-index repack' was added in ce1e4a1\n(midx: implement midx_repack(), 2019-06-10).\n\nThe 'incremental-repack' task runs the following steps:\n\n1. 'git multi-pack-index write' creates a multi-pack-index file if\n   one did not exist, and otherwise will update the multi-pack-index\n   with any new pack-files that appeared since the last write. This\n   is particularly relevant with the background fetch job.\n\n   When the multi-pack-index sees two copies of the same object, it\n   stores the offset data into the newer pack-file. This means that\n   some old pack-files could become \"unreferenced\" which I will use\n   to mean \"a pack-file that is in the pack-file list of the\n   multi-pack-index but none of the objects in the multi-pack-index\n   reference a location inside that pack-file.\"\n\n2. 'git multi-pack-index expire' deletes any unreferenced pack-files\n   and updaes the multi-pack-index to drop those pack-files from the\n   list. This is safe to do as concurrent Git processes will see the\n   multi-pack-index and not open those packs when looking for object\n   contents. (Similar to the 'loose-objects' job, there are some Git\n   commands that open pack-files regardless of the multi-pack-index,\n   but they are rarely used. Further, a user that self-selects to\n   use background operations would likely refrain from using those\n   commands.)\n\n3. 'git multi-pack-index repack --bacth-size=<size>' collects a set\n   of pack-files that are listed in the multi-pack-index and creates\n   a new pack-file containing the objects whose offsets are listed\n   by the multi-pack-index to be in those objects. The set of pack-\n   files is selected greedily by sorting the pack-files by modified\n   time and adding a pack-file to the set if its \"expected size\" is\n   smaller than the batch size until the total expected size of the\n   selected pack-files is at least the batch size. The \"expected\n   size\" is calculated by taking the size of the pack-file divided\n   by the number of objects in the pack-file and multiplied by the\n   number of objects from the multi-pack-index with offset in that\n   pack-file. The expected size approximates how much data from that\n   pack-file will contribute to the resulting pack-file size. The\n   intention is that the resulting pack-file will be close in size\n   to the provided batch size.\n\n   The next run of the incremental-repack task will delete these\n   repacked pack-files during the 'expire' step.\n\n   In this version, the batch size is set to \"0\" which ignores the\n   size restrictions when selecting the pack-files. It instead\n   selects all pack-files and repacks all packed objects into a\n   single pack-file. This will be updated in the next change, but\n   it requires doing some calculations that are better isolated to\n   a separate change.\n\nThese steps are based on a similar background maintenance step in\nScalar (and VFS for Git) [1]. This was incredibly effective for\nusers of the Windows OS repository. After using the same VFS for Git\nrepository for over a year, some users had _thousands_ of pack-files\nthat combined to up to 250 GB of data. We noticed a few users were\nrunning into the open file descriptor limits (due in part to a bug\nin the multi-pack-index fixed by af96fe3 (midx: add packs to\npacked_git linked list, 2019-04-29).\n\nThese pack-files were mostly small since they contained the commits\nand trees that were pushed to the origin in a given hour. The GVFS\nprotocol includes a \"prefetch\" step that asks for pre-computed pack-\nfiles containing commits and trees by timestamp. These pack-files\nwere grouped into \"daily\" pack-files once a day for up to 30 days.\nIf a user did not request prefetch packs for over 30 days, then they\nwould get the entire history of commits and trees in a new, large\npack-file. This led to a large number of pack-files that had poor\ndelta compression.\n\nBy running this pack-file maintenance step once per day, these repos\nwith thousands of packs spanning 200+ GB dropped to dozens of pack-\nfiles spanning 30-50 GB. This was done all without removing objects\nfrom the system and using a constant batch size of two gigabytes.\nOnce the work was done to reduce the pack-files to small sizes, the\nbatch size of two gigabytes means that not every run triggers a\nrepack operation, so the following run will not expire a pack-file.\nThis has kept these repos in a \"clean\" state.\n\n[1] https://github.com/microsoft/scalar/blob/master/Scalar.Common/Maintenance/PackfileMaintenanceStep.cs\n\nSigned-off-by: Derrick Stolee <dstolee@microsoft.com>\n---\n Documentation/git-maintenance.txt | 15 ++++++\n builtin/gc.c                      | 77 +++++++++++++++++++++++++++++++\n midx.c                            |  2 +-\n midx.h                            |  1 +\n t/t5319-multi-pack-index.sh       |  1 +\n t/t7900-maintenance.sh            | 38 +++++++++++++++\n 6 files changed, 133 insertions(+), 1 deletion(-)\n\ndiff --git a/Documentation/git-maintenance.txt b/Documentation/git-maintenance.txt\nindex fc95eb594f..b44efb05a3 100644\n--- a/Documentation/git-maintenance.txt\n+++ b/Documentation/git-maintenance.txt\n@@ -85,6 +85,21 @@ loose-objects::\n \tadvisable to enable both the `loose-objects` and `gc` tasks at the\n \tsame time.\n \n+incremental-repack::\n+\tThe `incremental-repack` job repacks the object directory\n+\tusing the `multi-pack-index` feature. In order to prevent race\n+\tconditions with concurrent Git commands, it follows a two-step\n+\tprocess. First, it deletes any pack-files included in the\n+\t`multi-pack-index` where none of the objects in the\n+\t`multi-pack-index` reference those pack-files; this only happens\n+\tif all objects in the pack-file are also stored in a newer\n+\tpack-file. Second, it selects a group of pack-files whose \"expected\n+\tsize\" is below the batch size until the group has total expected\n+\tsize at least the batch size; see the `--batch-size` option for\n+\tthe `repack` subcommand in linkgit:git-multi-pack-index[1]. The\n+\tdefault batch-size is zero, which is a special case that attempts\n+\tto repack all pack-files into a single pack-file.\n+\n OPTIONS\n -------\n --auto::\ndiff --git a/builtin/gc.c b/builtin/gc.c\nindex 25245bcc10..fbf84996fa 100644\n--- a/builtin/gc.c\n+++ b/builtin/gc.c\n@@ -30,6 +30,7 @@\n #include \"promisor-remote.h\"\n #include \"refs.h\"\n #include \"remote.h\"\n+#include \"midx.h\"\n \n #define FAILED_RUN \"failed to run %s\"\n \n@@ -1001,6 +1002,77 @@ static int maintenance_task_loose_objects(struct maintenance_run_opts *opts)\n \treturn prune_packed(opts) || pack_loose(opts);\n }\n \n+static int multi_pack_index_write(struct maintenance_run_opts *opts)\n+{\n+\tstruct child_process child = CHILD_PROCESS_INIT;\n+\n+\tchild.git_cmd = 1;\n+\tstrvec_pushl(&child.args, \"multi-pack-index\", \"write\", NULL);\n+\n+\tif (opts->quiet)\n+\t\tstrvec_push(&child.args, \"--no-progress\");\n+\n+\tif (run_command(&child))\n+\t\treturn error(_(\"failed to write multi-pack-index\"));\n+\n+\treturn 0;\n+}\n+\n+static int multi_pack_index_expire(struct maintenance_run_opts *opts)\n+{\n+\tstruct child_process child = CHILD_PROCESS_INIT;\n+\n+\tchild.git_cmd = 1;\n+\tstrvec_pushl(&child.args, \"multi-pack-index\", \"expire\", NULL);\n+\n+\tif (opts->quiet)\n+\t\tstrvec_push(&child.args, \"--no-progress\");\n+\n+\tclose_object_store(the_repository->objects);\n+\n+\tif (run_command(&child))\n+\t\treturn error(_(\"'git multi-pack-index expire' failed\"));\n+\n+\treturn 0;\n+}\n+\n+static int multi_pack_index_repack(struct maintenance_run_opts *opts)\n+{\n+\tstruct child_process child = CHILD_PROCESS_INIT;\n+\n+\tchild.git_cmd = 1;\n+\tstrvec_pushl(&child.args, \"multi-pack-index\", \"repack\", NULL);\n+\n+\tif (opts->quiet)\n+\t\tstrvec_push(&child.args, \"--no-progress\");\n+\n+\tstrvec_push(&child.args, \"--batch-size=0\");\n+\n+\tclose_object_store(the_repository->objects);\n+\n+\tif (run_command(&child))\n+\t\treturn error(_(\"'git multi-pack-index repack' failed\"));\n+\n+\treturn 0;\n+}\n+\n+static int maintenance_task_incremental_repack(struct maintenance_run_opts *opts)\n+{\n+\tprepare_repo_settings(the_repository);\n+\tif (!the_repository->settings.core_multi_pack_index) {\n+\t\twarning(_(\"skipping incremental-repack task because core.multiPackIndex is disabled\"));\n+\t\treturn 0;\n+\t}\n+\n+\tif (multi_pack_index_write(opts))\n+\t\treturn 1;\n+\tif (multi_pack_index_expire(opts))\n+\t\treturn 1;\n+\tif (multi_pack_index_repack(opts))\n+\t\treturn 1;\n+\treturn 0;\n+}\n+\n typedef int maintenance_task_fn(struct maintenance_run_opts *opts);\n \n /*\n@@ -1023,6 +1095,7 @@ struct maintenance_task {\n enum maintenance_task_label {\n \tTASK_PREFETCH,\n \tTASK_LOOSE_OBJECTS,\n+\tTASK_INCREMENTAL_REPACK,\n \tTASK_GC,\n \tTASK_COMMIT_GRAPH,\n \n@@ -1040,6 +1113,10 @@ static struct maintenance_task tasks[] = {\n \t\tmaintenance_task_loose_objects,\n \t\tloose_object_auto_condition,\n \t},\n+\t[TASK_INCREMENTAL_REPACK] = {\n+\t\t\"incremental-repack\",\n+\t\tmaintenance_task_incremental_repack,\n+\t},\n \t[TASK_GC] = {\n \t\t\"gc\",\n \t\tmaintenance_task_gc,\ndiff --git a/midx.c b/midx.c\nindex aa37d5da86..66d7053d83 100644\n--- a/midx.c\n+++ b/midx.c\n@@ -37,7 +37,7 @@\n \n #define PACK_EXPIRED UINT_MAX\n \n-static char *get_midx_filename(const char *object_dir)\n+char *get_midx_filename(const char *object_dir)\n {\n \treturn xstrfmt(\"%s/pack/multi-pack-index\", object_dir);\n }\ndiff --git a/midx.h b/midx.h\nindex b18cf53bc4..baeecc70c9 100644\n--- a/midx.h\n+++ b/midx.h\n@@ -37,6 +37,7 @@ struct multi_pack_index {\n \n #define MIDX_PROGRESS     (1 << 0)\n \n+char *get_midx_filename(const char *object_dir);\n struct multi_pack_index *load_multi_pack_index(const char *object_dir, int local);\n int prepare_midx_pack(struct repository *r, struct multi_pack_index *m, uint32_t pack_int_id);\n int bsearch_midx(const struct object_id *oid, struct multi_pack_index *m, uint32_t *result);\ndiff --git a/t/t5319-multi-pack-index.sh b/t/t5319-multi-pack-index.sh\nindex ec87f616c6..2f942ee1fa 100755\n--- a/t/t5319-multi-pack-index.sh\n+++ b/t/t5319-multi-pack-index.sh\n@@ -3,6 +3,7 @@\n test_description='multi-pack-indexes'\n . ./test-lib.sh\n \n+GIT_TEST_MULTI_PACK_INDEX=0\n objdir=.git/objects\n \n midx_read_expect () {\ndiff --git a/t/t7900-maintenance.sh b/t/t7900-maintenance.sh\nindex efda1cf69b..dde28cf837 100755\n--- a/t/t7900-maintenance.sh\n+++ b/t/t7900-maintenance.sh\n@@ -5,6 +5,7 @@ test_description='git maintenance builtin'\n . ./test-lib.sh\n \n GIT_TEST_COMMIT_GRAPH=0\n+GIT_TEST_MULTI_PACK_INDEX=0\n \n test_expect_success 'help text' '\n \ttest_expect_code 129 git maintenance -h 2>err &&\n@@ -150,4 +151,41 @@ test_expect_success 'maintenance.loose-objects.auto' '\n \tdone\n '\n \n+test_expect_success 'incremental-repack task' '\n+\tpackDir=.git/objects/pack &&\n+\tfor i in $(test_seq 1 5)\n+\tdo\n+\t\ttest_commit $i || return 1\n+\tdone &&\n+\n+\t# Create three disjoint pack-files with size BIG, small, small.\n+\techo HEAD~2 | git pack-objects --revs $packDir/test-1 &&\n+\ttest_tick &&\n+\tgit pack-objects --revs $packDir/test-2 <<-\\EOF &&\n+\tHEAD~1\n+\t^HEAD~2\n+\tEOF\n+\ttest_tick &&\n+\tgit pack-objects --revs $packDir/test-3 <<-\\EOF &&\n+\tHEAD\n+\t^HEAD~1\n+\tEOF\n+\trm -f $packDir/pack-* &&\n+\trm -f $packDir/loose-* &&\n+\tls $packDir/*.pack >packs-before &&\n+\ttest_line_count = 3 packs-before &&\n+\n+\t# the job repacks the two into a new pack, but does not\n+\t# delete the old ones.\n+\tgit maintenance run --task=incremental-repack &&\n+\tls $packDir/*.pack >packs-between &&\n+\ttest_line_count = 4 packs-between &&\n+\n+\t# the job deletes the two old packs, and does not write\n+\t# a new one because only one pack remains.\n+\tgit maintenance run --task=incremental-repack &&\n+\tls .git/objects/pack/*.pack >packs-after &&\n+\ttest_line_count = 1 packs-after\n+'\n+\n test_done\n-- \ngitgitgadget\n\n"},{"id":"404459","messageId":"e9bb32f53ade2067f773bfe6e5c13ed1a5d694a6.1598380599.git.gitgitgadget@gmail.com","threadId":"54001","inReplyTo":"pull.696.v3.git.1598380599.gitgitgadget@gmail.com","subject":"[PATCH v3 8/8] maintenance: add incremental-repack auto condition","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2020-08-25T18:36:39Z","receivedAt":"2020-08-25T18:37:05Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"From: Derrick Stolee <dstolee@microsoft.com>\n\nThe incremental-repack task updates the multi-pack-index by deleting pack-\nfiles that have been replaced with new packs, then repacking a batch of\nsmall pack-files into a larger pack-file. This incremental repack is faster\nthan rewriting all object data, but is slower than some other\nmaintenance activities.\n\nThe 'maintenance.incremental-repack.auto' config option specifies how many\npack-files should exist outside of the multi-pack-index before running\nthe step. These pack-files could be created by 'git fetch' commands or\nby the loose-objects task. The default value is 10.\n\nSetting the option to zero disables the task with the '--auto' option,\nand a negative value makes the task run every time.\n\nSigned-off-by: Derrick Stolee <dstolee@microsoft.com>\n---\n Documentation/config/maintenance.txt |  9 ++++++++\n builtin/gc.c                         | 31 ++++++++++++++++++++++++++++\n t/t7900-maintenance.sh               | 31 ++++++++++++++++++++++++++++\n 3 files changed, 71 insertions(+)\n\ndiff --git a/Documentation/config/maintenance.txt b/Documentation/config/maintenance.txt\nindex c31613be62..a0706d8f09 100644\n--- a/Documentation/config/maintenance.txt\n+++ b/Documentation/config/maintenance.txt\n@@ -23,3 +23,12 @@ maintenance.loose-objects.auto::\n \tpositive value implies the command should run when the number of\n \tloose objects is at least the value of `maintenance.loose-objects.auto`.\n \tThe default value is 100.\n+\n+maintenance.incremental-repack.auto::\n+\tThis integer config option controls how often the `incremental-repack`\n+\ttask should be run as part of `git maintenance run --auto`. If zero,\n+\tthen the `incremental-repack` task will not run with the `--auto`\n+\toption. A negative value will force the task to run every time.\n+\tOtherwise, a positive value implies the command should run when the\n+\tnumber of pack-files not in the multi-pack-index is at least the value\n+\tof `maintenance.incremental-repack.auto`. The default value is 10.\ndiff --git a/builtin/gc.c b/builtin/gc.c\nindex e043403400..f8459df04c 100644\n--- a/builtin/gc.c\n+++ b/builtin/gc.c\n@@ -31,6 +31,7 @@\n #include \"refs.h\"\n #include \"remote.h\"\n #include \"midx.h\"\n+#include \"object-store.h\"\n \n #define FAILED_RUN \"failed to run %s\"\n \n@@ -1002,6 +1003,35 @@ static int maintenance_task_loose_objects(struct maintenance_run_opts *opts)\n \treturn prune_packed(opts) || pack_loose(opts);\n }\n \n+static int incremental_repack_auto_condition(void)\n+{\n+\tstruct packed_git *p;\n+\tint enabled;\n+\tint incremental_repack_auto_limit = 10;\n+\tint count = 0;\n+\n+\tif (git_config_get_bool(\"core.multiPackIndex\", &enabled) ||\n+\t    !enabled)\n+\t\treturn 0;\n+\n+\tgit_config_get_int(\"maintenance.incremental-repack.auto\",\n+\t\t\t   &incremental_repack_auto_limit);\n+\n+\tif (!incremental_repack_auto_limit)\n+\t\treturn 0;\n+\tif (incremental_repack_auto_limit < 0)\n+\t\treturn 1;\n+\n+\tfor (p = get_packed_git(the_repository);\n+\t     count < incremental_repack_auto_limit && p;\n+\t     p = p->next) {\n+\t\tif (!p->multi_pack_index)\n+\t\t\tcount++;\n+\t}\n+\n+\treturn count >= incremental_repack_auto_limit;\n+}\n+\n static int multi_pack_index_write(struct maintenance_run_opts *opts)\n {\n \tstruct child_process child = CHILD_PROCESS_INIT;\n@@ -1157,6 +1187,7 @@ static struct maintenance_task tasks[] = {\n \t[TASK_INCREMENTAL_REPACK] = {\n \t\t\"incremental-repack\",\n \t\tmaintenance_task_incremental_repack,\n+\t\tincremental_repack_auto_condition,\n \t},\n \t[TASK_GC] = {\n \t\t\"gc\",\ndiff --git a/t/t7900-maintenance.sh b/t/t7900-maintenance.sh\nindex 5c08afc19a..6f878b0141 100755\n--- a/t/t7900-maintenance.sh\n+++ b/t/t7900-maintenance.sh\n@@ -220,4 +220,35 @@ test_expect_success EXPENSIVE 'incremental-repack 2g limit' '\n \t\t --no-progress --batch-size=2147483647 <run-2g.txt\n '\n \n+test_expect_success 'maintenance.incremental-repack.auto' '\n+\tgit repack -adk &&\n+\tgit config core.multiPackIndex true &&\n+\tgit multi-pack-index write &&\n+\tGIT_TRACE2_EVENT=\"$(pwd)/midx-init.txt\" git \\\n+\t\t-c maintenance.incremental-repack.auto=1 \\\n+\t\tmaintenance run --auto --task=incremental-repack 2>/dev/null &&\n+\ttest_subcommand ! git multi-pack-index write --no-progress <midx-init.txt &&\n+\tfor i in 1 2\n+\tdo\n+\t\ttest_commit A-$i &&\n+\t\tgit pack-objects --revs .git/objects/pack/pack <<-\\EOF &&\n+\t\tHEAD\n+\t\t^HEAD~1\n+\t\tEOF\n+\t\tGIT_TRACE2_EVENT=$(pwd)/trace-A-$i git \\\n+\t\t\t-c maintenance.incremental-repack.auto=2 \\\n+\t\t\tmaintenance run --auto --task=incremental-repack 2>/dev/null &&\n+\t\ttest_subcommand ! git multi-pack-index write --no-progress <trace-A-$i &&\n+\t\ttest_commit B-$i &&\n+\t\tgit pack-objects --revs .git/objects/pack/pack <<-\\EOF &&\n+\t\tHEAD\n+\t\t^HEAD~1\n+\t\tEOF\n+\t\tGIT_TRACE2_EVENT=$(pwd)/trace-B-$i git \\\n+\t\t\t-c maintenance.incremental-repack.auto=2 \\\n+\t\t\tmaintenance run --auto --task=incremental-repack 2>/dev/null &&\n+\t\ttest_subcommand git multi-pack-index write --no-progress <trace-B-$i || return 1\n+\tdone\n+'\n+\n test_done\n-- \ngitgitgadget\n"},{"id":"404460","messageId":"f3b25a9927fe560b764850ea880a71932ec2af32.1598380599.git.gitgitgadget@gmail.com","threadId":"54001","inReplyTo":"pull.696.v3.git.1598380599.gitgitgadget@gmail.com","subject":"[PATCH v3 7/8] maintenance: auto-size incremental-repack batch","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2020-08-25T18:36:38Z","receivedAt":"2020-08-25T18:37:06Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"From: Derrick Stolee <dstolee@microsoft.com>\n\nWhen repacking during the 'incremental-repack' task, we use the\n--batch-size option in 'git multi-pack-index repack'. The initial setting\nused --batch-size=0 to repack everything into a single pack-file. This is\nnot sustainable for a large repository. The amount of work required is\nalso likely to use too many system resources for a background job.\n\nUpdate the 'incremental-repack' task by dynamically computing a\n--batch-size option based on the current pack-file structure.\n\nThe dynamic default size is computed with this idea in mind for a client\nrepository that was cloned from a very large remote: there is likely one\n\"big\" pack-file that was created at clone time. Thus, do not try\nrepacking it as it is likely packed efficiently by the server.\n\nInstead, we select the second-largest pack-file, and create a batch size\nthat is one larger than that pack-file. If there are three or more\npack-files, then this guarantees that at least two will be combined into\na new pack-file.\n\nOf course, this means that the second-largest pack-file size is likely\nto grow over time and may eventually surpass the initially-cloned\npack-file. Recall that the pack-file batch is selected in a greedy\nmanner: the packs are considered from oldest to newest and are selected\nif they have size smaller than the batch size until the total selected\nsize is larger than the batch size. Thus, that oldest \"clone\" pack will\nbe first to repack after the new data creates a pack larger than that.\n\nWe also want to place some limits on how large these pack-files become,\nin order to bound the amount of time spent repacking. A maximum\nbatch-size of two gigabytes means that large repositories will never be\npacked into a single pack-file using this job, but also that repack is\nrather expensive. This is a trade-off that is valuable to have if the\nmaintenance is being run automatically or in the background. Users who\ntruly want to optimize for space and performance (and are willing to pay\nthe upfront cost of a full repack) can use the 'gc' task to do so.\n\nCreate a test for this two gigabyte limit by creating an EXPENSIVE test\nthat generates two pack-files of roughly 2.5 gigabytes in size, then\nperforms an incremental repack. Check that the --batch-size argument in\nthe subcommand uses the hard-coded maximum.\n\nHelped-by: Chris Torek <chris.torek@gmail.com>\nReported-by: Son Luong Ngoc <sluongng@gmail.com>\nSigned-off-by: Derrick Stolee <dstolee@microsoft.com>\n---\n builtin/gc.c           | 43 +++++++++++++++++++++++++++++++++++++++++-\n t/t7900-maintenance.sh | 36 +++++++++++++++++++++++++++++++++--\n 2 files changed, 76 insertions(+), 3 deletions(-)\n\ndiff --git a/builtin/gc.c b/builtin/gc.c\nindex fbf84996fa..e043403400 100644\n--- a/builtin/gc.c\n+++ b/builtin/gc.c\n@@ -1036,6 +1036,46 @@ static int multi_pack_index_expire(struct maintenance_run_opts *opts)\n \treturn 0;\n }\n \n+#define TWO_GIGABYTES (INT32_MAX)\n+\n+static off_t get_auto_pack_size(void)\n+{\n+\t/*\n+\t * The \"auto\" value is special: we optimize for\n+\t * one large pack-file (i.e. from a clone) and\n+\t * expect the rest to be small and they can be\n+\t * repacked quickly.\n+\t *\n+\t * The strategy we select here is to select a\n+\t * size that is one more than the second largest\n+\t * pack-file. This ensures that we will repack\n+\t * at least two packs if there are three or more\n+\t * packs.\n+\t */\n+\toff_t max_size = 0;\n+\toff_t second_largest_size = 0;\n+\toff_t result_size;\n+\tstruct packed_git *p;\n+\tstruct repository *r = the_repository;\n+\n+\treprepare_packed_git(r);\n+\tfor (p = get_all_packs(r); p; p = p->next) {\n+\t\tif (p->pack_size > max_size) {\n+\t\t\tsecond_largest_size = max_size;\n+\t\t\tmax_size = p->pack_size;\n+\t\t} else if (p->pack_size > second_largest_size)\n+\t\t\tsecond_largest_size = p->pack_size;\n+\t}\n+\n+\tresult_size = second_largest_size + 1;\n+\n+\t/* But limit ourselves to a batch size of 2g */\n+\tif (result_size > TWO_GIGABYTES)\n+\t\tresult_size = TWO_GIGABYTES;\n+\n+\treturn result_size;\n+}\n+\n static int multi_pack_index_repack(struct maintenance_run_opts *opts)\n {\n \tstruct child_process child = CHILD_PROCESS_INIT;\n@@ -1046,7 +1086,8 @@ static int multi_pack_index_repack(struct maintenance_run_opts *opts)\n \tif (opts->quiet)\n \t\tstrvec_push(&child.args, \"--no-progress\");\n \n-\tstrvec_push(&child.args, \"--batch-size=0\");\n+\tstrvec_pushf(&child.args, \"--batch-size=%\"PRIuMAX,\n+\t\t\t\t  (uintmax_t)get_auto_pack_size());\n \n \tclose_object_store(the_repository->objects);\n \ndiff --git a/t/t7900-maintenance.sh b/t/t7900-maintenance.sh\nindex dde28cf837..5c08afc19a 100755\n--- a/t/t7900-maintenance.sh\n+++ b/t/t7900-maintenance.sh\n@@ -182,10 +182,42 @@ test_expect_success 'incremental-repack task' '\n \ttest_line_count = 4 packs-between &&\n \n \t# the job deletes the two old packs, and does not write\n-\t# a new one because only one pack remains.\n+\t# a new one because the batch size is not high enough to\n+\t# pack the largest pack-file.\n \tgit maintenance run --task=incremental-repack &&\n \tls .git/objects/pack/*.pack >packs-after &&\n-\ttest_line_count = 1 packs-after\n+\ttest_line_count = 2 packs-after\n+'\n+\n+test_expect_success EXPENSIVE 'incremental-repack 2g limit' '\n+\tfor i in $(test_seq 1 5)\n+\tdo\n+\t\ttest-tool genrandom foo$i $((512 * 1024 * 1024 + 1)) >>big ||\n+\t\treturn 1\n+\tdone &&\n+\tgit add big &&\n+\tgit commit -m \"Add big file (1)\" &&\n+\n+\t# ensure any possible loose objects are in a pack-file\n+\tgit maintenance run --task=loose-objects &&\n+\n+\trm big &&\n+\tfor i in $(test_seq 6 10)\n+\tdo\n+\t\ttest-tool genrandom foo$i $((512 * 1024 * 1024 + 1)) >>big ||\n+\t\treturn 1\n+\tdone &&\n+\tgit add big &&\n+\tgit commit -m \"Add big file (2)\" &&\n+\n+\t# ensure any possible loose objects are in a pack-file\n+\tgit maintenance run --task=loose-objects &&\n+\n+\t# Now run the incremental-repack task and check the batch-size\n+\tGIT_TRACE2_EVENT=\"$(pwd)/run-2g.txt\" git maintenance run \\\n+\t\t--task=incremental-repack 2>/dev/null &&\n+\ttest_subcommand git multi-pack-index repack \\\n+\t\t --no-progress --batch-size=2147483647 <run-2g.txt\n '\n \n test_done\n-- \ngitgitgadget\n\n"},{"id":"404479","messageId":"xmqqpn7ezbl4.fsf@gitster.c.googlers.com","threadId":"54001","inReplyTo":"pull.696.v3.git.1598380599.gitgitgadget@gmail.com","subject":"Re: [PATCH v3 0/8] Maintenance II: prefetch, loose-objects, incremental-repack tasks","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2020-08-25T20:59:03Z","receivedAt":"2020-08-25T20:59:11Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Derrick Stolee via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> This series is based on v3 of part I (ds/maintenance-part-1) [2].\n>\n> This patch series contains 9 patches that were going to be part of v4 of\n> ds/maintenance [1], but the discussion has gotten really long. To help, I'm\n> splitting out the portions that create and test the 'maintenance' builtin\n> from the additional tasks (prefetch, loose-objects, incremental-repack) that\n> can be brought in later.\n\nI gave it a quick look but the changes mostly are fallout from\nrenaming the options structure to maitenance_RUN_opts and loss of\nmidx verify while a task rewrites midx; iow, no significant change\nthat are likely to become controversial.\n"},{"id":"404525","messageId":"56C4015E-B431-4B16-8E05-5919BFA71E80@gmail.com","threadId":"54001","inReplyTo":"pull.696.v3.git.1598380599.gitgitgadget@gmail.com","subject":"Re: [PATCH v3 0/8] Maintenance II: prefetch, loose-objects, incremental-repack tasks","fromName":"Son Luong Ngoc","fromEmail":"sluongng@gmail.com","sentAt":"2020-08-26T15:15:41Z","receivedAt":"2020-08-26T15:16:15Z","isPatch":true,"sender":{"key":"sluongng@gmail.com","avatar":"https://avatars.githubusercontent.com/u/26684313?v=4"},"body":"Hi Derrick,\n\n> On Aug 25, 2020, at 20:36, Derrick Stolee via GitGitGadget <gitgitgadget@gmail.com> wrote:\n> \n\n...\n\n> \n> Updates since v2\n> ================\n> \n> * Dropped \"fetch: optionally allow disabling FETCH_HEAD update\"\n> \n> \n> * A lot of fallout from the change in the option parsing in v3 of\n>   Maintenance II.\n> \n> \n> * Dropped the \"verify, and delete and rewrite on failure\" logic from the\n>   incremental-repack task. This might be added again later after it can be\n>   tested more thoroughly.\n\nPerhaps I missed some conversations related to this change but\nwhy was this verify-rewrite strategy dropped?\n\nWas the problem such strategy were created to solve is now no longer a concern?\n\nI feel like it would be much better to add it in and then remove it using a separated commit?\nThat way we can follow the reasoning behind these decisions via commit message.\n\n> \n\n...\n\n> \n> -- \n> gitgitgadget\n\nThanks,\nSon Luong."},{"id":"404532","messageId":"9a236846-7631-d999-1579-332d3b3341da@gmail.com","threadId":"54001","inReplyTo":"56C4015E-B431-4B16-8E05-5919BFA71E80@gmail.com","subject":"Re: [PATCH v3 0/8] Maintenance II: prefetch, loose-objects, incremental-repack tasks","fromName":"Derrick Stolee","fromEmail":"stolee@gmail.com","sentAt":"2020-08-26T16:21:53Z","receivedAt":"2020-08-26T16:22:00Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"On 8/26/2020 11:15 AM, Son Luong Ngoc wrote:\n> Hi Derrick,\n> \n>> On Aug 25, 2020, at 20:36, Derrick Stolee via GitGitGadget <gitgitgadget@gmail.com> wrote:\n>> * Dropped the \"verify, and delete and rewrite on failure\" logic from the\n>>   incremental-repack task. This might be added again later after it can be\n>>   tested more thoroughly.\n> \n> Perhaps I missed some conversations related to this change but\n> why was this verify-rewrite strategy dropped?\n> \n> Was the problem such strategy were created to solve is now no longer a concern?\n> \n> I feel like it would be much better to add it in and then remove it using a separated commit?\n> That way we can follow the reasoning behind these decisions via commit message.\n\nThe most-recent message was [1]\n\n[1] https://lore.kernel.org/git/20200819174322.3087791-1-jonathantanmy@google.com/\n\nFor now, I'd rather move forward with this simpler task\nand I will revisit the \"verify and fix\" situation when\nit can be done in a focused way instead of being surrounded\nby builtin boilerplate and other basics of the maintenance\nfeature. Specifically, it would help to have a way to test\nthe logic. In Scalar, I was able to mock the Git commands\nand return failures in specific places. A similar approach\ncould be done here, or perhaps there is another way to be\nconfident that the \"verify and fix\" logic is actually\nhelpful.\n\nThanks,\n-Stolee\n"},{"id":"406163","messageId":"20200922230535.903375-1-jonathantanmy@google.com","threadId":"54001","inReplyTo":"da64c51a8182ec13aeed8f0157079fb29a09ee85.1598380599.git.gitgitgadget@gmail.com","subject":"Re: [PATCH v3 1/8] maintenance: add prefetch task","fromName":"Jonathan Tan","fromEmail":"jonathantanmy@google.com","sentAt":"2020-09-22T23:05:35Z","receivedAt":"2020-09-22T23:05:42Z","isPatch":true,"sender":{"key":"jonathantanmy@fastmail.com","avatar":null},"body":"> From: Derrick Stolee <dstolee@microsoft.com>\n> \n> When working with very large repositories, an incremental 'git fetch'\n> command can download a large amount of data. If there are many other\n> users pushing to a common repo, then this data can rival the initial\n> pack-file size of a 'git clone' of a medium-size repo.\n> \n> Users may want to keep the data on their local repos as close as\n> possible to the data on the remote repos by fetching periodically in\n> the background. This can break up a large daily fetch into several\n> smaller hourly fetches.\n> \n> The task is called \"prefetch\" because it is work done in advance\n> of a foreground fetch to make that 'git fetch' command much faster.\n> \n> However, if we simply ran 'git fetch <remote>' in the background,\n> then the user running a foregroudn 'git fetch <remote>' would lose\n\n-> foreground\n\nI have some more minor comments that I will send as individual replies,\nbut overall, the patch set looks good to me.\n\n> +static int append_remote(struct remote *remote, void *cbdata)\n> +{\n> +\tstruct string_list *remotes = (struct string_list *)cbdata;\n> +\n> +\tstring_list_append(remotes, remote->name);\n> +\treturn 0;\n> +}\n> +\n> +static int maintenance_task_prefetch(struct maintenance_run_opts *opts)\n> +{\n> +\tint result = 0;\n> +\tstruct string_list_item *item;\n> +\tstruct string_list remotes = STRING_LIST_INIT_DUP;\n> +\n> +\tif (for_each_remote(append_remote, &remotes)) {\n> +\t\terror(_(\"failed to fill remotes\"));\n> +\t\tresult = 1;\n> +\t\tgoto cleanup;\n> +\t}\n> +\n> +\tfor_each_string_list_item(item, &remotes)\n> +\t\tresult |= fetch_remote(item->string, opts);\n> +\n> +cleanup:\n> +\tstring_list_clear(&remotes, 0);\n> +\treturn result;\n> +}\n\nI was wondering why the generation of the list and the iteration was\nsplit up, but I see that you want to attempt to fetch each remote even\nif one of them fails.\n"},{"id":"406164","messageId":"20200922230937.906065-1-jonathantanmy@google.com","threadId":"54001","inReplyTo":"75e846456b8ac4a3518ecc9051c927539dd7ff08.1598380599.git.gitgitgadget@gmail.com","subject":"Re: [PATCH v3 2/8] maintenance: add loose-objects task","fromName":"Jonathan Tan","fromEmail":"jonathantanmy@google.com","sentAt":"2020-09-22T23:09:37Z","receivedAt":"2020-09-22T23:09:42Z","isPatch":true,"sender":{"key":"jonathantanmy@fastmail.com","avatar":null},"body":"> Create a 'loose-objects' task for the 'git maintenance run' command.\n> This helps clean up loose objects without disrupting concurrent Git\n> commands using the following sequence of events:\n> \n> 1. Run 'git prune-packed' to delete any loose objects that exist\n>    in a pack-file. Concurrent commands will prefer the packed\n>    version of the object to the loose version. (Of course, there\n>    are exceptions for commands that specifically care about the\n>    location of an object. These are rare for a user to run on\n>    purpose, and we hope a user that has selected background\n>    maintenance will not be trying to do foreground maintenance.)\n> \n> 2. Run 'git pack-objects' on a batch of loose objects. These\n>    objects are grouped by scanning the loose object directories in\n>    lexicographic order until listing all loose objects -or-\n>    reaching 50,000 objects. This is more than enough if the loose\n>    objects are created only by a user doing normal development.\n>    We noticed users with _millions_ of loose objects because VFS\n>    for Git downloads blobs on-demand when a file read operation\n>    requires populating a virtual file. \n\n[snip]\n\n> This has potential of\n>    happening in partial clones if someone runs 'git grep' or\n>    otherwise evades the batch-download feature for requesting\n>    promisor objects.\n\nThis part is not strictly true, as even when Git lazy-fetches one\nobject, it fetches it in the form of a packfile - so maybe remove this\nsentence.\n\nThis is nevertheless a good feature to have - loose objects may not be\ncreated during lazy fetches, but they definitely are created during\nnormal operation (e.g. commits). Git, as a whole, prefers packfiles over\nloose objects, and just packing the loose objects themselves instead of\nrunning repack (which goes through all reachable objects) is definitely\nbetter for large repositories.\n"},{"id":"406165","messageId":"20200922231501.908887-1-jonathantanmy@google.com","threadId":"54001","inReplyTo":"d6e382c43effe063fb1137659f616d414ee52682.1598380599.git.gitgitgadget@gmail.com","subject":"Re: [PATCH v3 3/8] maintenance: create auto condition for loose-objects","fromName":"Jonathan Tan","fromEmail":"jonathantanmy@google.com","sentAt":"2020-09-22T23:15:01Z","receivedAt":"2020-09-22T23:15:06Z","isPatch":true,"sender":{"key":"jonathantanmy@fastmail.com","avatar":null},"body":"> +test_expect_success 'maintenance.loose-objects.auto' '\n> +\tgit repack -adk &&\n> +\tGIT_TRACE2_EVENT=\"$(pwd)/trace-lo1.txt\" \\\n> +\t\tgit -c maintenance.loose-objects.auto=1 maintenance \\\n> +\t\trun --auto --task=loose-objects 2>/dev/null &&\n> +\ttest_subcommand ! git prune-packed --quiet <trace-lo1.txt &&\n> +\tfor i in 1 2\n\nAny reason why this is run twice?\n\n> +\tdo\n> +\t\tprintf data-A-$i | git hash-object -t blob --stdin -w &&\n> +\t\tGIT_TRACE2_EVENT=\"$(pwd)/trace-loA-$i\" \\\n> +\t\t\tgit -c maintenance.loose-objects.auto=2 \\\n> +\t\t\tmaintenance run --auto --task=loose-objects 2>/dev/null &&\n> +\t\ttest_subcommand ! git prune-packed --quiet <trace-loA-$i &&\n\nOK - there is only 1 loose object so the loose-objects task doesn't\nget run.\n\n> +\t\tprintf data-B-$i | git hash-object -t blob --stdin -w &&\n> +\t\tGIT_TRACE2_EVENT=\"$(pwd)/trace-loB-$i\" \\\n> +\t\t\tgit -c maintenance.loose-objects.auto=2 \\\n> +\t\t\tmaintenance run --auto --task=loose-objects 2>/dev/null &&\n> +\t\ttest_subcommand git prune-packed --quiet <trace-loB-$i &&\n\nOK - there are 2 loose objects so the loose-objects task gets run. But\nwe need to remember that the first time it is run, only the packfile\ngets created - the loose objects aren't deleted. \"prune-packed\" here is\nto show that the loose-objects task is run, but it has no effect.\n\n> +\t\tGIT_TRACE2_EVENT=\"$(pwd)/trace-loC-$i\" \\\n> +\t\t\tgit -c maintenance.loose-objects.auto=2 \\\n> +\t\t\tmaintenance run --auto --task=loose-objects 2>/dev/null &&\n> +\t\ttest_subcommand git prune-packed --quiet <trace-loC-$i || return 1\n\nOK - the 2 loose objects still exist, so the loose-objects task gets\nrun. \"prune-packed\" here shows that the loose-objects task is run.\n"},{"id":"406166","messageId":"20200922231652.910686-1-jonathantanmy@google.com","threadId":"54001","inReplyTo":"d0f2ec70d9dc0b4901704d48bf5cbddbd6e50c01.1598380599.git.gitgitgadget@gmail.com","subject":"Re: [PATCH v3 4/8] midx: enable core.multiPackIndex by default","fromName":"Jonathan Tan","fromEmail":"jonathantanmy@google.com","sentAt":"2020-09-22T23:16:52Z","receivedAt":"2020-09-22T23:16:57Z","isPatch":true,"sender":{"key":"jonathantanmy@fastmail.com","avatar":null},"body":"> diff --git a/repository.h b/repository.h\n> index 3c1f7d54bd..3901ce0b65 100644\n> --- a/repository.h\n> +++ b/repository.h\n> @@ -37,6 +37,8 @@ struct repo_settings {\n>  \n>  \tint pack_use_sparse;\n>  \tenum fetch_negotiation_setting fetch_negotiation_algorithm;\n> +\n> +\tint core_multi_pack_index;\n\nThe only thing I noticed here is why this isn't a bit (\"unsigned\ncore_multi_pack_index : 1\"), but I guess it is consistent with \"int\npack_use_sparse\".\n"},{"id":"406167","messageId":"20200922232654.914862-1-jonathantanmy@google.com","threadId":"54001","inReplyTo":"0dd26bb584bb7e8b9616eb32f7b1235462df77fa.1598380599.git.gitgitgadget@gmail.com","subject":"Re: [PATCH v3 6/8] maintenance: add incremental-repack task","fromName":"Jonathan Tan","fromEmail":"jonathantanmy@google.com","sentAt":"2020-09-22T23:26:54Z","receivedAt":"2020-09-22T23:26:58Z","isPatch":true,"sender":{"key":"jonathantanmy@fastmail.com","avatar":null},"body":"> +incremental-repack::\n> +\tThe `incremental-repack` job repacks the object directory\n> +\tusing the `multi-pack-index` feature. In order to prevent race\n> +\tconditions with concurrent Git commands, it follows a two-step\n> +\tprocess.\n\n[snip]\n\n> First, it deletes any pack-files included in the\n> +\t`multi-pack-index` where none of the objects in the\n> +\t`multi-pack-index` reference those pack-files; this only happens\n> +\tif all objects in the pack-file are also stored in a newer\n> +\tpack-file. Second, it selects a group of pack-files whose \"expected\n> +\tsize\" is below the batch size until the group has total expected\n> +\tsize at least the batch size; see the `--batch-size` option for\n> +\tthe `repack` subcommand in linkgit:git-multi-pack-index[1]. The\n> +\tdefault batch-size is zero, which is a special case that attempts\n> +\tto repack all pack-files into a single pack-file.\n\nThis lacks the detail of what happens to the selected group of packfiles\n(in the second step) - in particular, that a new packfile is created and\nthe MIDX is rewritten so that all references to the selected group are\nupdated to refer to the new packfile instead, thus making it possible to\ndelete the selected group of packfiles in a subsequent first step. All\nthis is explained in the documentation of git-multi-pack-index (expire\nand repack), though, so it might be better to refer to that. E.g.\n\n  First, it calls `multi-pack-index expire` to delete packfiles\n  unreferenced by the MIDX file. Second, it calls `multi-pack-index\n  repack` to select several small packfiles and repack them into a\n  bigger one, and then update the MIDX entries that refer to the small\n  packfiles to refer to the big one instead, thus preparing it for\n  deletion upon a subsequent `multi-pack-index expire` invocation. The\n  selection of the small packfiles is such that the expected size of the\n  big packfile is at least the batch size; see the ...\n\n> diff --git a/midx.c b/midx.c\n> index aa37d5da86..66d7053d83 100644\n> --- a/midx.c\n> +++ b/midx.c\n> @@ -37,7 +37,7 @@\n>  \n>  #define PACK_EXPIRED UINT_MAX\n>  \n> -static char *get_midx_filename(const char *object_dir)\n> +char *get_midx_filename(const char *object_dir)\n>  {\n>  \treturn xstrfmt(\"%s/pack/multi-pack-index\", object_dir);\n>  }\n> diff --git a/midx.h b/midx.h\n> index b18cf53bc4..baeecc70c9 100644\n> --- a/midx.h\n> +++ b/midx.h\n> @@ -37,6 +37,7 @@ struct multi_pack_index {\n>  \n>  #define MIDX_PROGRESS     (1 << 0)\n>  \n> +char *get_midx_filename(const char *object_dir);\n>  struct multi_pack_index *load_multi_pack_index(const char *object_dir, int local);\n>  int prepare_midx_pack(struct repository *r, struct multi_pack_index *m, uint32_t pack_int_id);\n>  int bsearch_midx(const struct object_id *oid, struct multi_pack_index *m, uint32_t *result);\n\nDo we need get_midx_filename() to be global?\n"},{"id":"406168","messageId":"20200922235258.920928-1-jonathantanmy@google.com","threadId":"54001","inReplyTo":"e9bb32f53ade2067f773bfe6e5c13ed1a5d694a6.1598380599.git.gitgitgadget@gmail.com","subject":"Re: [PATCH v3 8/8] maintenance: add incremental-repack auto condition","fromName":"Jonathan Tan","fromEmail":"jonathantanmy@google.com","sentAt":"2020-09-22T23:52:58Z","receivedAt":"2020-09-22T23:53:02Z","isPatch":true,"sender":{"key":"jonathantanmy@fastmail.com","avatar":null},"body":"> diff --git a/t/t7900-maintenance.sh b/t/t7900-maintenance.sh\n> index 5c08afc19a..6f878b0141 100755\n> --- a/t/t7900-maintenance.sh\n> +++ b/t/t7900-maintenance.sh\n> @@ -220,4 +220,35 @@ test_expect_success EXPENSIVE 'incremental-repack 2g limit' '\n>  \t\t --no-progress --batch-size=2147483647 <run-2g.txt\n>  '\n>  \n> +test_expect_success 'maintenance.incremental-repack.auto' '\n> +\tgit repack -adk &&\n> +\tgit config core.multiPackIndex true &&\n> +\tgit multi-pack-index write &&\n\nAfter this, there is exactly one pack, and it is in the MIDX.\n\n> +\tGIT_TRACE2_EVENT=\"$(pwd)/midx-init.txt\" git \\\n> +\t\t-c maintenance.incremental-repack.auto=1 \\\n> +\t\tmaintenance run --auto --task=incremental-repack 2>/dev/null &&\n> +\ttest_subcommand ! git multi-pack-index write --no-progress <midx-init.txt &&\n> +\tfor i in 1 2\n\nSame comment as in the earlier patch - why 2 iterations?\n\n> +\tdo\n> +\t\ttest_commit A-$i &&\n> +\t\tgit pack-objects --revs .git/objects/pack/pack <<-\\EOF &&\n> +\t\tHEAD\n> +\t\t^HEAD~1\n> +\t\tEOF\n> +\t\tGIT_TRACE2_EVENT=$(pwd)/trace-A-$i git \\\n> +\t\t\t-c maintenance.incremental-repack.auto=2 \\\n> +\t\t\tmaintenance run --auto --task=incremental-repack 2>/dev/null &&\n> +\t\ttest_subcommand ! git multi-pack-index write --no-progress <trace-A-$i &&\n\nOK - one pack not in the MIDX, so this does not get run.\n\n> +\t\ttest_commit B-$i &&\n> +\t\tgit pack-objects --revs .git/objects/pack/pack <<-\\EOF &&\n> +\t\tHEAD\n> +\t\t^HEAD~1\n> +\t\tEOF\n> +\t\tGIT_TRACE2_EVENT=$(pwd)/trace-B-$i git \\\n> +\t\t\t-c maintenance.incremental-repack.auto=2 \\\n> +\t\t\tmaintenance run --auto --task=incremental-repack 2>/dev/null &&\n> +\t\ttest_subcommand git multi-pack-index write --no-progress <trace-B-$i || return 1\n\nOK - 2 packs not in the MIDX, so this gets run.\n\nAs I said in my review of patch 1, apart from my minor comments in this\nand the preceding patches, these patches look good to me.\n"},{"id":"406264","messageId":"446e805a-6b83-a6b7-18a3-f99ac85a6fe1@gmail.com","threadId":"54001","inReplyTo":"20200922230937.906065-1-jonathantanmy@google.com","subject":"Re: [PATCH v3 2/8] maintenance: add loose-objects task","fromName":"Derrick Stolee","fromEmail":"stolee@gmail.com","sentAt":"2020-09-24T13:45:22Z","receivedAt":"2020-09-24T13:45:27Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"On 9/22/2020 7:09 PM, Jonathan Tan wrote:\n>> Create a 'loose-objects' task for the 'git maintenance run' command.\n>> This helps clean up loose objects without disrupting concurrent Git\n>> commands using the following sequence of events:\n>>\n>> 1. Run 'git prune-packed' to delete any loose objects that exist\n>>    in a pack-file. Concurrent commands will prefer the packed\n>>    version of the object to the loose version. (Of course, there\n>>    are exceptions for commands that specifically care about the\n>>    location of an object. These are rare for a user to run on\n>>    purpose, and we hope a user that has selected background\n>>    maintenance will not be trying to do foreground maintenance.)\n>>\n>> 2. Run 'git pack-objects' on a batch of loose objects. These\n>>    objects are grouped by scanning the loose object directories in\n>>    lexicographic order until listing all loose objects -or-\n>>    reaching 50,000 objects. This is more than enough if the loose\n>>    objects are created only by a user doing normal development.\n>>    We noticed users with _millions_ of loose objects because VFS\n>>    for Git downloads blobs on-demand when a file read operation\n>>    requires populating a virtual file. \n> \n> [snip]\n> \n>> This has potential of\n>>    happening in partial clones if someone runs 'git grep' or\n>>    otherwise evades the batch-download feature for requesting\n>>    promisor objects.\n> \n> This part is not strictly true, as even when Git lazy-fetches one\n> object, it fetches it in the form of a packfile - so maybe remove this\n> sentence.\n\nThis is a good point. I just did some testing and we do store these\nsingle-object downloads as pack-files. My misunderstanding is due to\nmy own bias and experience with the GVFS protocol.\n\nI have also heard that \"git fetch\" might explode some small pack-files\ninto loose objects, and I guess I expected the same here. However, that\nis not the case for partial clone. I'll remove this.\n\n> This is nevertheless a good feature to have - loose objects may not be\n> created during lazy fetches, but they definitely are created during\n> normal operation (e.g. commits). Git, as a whole, prefers packfiles over\n> loose objects, and just packing the loose objects themselves instead of\n> running repack (which goes through all reachable objects) is definitely\n> better for large repositories.\n\nThanks,\n-Stolee\n\n"},{"id":"406265","messageId":"07500c68-9fe9-008d-41e7-51a4a1ef4650@gmail.com","threadId":"54001","inReplyTo":"20200922231501.908887-1-jonathantanmy@google.com","subject":"Re: [PATCH v3 3/8] maintenance: create auto condition for loose-objects","fromName":"Derrick Stolee","fromEmail":"stolee@gmail.com","sentAt":"2020-09-24T13:51:07Z","receivedAt":"2020-09-24T13:51:12Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"On 9/22/2020 7:15 PM, Jonathan Tan wrote:\n>> +test_expect_success 'maintenance.loose-objects.auto' '\n>> +\tgit repack -adk &&\n>> +\tGIT_TRACE2_EVENT=\"$(pwd)/trace-lo1.txt\" \\\n>> +\t\tgit -c maintenance.loose-objects.auto=1 maintenance \\\n>> +\t\trun --auto --task=loose-objects 2>/dev/null &&\n>> +\ttest_subcommand ! git prune-packed --quiet <trace-lo1.txt &&\n>> +\tfor i in 1 2\n> \n> Any reason why this is run twice?\n\nI think the original reason was to demonstrate how two runs interact,\nbut then that was done in the middle of the loop body so the loop is\nnot necessary.\n\nThanks,\n-Stolee\n\n"},{"id":"406266","messageId":"d48be601-4e9e-ed9c-93f8-47e6e295adac@gmail.com","threadId":"54001","inReplyTo":"20200922231652.910686-1-jonathantanmy@google.com","subject":"Re: [PATCH v3 4/8] midx: enable core.multiPackIndex by default","fromName":"Derrick Stolee","fromEmail":"stolee@gmail.com","sentAt":"2020-09-24T13:53:15Z","receivedAt":"2020-09-24T13:53:20Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"On 9/22/2020 7:16 PM, Jonathan Tan wrote:\n>> diff --git a/repository.h b/repository.h\n>> index 3c1f7d54bd..3901ce0b65 100644\n>> --- a/repository.h\n>> +++ b/repository.h\n>> @@ -37,6 +37,8 @@ struct repo_settings {\n>>  \n>>  \tint pack_use_sparse;\n>>  \tenum fetch_negotiation_setting fetch_negotiation_algorithm;\n>> +\n>> +\tint core_multi_pack_index;\n> \n> The only thing I noticed here is why this isn't a bit (\"unsigned\n> core_multi_pack_index : 1\"), but I guess it is consistent with \"int\n> pack_use_sparse\".\n\nThe _real_ reason is that we initialize these to -1 in\nprepare_repo_settings() so UPDATE_DEFAULT_BOOL() knows that\nthe setting was not populated by a config value.\n\nThanks,\n-Stolee\n\n"},{"id":"406267","messageId":"691ac9f0-0678-8c95-18ad-7b54f92ae531@gmail.com","threadId":"54001","inReplyTo":"20200922232654.914862-1-jonathantanmy@google.com","subject":"Re: [PATCH v3 6/8] maintenance: add incremental-repack task","fromName":"Derrick Stolee","fromEmail":"stolee@gmail.com","sentAt":"2020-09-24T14:05:17Z","receivedAt":"2020-09-24T14:05:22Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"On 9/22/2020 7:26 PM, Jonathan Tan wrote:\n>> +incremental-repack::\n>> +\tThe `incremental-repack` job repacks the object directory\n>> +\tusing the `multi-pack-index` feature. In order to prevent race\n>> +\tconditions with concurrent Git commands, it follows a two-step\n>> +\tprocess.\n> \n> [snip]\n> \n>> First, it deletes any pack-files included in the\n>> +\t`multi-pack-index` where none of the objects in the\n>> +\t`multi-pack-index` reference those pack-files; this only happens\n>> +\tif all objects in the pack-file are also stored in a newer\n>> +\tpack-file. Second, it selects a group of pack-files whose \"expected\n>> +\tsize\" is below the batch size until the group has total expected\n>> +\tsize at least the batch size; see the `--batch-size` option for\n>> +\tthe `repack` subcommand in linkgit:git-multi-pack-index[1]. The\n>> +\tdefault batch-size is zero, which is a special case that attempts\n>> +\tto repack all pack-files into a single pack-file.\n> \n> This lacks the detail of what happens to the selected group of packfiles\n> (in the second step) - in particular, that a new packfile is created and\n> the MIDX is rewritten so that all references to the selected group are\n> updated to refer to the new packfile instead, thus making it possible to\n> delete the selected group of packfiles in a subsequent first step. All\n> this is explained in the documentation of git-multi-pack-index (expire\n> and repack), though, so it might be better to refer to that. E.g.\n\nHere is my attempt to incorporate your recommendations into this doc:\n\nincremental-repack::\n\tThe `incremental-repack` job repacks the object directory\n\tusing the `multi-pack-index` feature. In order to prevent race\n\tconditions with concurrent Git commands, it follows a two-step\n\tprocess. First, it calls `git multi-pack-index expire` to delete\n\tpack-files unreferenced by the `multi-pack-index` file. Second, it\n\tcalls `git multi-pack-index repack` to select several small\n\tpack-files and repack them into a bigger one, and then update the\n\t`multi-pack-index` entries that refer to the small pack-files to\n\trefer to the new pack-file. This prepares those small pack-files\n\tfor deletion upon the next run of `git multi-pack-index expire`.\n\tThe selection of the small pack-files is such that the expected\n\tsize of the big pack-file is at least the batch size; see the\n\t`--batch-size` option for the `repack` subcommand in\n\tlinkgit:git-multi-pack-index[1]. The default batch-size is zero,\n\twhich is a special case that attempts to repack all pack-files\n\tinto a single pack-file.\n\n> Do we need get_midx_filename() to be global?\n\nNo, that does not appear to be important (to this patch). It _was_\nnecessary when doing the \"verify, then delete if problematic\" mode.\nThanks for catching it now that it is not necessary.\n\nThanks,\n-Stolee\n \n\n"},{"id":"406286","messageId":"20200924220124.1525034-1-jonathantanmy@google.com","threadId":"54001","inReplyTo":"691ac9f0-0678-8c95-18ad-7b54f92ae531@gmail.com","subject":"Re: [PATCH v3 6/8] maintenance: add incremental-repack task","fromName":"Jonathan Tan","fromEmail":"jonathantanmy@google.com","sentAt":"2020-09-24T22:01:24Z","receivedAt":"2020-09-24T22:01:37Z","isPatch":true,"sender":{"key":"jonathantanmy@fastmail.com","avatar":null},"body":"> Here is my attempt to incorporate your recommendations into this doc:\n> \n> incremental-repack::\n> \tThe `incremental-repack` job repacks the object directory\n> \tusing the `multi-pack-index` feature. In order to prevent race\n> \tconditions with concurrent Git commands, it follows a two-step\n> \tprocess. First, it calls `git multi-pack-index expire` to delete\n> \tpack-files unreferenced by the `multi-pack-index` file. Second, it\n> \tcalls `git multi-pack-index repack` to select several small\n> \tpack-files and repack them into a bigger one, and then update the\n> \t`multi-pack-index` entries that refer to the small pack-files to\n> \trefer to the new pack-file. This prepares those small pack-files\n> \tfor deletion upon the next run of `git multi-pack-index expire`.\n> \tThe selection of the small pack-files is such that the expected\n> \tsize of the big pack-file is at least the batch size; see the\n> \t`--batch-size` option for the `repack` subcommand in\n> \tlinkgit:git-multi-pack-index[1]. The default batch-size is zero,\n> \twhich is a special case that attempts to repack all pack-files\n> \tinto a single pack-file.\n\nThanks, this looks good.\n"},{"id":"406322","messageId":"pull.696.v4.git.1601037218.gitgitgadget@gmail.com","threadId":"54001","inReplyTo":"pull.696.v3.git.1598380599.gitgitgadget@gmail.com","subject":"[PATCH v4 0/8] Maintenance II: prefetch, loose-objects, incremental-repack tasks","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2020-09-25T12:33:30Z","receivedAt":"2020-09-25T12:33:44Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"This series is based on ds/maintenance-part-1 [2].\n\nThis patch series contains 9 patches that were going to be part of v4 of\nds/maintenance [1], but the discussion has gotten really long. To help, I'm\nsplitting out the portions that create and test the 'maintenance' builtin\nfrom the additional tasks (prefetch, loose-objects, incremental-repack) that\ncan be brought in later.\n\n[1] \nhttps://lore.kernel.org/git/pull.671.git.1594131695.gitgitgadget@gmail.com/\n[2] \nhttps://lore.kernel.org/git/pull.695.v3.git.1598380426.gitgitgadget@gmail.com/\n\nAs detailed in [2], the 'git maintenance run' subcommand will run certain\ntasks based on config options or the --task= arguments. The --auto option\nindicates to the task to only run based on some internal check that there\nhas been \"enough\" change in that domain to merit the work. In the case of\nthe 'gc' task, this also reduces the amount of work done. \n\nThe new maintenance tasks in this series are:\n\n * 'loose-objects' : prune packed loose objects, then create a new pack from\n   a batch of loose objects.\n * 'pack-files' : expire redundant packs from the multi-pack-index, then\n   repack using the multi-pack-index's incremental repack strategy.\n * 'prefetch' : fetch from each remote, storing the refs in 'refs/prefetch/\n   /'.\n\nThese tasks are all disabled by default, but can be enabled with config\noptions or run explicitly using \"git maintenance run --task=\". \n\nSince [2] replaced the 'git gc --auto' calls with 'git maintenance run\n--auto' at the end of some Git commands, users could replace the 'gc' task\nwith these lighter-weight changes for foreground maintenance.\n\nThe 'git maintenance' builtin has a 'run' subcommand so it can be extended\nlater with subcommands that manage background maintenance, such as 'start'\nor 'stop'. These are not the subject of this series, as it is important to\nfocus on the maintenance activities themselves. I have an RFC series for\nthis available at [3].\n\n[3] \nhttps://lore.kernel.org/git/pull.680.git.1597857408.gitgitgadget@gmail.com/\n\nUpdates in v3\n=============\n\n * Several commit message, documentation, and test updates from Jonathan\n   Tan's helpful review!\n\nUpdates since v2\n================\n\n * Dropped \"fetch: optionally allow disabling FETCH_HEAD update\"\n   \n   \n * A lot of fallout from the change in the option parsing in v3 of\n   Maintenance II.\n   \n   \n * Dropped the \"verify, and delete and rewrite on failure\" logic from the\n   incremental-repack task. This might be added again later after it can be\n   tested more thoroughly.\n   \n   \n\nUpdates since v1 (of this series)\n=================================\n\n * PATCH 1 (\"fetch: optionally allow disabling FETCH_HEAD update\") was\n   rewritten on-list. Getting a version out with this patch is the main\n   reason for rolling a v2. (That, and Part I is re-rolled with a v2 and I\n   want to make sure this series applies cleanly.)\n   \n   \n * The 'prefetch' and 'loose-objects' tasks had some review, but my proposed\n   changes were not acked, so they may need another review.\n   \n   \n\nUPDATES since v3 of [1]\n=======================\n\n * The biggest change here is the use of \"test_subcommand\", based on\n   Jonathan Nieder's approach. This requires having the exact command-line\n   figured out, which now requires spelling out all --no- [quiet%7Cprogress] \n   options. I also added a bunch of \"2>/dev/null\" checks because of the\n   isatty(2) calls. Without that, the behavior will change depending on\n   whether the test is run with -x/-v or without.\n   \n   \n * The 0x7FFF/0x7FFFFFFF constant problem is fixed with an EXPENSIVE test\n   that verifies it.\n   \n   \n * The option parsing has changed to use a local struct and pass that struct\n   to the helper methods. This is instead of having a global singleton.\n   \n   \n\nThanks, -Stolee\n\nDerrick Stolee (8):\n  maintenance: add prefetch task\n  maintenance: add loose-objects task\n  maintenance: create auto condition for loose-objects\n  midx: enable core.multiPackIndex by default\n  midx: use start_delayed_progress()\n  maintenance: add incremental-repack task\n  maintenance: auto-size incremental-repack batch\n  maintenance: add incremental-repack auto condition\n\n Documentation/config/core.txt        |   4 +-\n Documentation/config/maintenance.txt |  18 ++\n Documentation/git-maintenance.txt    |  48 ++++\n builtin/gc.c                         | 326 +++++++++++++++++++++++++++\n midx.c                               |  21 +-\n repo-settings.c                      |   6 +\n repository.h                         |   2 +\n t/t5319-multi-pack-index.sh          |  15 +-\n t/t7900-maintenance.sh               | 185 +++++++++++++++\n 9 files changed, 603 insertions(+), 22 deletions(-)\n\n\nbase-commit: 25914c4fdeefd99b06e134496dfb9bbb58a5c417\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-696%2Fderrickstolee%2Fmaintenance%2Fgc-v4\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-696/derrickstolee/maintenance/gc-v4\nPull-Request: https://github.com/gitgitgadget/git/pull/696\n\nRange-diff vs v3:\n\n 1:  da64c51a81 ! 1:  7a62e224cf maintenance: add prefetch task\n     @@ Commit message\n          of a foreground fetch to make that 'git fetch' command much faster.\n      \n          However, if we simply ran 'git fetch <remote>' in the background,\n     -    then the user running a foregroudn 'git fetch <remote>' would lose\n     +    then the user running a foreground 'git fetch <remote>' would lose\n          some important feedback when a new branch appears or an existing\n          branch updates. This is especially true if a remote branch is\n          force-updated and this isn't noticed by the user because it occurred\n 2:  75e846456b ! 2:  f3a16fd324 maintenance: add loose-objects task\n     @@ Commit message\n             objects are created only by a user doing normal development.\n             We noticed users with _millions_ of loose objects because VFS\n             for Git downloads blobs on-demand when a file read operation\n     -       requires populating a virtual file. This has potential of\n     -       happening in partial clones if someone runs 'git grep' or\n     -       otherwise evades the batch-download feature for requesting\n     -       promisor objects.\n     +       requires populating a virtual file.\n      \n          This step is based on a similar step in Scalar [1] and VFS for Git.\n          [1] https://github.com/microsoft/scalar/blob/master/Scalar.Common/Maintenance/LooseObjectsStep.cs\n 3:  d6e382c43e ! 3:  931fff4883 maintenance: create auto condition for loose-objects\n     @@ t/t7900-maintenance.sh: test_expect_success 'loose-objects task' '\n      +\t\tgit -c maintenance.loose-objects.auto=1 maintenance \\\n      +\t\trun --auto --task=loose-objects 2>/dev/null &&\n      +\ttest_subcommand ! git prune-packed --quiet <trace-lo1.txt &&\n     -+\tfor i in 1 2\n     -+\tdo\n     -+\t\tprintf data-A-$i | git hash-object -t blob --stdin -w &&\n     -+\t\tGIT_TRACE2_EVENT=\"$(pwd)/trace-loA-$i\" \\\n     -+\t\t\tgit -c maintenance.loose-objects.auto=2 \\\n     -+\t\t\tmaintenance run --auto --task=loose-objects 2>/dev/null &&\n     -+\t\ttest_subcommand ! git prune-packed --quiet <trace-loA-$i &&\n     -+\t\tprintf data-B-$i | git hash-object -t blob --stdin -w &&\n     -+\t\tGIT_TRACE2_EVENT=\"$(pwd)/trace-loB-$i\" \\\n     -+\t\t\tgit -c maintenance.loose-objects.auto=2 \\\n     -+\t\t\tmaintenance run --auto --task=loose-objects 2>/dev/null &&\n     -+\t\ttest_subcommand git prune-packed --quiet <trace-loB-$i &&\n     -+\t\tGIT_TRACE2_EVENT=\"$(pwd)/trace-loC-$i\" \\\n     -+\t\t\tgit -c maintenance.loose-objects.auto=2 \\\n     -+\t\t\tmaintenance run --auto --task=loose-objects 2>/dev/null &&\n     -+\t\ttest_subcommand git prune-packed --quiet <trace-loC-$i || return 1\n     -+\tdone\n     ++\tprintf data-A | git hash-object -t blob --stdin -w &&\n     ++\tGIT_TRACE2_EVENT=\"$(pwd)/trace-loA\" \\\n     ++\t\tgit -c maintenance.loose-objects.auto=2 \\\n     ++\t\tmaintenance run --auto --task=loose-objects 2>/dev/null &&\n     ++\ttest_subcommand ! git prune-packed --quiet <trace-loA &&\n     ++\tprintf data-B | git hash-object -t blob --stdin -w &&\n     ++\tGIT_TRACE2_EVENT=\"$(pwd)/trace-loB\" \\\n     ++\t\tgit -c maintenance.loose-objects.auto=2 \\\n     ++\t\tmaintenance run --auto --task=loose-objects 2>/dev/null &&\n     ++\ttest_subcommand git prune-packed --quiet <trace-loB &&\n     ++\tGIT_TRACE2_EVENT=\"$(pwd)/trace-loC\" \\\n     ++\t\tgit -c maintenance.loose-objects.auto=2 \\\n     ++\t\tmaintenance run --auto --task=loose-objects 2>/dev/null &&\n     ++\ttest_subcommand git prune-packed --quiet <trace-loC\n      +'\n      +\n       test_done\n 4:  d0f2ec70d9 = 4:  0fe2036aa8 midx: enable core.multiPackIndex by default\n 5:  2cd3c803d9 = 5:  ce435bf784 midx: use start_delayed_progress()\n 6:  0dd26bb584 ! 6:  d934899253 maintenance: add incremental-repack task\n     @@ Documentation/git-maintenance.txt: loose-objects::\n      +\tThe `incremental-repack` job repacks the object directory\n      +\tusing the `multi-pack-index` feature. In order to prevent race\n      +\tconditions with concurrent Git commands, it follows a two-step\n     -+\tprocess. First, it deletes any pack-files included in the\n     -+\t`multi-pack-index` where none of the objects in the\n     -+\t`multi-pack-index` reference those pack-files; this only happens\n     -+\tif all objects in the pack-file are also stored in a newer\n     -+\tpack-file. Second, it selects a group of pack-files whose \"expected\n     -+\tsize\" is below the batch size until the group has total expected\n     -+\tsize at least the batch size; see the `--batch-size` option for\n     -+\tthe `repack` subcommand in linkgit:git-multi-pack-index[1]. The\n     -+\tdefault batch-size is zero, which is a special case that attempts\n     -+\tto repack all pack-files into a single pack-file.\n     ++\tprocess. First, it calls `git multi-pack-index expire` to delete\n     ++\tpack-files unreferenced by the `multi-pack-index` file. Second, it\n     ++\tcalls `git multi-pack-index repack` to select several small\n     ++\tpack-files and repack them into a bigger one, and then update the\n     ++\t`multi-pack-index` entries that refer to the small pack-files to\n     ++\trefer to the new pack-file. This prepares those small pack-files\n     ++\tfor deletion upon the next run of `git multi-pack-index expire`.\n     ++\tThe selection of the small pack-files is such that the expected\n     ++\tsize of the big pack-file is at least the batch size; see the\n     ++\t`--batch-size` option for the `repack` subcommand in\n     ++\tlinkgit:git-multi-pack-index[1]. The default batch-size is zero,\n     ++\twhich is a special case that attempts to repack all pack-files\n     ++\tinto a single pack-file.\n      +\n       OPTIONS\n       -------\n       --auto::\n      \n       ## builtin/gc.c ##\n     -@@\n     - #include \"promisor-remote.h\"\n     - #include \"refs.h\"\n     - #include \"remote.h\"\n     -+#include \"midx.h\"\n     - \n     - #define FAILED_RUN \"failed to run %s\"\n     - \n      @@ builtin/gc.c: static int maintenance_task_loose_objects(struct maintenance_run_opts *opts)\n       \treturn prune_packed(opts) || pack_loose(opts);\n       }\n     @@ builtin/gc.c: static struct maintenance_task tasks[] = {\n       \t\t\"gc\",\n       \t\tmaintenance_task_gc,\n      \n     - ## midx.c ##\n     -@@\n     - \n     - #define PACK_EXPIRED UINT_MAX\n     - \n     --static char *get_midx_filename(const char *object_dir)\n     -+char *get_midx_filename(const char *object_dir)\n     - {\n     - \treturn xstrfmt(\"%s/pack/multi-pack-index\", object_dir);\n     - }\n     -\n     - ## midx.h ##\n     -@@ midx.h: struct multi_pack_index {\n     - \n     - #define MIDX_PROGRESS     (1 << 0)\n     - \n     -+char *get_midx_filename(const char *object_dir);\n     - struct multi_pack_index *load_multi_pack_index(const char *object_dir, int local);\n     - int prepare_midx_pack(struct repository *r, struct multi_pack_index *m, uint32_t pack_int_id);\n     - int bsearch_midx(const struct object_id *oid, struct multi_pack_index *m, uint32_t *result);\n     -\n       ## t/t5319-multi-pack-index.sh ##\n      @@\n       test_description='multi-pack-indexes'\n     @@ t/t7900-maintenance.sh: test_description='git maintenance builtin'\n       test_expect_success 'help text' '\n       \ttest_expect_code 129 git maintenance -h 2>err &&\n      @@ t/t7900-maintenance.sh: test_expect_success 'maintenance.loose-objects.auto' '\n     - \tdone\n     + \ttest_subcommand git prune-packed --quiet <trace-loC\n       '\n       \n      +test_expect_success 'incremental-repack task' '\n 7:  f3b25a9927 = 7:  bade7706d5 maintenance: auto-size incremental-repack batch\n 8:  e9bb32f53a ! 8:  f660dd1890 maintenance: add incremental-repack auto condition\n     @@ Documentation/config/maintenance.txt: maintenance.loose-objects.auto::\n      \n       ## builtin/gc.c ##\n      @@\n     + #include \"promisor-remote.h\"\n       #include \"refs.h\"\n       #include \"remote.h\"\n     - #include \"midx.h\"\n      +#include \"object-store.h\"\n       \n       #define FAILED_RUN \"failed to run %s\"\n     @@ t/t7900-maintenance.sh: test_expect_success EXPENSIVE 'incremental-repack 2g lim\n      +\t\t-c maintenance.incremental-repack.auto=1 \\\n      +\t\tmaintenance run --auto --task=incremental-repack 2>/dev/null &&\n      +\ttest_subcommand ! git multi-pack-index write --no-progress <midx-init.txt &&\n     -+\tfor i in 1 2\n     -+\tdo\n     -+\t\ttest_commit A-$i &&\n     -+\t\tgit pack-objects --revs .git/objects/pack/pack <<-\\EOF &&\n     -+\t\tHEAD\n     -+\t\t^HEAD~1\n     -+\t\tEOF\n     -+\t\tGIT_TRACE2_EVENT=$(pwd)/trace-A-$i git \\\n     -+\t\t\t-c maintenance.incremental-repack.auto=2 \\\n     -+\t\t\tmaintenance run --auto --task=incremental-repack 2>/dev/null &&\n     -+\t\ttest_subcommand ! git multi-pack-index write --no-progress <trace-A-$i &&\n     -+\t\ttest_commit B-$i &&\n     -+\t\tgit pack-objects --revs .git/objects/pack/pack <<-\\EOF &&\n     -+\t\tHEAD\n     -+\t\t^HEAD~1\n     -+\t\tEOF\n     -+\t\tGIT_TRACE2_EVENT=$(pwd)/trace-B-$i git \\\n     -+\t\t\t-c maintenance.incremental-repack.auto=2 \\\n     -+\t\t\tmaintenance run --auto --task=incremental-repack 2>/dev/null &&\n     -+\t\ttest_subcommand git multi-pack-index write --no-progress <trace-B-$i || return 1\n     -+\tdone\n     ++\ttest_commit A &&\n     ++\tgit pack-objects --revs .git/objects/pack/pack <<-\\EOF &&\n     ++\tHEAD\n     ++\t^HEAD~1\n     ++\tEOF\n     ++\tGIT_TRACE2_EVENT=$(pwd)/trace-A git \\\n     ++\t\t-c maintenance.incremental-repack.auto=2 \\\n     ++\t\tmaintenance run --auto --task=incremental-repack 2>/dev/null &&\n     ++\ttest_subcommand ! git multi-pack-index write --no-progress <trace-A &&\n     ++\ttest_commit B &&\n     ++\tgit pack-objects --revs .git/objects/pack/pack <<-\\EOF &&\n     ++\tHEAD\n     ++\t^HEAD~1\n     ++\tEOF\n     ++\tGIT_TRACE2_EVENT=$(pwd)/trace-B git \\\n     ++\t\t-c maintenance.incremental-repack.auto=2 \\\n     ++\t\tmaintenance run --auto --task=incremental-repack 2>/dev/null &&\n     ++\ttest_subcommand git multi-pack-index write --no-progress <trace-B\n      +'\n      +\n       test_done\n\n-- \ngitgitgadget\n"},{"id":"406323","messageId":"7a62e224cfa4e353572d90ad2bdef0b9e348def4.1601037218.git.gitgitgadget@gmail.com","threadId":"54001","inReplyTo":"pull.696.v4.git.1601037218.gitgitgadget@gmail.com","subject":"[PATCH v4 1/8] maintenance: add prefetch task","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2020-09-25T12:33:31Z","receivedAt":"2020-09-25T12:33:46Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"From: Derrick Stolee <dstolee@microsoft.com>\n\nWhen working with very large repositories, an incremental 'git fetch'\ncommand can download a large amount of data. If there are many other\nusers pushing to a common repo, then this data can rival the initial\npack-file size of a 'git clone' of a medium-size repo.\n\nUsers may want to keep the data on their local repos as close as\npossible to the data on the remote repos by fetching periodically in\nthe background. This can break up a large daily fetch into several\nsmaller hourly fetches.\n\nThe task is called \"prefetch\" because it is work done in advance\nof a foreground fetch to make that 'git fetch' command much faster.\n\nHowever, if we simply ran 'git fetch <remote>' in the background,\nthen the user running a foreground 'git fetch <remote>' would lose\nsome important feedback when a new branch appears or an existing\nbranch updates. This is especially true if a remote branch is\nforce-updated and this isn't noticed by the user because it occurred\nin the background. Further, the functionality of 'git push\n--force-with-lease' becomes suspect.\n\nWhen running 'git fetch <remote> <options>' in the background, use\nthe following options for careful updating:\n\n1. --no-tags prevents getting a new tag when a user wants to see\n   the new tags appear in their foreground fetches.\n\n2. --refmap= removes the configured refspec which usually updates\n   refs/remotes/<remote>/* with the refs advertised by the remote.\n   While this looks confusing, this was documented and tested by\n   b40a50264ac (fetch: document and test --refmap=\"\", 2020-01-21),\n   including this sentence in the documentation:\n\n\tProviding an empty `<refspec>` to the `--refmap` option\n\tcauses Git to ignore the configured refspecs and rely\n\tentirely on the refspecs supplied as command-line arguments.\n\n3. By adding a new refspec \"+refs/heads/*:refs/prefetch/<remote>/*\"\n   we can ensure that we actually load the new values somewhere in\n   our refspace while not updating refs/heads or refs/remotes. By\n   storing these refs here, the commit-graph job will update the\n   commit-graph with the commits from these hidden refs.\n\n4. --prune will delete the refs/prefetch/<remote> refs that no\n   longer appear on the remote.\n\n5. --no-write-fetch-head prevents updating FETCH_HEAD.\n\nWe've been using this step as a critical background job in Scalar\n[1] (and VFS for Git). This solved a pain point that was showing up\nin user reports: fetching was a pain! Users do not like waiting to\ndownload the data that was created while they were away from their\nmachines. After implementing background fetch, the foreground fetch\ncommands sped up significantly because they mostly just update refs\nand download a small amount of new data. The effect is especially\ndramatic when paried with --no-show-forced-udpates (through\nfetch.showForcedUpdates=false).\n\n[1] https://github.com/microsoft/scalar/blob/master/Scalar.Common/Maintenance/FetchStep.cs\n\nSigned-off-by: Derrick Stolee <dstolee@microsoft.com>\n---\n Documentation/git-maintenance.txt | 15 +++++++++\n builtin/gc.c                      | 51 +++++++++++++++++++++++++++++++\n t/t7900-maintenance.sh            | 26 ++++++++++++++++\n 3 files changed, 92 insertions(+)\n\ndiff --git a/Documentation/git-maintenance.txt b/Documentation/git-maintenance.txt\nindex 6abcb8255a..12668fccf7 100644\n--- a/Documentation/git-maintenance.txt\n+++ b/Documentation/git-maintenance.txt\n@@ -47,6 +47,21 @@ commit-graph::\n \t`commit-graph-chain` file. They will be deleted by a later run based\n \ton the expiration delay.\n \n+prefetch::\n+\tThe `prefetch` task updates the object directory with the latest\n+\tobjects from all registered remotes. For each remote, a `git fetch`\n+\tcommand is run. The refmap is custom to avoid updating local or remote\n+\tbranches (those in `refs/heads` or `refs/remotes`). Instead, the\n+\tremote refs are stored in `refs/prefetch/<remote>/`. Also, tags are\n+\tnot updated.\n++\n+This is done to avoid disrupting the remote-tracking branches. The end users\n+expect these refs to stay unmoved unless they initiate a fetch.  With prefetch\n+task, however, the objects necessary to complete a later real fetch would\n+already be obtained, so the real fetch would go faster.  In the ideal case,\n+it will just become an update to bunch of remote-tracking branches without\n+any object transfer.\n+\n gc::\n \tClean up unnecessary files and optimize the local repository. \"GC\"\n \tstands for \"garbage collection,\" but this task performs many\ndiff --git a/builtin/gc.c b/builtin/gc.c\nindex 090959350e..5e469488f4 100644\n--- a/builtin/gc.c\n+++ b/builtin/gc.c\n@@ -29,6 +29,7 @@\n #include \"tree.h\"\n #include \"promisor-remote.h\"\n #include \"refs.h\"\n+#include \"remote.h\"\n \n #define FAILED_RUN \"failed to run %s\"\n \n@@ -816,6 +817,51 @@ static int maintenance_task_commit_graph(struct maintenance_run_opts *opts)\n \treturn 0;\n }\n \n+static int fetch_remote(const char *remote, struct maintenance_run_opts *opts)\n+{\n+\tstruct child_process child = CHILD_PROCESS_INIT;\n+\n+\tchild.git_cmd = 1;\n+\tstrvec_pushl(&child.args, \"fetch\", remote, \"--prune\", \"--no-tags\",\n+\t\t     \"--no-write-fetch-head\", \"--recurse-submodules=no\",\n+\t\t     \"--refmap=\", NULL);\n+\n+\tif (opts->quiet)\n+\t\tstrvec_push(&child.args, \"--quiet\");\n+\n+\tstrvec_pushf(&child.args, \"+refs/heads/*:refs/prefetch/%s/*\", remote);\n+\n+\treturn !!run_command(&child);\n+}\n+\n+static int append_remote(struct remote *remote, void *cbdata)\n+{\n+\tstruct string_list *remotes = (struct string_list *)cbdata;\n+\n+\tstring_list_append(remotes, remote->name);\n+\treturn 0;\n+}\n+\n+static int maintenance_task_prefetch(struct maintenance_run_opts *opts)\n+{\n+\tint result = 0;\n+\tstruct string_list_item *item;\n+\tstruct string_list remotes = STRING_LIST_INIT_DUP;\n+\n+\tif (for_each_remote(append_remote, &remotes)) {\n+\t\terror(_(\"failed to fill remotes\"));\n+\t\tresult = 1;\n+\t\tgoto cleanup;\n+\t}\n+\n+\tfor_each_string_list_item(item, &remotes)\n+\t\tresult |= fetch_remote(item->string, opts);\n+\n+cleanup:\n+\tstring_list_clear(&remotes, 0);\n+\treturn result;\n+}\n+\n static int maintenance_task_gc(struct maintenance_run_opts *opts)\n {\n \tstruct child_process child = CHILD_PROCESS_INIT;\n@@ -854,6 +900,7 @@ struct maintenance_task {\n };\n \n enum maintenance_task_label {\n+\tTASK_PREFETCH,\n \tTASK_GC,\n \tTASK_COMMIT_GRAPH,\n \n@@ -862,6 +909,10 @@ enum maintenance_task_label {\n };\n \n static struct maintenance_task tasks[] = {\n+\t[TASK_PREFETCH] = {\n+\t\t\"prefetch\",\n+\t\tmaintenance_task_prefetch,\n+\t},\n \t[TASK_GC] = {\n \t\t\"gc\",\n \t\tmaintenance_task_gc,\ndiff --git a/t/t7900-maintenance.sh b/t/t7900-maintenance.sh\nindex 53c883531e..045524e6ad 100755\n--- a/t/t7900-maintenance.sh\n+++ b/t/t7900-maintenance.sh\n@@ -62,4 +62,30 @@ test_expect_success 'run --task duplicate' '\n \ttest_i18ngrep \"cannot be selected multiple times\" err\n '\n \n+test_expect_success 'run --task=prefetch with no remotes' '\n+\tgit maintenance run --task=prefetch 2>err &&\n+\ttest_must_be_empty err\n+'\n+\n+test_expect_success 'prefetch multiple remotes' '\n+\tgit clone . clone1 &&\n+\tgit clone . clone2 &&\n+\tgit remote add remote1 \"file://$(pwd)/clone1\" &&\n+\tgit remote add remote2 \"file://$(pwd)/clone2\" &&\n+\tgit -C clone1 switch -c one &&\n+\tgit -C clone2 switch -c two &&\n+\ttest_commit -C clone1 one &&\n+\ttest_commit -C clone2 two &&\n+\tGIT_TRACE2_EVENT=\"$(pwd)/run-prefetch.txt\" git maintenance run --task=prefetch 2>/dev/null &&\n+\tfetchargs=\"--prune --no-tags --no-write-fetch-head --recurse-submodules=no --refmap= --quiet\" &&\n+\ttest_subcommand git fetch remote1 $fetchargs +refs/heads/\\\\*:refs/prefetch/remote1/\\\\* <run-prefetch.txt &&\n+\ttest_subcommand git fetch remote2 $fetchargs +refs/heads/\\\\*:refs/prefetch/remote2/\\\\* <run-prefetch.txt &&\n+\ttest_path_is_missing .git/refs/remotes &&\n+\tgit log prefetch/remote1/one &&\n+\tgit log prefetch/remote2/two &&\n+\tgit fetch --all &&\n+\ttest_cmp_rev refs/remotes/remote1/one refs/prefetch/remote1/one &&\n+\ttest_cmp_rev refs/remotes/remote2/two refs/prefetch/remote2/two\n+'\n+\n test_done\n-- \ngitgitgadget\n\n"},{"id":"406324","messageId":"931fff4883a3da26a296af69a9c5ccc3a5037d71.1601037218.git.gitgitgadget@gmail.com","threadId":"54001","inReplyTo":"pull.696.v4.git.1601037218.gitgitgadget@gmail.com","subject":"[PATCH v4 3/8] maintenance: create auto condition for loose-objects","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2020-09-25T12:33:33Z","receivedAt":"2020-09-25T12:33:48Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"From: Derrick Stolee <dstolee@microsoft.com>\n\nThe loose-objects task deletes loose objects that already exist in a\npack-file, then place the remaining loose objects into a new pack-file.\nIf this step runs all the time, then we risk creating pack-files with\nvery few objects with every 'git commit' process. To prevent\noverwhelming the packs directory with small pack-files, place a minimum\nnumber of objects to justify the task.\n\nThe 'maintenance.loose-objects.auto' config option specifies a minimum\nnumber of loose objects to justify the task to run under the '--auto'\noption. This defaults to 100 loose objects. Setting the value to zero\nwill prevent the step from running under '--auto' while a negative value\nwill force it to run every time.\n\nSigned-off-by: Derrick Stolee <dstolee@microsoft.com>\n---\n Documentation/config/maintenance.txt |  9 +++++++++\n builtin/gc.c                         | 30 ++++++++++++++++++++++++++++\n t/t7900-maintenance.sh               | 22 ++++++++++++++++++++\n 3 files changed, 61 insertions(+)\n\ndiff --git a/Documentation/config/maintenance.txt b/Documentation/config/maintenance.txt\nindex 7cc6700d57..c31613be62 100644\n--- a/Documentation/config/maintenance.txt\n+++ b/Documentation/config/maintenance.txt\n@@ -14,3 +14,12 @@ maintenance.commit-graph.auto::\n \treachable commits that are not in the commit-graph file is at least\n \tthe value of `maintenance.commit-graph.auto`. The default value is\n \t100.\n+\n+maintenance.loose-objects.auto::\n+\tThis integer config option controls how often the `loose-objects` task\n+\tshould be run as part of `git maintenance run --auto`. If zero, then\n+\tthe `loose-objects` task will not run with the `--auto` option. A\n+\tnegative value will force the task to run every time. Otherwise, a\n+\tpositive value implies the command should run when the number of\n+\tloose objects is at least the value of `maintenance.loose-objects.auto`.\n+\tThe default value is 100.\ndiff --git a/builtin/gc.c b/builtin/gc.c\nindex c9db8555b9..4403827481 100644\n--- a/builtin/gc.c\n+++ b/builtin/gc.c\n@@ -899,6 +899,35 @@ struct write_loose_object_data {\n \tint batch_size;\n };\n \n+static int loose_object_auto_limit = 100;\n+\n+static int loose_object_count(const struct object_id *oid,\n+\t\t\t       const char *path,\n+\t\t\t       void *data)\n+{\n+\tint *count = (int*)data;\n+\tif (++(*count) >= loose_object_auto_limit)\n+\t\treturn 1;\n+\treturn 0;\n+}\n+\n+static int loose_object_auto_condition(void)\n+{\n+\tint count = 0;\n+\n+\tgit_config_get_int(\"maintenance.loose-objects.auto\",\n+\t\t\t   &loose_object_auto_limit);\n+\n+\tif (!loose_object_auto_limit)\n+\t\treturn 0;\n+\tif (loose_object_auto_limit < 0)\n+\t\treturn 1;\n+\n+\treturn for_each_loose_file_in_objdir(the_repository->objects->odb->path,\n+\t\t\t\t\t     loose_object_count,\n+\t\t\t\t\t     NULL, NULL, &count);\n+}\n+\n static int bail_on_loose(const struct object_id *oid,\n \t\t\t const char *path,\n \t\t\t void *data)\n@@ -1009,6 +1038,7 @@ static struct maintenance_task tasks[] = {\n \t[TASK_LOOSE_OBJECTS] = {\n \t\t\"loose-objects\",\n \t\tmaintenance_task_loose_objects,\n+\t\tloose_object_auto_condition,\n \t},\n \t[TASK_GC] = {\n \t\t\"gc\",\ndiff --git a/t/t7900-maintenance.sh b/t/t7900-maintenance.sh\nindex b3fc7c8670..27565c55a2 100755\n--- a/t/t7900-maintenance.sh\n+++ b/t/t7900-maintenance.sh\n@@ -127,4 +127,26 @@ test_expect_success 'loose-objects task' '\n \ttest_cmp packs-between packs-after\n '\n \n+test_expect_success 'maintenance.loose-objects.auto' '\n+\tgit repack -adk &&\n+\tGIT_TRACE2_EVENT=\"$(pwd)/trace-lo1.txt\" \\\n+\t\tgit -c maintenance.loose-objects.auto=1 maintenance \\\n+\t\trun --auto --task=loose-objects 2>/dev/null &&\n+\ttest_subcommand ! git prune-packed --quiet <trace-lo1.txt &&\n+\tprintf data-A | git hash-object -t blob --stdin -w &&\n+\tGIT_TRACE2_EVENT=\"$(pwd)/trace-loA\" \\\n+\t\tgit -c maintenance.loose-objects.auto=2 \\\n+\t\tmaintenance run --auto --task=loose-objects 2>/dev/null &&\n+\ttest_subcommand ! git prune-packed --quiet <trace-loA &&\n+\tprintf data-B | git hash-object -t blob --stdin -w &&\n+\tGIT_TRACE2_EVENT=\"$(pwd)/trace-loB\" \\\n+\t\tgit -c maintenance.loose-objects.auto=2 \\\n+\t\tmaintenance run --auto --task=loose-objects 2>/dev/null &&\n+\ttest_subcommand git prune-packed --quiet <trace-loB &&\n+\tGIT_TRACE2_EVENT=\"$(pwd)/trace-loC\" \\\n+\t\tgit -c maintenance.loose-objects.auto=2 \\\n+\t\tmaintenance run --auto --task=loose-objects 2>/dev/null &&\n+\ttest_subcommand git prune-packed --quiet <trace-loC\n+'\n+\n test_done\n-- \ngitgitgadget\n\n"},{"id":"406325","messageId":"0fe2036aa84794a3c592f294eceb8df76a637207.1601037218.git.gitgitgadget@gmail.com","threadId":"54001","inReplyTo":"pull.696.v4.git.1601037218.gitgitgadget@gmail.com","subject":"[PATCH v4 4/8] midx: enable core.multiPackIndex by default","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2020-09-25T12:33:34Z","receivedAt":"2020-09-25T12:33:49Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"From: Derrick Stolee <dstolee@microsoft.com>\n\nThe core.multiPackIndex setting has been around since c4d25228ebb\n(config: create core.multiPackIndex setting, 2018-07-12), but has been\ndisabled by default. If a user wishes to use the multi-pack-index\nfeature, then they must enable this config and run 'git multi-pack-index\nwrite'.\n\nThe multi-pack-index feature is relatively stable now, so make the\nconfig option true by default. For users that do not use a\nmulti-pack-index, the only extra cost will be a file lookup to see if a\nmulti-pack-index file exists (once per process, per object directory).\n\nAlso, this config option will be referenced by an upcoming\n\"incremental-repack\" task in the maintenance builtin, so move the config\noption into the repository settings struct. Note that if\nGIT_TEST_MULTI_PACK_INDEX=1, then we want to ignore the config option\nand treat core.multiPackIndex as enabled.\n\nSigned-off-by: Derrick Stolee <dstolee@microsoft.com>\n---\n Documentation/config/core.txt |  4 ++--\n midx.c                        | 11 +++--------\n repo-settings.c               |  6 ++++++\n repository.h                  |  2 ++\n 4 files changed, 13 insertions(+), 10 deletions(-)\n\ndiff --git a/Documentation/config/core.txt b/Documentation/config/core.txt\nindex 74619a9c03..86c91d5381 100644\n--- a/Documentation/config/core.txt\n+++ b/Documentation/config/core.txt\n@@ -606,8 +606,8 @@ core.useReplaceRefs::\n \n core.multiPackIndex::\n \tUse the multi-pack-index file to track multiple packfiles using a\n-\tsingle index. See link:technical/multi-pack-index.html[the\n-\tmulti-pack-index design document].\n+\tsingle index. See linkgit:git-multi-pack-index[1] for more\n+\tinformation. Defaults to true.\n \n core.sparseCheckout::\n \tEnable \"sparse checkout\" feature. See linkgit:git-sparse-checkout[1]\ndiff --git a/midx.c b/midx.c\nindex a5fb797ede..ef499cf504 100644\n--- a/midx.c\n+++ b/midx.c\n@@ -10,6 +10,7 @@\n #include \"progress.h\"\n #include \"trace2.h\"\n #include \"run-command.h\"\n+#include \"repository.h\"\n \n #define MIDX_SIGNATURE 0x4d494458 /* \"MIDX\" */\n #define MIDX_VERSION 1\n@@ -384,15 +385,9 @@ int prepare_multi_pack_index_one(struct repository *r, const char *object_dir, i\n {\n \tstruct multi_pack_index *m;\n \tstruct multi_pack_index *m_search;\n-\tint config_value;\n-\tstatic int env_value = -1;\n \n-\tif (env_value < 0)\n-\t\tenv_value = git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0);\n-\n-\tif (!env_value &&\n-\t    (repo_config_get_bool(r, \"core.multipackindex\", &config_value) ||\n-\t    !config_value))\n+\tprepare_repo_settings(r);\n+\tif (!r->settings.core_multi_pack_index)\n \t\treturn 0;\n \n \tfor (m_search = r->objects->multi_pack_index; m_search; m_search = m_search->next)\ndiff --git a/repo-settings.c b/repo-settings.c\nindex 0918408b34..5bd2c22726 100644\n--- a/repo-settings.c\n+++ b/repo-settings.c\n@@ -1,6 +1,7 @@\n #include \"cache.h\"\n #include \"config.h\"\n #include \"repository.h\"\n+#include \"midx.h\"\n \n #define UPDATE_DEFAULT_BOOL(s,v) do { if (s == -1) { s = v; } } while(0)\n \n@@ -47,6 +48,11 @@ void prepare_repo_settings(struct repository *r)\n \t\tr->settings.pack_use_sparse = value;\n \tUPDATE_DEFAULT_BOOL(r->settings.pack_use_sparse, 1);\n \n+\tvalue = git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0);\n+\tif (value || !repo_config_get_bool(r, \"core.multipackindex\", &value))\n+\t\tr->settings.core_multi_pack_index = value;\n+\tUPDATE_DEFAULT_BOOL(r->settings.core_multi_pack_index, 1);\n+\n \tif (!repo_config_get_bool(r, \"feature.manyfiles\", &value) && value) {\n \t\tUPDATE_DEFAULT_BOOL(r->settings.index_version, 4);\n \t\tUPDATE_DEFAULT_BOOL(r->settings.core_untracked_cache, UNTRACKED_CACHE_WRITE);\ndiff --git a/repository.h b/repository.h\nindex 3c1f7d54bd..3901ce0b65 100644\n--- a/repository.h\n+++ b/repository.h\n@@ -37,6 +37,8 @@ struct repo_settings {\n \n \tint pack_use_sparse;\n \tenum fetch_negotiation_setting fetch_negotiation_algorithm;\n+\n+\tint core_multi_pack_index;\n };\n \n struct repository {\n-- \ngitgitgadget\n\n"},{"id":"406329","messageId":"f3a16fd324a6b74ce93e8b764553ff7f4705b42e.1601037218.git.gitgitgadget@gmail.com","threadId":"54001","inReplyTo":"pull.696.v4.git.1601037218.gitgitgadget@gmail.com","subject":"[PATCH v4 2/8] maintenance: add loose-objects task","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2020-09-25T12:33:32Z","receivedAt":"2020-09-25T12:33:50Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"From: Derrick Stolee <dstolee@microsoft.com>\n\nOne goal of background maintenance jobs is to allow a user to\ndisable auto-gc (gc.auto=0) but keep their repository in a clean\nstate. Without any cleanup, loose objects will clutter the object\ndatabase and slow operations. In addition, the loose objects will\ntake up extra space because they are not stored with deltas against\nsimilar objects.\n\nCreate a 'loose-objects' task for the 'git maintenance run' command.\nThis helps clean up loose objects without disrupting concurrent Git\ncommands using the following sequence of events:\n\n1. Run 'git prune-packed' to delete any loose objects that exist\n   in a pack-file. Concurrent commands will prefer the packed\n   version of the object to the loose version. (Of course, there\n   are exceptions for commands that specifically care about the\n   location of an object. These are rare for a user to run on\n   purpose, and we hope a user that has selected background\n   maintenance will not be trying to do foreground maintenance.)\n\n2. Run 'git pack-objects' on a batch of loose objects. These\n   objects are grouped by scanning the loose object directories in\n   lexicographic order until listing all loose objects -or-\n   reaching 50,000 objects. This is more than enough if the loose\n   objects are created only by a user doing normal development.\n   We noticed users with _millions_ of loose objects because VFS\n   for Git downloads blobs on-demand when a file read operation\n   requires populating a virtual file.\n\nThis step is based on a similar step in Scalar [1] and VFS for Git.\n[1] https://github.com/microsoft/scalar/blob/master/Scalar.Common/Maintenance/LooseObjectsStep.cs\n\nSigned-off-by: Derrick Stolee <dstolee@microsoft.com>\n---\n Documentation/git-maintenance.txt | 15 +++++\n builtin/gc.c                      | 97 +++++++++++++++++++++++++++++++\n t/t7900-maintenance.sh            | 39 +++++++++++++\n 3 files changed, 151 insertions(+)\n\ndiff --git a/Documentation/git-maintenance.txt b/Documentation/git-maintenance.txt\nindex 12668fccf7..fc95eb594f 100644\n--- a/Documentation/git-maintenance.txt\n+++ b/Documentation/git-maintenance.txt\n@@ -70,6 +70,21 @@ gc::\n \tbe disruptive in some situations, as it deletes stale data. See\n \tlinkgit:git-gc[1] for more details on garbage collection in Git.\n \n+loose-objects::\n+\tThe `loose-objects` job cleans up loose objects and places them into\n+\tpack-files. In order to prevent race conditions with concurrent Git\n+\tcommands, it follows a two-step process. First, it deletes any loose\n+\tobjects that already exist in a pack-file; concurrent Git processes\n+\twill examine the pack-file for the object data instead of the loose\n+\tobject. Second, it creates a new pack-file (starting with \"loose-\")\n+\tcontaining a batch of loose objects. The batch size is limited to 50\n+\tthousand objects to prevent the job from taking too long on a\n+\trepository with many loose objects. The `gc` task writes unreachable\n+\tobjects as loose objects to be cleaned up by a later step only if\n+\tthey are not re-added to a pack-file; for this reason it is not\n+\tadvisable to enable both the `loose-objects` and `gc` tasks at the\n+\tsame time.\n+\n OPTIONS\n -------\n --auto::\ndiff --git a/builtin/gc.c b/builtin/gc.c\nindex 5e469488f4..c9db8555b9 100644\n--- a/builtin/gc.c\n+++ b/builtin/gc.c\n@@ -880,6 +880,98 @@ static int maintenance_task_gc(struct maintenance_run_opts *opts)\n \treturn run_command(&child);\n }\n \n+static int prune_packed(struct maintenance_run_opts *opts)\n+{\n+\tstruct child_process child = CHILD_PROCESS_INIT;\n+\n+\tchild.git_cmd = 1;\n+\tstrvec_push(&child.args, \"prune-packed\");\n+\n+\tif (opts->quiet)\n+\t\tstrvec_push(&child.args, \"--quiet\");\n+\n+\treturn !!run_command(&child);\n+}\n+\n+struct write_loose_object_data {\n+\tFILE *in;\n+\tint count;\n+\tint batch_size;\n+};\n+\n+static int bail_on_loose(const struct object_id *oid,\n+\t\t\t const char *path,\n+\t\t\t void *data)\n+{\n+\treturn 1;\n+}\n+\n+static int write_loose_object_to_stdin(const struct object_id *oid,\n+\t\t\t\t       const char *path,\n+\t\t\t\t       void *data)\n+{\n+\tstruct write_loose_object_data *d = (struct write_loose_object_data *)data;\n+\n+\tfprintf(d->in, \"%s\\n\", oid_to_hex(oid));\n+\n+\treturn ++(d->count) > d->batch_size;\n+}\n+\n+static int pack_loose(struct maintenance_run_opts *opts)\n+{\n+\tstruct repository *r = the_repository;\n+\tint result = 0;\n+\tstruct write_loose_object_data data;\n+\tstruct child_process pack_proc = CHILD_PROCESS_INIT;\n+\n+\t/*\n+\t * Do not start pack-objects process\n+\t * if there are no loose objects.\n+\t */\n+\tif (!for_each_loose_file_in_objdir(r->objects->odb->path,\n+\t\t\t\t\t   bail_on_loose,\n+\t\t\t\t\t   NULL, NULL, NULL))\n+\t\treturn 0;\n+\n+\tpack_proc.git_cmd = 1;\n+\n+\tstrvec_push(&pack_proc.args, \"pack-objects\");\n+\tif (opts->quiet)\n+\t\tstrvec_push(&pack_proc.args, \"--quiet\");\n+\tstrvec_pushf(&pack_proc.args, \"%s/pack/loose\", r->objects->odb->path);\n+\n+\tpack_proc.in = -1;\n+\n+\tif (start_command(&pack_proc)) {\n+\t\terror(_(\"failed to start 'git pack-objects' process\"));\n+\t\treturn 1;\n+\t}\n+\n+\tdata.in = xfdopen(pack_proc.in, \"w\");\n+\tdata.count = 0;\n+\tdata.batch_size = 50000;\n+\n+\tfor_each_loose_file_in_objdir(r->objects->odb->path,\n+\t\t\t\t      write_loose_object_to_stdin,\n+\t\t\t\t      NULL,\n+\t\t\t\t      NULL,\n+\t\t\t\t      &data);\n+\n+\tfclose(data.in);\n+\n+\tif (finish_command(&pack_proc)) {\n+\t\terror(_(\"failed to finish 'git pack-objects' process\"));\n+\t\tresult = 1;\n+\t}\n+\n+\treturn result;\n+}\n+\n+static int maintenance_task_loose_objects(struct maintenance_run_opts *opts)\n+{\n+\treturn prune_packed(opts) || pack_loose(opts);\n+}\n+\n typedef int maintenance_task_fn(struct maintenance_run_opts *opts);\n \n /*\n@@ -901,6 +993,7 @@ struct maintenance_task {\n \n enum maintenance_task_label {\n \tTASK_PREFETCH,\n+\tTASK_LOOSE_OBJECTS,\n \tTASK_GC,\n \tTASK_COMMIT_GRAPH,\n \n@@ -913,6 +1006,10 @@ static struct maintenance_task tasks[] = {\n \t\t\"prefetch\",\n \t\tmaintenance_task_prefetch,\n \t},\n+\t[TASK_LOOSE_OBJECTS] = {\n+\t\t\"loose-objects\",\n+\t\tmaintenance_task_loose_objects,\n+\t},\n \t[TASK_GC] = {\n \t\t\"gc\",\n \t\tmaintenance_task_gc,\ndiff --git a/t/t7900-maintenance.sh b/t/t7900-maintenance.sh\nindex 045524e6ad..b3fc7c8670 100755\n--- a/t/t7900-maintenance.sh\n+++ b/t/t7900-maintenance.sh\n@@ -88,4 +88,43 @@ test_expect_success 'prefetch multiple remotes' '\n \ttest_cmp_rev refs/remotes/remote2/two refs/prefetch/remote2/two\n '\n \n+test_expect_success 'loose-objects task' '\n+\t# Repack everything so we know the state of the object dir\n+\tgit repack -adk &&\n+\n+\t# Hack to stop maintenance from running during \"git commit\"\n+\techo in use >.git/objects/maintenance.lock &&\n+\n+\t# Assuming that \"git commit\" creates at least one loose object\n+\ttest_commit create-loose-object &&\n+\trm .git/objects/maintenance.lock &&\n+\n+\tls .git/objects >obj-dir-before &&\n+\ttest_file_not_empty obj-dir-before &&\n+\tls .git/objects/pack/*.pack >packs-before &&\n+\ttest_line_count = 1 packs-before &&\n+\n+\t# The first run creates a pack-file\n+\t# but does not delete loose objects.\n+\tgit maintenance run --task=loose-objects &&\n+\tls .git/objects >obj-dir-between &&\n+\ttest_cmp obj-dir-before obj-dir-between &&\n+\tls .git/objects/pack/*.pack >packs-between &&\n+\ttest_line_count = 2 packs-between &&\n+\tls .git/objects/pack/loose-*.pack >loose-packs &&\n+\ttest_line_count = 1 loose-packs &&\n+\n+\t# The second run deletes loose objects\n+\t# but does not create a pack-file.\n+\tgit maintenance run --task=loose-objects &&\n+\tls .git/objects >obj-dir-after &&\n+\tcat >expect <<-\\EOF &&\n+\tinfo\n+\tpack\n+\tEOF\n+\ttest_cmp expect obj-dir-after &&\n+\tls .git/objects/pack/*.pack >packs-after &&\n+\ttest_cmp packs-between packs-after\n+'\n+\n test_done\n-- \ngitgitgadget\n\n"},{"id":"406327","messageId":"ce435bf78430c7a996ecac6a39f789e2cf8ac7cb.1601037218.git.gitgitgadget@gmail.com","threadId":"54001","inReplyTo":"pull.696.v4.git.1601037218.gitgitgadget@gmail.com","subject":"[PATCH v4 5/8] midx: use start_delayed_progress()","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2020-09-25T12:33:35Z","receivedAt":"2020-09-25T12:33:51Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"From: Derrick Stolee <dstolee@microsoft.com>\n\nNow that the multi-pack-index may be written as part of auto maintenance\nat the end of a command, reduce the progress output when the operations\nare quick. Use start_delayed_progress() instead of start_progress().\n\nUpdate t5319-multi-pack-index.sh to use GIT_PROGRESS_DELAY=0 now that\nthe progress indicators are conditional.\n\nSigned-off-by: Derrick Stolee <dstolee@microsoft.com>\n---\n midx.c                      | 10 +++++-----\n t/t5319-multi-pack-index.sh | 14 +++++++-------\n 2 files changed, 12 insertions(+), 12 deletions(-)\n\ndiff --git a/midx.c b/midx.c\nindex ef499cf504..aa37d5da86 100644\n--- a/midx.c\n+++ b/midx.c\n@@ -832,7 +832,7 @@ static int write_midx_internal(const char *object_dir, struct multi_pack_index *\n \n \tpacks.pack_paths_checked = 0;\n \tif (flags & MIDX_PROGRESS)\n-\t\tpacks.progress = start_progress(_(\"Adding packfiles to multi-pack-index\"), 0);\n+\t\tpacks.progress = start_delayed_progress(_(\"Adding packfiles to multi-pack-index\"), 0);\n \telse\n \t\tpacks.progress = NULL;\n \n@@ -969,7 +969,7 @@ static int write_midx_internal(const char *object_dir, struct multi_pack_index *\n \t}\n \n \tif (flags & MIDX_PROGRESS)\n-\t\tprogress = start_progress(_(\"Writing chunks to multi-pack-index\"),\n+\t\tprogress = start_delayed_progress(_(\"Writing chunks to multi-pack-index\"),\n \t\t\t\t\t  num_chunks);\n \tfor (i = 0; i < num_chunks; i++) {\n \t\tif (written != chunk_offsets[i])\n@@ -1104,7 +1104,7 @@ int verify_midx_file(struct repository *r, const char *object_dir, unsigned flag\n \t\treturn 0;\n \n \tif (flags & MIDX_PROGRESS)\n-\t\tprogress = start_progress(_(\"Looking for referenced packfiles\"),\n+\t\tprogress = start_delayed_progress(_(\"Looking for referenced packfiles\"),\n \t\t\t\t\t  m->num_packs);\n \tfor (i = 0; i < m->num_packs; i++) {\n \t\tif (prepare_midx_pack(r, m, i))\n@@ -1225,7 +1225,7 @@ int expire_midx_packs(struct repository *r, const char *object_dir, unsigned fla\n \tcount = xcalloc(m->num_packs, sizeof(uint32_t));\n \n \tif (flags & MIDX_PROGRESS)\n-\t\tprogress = start_progress(_(\"Counting referenced objects\"),\n+\t\tprogress = start_delayed_progress(_(\"Counting referenced objects\"),\n \t\t\t\t\t  m->num_objects);\n \tfor (i = 0; i < m->num_objects; i++) {\n \t\tint pack_int_id = nth_midxed_pack_int_id(m, i);\n@@ -1235,7 +1235,7 @@ int expire_midx_packs(struct repository *r, const char *object_dir, unsigned fla\n \tstop_progress(&progress);\n \n \tif (flags & MIDX_PROGRESS)\n-\t\tprogress = start_progress(_(\"Finding and deleting unreferenced packfiles\"),\n+\t\tprogress = start_delayed_progress(_(\"Finding and deleting unreferenced packfiles\"),\n \t\t\t\t\t  m->num_packs);\n \tfor (i = 0; i < m->num_packs; i++) {\n \t\tchar *pack_name;\ndiff --git a/t/t5319-multi-pack-index.sh b/t/t5319-multi-pack-index.sh\nindex 7dfff0f8f4..ec87f616c6 100755\n--- a/t/t5319-multi-pack-index.sh\n+++ b/t/t5319-multi-pack-index.sh\n@@ -171,12 +171,12 @@ test_expect_success 'write progress off for redirected stderr' '\n '\n \n test_expect_success 'write force progress on for stderr' '\n-\tgit multi-pack-index --object-dir=$objdir --progress write 2>err &&\n+\tGIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir --progress write 2>err &&\n \ttest_file_not_empty err\n '\n \n test_expect_success 'write with the --no-progress option' '\n-\tgit multi-pack-index --object-dir=$objdir --no-progress write 2>err &&\n+\tGIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir --no-progress write 2>err &&\n \ttest_line_count = 0 err\n '\n \n@@ -333,17 +333,17 @@ test_expect_success 'git-fsck incorrect offset' '\n '\n \n test_expect_success 'repack progress off for redirected stderr' '\n-\tgit multi-pack-index --object-dir=$objdir repack 2>err &&\n+\tGIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir repack 2>err &&\n \ttest_line_count = 0 err\n '\n \n test_expect_success 'repack force progress on for stderr' '\n-\tgit multi-pack-index --object-dir=$objdir --progress repack 2>err &&\n+\tGIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir --progress repack 2>err &&\n \ttest_file_not_empty err\n '\n \n test_expect_success 'repack with the --no-progress option' '\n-\tgit multi-pack-index --object-dir=$objdir --no-progress repack 2>err &&\n+\tGIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir --no-progress repack 2>err &&\n \ttest_line_count = 0 err\n '\n \n@@ -487,7 +487,7 @@ test_expect_success 'expire progress off for redirected stderr' '\n test_expect_success 'expire force progress on for stderr' '\n \t(\n \t\tcd dup &&\n-\t\tgit multi-pack-index --progress expire 2>err &&\n+\t\tGIT_PROGRESS_DELAY=0 git multi-pack-index --progress expire 2>err &&\n \t\ttest_file_not_empty err\n \t)\n '\n@@ -495,7 +495,7 @@ test_expect_success 'expire force progress on for stderr' '\n test_expect_success 'expire with the --no-progress option' '\n \t(\n \t\tcd dup &&\n-\t\tgit multi-pack-index --no-progress expire 2>err &&\n+\t\tGIT_PROGRESS_DELAY=0 git multi-pack-index --no-progress expire 2>err &&\n \t\ttest_line_count = 0 err\n \t)\n '\n-- \ngitgitgadget\n\n"},{"id":"406326","messageId":"bade7706d54991a4817409907f607fad2fc18874.1601037218.git.gitgitgadget@gmail.com","threadId":"54001","inReplyTo":"pull.696.v4.git.1601037218.gitgitgadget@gmail.com","subject":"[PATCH v4 7/8] maintenance: auto-size incremental-repack batch","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2020-09-25T12:33:37Z","receivedAt":"2020-09-25T12:33:52Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"From: Derrick Stolee <dstolee@microsoft.com>\n\nWhen repacking during the 'incremental-repack' task, we use the\n--batch-size option in 'git multi-pack-index repack'. The initial setting\nused --batch-size=0 to repack everything into a single pack-file. This is\nnot sustainable for a large repository. The amount of work required is\nalso likely to use too many system resources for a background job.\n\nUpdate the 'incremental-repack' task by dynamically computing a\n--batch-size option based on the current pack-file structure.\n\nThe dynamic default size is computed with this idea in mind for a client\nrepository that was cloned from a very large remote: there is likely one\n\"big\" pack-file that was created at clone time. Thus, do not try\nrepacking it as it is likely packed efficiently by the server.\n\nInstead, we select the second-largest pack-file, and create a batch size\nthat is one larger than that pack-file. If there are three or more\npack-files, then this guarantees that at least two will be combined into\na new pack-file.\n\nOf course, this means that the second-largest pack-file size is likely\nto grow over time and may eventually surpass the initially-cloned\npack-file. Recall that the pack-file batch is selected in a greedy\nmanner: the packs are considered from oldest to newest and are selected\nif they have size smaller than the batch size until the total selected\nsize is larger than the batch size. Thus, that oldest \"clone\" pack will\nbe first to repack after the new data creates a pack larger than that.\n\nWe also want to place some limits on how large these pack-files become,\nin order to bound the amount of time spent repacking. A maximum\nbatch-size of two gigabytes means that large repositories will never be\npacked into a single pack-file using this job, but also that repack is\nrather expensive. This is a trade-off that is valuable to have if the\nmaintenance is being run automatically or in the background. Users who\ntruly want to optimize for space and performance (and are willing to pay\nthe upfront cost of a full repack) can use the 'gc' task to do so.\n\nCreate a test for this two gigabyte limit by creating an EXPENSIVE test\nthat generates two pack-files of roughly 2.5 gigabytes in size, then\nperforms an incremental repack. Check that the --batch-size argument in\nthe subcommand uses the hard-coded maximum.\n\nHelped-by: Chris Torek <chris.torek@gmail.com>\nReported-by: Son Luong Ngoc <sluongng@gmail.com>\nSigned-off-by: Derrick Stolee <dstolee@microsoft.com>\n---\n builtin/gc.c           | 43 +++++++++++++++++++++++++++++++++++++++++-\n t/t7900-maintenance.sh | 36 +++++++++++++++++++++++++++++++++--\n 2 files changed, 76 insertions(+), 3 deletions(-)\n\ndiff --git a/builtin/gc.c b/builtin/gc.c\nindex 5f877b097a..8d22361fa9 100644\n--- a/builtin/gc.c\n+++ b/builtin/gc.c\n@@ -1035,6 +1035,46 @@ static int multi_pack_index_expire(struct maintenance_run_opts *opts)\n \treturn 0;\n }\n \n+#define TWO_GIGABYTES (INT32_MAX)\n+\n+static off_t get_auto_pack_size(void)\n+{\n+\t/*\n+\t * The \"auto\" value is special: we optimize for\n+\t * one large pack-file (i.e. from a clone) and\n+\t * expect the rest to be small and they can be\n+\t * repacked quickly.\n+\t *\n+\t * The strategy we select here is to select a\n+\t * size that is one more than the second largest\n+\t * pack-file. This ensures that we will repack\n+\t * at least two packs if there are three or more\n+\t * packs.\n+\t */\n+\toff_t max_size = 0;\n+\toff_t second_largest_size = 0;\n+\toff_t result_size;\n+\tstruct packed_git *p;\n+\tstruct repository *r = the_repository;\n+\n+\treprepare_packed_git(r);\n+\tfor (p = get_all_packs(r); p; p = p->next) {\n+\t\tif (p->pack_size > max_size) {\n+\t\t\tsecond_largest_size = max_size;\n+\t\t\tmax_size = p->pack_size;\n+\t\t} else if (p->pack_size > second_largest_size)\n+\t\t\tsecond_largest_size = p->pack_size;\n+\t}\n+\n+\tresult_size = second_largest_size + 1;\n+\n+\t/* But limit ourselves to a batch size of 2g */\n+\tif (result_size > TWO_GIGABYTES)\n+\t\tresult_size = TWO_GIGABYTES;\n+\n+\treturn result_size;\n+}\n+\n static int multi_pack_index_repack(struct maintenance_run_opts *opts)\n {\n \tstruct child_process child = CHILD_PROCESS_INIT;\n@@ -1045,7 +1085,8 @@ static int multi_pack_index_repack(struct maintenance_run_opts *opts)\n \tif (opts->quiet)\n \t\tstrvec_push(&child.args, \"--no-progress\");\n \n-\tstrvec_push(&child.args, \"--batch-size=0\");\n+\tstrvec_pushf(&child.args, \"--batch-size=%\"PRIuMAX,\n+\t\t\t\t  (uintmax_t)get_auto_pack_size());\n \n \tclose_object_store(the_repository->objects);\n \ndiff --git a/t/t7900-maintenance.sh b/t/t7900-maintenance.sh\nindex a2db2291b0..9e6ea23f35 100755\n--- a/t/t7900-maintenance.sh\n+++ b/t/t7900-maintenance.sh\n@@ -181,10 +181,42 @@ test_expect_success 'incremental-repack task' '\n \ttest_line_count = 4 packs-between &&\n \n \t# the job deletes the two old packs, and does not write\n-\t# a new one because only one pack remains.\n+\t# a new one because the batch size is not high enough to\n+\t# pack the largest pack-file.\n \tgit maintenance run --task=incremental-repack &&\n \tls .git/objects/pack/*.pack >packs-after &&\n-\ttest_line_count = 1 packs-after\n+\ttest_line_count = 2 packs-after\n+'\n+\n+test_expect_success EXPENSIVE 'incremental-repack 2g limit' '\n+\tfor i in $(test_seq 1 5)\n+\tdo\n+\t\ttest-tool genrandom foo$i $((512 * 1024 * 1024 + 1)) >>big ||\n+\t\treturn 1\n+\tdone &&\n+\tgit add big &&\n+\tgit commit -m \"Add big file (1)\" &&\n+\n+\t# ensure any possible loose objects are in a pack-file\n+\tgit maintenance run --task=loose-objects &&\n+\n+\trm big &&\n+\tfor i in $(test_seq 6 10)\n+\tdo\n+\t\ttest-tool genrandom foo$i $((512 * 1024 * 1024 + 1)) >>big ||\n+\t\treturn 1\n+\tdone &&\n+\tgit add big &&\n+\tgit commit -m \"Add big file (2)\" &&\n+\n+\t# ensure any possible loose objects are in a pack-file\n+\tgit maintenance run --task=loose-objects &&\n+\n+\t# Now run the incremental-repack task and check the batch-size\n+\tGIT_TRACE2_EVENT=\"$(pwd)/run-2g.txt\" git maintenance run \\\n+\t\t--task=incremental-repack 2>/dev/null &&\n+\ttest_subcommand git multi-pack-index repack \\\n+\t\t --no-progress --batch-size=2147483647 <run-2g.txt\n '\n \n test_done\n-- \ngitgitgadget\n\n"},{"id":"406330","messageId":"f660dd1890656609dc9ed433579323488a3ca648.1601037218.git.gitgitgadget@gmail.com","threadId":"54001","inReplyTo":"pull.696.v4.git.1601037218.gitgitgadget@gmail.com","subject":"[PATCH v4 8/8] maintenance: add incremental-repack auto condition","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2020-09-25T12:33:38Z","receivedAt":"2020-09-25T12:33:53Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"From: Derrick Stolee <dstolee@microsoft.com>\n\nThe incremental-repack task updates the multi-pack-index by deleting pack-\nfiles that have been replaced with new packs, then repacking a batch of\nsmall pack-files into a larger pack-file. This incremental repack is faster\nthan rewriting all object data, but is slower than some other\nmaintenance activities.\n\nThe 'maintenance.incremental-repack.auto' config option specifies how many\npack-files should exist outside of the multi-pack-index before running\nthe step. These pack-files could be created by 'git fetch' commands or\nby the loose-objects task. The default value is 10.\n\nSetting the option to zero disables the task with the '--auto' option,\nand a negative value makes the task run every time.\n\nSigned-off-by: Derrick Stolee <dstolee@microsoft.com>\n---\n Documentation/config/maintenance.txt |  9 ++++++++\n builtin/gc.c                         | 31 ++++++++++++++++++++++++++++\n t/t7900-maintenance.sh               | 28 +++++++++++++++++++++++++\n 3 files changed, 68 insertions(+)\n\ndiff --git a/Documentation/config/maintenance.txt b/Documentation/config/maintenance.txt\nindex c31613be62..a0706d8f09 100644\n--- a/Documentation/config/maintenance.txt\n+++ b/Documentation/config/maintenance.txt\n@@ -23,3 +23,12 @@ maintenance.loose-objects.auto::\n \tpositive value implies the command should run when the number of\n \tloose objects is at least the value of `maintenance.loose-objects.auto`.\n \tThe default value is 100.\n+\n+maintenance.incremental-repack.auto::\n+\tThis integer config option controls how often the `incremental-repack`\n+\ttask should be run as part of `git maintenance run --auto`. If zero,\n+\tthen the `incremental-repack` task will not run with the `--auto`\n+\toption. A negative value will force the task to run every time.\n+\tOtherwise, a positive value implies the command should run when the\n+\tnumber of pack-files not in the multi-pack-index is at least the value\n+\tof `maintenance.incremental-repack.auto`. The default value is 10.\ndiff --git a/builtin/gc.c b/builtin/gc.c\nindex 8d22361fa9..2b99596ec8 100644\n--- a/builtin/gc.c\n+++ b/builtin/gc.c\n@@ -30,6 +30,7 @@\n #include \"promisor-remote.h\"\n #include \"refs.h\"\n #include \"remote.h\"\n+#include \"object-store.h\"\n \n #define FAILED_RUN \"failed to run %s\"\n \n@@ -1001,6 +1002,35 @@ static int maintenance_task_loose_objects(struct maintenance_run_opts *opts)\n \treturn prune_packed(opts) || pack_loose(opts);\n }\n \n+static int incremental_repack_auto_condition(void)\n+{\n+\tstruct packed_git *p;\n+\tint enabled;\n+\tint incremental_repack_auto_limit = 10;\n+\tint count = 0;\n+\n+\tif (git_config_get_bool(\"core.multiPackIndex\", &enabled) ||\n+\t    !enabled)\n+\t\treturn 0;\n+\n+\tgit_config_get_int(\"maintenance.incremental-repack.auto\",\n+\t\t\t   &incremental_repack_auto_limit);\n+\n+\tif (!incremental_repack_auto_limit)\n+\t\treturn 0;\n+\tif (incremental_repack_auto_limit < 0)\n+\t\treturn 1;\n+\n+\tfor (p = get_packed_git(the_repository);\n+\t     count < incremental_repack_auto_limit && p;\n+\t     p = p->next) {\n+\t\tif (!p->multi_pack_index)\n+\t\t\tcount++;\n+\t}\n+\n+\treturn count >= incremental_repack_auto_limit;\n+}\n+\n static int multi_pack_index_write(struct maintenance_run_opts *opts)\n {\n \tstruct child_process child = CHILD_PROCESS_INIT;\n@@ -1156,6 +1186,7 @@ static struct maintenance_task tasks[] = {\n \t[TASK_INCREMENTAL_REPACK] = {\n \t\t\"incremental-repack\",\n \t\tmaintenance_task_incremental_repack,\n+\t\tincremental_repack_auto_condition,\n \t},\n \t[TASK_GC] = {\n \t\t\"gc\",\ndiff --git a/t/t7900-maintenance.sh b/t/t7900-maintenance.sh\nindex 9e6ea23f35..55116c2f04 100755\n--- a/t/t7900-maintenance.sh\n+++ b/t/t7900-maintenance.sh\n@@ -219,4 +219,32 @@ test_expect_success EXPENSIVE 'incremental-repack 2g limit' '\n \t\t --no-progress --batch-size=2147483647 <run-2g.txt\n '\n \n+test_expect_success 'maintenance.incremental-repack.auto' '\n+\tgit repack -adk &&\n+\tgit config core.multiPackIndex true &&\n+\tgit multi-pack-index write &&\n+\tGIT_TRACE2_EVENT=\"$(pwd)/midx-init.txt\" git \\\n+\t\t-c maintenance.incremental-repack.auto=1 \\\n+\t\tmaintenance run --auto --task=incremental-repack 2>/dev/null &&\n+\ttest_subcommand ! git multi-pack-index write --no-progress <midx-init.txt &&\n+\ttest_commit A &&\n+\tgit pack-objects --revs .git/objects/pack/pack <<-\\EOF &&\n+\tHEAD\n+\t^HEAD~1\n+\tEOF\n+\tGIT_TRACE2_EVENT=$(pwd)/trace-A git \\\n+\t\t-c maintenance.incremental-repack.auto=2 \\\n+\t\tmaintenance run --auto --task=incremental-repack 2>/dev/null &&\n+\ttest_subcommand ! git multi-pack-index write --no-progress <trace-A &&\n+\ttest_commit B &&\n+\tgit pack-objects --revs .git/objects/pack/pack <<-\\EOF &&\n+\tHEAD\n+\t^HEAD~1\n+\tEOF\n+\tGIT_TRACE2_EVENT=$(pwd)/trace-B git \\\n+\t\t-c maintenance.incremental-repack.auto=2 \\\n+\t\tmaintenance run --auto --task=incremental-repack 2>/dev/null &&\n+\ttest_subcommand git multi-pack-index write --no-progress <trace-B\n+'\n+\n test_done\n-- \ngitgitgadget\n"},{"id":"406328","messageId":"d934899253c134298e1a9a7e2e0028bccc1c60d0.1601037218.git.gitgitgadget@gmail.com","threadId":"54001","inReplyTo":"pull.696.v4.git.1601037218.gitgitgadget@gmail.com","subject":"[PATCH v4 6/8] maintenance: add incremental-repack task","fromName":"Derrick Stolee via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2020-09-25T12:33:36Z","receivedAt":"2020-09-25T12:33:55Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"From: Derrick Stolee <dstolee@microsoft.com>\n\nThe previous change cleaned up loose objects using the\n'loose-objects' that can be run safely in the background. Add a\nsimilar job that performs similar cleanups for pack-files.\n\nOne issue with running 'git repack' is that it is designed to\nrepack all pack-files into a single pack-file. While this is the\nmost space-efficient way to store object data, it is not time or\nmemory efficient. This becomes extremely important if the repo is\nso large that a user struggles to store two copies of the pack on\ntheir disk.\n\nInstead, perform an \"incremental\" repack by collecting a few small\npack-files into a new pack-file. The multi-pack-index facilitates\nthis process ever since 'git multi-pack-index expire' was added in\n19575c7 (multi-pack-index: implement 'expire' subcommand,\n2019-06-10) and 'git multi-pack-index repack' was added in ce1e4a1\n(midx: implement midx_repack(), 2019-06-10).\n\nThe 'incremental-repack' task runs the following steps:\n\n1. 'git multi-pack-index write' creates a multi-pack-index file if\n   one did not exist, and otherwise will update the multi-pack-index\n   with any new pack-files that appeared since the last write. This\n   is particularly relevant with the background fetch job.\n\n   When the multi-pack-index sees two copies of the same object, it\n   stores the offset data into the newer pack-file. This means that\n   some old pack-files could become \"unreferenced\" which I will use\n   to mean \"a pack-file that is in the pack-file list of the\n   multi-pack-index but none of the objects in the multi-pack-index\n   reference a location inside that pack-file.\"\n\n2. 'git multi-pack-index expire' deletes any unreferenced pack-files\n   and updaes the multi-pack-index to drop those pack-files from the\n   list. This is safe to do as concurrent Git processes will see the\n   multi-pack-index and not open those packs when looking for object\n   contents. (Similar to the 'loose-objects' job, there are some Git\n   commands that open pack-files regardless of the multi-pack-index,\n   but they are rarely used. Further, a user that self-selects to\n   use background operations would likely refrain from using those\n   commands.)\n\n3. 'git multi-pack-index repack --bacth-size=<size>' collects a set\n   of pack-files that are listed in the multi-pack-index and creates\n   a new pack-file containing the objects whose offsets are listed\n   by the multi-pack-index to be in those objects. The set of pack-\n   files is selected greedily by sorting the pack-files by modified\n   time and adding a pack-file to the set if its \"expected size\" is\n   smaller than the batch size until the total expected size of the\n   selected pack-files is at least the batch size. The \"expected\n   size\" is calculated by taking the size of the pack-file divided\n   by the number of objects in the pack-file and multiplied by the\n   number of objects from the multi-pack-index with offset in that\n   pack-file. The expected size approximates how much data from that\n   pack-file will contribute to the resulting pack-file size. The\n   intention is that the resulting pack-file will be close in size\n   to the provided batch size.\n\n   The next run of the incremental-repack task will delete these\n   repacked pack-files during the 'expire' step.\n\n   In this version, the batch size is set to \"0\" which ignores the\n   size restrictions when selecting the pack-files. It instead\n   selects all pack-files and repacks all packed objects into a\n   single pack-file. This will be updated in the next change, but\n   it requires doing some calculations that are better isolated to\n   a separate change.\n\nThese steps are based on a similar background maintenance step in\nScalar (and VFS for Git) [1]. This was incredibly effective for\nusers of the Windows OS repository. After using the same VFS for Git\nrepository for over a year, some users had _thousands_ of pack-files\nthat combined to up to 250 GB of data. We noticed a few users were\nrunning into the open file descriptor limits (due in part to a bug\nin the multi-pack-index fixed by af96fe3 (midx: add packs to\npacked_git linked list, 2019-04-29).\n\nThese pack-files were mostly small since they contained the commits\nand trees that were pushed to the origin in a given hour. The GVFS\nprotocol includes a \"prefetch\" step that asks for pre-computed pack-\nfiles containing commits and trees by timestamp. These pack-files\nwere grouped into \"daily\" pack-files once a day for up to 30 days.\nIf a user did not request prefetch packs for over 30 days, then they\nwould get the entire history of commits and trees in a new, large\npack-file. This led to a large number of pack-files that had poor\ndelta compression.\n\nBy running this pack-file maintenance step once per day, these repos\nwith thousands of packs spanning 200+ GB dropped to dozens of pack-\nfiles spanning 30-50 GB. This was done all without removing objects\nfrom the system and using a constant batch size of two gigabytes.\nOnce the work was done to reduce the pack-files to small sizes, the\nbatch size of two gigabytes means that not every run triggers a\nrepack operation, so the following run will not expire a pack-file.\nThis has kept these repos in a \"clean\" state.\n\n[1] https://github.com/microsoft/scalar/blob/master/Scalar.Common/Maintenance/PackfileMaintenanceStep.cs\n\nSigned-off-by: Derrick Stolee <dstolee@microsoft.com>\n---\n Documentation/git-maintenance.txt | 18 ++++++++\n builtin/gc.c                      | 76 +++++++++++++++++++++++++++++++\n t/t5319-multi-pack-index.sh       |  1 +\n t/t7900-maintenance.sh            | 38 ++++++++++++++++\n 4 files changed, 133 insertions(+)\n\ndiff --git a/Documentation/git-maintenance.txt b/Documentation/git-maintenance.txt\nindex fc95eb594f..3f5d8946b4 100644\n--- a/Documentation/git-maintenance.txt\n+++ b/Documentation/git-maintenance.txt\n@@ -85,6 +85,24 @@ loose-objects::\n \tadvisable to enable both the `loose-objects` and `gc` tasks at the\n \tsame time.\n \n+incremental-repack::\n+\tThe `incremental-repack` job repacks the object directory\n+\tusing the `multi-pack-index` feature. In order to prevent race\n+\tconditions with concurrent Git commands, it follows a two-step\n+\tprocess. First, it calls `git multi-pack-index expire` to delete\n+\tpack-files unreferenced by the `multi-pack-index` file. Second, it\n+\tcalls `git multi-pack-index repack` to select several small\n+\tpack-files and repack them into a bigger one, and then update the\n+\t`multi-pack-index` entries that refer to the small pack-files to\n+\trefer to the new pack-file. This prepares those small pack-files\n+\tfor deletion upon the next run of `git multi-pack-index expire`.\n+\tThe selection of the small pack-files is such that the expected\n+\tsize of the big pack-file is at least the batch size; see the\n+\t`--batch-size` option for the `repack` subcommand in\n+\tlinkgit:git-multi-pack-index[1]. The default batch-size is zero,\n+\twhich is a special case that attempts to repack all pack-files\n+\tinto a single pack-file.\n+\n OPTIONS\n -------\n --auto::\ndiff --git a/builtin/gc.c b/builtin/gc.c\nindex 4403827481..5f877b097a 100644\n--- a/builtin/gc.c\n+++ b/builtin/gc.c\n@@ -1001,6 +1001,77 @@ static int maintenance_task_loose_objects(struct maintenance_run_opts *opts)\n \treturn prune_packed(opts) || pack_loose(opts);\n }\n \n+static int multi_pack_index_write(struct maintenance_run_opts *opts)\n+{\n+\tstruct child_process child = CHILD_PROCESS_INIT;\n+\n+\tchild.git_cmd = 1;\n+\tstrvec_pushl(&child.args, \"multi-pack-index\", \"write\", NULL);\n+\n+\tif (opts->quiet)\n+\t\tstrvec_push(&child.args, \"--no-progress\");\n+\n+\tif (run_command(&child))\n+\t\treturn error(_(\"failed to write multi-pack-index\"));\n+\n+\treturn 0;\n+}\n+\n+static int multi_pack_index_expire(struct maintenance_run_opts *opts)\n+{\n+\tstruct child_process child = CHILD_PROCESS_INIT;\n+\n+\tchild.git_cmd = 1;\n+\tstrvec_pushl(&child.args, \"multi-pack-index\", \"expire\", NULL);\n+\n+\tif (opts->quiet)\n+\t\tstrvec_push(&child.args, \"--no-progress\");\n+\n+\tclose_object_store(the_repository->objects);\n+\n+\tif (run_command(&child))\n+\t\treturn error(_(\"'git multi-pack-index expire' failed\"));\n+\n+\treturn 0;\n+}\n+\n+static int multi_pack_index_repack(struct maintenance_run_opts *opts)\n+{\n+\tstruct child_process child = CHILD_PROCESS_INIT;\n+\n+\tchild.git_cmd = 1;\n+\tstrvec_pushl(&child.args, \"multi-pack-index\", \"repack\", NULL);\n+\n+\tif (opts->quiet)\n+\t\tstrvec_push(&child.args, \"--no-progress\");\n+\n+\tstrvec_push(&child.args, \"--batch-size=0\");\n+\n+\tclose_object_store(the_repository->objects);\n+\n+\tif (run_command(&child))\n+\t\treturn error(_(\"'git multi-pack-index repack' failed\"));\n+\n+\treturn 0;\n+}\n+\n+static int maintenance_task_incremental_repack(struct maintenance_run_opts *opts)\n+{\n+\tprepare_repo_settings(the_repository);\n+\tif (!the_repository->settings.core_multi_pack_index) {\n+\t\twarning(_(\"skipping incremental-repack task because core.multiPackIndex is disabled\"));\n+\t\treturn 0;\n+\t}\n+\n+\tif (multi_pack_index_write(opts))\n+\t\treturn 1;\n+\tif (multi_pack_index_expire(opts))\n+\t\treturn 1;\n+\tif (multi_pack_index_repack(opts))\n+\t\treturn 1;\n+\treturn 0;\n+}\n+\n typedef int maintenance_task_fn(struct maintenance_run_opts *opts);\n \n /*\n@@ -1023,6 +1094,7 @@ struct maintenance_task {\n enum maintenance_task_label {\n \tTASK_PREFETCH,\n \tTASK_LOOSE_OBJECTS,\n+\tTASK_INCREMENTAL_REPACK,\n \tTASK_GC,\n \tTASK_COMMIT_GRAPH,\n \n@@ -1040,6 +1112,10 @@ static struct maintenance_task tasks[] = {\n \t\tmaintenance_task_loose_objects,\n \t\tloose_object_auto_condition,\n \t},\n+\t[TASK_INCREMENTAL_REPACK] = {\n+\t\t\"incremental-repack\",\n+\t\tmaintenance_task_incremental_repack,\n+\t},\n \t[TASK_GC] = {\n \t\t\"gc\",\n \t\tmaintenance_task_gc,\ndiff --git a/t/t5319-multi-pack-index.sh b/t/t5319-multi-pack-index.sh\nindex ec87f616c6..2f942ee1fa 100755\n--- a/t/t5319-multi-pack-index.sh\n+++ b/t/t5319-multi-pack-index.sh\n@@ -3,6 +3,7 @@\n test_description='multi-pack-indexes'\n . ./test-lib.sh\n \n+GIT_TEST_MULTI_PACK_INDEX=0\n objdir=.git/objects\n \n midx_read_expect () {\ndiff --git a/t/t7900-maintenance.sh b/t/t7900-maintenance.sh\nindex 27565c55a2..a2db2291b0 100755\n--- a/t/t7900-maintenance.sh\n+++ b/t/t7900-maintenance.sh\n@@ -5,6 +5,7 @@ test_description='git maintenance builtin'\n . ./test-lib.sh\n \n GIT_TEST_COMMIT_GRAPH=0\n+GIT_TEST_MULTI_PACK_INDEX=0\n \n test_expect_success 'help text' '\n \ttest_expect_code 129 git maintenance -h 2>err &&\n@@ -149,4 +150,41 @@ test_expect_success 'maintenance.loose-objects.auto' '\n \ttest_subcommand git prune-packed --quiet <trace-loC\n '\n \n+test_expect_success 'incremental-repack task' '\n+\tpackDir=.git/objects/pack &&\n+\tfor i in $(test_seq 1 5)\n+\tdo\n+\t\ttest_commit $i || return 1\n+\tdone &&\n+\n+\t# Create three disjoint pack-files with size BIG, small, small.\n+\techo HEAD~2 | git pack-objects --revs $packDir/test-1 &&\n+\ttest_tick &&\n+\tgit pack-objects --revs $packDir/test-2 <<-\\EOF &&\n+\tHEAD~1\n+\t^HEAD~2\n+\tEOF\n+\ttest_tick &&\n+\tgit pack-objects --revs $packDir/test-3 <<-\\EOF &&\n+\tHEAD\n+\t^HEAD~1\n+\tEOF\n+\trm -f $packDir/pack-* &&\n+\trm -f $packDir/loose-* &&\n+\tls $packDir/*.pack >packs-before &&\n+\ttest_line_count = 3 packs-before &&\n+\n+\t# the job repacks the two into a new pack, but does not\n+\t# delete the old ones.\n+\tgit maintenance run --task=incremental-repack &&\n+\tls $packDir/*.pack >packs-between &&\n+\ttest_line_count = 4 packs-between &&\n+\n+\t# the job deletes the two old packs, and does not write\n+\t# a new one because only one pack remains.\n+\tgit maintenance run --task=incremental-repack &&\n+\tls .git/objects/pack/*.pack >packs-after &&\n+\ttest_line_count = 1 packs-after\n+'\n+\n test_done\n-- \ngitgitgadget\n\n"},{"id":"406371","messageId":"xmqqh7rllos3.fsf@gitster.c.googlers.com","threadId":"54001","inReplyTo":"931fff4883a3da26a296af69a9c5ccc3a5037d71.1601037218.git.gitgitgadget@gmail.com","subject":"Re: [PATCH v4 3/8] maintenance: create auto condition for loose-objects","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2020-09-25T18:00:44Z","receivedAt":"2020-09-25T18:00:55Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Derrick Stolee via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> diff --git a/t/t7900-maintenance.sh b/t/t7900-maintenance.sh\n> index b3fc7c8670..27565c55a2 100755\n> --- a/t/t7900-maintenance.sh\n> +++ b/t/t7900-maintenance.sh\n> @@ -127,4 +127,26 @@ test_expect_success 'loose-objects task' '\n>  \ttest_cmp packs-between packs-after\n>  '\n>  \n> +test_expect_success 'maintenance.loose-objects.auto' '\n> +\tgit repack -adk &&\n> +\tGIT_TRACE2_EVENT=\"$(pwd)/trace-lo1.txt\" \\\n> +\t\tgit -c maintenance.loose-objects.auto=1 maintenance \\\n> +\t\trun --auto --task=loose-objects 2>/dev/null &&\n> +\ttest_subcommand ! git prune-packed --quiet <trace-lo1.txt &&\n> +\tprintf data-A | git hash-object -t blob --stdin -w &&\n> +\tGIT_TRACE2_EVENT=\"$(pwd)/trace-loA\" \\\n> +\t\tgit -c maintenance.loose-objects.auto=2 \\\n> +\t\tmaintenance run --auto --task=loose-objects 2>/dev/null &&\n> +\ttest_subcommand ! git prune-packed --quiet <trace-loA &&\n> +\tprintf data-B | git hash-object -t blob --stdin -w &&\n\nIs it essential for the purpose of the test somehow that the data\nused for the test are all incomplete files that lack the end-of-line\nat the end of the file?  Use of 'printf' sends such a signal to\nconfuse readers.\n\nUse of test_write_lines to write a single line may feel overkill,\nbut it may be less cryptic, as newer parts of testsuite are\nencouraged to use it over 'echo' and raw 'printf'.\n\n> +\tGIT_TRACE2_EVENT=\"$(pwd)/trace-loB\" \\\n> +\t\tgit -c maintenance.loose-objects.auto=2 \\\n> +\t\tmaintenance run --auto --task=loose-objects 2>/dev/null &&\n> +\ttest_subcommand git prune-packed --quiet <trace-loB &&\n> +\tGIT_TRACE2_EVENT=\"$(pwd)/trace-loC\" \\\n> +\t\tgit -c maintenance.loose-objects.auto=2 \\\n> +\t\tmaintenance run --auto --task=loose-objects 2>/dev/null &&\n> +\ttest_subcommand git prune-packed --quiet <trace-loC\n> +'\n\n"},{"id":"406378","messageId":"d9b60edf-a06a-85ec-6676-a2e6b1a9a506@gmail.com","threadId":"54001","inReplyTo":"xmqqh7rllos3.fsf@gitster.c.googlers.com","subject":"Re: [PATCH v4 3/8] maintenance: create auto condition for loose-objects","fromName":"Derrick Stolee","fromEmail":"stolee@gmail.com","sentAt":"2020-09-25T18:43:09Z","receivedAt":"2020-09-25T18:43:15Z","isPatch":true,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"On 9/25/2020 2:00 PM, Junio C Hamano wrote:\n> \"Derrick Stolee via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n>> +\tprintf data-B | git hash-object -t blob --stdin -w &&\n> \n> Is it essential for the purpose of the test somehow that the data\n> used for the test are all incomplete files that lack the end-of-line\n> at the end of the file?  Use of 'printf' sends such a signal to\n> confuse readers.\n> \n> Use of test_write_lines to write a single line may feel overkill,\n> but it may be less cryptic, as newer parts of testsuite are\n> encouraged to use it over 'echo' and raw 'printf'.\n\nI suppose it could be more standard. It's not particularly\nimportant what the data is here, so lacking a newline seems\ninnocuous enough to me. I'm happy to agree with standards\nelsewhere to avoid being a bad example.\n\nA simple s/printf/test_write_lines/ appears to do the trick:\n\ndiff --git a/t/t7900-maintenance.sh b/t/t7900-maintenance.sh\nindex f259485990..a1bd0029c6 100755\n--- a/t/t7900-maintenance.sh\n+++ b/t/t7900-maintenance.sh\n@@ -147,12 +147,12 @@ test_expect_success 'maintenance.loose-objects.auto' '\n                git -c maintenance.loose-objects.auto=1 maintenance \\\n                run --auto --task=loose-objects 2>/dev/null &&\n        test_subcommand ! git prune-packed --quiet <trace-lo1.txt &&\n-       printf data-A | git hash-object -t blob --stdin -w &&\n+       test_write_lines data-A | git hash-object -t blob --stdin -w &&\n        GIT_TRACE2_EVENT=\"$(pwd)/trace-loA\" \\\n                git -c maintenance.loose-objects.auto=2 \\\n                maintenance run --auto --task=loose-objects 2>/dev/null &&\n        test_subcommand ! git prune-packed --quiet <trace-loA &&\n-       printf data-B | git hash-object -t blob --stdin -w &&\n+       test_write_lines data-B | git hash-object -t blob --stdin -w &&\n        GIT_TRACE2_EVENT=\"$(pwd)/trace-loB\" \\\n                git -c maintenance.loose-objects.auto=2 \\\n                maintenance run --auto --task=loose-objects 2>/dev/null &&\n\nThanks,\n-Stolee\n"}]}