Skip to content

Commit

Permalink
remote: prefetch config
Browse files Browse the repository at this point in the history
Large repositories often contain numerous branches and refs, many of
which individual users may not need. This commit introduces a new
configuration option (`remote.<remote>.prefetch`) to allow
users to specify which remotes to prefetch during
the maintenance task.

Key behaviors:
1. If `remote.<remote>.prefetch` is unset or true, running
   `git-maintenance` will prefetch all refs for the remote.
2. If `remote.<remote>.prefetch` is set to false, the remote
   will be ignored for prefetching.

In a future change, we could also allow restricting the refs that are
prefetched per remote using the `prefetchref` config option per remote.

Both of these options in unison would allow users to optimize their
prefetch operations, reducing network traffic and disk usage.

Signed-off-by: Shubham Kanodia <shubham.kanodia10@gmail.com>
  • Loading branch information
pastelsky committed Sep 5, 2024
1 parent 2e7b89e commit 5d33b4e
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 3 deletions.
5 changes: 5 additions & 0 deletions Documentation/config/remote.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ remote.<name>.fetch::
The default set of "refspec" for linkgit:git-fetch[1]. See
linkgit:git-fetch[1].

remote.<name>.prefetch::
If false, refs from the remote would not be prefetched for
the prefetch task in linkgit:git-maintenance[1]. If not set,
the value is assumed to be true.

remote.<name>.push::
The default set of "refspec" for linkgit:git-push[1]. See
linkgit:git-push[1].
Expand Down
7 changes: 4 additions & 3 deletions Documentation/git-maintenance.txt
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,10 @@ commit-graph::

prefetch::
The `prefetch` task updates the object directory with the latest
objects from all registered remotes. For each remote, a `git fetch`
command is run. The configured refspec is modified to place all
requested refs within `refs/prefetch/`. Also, tags are not updated.
objects from all registered remotes unless they've disabled prefetch
using `remote.<remote>.prefetch` set to `false`. For each such remote,
a `git fetch` command is run. The configured refspec is modified to place
all requested refs within `refs/prefetch/`. Also, tags are not updated.
+
This is done to avoid disrupting the remote-tracking branches. The end users
expect these refs to stay unmoved unless they initiate a fetch. However,
Expand Down
3 changes: 3 additions & 0 deletions builtin/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,9 @@ static int fetch_remote(struct remote *remote, void *cbdata)
if (remote->skip_default_update)
return 0;

if (remote->prefetch == 0)
return 0;

child.git_cmd = 1;
strvec_pushl(&child.args, "fetch", remote->name,
"--prefetch", "--prune", "--no-tags",
Expand Down
3 changes: 3 additions & 0 deletions remote.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ static struct remote *make_remote(struct remote_state *remote_state,
CALLOC_ARRAY(ret, 1);
ret->prune = -1; /* unspecified */
ret->prune_tags = -1; /* unspecified */
ret->prefetch = -1; /* unspecified */
ret->name = xstrndup(name, len);
refspec_init(&ret->push, REFSPEC_PUSH);
refspec_init(&ret->fetch, REFSPEC_FETCH);
Expand Down Expand Up @@ -456,6 +457,8 @@ static int handle_config(const char *key, const char *value,
remote->prune = git_config_bool(key, value);
else if (!strcmp(subkey, "prunetags"))
remote->prune_tags = git_config_bool(key, value);
else if (!strcmp(subkey, "prefetch"))
remote->prefetch = git_config_bool(key, value);
else if (!strcmp(subkey, "url")) {
if (!value)
return config_error_nonbool(key);
Expand Down
9 changes: 9 additions & 0 deletions remote.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ struct remote {

struct refspec fetch;

/*
* The setting for whether to prefetch from a remote
* when a fetch is invoked with a prefetch flag.
* -1 = unset
* 0 = don't prefetch from this remote
* 1 = prefetch from this remote
*/
int prefetch;

/*
* The setting for whether to fetch tags (as a separate rule from the
* configured refspecs);
Expand Down
42 changes: 42 additions & 0 deletions t/t7900-maintenance.sh
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,48 @@ test_expect_success 'prefetch multiple remotes' '
test_subcommand git fetch remote2 $fetchargs <skip-remote1.txt
'

test_expect_success 'prefetch respects remote.*.prefetch config' '
test_create_repo prefetch-test-config &&
(
cd prefetch-test-config &&
test_commit initial &&
test_create_repo clone1 &&
test_create_repo clone2 &&
test_create_repo clone3 &&
git remote add remote1 "file://$(pwd)/clone1" &&
git remote add remote2 "file://$(pwd)/clone2" &&
git remote add remote3 "file://$(pwd)/clone3" &&
git config remote.remote1.prefetch false &&
git config remote.remote2.prefetch true &&
# remote3 is left unset
# Make changes in all clones
git -C clone1 switch -c one &&
git -C clone2 switch -c two &&
git -C clone3 switch -c three &&
test_commit -C clone1 one &&
test_commit -C clone2 two &&
test_commit -C clone3 three &&
# Run maintenance prefetch task
GIT_TRACE2_EVENT="$(pwd)/prefetch.txt" git maintenance run --task=prefetch 2>/dev/null &&
# Check that if remotes were prefetched properly
fetchargs="--prefetch --prune --no-tags --no-write-fetch-head --recurse-submodules=no --quiet" &&
test_subcommand ! git fetch remote1 $fetchargs <prefetch.txt &&
test_subcommand git fetch remote2 $fetchargs <prefetch.txt &&
test_subcommand git fetch remote3 $fetchargs <prefetch.txt &&
# Verify that changes are in the prefetch refs for remote2 and remote3, but not remote1
test_must_fail git rev-parse refs/prefetch/remotes/remote1/one &&
git fetch --all &&
test_cmp_rev refs/remotes/remote2/two refs/prefetch/remotes/remote2/two &&
test_cmp_rev refs/remotes/remote3/three refs/prefetch/remotes/remote3/three
)
'

test_expect_success 'loose-objects task' '
# Repack everything so we know the state of the object dir
git repack -adk &&
Expand Down

0 comments on commit 5d33b4e

Please sign in to comment.