Skip to content

Commit 737c5a9

Browse files
Michael Schubertgitster
Michael Schubert
authored andcommitted
fetch: make --prune configurable
Without "git fetch --prune", remote-tracking branches for a branch the other side already has removed will stay forever. Some people want to always run "git fetch --prune". To accommodate users who want to either prune always or when fetching from a particular remote, add two new configuration variables "fetch.prune" and "remote.<name>.prune": - "fetch.prune" allows to enable prune for all fetch operations. - "remote.<name>.prune" allows to change the behaviour per remote. The latter will naturally override the former, and the --[no-]prune option from the command line will override the configured default. Since --prune is a potentially destructive operation (Git doesn't keep reflogs for deleted references yet), we don't want to prune without users consent, so this configuration will not be on by default. Helped-by: Junio C Hamano <[email protected]> Signed-off-by: Michael Schubert <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent edca415 commit 737c5a9

File tree

5 files changed

+130
-5
lines changed

5 files changed

+130
-5
lines changed

Documentation/config.txt

+10
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,10 @@ fetch.unpackLimit::
10491049
especially on slow filesystems. If not set, the value of
10501050
`transfer.unpackLimit` is used instead.
10511051

1052+
fetch.prune::
1053+
If true, fetch will automatically behave as if the `--prune`
1054+
option was given on the command line. See also `remote.<name>.prune`.
1055+
10521056
format.attach::
10531057
Enable multipart/mixed attachments as the default for
10541058
'format-patch'. The value can also be a double quoted string
@@ -1984,6 +1988,12 @@ remote.<name>.vcs::
19841988
Setting this to a value <vcs> will cause Git to interact with
19851989
the remote with the git-remote-<vcs> helper.
19861990

1991+
remote.<name>.prune::
1992+
When set to true, fetching from this remote by default will also
1993+
remove any remote-tracking branches which no longer exist on the
1994+
remote (as if the `--prune` option was give on the command line).
1995+
Overrides `fetch.prune` settings, if any.
1996+
19871997
remotes.<group>::
19881998
The list of remotes which are fetched by "git remote update
19891999
<group>". See linkgit:git-remote[1].

builtin/fetch.c

+34-5
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ enum {
3030
TAGS_SET = 2
3131
};
3232

33-
static int all, append, dry_run, force, keep, multiple, prune, update_head_ok, verbosity;
33+
static int fetch_prune_config = -1; /* unspecified */
34+
static int prune = -1; /* unspecified */
35+
#define PRUNE_BY_DEFAULT 0 /* do we prune by default? */
36+
37+
static int all, append, dry_run, force, keep, multiple, update_head_ok, verbosity;
3438
static int progress = -1, recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
3539
static int tags = TAGS_DEFAULT, unshallow;
3640
static const char *depth;
@@ -54,6 +58,15 @@ static int option_parse_recurse_submodules(const struct option *opt,
5458
return 0;
5559
}
5660

61+
static int git_fetch_config(const char *k, const char *v, void *cb)
62+
{
63+
if (!strcmp(k, "fetch.prune")) {
64+
fetch_prune_config = git_config_bool(k, v);
65+
return 0;
66+
}
67+
return 0;
68+
}
69+
5770
static struct option builtin_fetch_options[] = {
5871
OPT__VERBOSITY(&verbosity),
5972
OPT_BOOLEAN(0, "all", &all,
@@ -69,8 +82,8 @@ static struct option builtin_fetch_options[] = {
6982
N_("fetch all tags and associated objects"), TAGS_SET),
7083
OPT_SET_INT('n', NULL, &tags,
7184
N_("do not fetch all tags (--no-tags)"), TAGS_UNSET),
72-
OPT_BOOLEAN('p', "prune", &prune,
73-
N_("prune remote-tracking branches no longer on remote")),
85+
OPT_BOOL('p', "prune", &prune,
86+
N_("prune remote-tracking branches no longer on remote")),
7487
{ OPTION_CALLBACK, 0, "recurse-submodules", NULL, N_("on-demand"),
7588
N_("control recursive fetching of submodules"),
7689
PARSE_OPT_OPTARG, option_parse_recurse_submodules },
@@ -739,7 +752,10 @@ static int do_fetch(struct transport *transport,
739752
return 1;
740753
}
741754
if (prune) {
742-
/* If --tags was specified, pretend the user gave us the canonical tags refspec */
755+
/*
756+
* If --tags was specified, pretend that the user gave us
757+
* the canonical tags refspec
758+
*/
743759
if (tags == TAGS_SET) {
744760
const char *tags_str = "refs/tags/*:refs/tags/*";
745761
struct refspec *tags_refspec, *refspec;
@@ -848,7 +864,7 @@ static void add_options_to_argv(struct argv_array *argv)
848864
{
849865
if (dry_run)
850866
argv_array_push(argv, "--dry-run");
851-
if (prune)
867+
if (prune > 0)
852868
argv_array_push(argv, "--prune");
853869
if (update_head_ok)
854870
argv_array_push(argv, "--update-head-ok");
@@ -916,6 +932,17 @@ static int fetch_one(struct remote *remote, int argc, const char **argv)
916932
"remote name from which new revisions should be fetched."));
917933

918934
transport = transport_get(remote, NULL);
935+
936+
if (prune < 0) {
937+
/* no command line request */
938+
if (0 <= transport->remote->prune)
939+
prune = transport->remote->prune;
940+
else if (0 <= fetch_prune_config)
941+
prune = fetch_prune_config;
942+
else
943+
prune = PRUNE_BY_DEFAULT;
944+
}
945+
919946
transport_set_verbosity(transport, verbosity, progress);
920947
if (upload_pack)
921948
set_option(TRANS_OPT_UPLOADPACK, upload_pack);
@@ -973,6 +1000,8 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
9731000
for (i = 1; i < argc; i++)
9741001
strbuf_addf(&default_rla, " %s", argv[i]);
9751002

1003+
git_config(git_fetch_config, NULL);
1004+
9761005
argc = parse_options(argc, argv, prefix,
9771006
builtin_fetch_options, builtin_fetch_usage, 0);
9781007

remote.c

+3
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ static struct remote *make_remote(const char *name, int len)
148148
}
149149

150150
ret = xcalloc(1, sizeof(struct remote));
151+
ret->prune = -1; /* unspecified */
151152
ALLOC_GROW(remotes, remotes_nr + 1, remotes_alloc);
152153
remotes[remotes_nr++] = ret;
153154
if (len)
@@ -419,6 +420,8 @@ static int handle_config(const char *key, const char *value, void *cb)
419420
remote->skip_default_update = git_config_bool(key, value);
420421
else if (!strcmp(subkey, ".skipfetchall"))
421422
remote->skip_default_update = git_config_bool(key, value);
423+
else if (!strcmp(subkey, ".prune"))
424+
remote->prune = git_config_bool(key, value);
422425
else if (!strcmp(subkey, ".url")) {
423426
const char *v;
424427
if (git_config_string(&v, key, value))

remote.h

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ struct remote {
4040
int fetch_tags;
4141
int skip_default_update;
4242
int mirror;
43+
int prune;
4344

4445
const char *receivepack;
4546
const char *uploadpack;

t/t5510-fetch.sh

+82
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,88 @@ test_expect_success "should be able to fetch with duplicate refspecs" '
471471
)
472472
'
473473

474+
# configured prune tests
475+
476+
set_config_tristate () {
477+
# var=$1 val=$2
478+
case "$2" in
479+
unset) test_unconfig "$1" ;;
480+
*) git config "$1" "$2" ;;
481+
esac
482+
}
483+
484+
test_configured_prune () {
485+
fetch_prune=$1 remote_origin_prune=$2 cmdline=$3 expected=$4
486+
487+
test_expect_success "prune fetch.prune=$1 remote.origin.prune=$2${3:+ $3}; $4" '
488+
# make sure a newbranch is there in . and also in one
489+
git branch -f newbranch &&
490+
(
491+
cd one &&
492+
test_unconfig fetch.prune &&
493+
test_unconfig remote.origin.prune &&
494+
git fetch &&
495+
git rev-parse --verify refs/remotes/origin/newbranch
496+
)
497+
498+
# now remove it
499+
git branch -d newbranch &&
500+
501+
# then test
502+
(
503+
cd one &&
504+
set_config_tristate fetch.prune $fetch_prune &&
505+
set_config_tristate remote.origin.prune $remote_origin_prune &&
506+
507+
git fetch $cmdline &&
508+
case "$expected" in
509+
pruned)
510+
test_must_fail git rev-parse --verify refs/remotes/origin/newbranch
511+
;;
512+
kept)
513+
git rev-parse --verify refs/remotes/origin/newbranch
514+
;;
515+
esac
516+
)
517+
'
518+
}
519+
520+
test_configured_prune unset unset "" kept
521+
test_configured_prune unset unset "--no-prune" kept
522+
test_configured_prune unset unset "--prune" pruned
523+
524+
test_configured_prune false unset "" kept
525+
test_configured_prune false unset "--no-prune" kept
526+
test_configured_prune false unset "--prune" pruned
527+
528+
test_configured_prune true unset "" pruned
529+
test_configured_prune true unset "--prune" pruned
530+
test_configured_prune true unset "--no-prune" kept
531+
532+
test_configured_prune unset false "" kept
533+
test_configured_prune unset false "--no-prune" kept
534+
test_configured_prune unset false "--prune" pruned
535+
536+
test_configured_prune false false "" kept
537+
test_configured_prune false false "--no-prune" kept
538+
test_configured_prune false false "--prune" pruned
539+
540+
test_configured_prune true false "" kept
541+
test_configured_prune true false "--prune" pruned
542+
test_configured_prune true false "--no-prune" kept
543+
544+
test_configured_prune unset true "" pruned
545+
test_configured_prune unset true "--no-prune" kept
546+
test_configured_prune unset true "--prune" pruned
547+
548+
test_configured_prune false true "" pruned
549+
test_configured_prune false true "--no-prune" kept
550+
test_configured_prune false true "--prune" pruned
551+
552+
test_configured_prune true true "" pruned
553+
test_configured_prune true true "--prune" pruned
554+
test_configured_prune true true "--no-prune" kept
555+
474556
test_expect_success 'all boundary commits are excluded' '
475557
test_commit base &&
476558
test_commit oneside &&

0 commit comments

Comments
 (0)