Skip to content

Commit 28cb5e6

Browse files
derrickstoleegitster
authored andcommitted
maintenance: add prefetch task
When working with very large repositories, an incremental 'git fetch' command can download a large amount of data. If there are many other users pushing to a common repo, then this data can rival the initial pack-file size of a 'git clone' of a medium-size repo. Users may want to keep the data on their local repos as close as possible to the data on the remote repos by fetching periodically in the background. This can break up a large daily fetch into several smaller hourly fetches. The task is called "prefetch" because it is work done in advance of a foreground fetch to make that 'git fetch' command much faster. However, if we simply ran 'git fetch <remote>' in the background, then the user running a foreground 'git fetch <remote>' would lose some important feedback when a new branch appears or an existing branch updates. This is especially true if a remote branch is force-updated and this isn't noticed by the user because it occurred in the background. Further, the functionality of 'git push --force-with-lease' becomes suspect. When running 'git fetch <remote> <options>' in the background, use the following options for careful updating: 1. --no-tags prevents getting a new tag when a user wants to see the new tags appear in their foreground fetches. 2. --refmap= removes the configured refspec which usually updates refs/remotes/<remote>/* with the refs advertised by the remote. While this looks confusing, this was documented and tested by b40a502 (fetch: document and test --refmap="", 2020-01-21), including this sentence in the documentation: Providing an empty `<refspec>` to the `--refmap` option causes Git to ignore the configured refspecs and rely entirely on the refspecs supplied as command-line arguments. 3. By adding a new refspec "+refs/heads/*:refs/prefetch/<remote>/*" we can ensure that we actually load the new values somewhere in our refspace while not updating refs/heads or refs/remotes. By storing these refs here, the commit-graph job will update the commit-graph with the commits from these hidden refs. 4. --prune will delete the refs/prefetch/<remote> refs that no longer appear on the remote. 5. --no-write-fetch-head prevents updating FETCH_HEAD. We've been using this step as a critical background job in Scalar [1] (and VFS for Git). This solved a pain point that was showing up in user reports: fetching was a pain! Users do not like waiting to download the data that was created while they were away from their machines. After implementing background fetch, the foreground fetch commands sped up significantly because they mostly just update refs and download a small amount of new data. The effect is especially dramatic when paried with --no-show-forced-udpates (through fetch.showForcedUpdates=false). [1] https://github.com/microsoft/scalar/blob/master/Scalar.Common/Maintenance/FetchStep.cs Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 25914c4 commit 28cb5e6

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

Documentation/git-maintenance.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,21 @@ commit-graph::
4747
`commit-graph-chain` file. They will be deleted by a later run based
4848
on the expiration delay.
4949

50+
prefetch::
51+
The `prefetch` task updates the object directory with the latest
52+
objects from all registered remotes. For each remote, a `git fetch`
53+
command is run. The refmap is custom to avoid updating local or remote
54+
branches (those in `refs/heads` or `refs/remotes`). Instead, the
55+
remote refs are stored in `refs/prefetch/<remote>/`. Also, tags are
56+
not updated.
57+
+
58+
This is done to avoid disrupting the remote-tracking branches. The end users
59+
expect these refs to stay unmoved unless they initiate a fetch. With prefetch
60+
task, however, the objects necessary to complete a later real fetch would
61+
already be obtained, so the real fetch would go faster. In the ideal case,
62+
it will just become an update to bunch of remote-tracking branches without
63+
any object transfer.
64+
5065
gc::
5166
Clean up unnecessary files and optimize the local repository. "GC"
5267
stands for "garbage collection," but this task performs many

builtin/gc.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "tree.h"
3030
#include "promisor-remote.h"
3131
#include "refs.h"
32+
#include "remote.h"
3233

3334
#define FAILED_RUN "failed to run %s"
3435

@@ -816,6 +817,51 @@ static int maintenance_task_commit_graph(struct maintenance_run_opts *opts)
816817
return 0;
817818
}
818819

820+
static int fetch_remote(const char *remote, struct maintenance_run_opts *opts)
821+
{
822+
struct child_process child = CHILD_PROCESS_INIT;
823+
824+
child.git_cmd = 1;
825+
strvec_pushl(&child.args, "fetch", remote, "--prune", "--no-tags",
826+
"--no-write-fetch-head", "--recurse-submodules=no",
827+
"--refmap=", NULL);
828+
829+
if (opts->quiet)
830+
strvec_push(&child.args, "--quiet");
831+
832+
strvec_pushf(&child.args, "+refs/heads/*:refs/prefetch/%s/*", remote);
833+
834+
return !!run_command(&child);
835+
}
836+
837+
static int append_remote(struct remote *remote, void *cbdata)
838+
{
839+
struct string_list *remotes = (struct string_list *)cbdata;
840+
841+
string_list_append(remotes, remote->name);
842+
return 0;
843+
}
844+
845+
static int maintenance_task_prefetch(struct maintenance_run_opts *opts)
846+
{
847+
int result = 0;
848+
struct string_list_item *item;
849+
struct string_list remotes = STRING_LIST_INIT_DUP;
850+
851+
if (for_each_remote(append_remote, &remotes)) {
852+
error(_("failed to fill remotes"));
853+
result = 1;
854+
goto cleanup;
855+
}
856+
857+
for_each_string_list_item(item, &remotes)
858+
result |= fetch_remote(item->string, opts);
859+
860+
cleanup:
861+
string_list_clear(&remotes, 0);
862+
return result;
863+
}
864+
819865
static int maintenance_task_gc(struct maintenance_run_opts *opts)
820866
{
821867
struct child_process child = CHILD_PROCESS_INIT;
@@ -854,6 +900,7 @@ struct maintenance_task {
854900
};
855901

856902
enum maintenance_task_label {
903+
TASK_PREFETCH,
857904
TASK_GC,
858905
TASK_COMMIT_GRAPH,
859906

@@ -862,6 +909,10 @@ enum maintenance_task_label {
862909
};
863910

864911
static struct maintenance_task tasks[] = {
912+
[TASK_PREFETCH] = {
913+
"prefetch",
914+
maintenance_task_prefetch,
915+
},
865916
[TASK_GC] = {
866917
"gc",
867918
maintenance_task_gc,

t/t7900-maintenance.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,30 @@ test_expect_success 'run --task duplicate' '
6262
test_i18ngrep "cannot be selected multiple times" err
6363
'
6464

65+
test_expect_success 'run --task=prefetch with no remotes' '
66+
git maintenance run --task=prefetch 2>err &&
67+
test_must_be_empty err
68+
'
69+
70+
test_expect_success 'prefetch multiple remotes' '
71+
git clone . clone1 &&
72+
git clone . clone2 &&
73+
git remote add remote1 "file://$(pwd)/clone1" &&
74+
git remote add remote2 "file://$(pwd)/clone2" &&
75+
git -C clone1 switch -c one &&
76+
git -C clone2 switch -c two &&
77+
test_commit -C clone1 one &&
78+
test_commit -C clone2 two &&
79+
GIT_TRACE2_EVENT="$(pwd)/run-prefetch.txt" git maintenance run --task=prefetch 2>/dev/null &&
80+
fetchargs="--prune --no-tags --no-write-fetch-head --recurse-submodules=no --refmap= --quiet" &&
81+
test_subcommand git fetch remote1 $fetchargs +refs/heads/\\*:refs/prefetch/remote1/\\* <run-prefetch.txt &&
82+
test_subcommand git fetch remote2 $fetchargs +refs/heads/\\*:refs/prefetch/remote2/\\* <run-prefetch.txt &&
83+
test_path_is_missing .git/refs/remotes &&
84+
git log prefetch/remote1/one &&
85+
git log prefetch/remote2/two &&
86+
git fetch --all &&
87+
test_cmp_rev refs/remotes/remote1/one refs/prefetch/remote1/one &&
88+
test_cmp_rev refs/remotes/remote2/two refs/prefetch/remote2/two
89+
'
90+
6591
test_done

0 commit comments

Comments
 (0)