Skip to content

Commit a5e10f8

Browse files
committed
Merge branch 'ms/fetch-prune-configuration'
Allow fetch.prune and remote.*.prune configuration variables to be set, and "git fetch" to behave as if "--prune" is given. "git fetch" that honors remote.*.prune is fine, but I wonder if we should somehow make "git push" aware of it as well. Perhaps remote.*.prune should not be just a boolean, but a 4-way "none", "push", "fetch", "both"? * ms/fetch-prune-configuration: fetch: make --prune configurable
2 parents bd5424f + 737c5a9 commit a5e10f8

File tree

5 files changed

+128
-3
lines changed

5 files changed

+128
-3
lines changed

Documentation/config.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,6 +1061,10 @@ fetch.unpackLimit::
10611061
especially on slow filesystems. If not set, the value of
10621062
`transfer.unpackLimit` is used instead.
10631063

1064+
fetch.prune::
1065+
If true, fetch will automatically behave as if the `--prune`
1066+
option was given on the command line. See also `remote.<name>.prune`.
1067+
10641068
format.attach::
10651069
Enable multipart/mixed attachments as the default for
10661070
'format-patch'. The value can also be a double quoted string
@@ -2024,6 +2028,12 @@ remote.<name>.vcs::
20242028
Setting this to a value <vcs> will cause Git to interact with
20252029
the remote with the git-remote-<vcs> helper.
20262030

2031+
remote.<name>.prune::
2032+
When set to true, fetching from this remote by default will also
2033+
remove any remote-tracking branches which no longer exist on the
2034+
remote (as if the `--prune` option was give on the command line).
2035+
Overrides `fetch.prune` settings, if any.
2036+
20272037
remotes.<group>::
20282038
The list of remotes which are fetched by "git remote update
20292039
<group>". See linkgit:git-remote[1].

builtin/fetch.c

Lines changed: 32 additions & 3 deletions
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_BOOL(0, "all", &all,
@@ -771,7 +784,10 @@ static int do_fetch(struct transport *transport,
771784
goto cleanup;
772785
}
773786
if (prune) {
774-
/* If --tags was specified, pretend the user gave us the canonical tags refspec */
787+
/*
788+
* If --tags was specified, pretend that the user gave us
789+
* the canonical tags refspec
790+
*/
775791
if (tags == TAGS_SET) {
776792
const char *tags_str = "refs/tags/*:refs/tags/*";
777793
struct refspec *tags_refspec, *refspec;
@@ -882,7 +898,7 @@ static void add_options_to_argv(struct argv_array *argv)
882898
{
883899
if (dry_run)
884900
argv_array_push(argv, "--dry-run");
885-
if (prune)
901+
if (prune > 0)
886902
argv_array_push(argv, "--prune");
887903
if (update_head_ok)
888904
argv_array_push(argv, "--update-head-ok");
@@ -950,6 +966,17 @@ static int fetch_one(struct remote *remote, int argc, const char **argv)
950966
"remote name from which new revisions should be fetched."));
951967

952968
transport = transport_get(remote, NULL);
969+
970+
if (prune < 0) {
971+
/* no command line request */
972+
if (0 <= transport->remote->prune)
973+
prune = transport->remote->prune;
974+
else if (0 <= fetch_prune_config)
975+
prune = fetch_prune_config;
976+
else
977+
prune = PRUNE_BY_DEFAULT;
978+
}
979+
953980
transport_set_verbosity(transport, verbosity, progress);
954981
if (upload_pack)
955982
set_option(TRANS_OPT_UPLOADPACK, upload_pack);
@@ -1007,6 +1034,8 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
10071034
for (i = 1; i < argc; i++)
10081035
strbuf_addf(&default_rla, " %s", argv[i]);
10091036

1037+
git_config(git_fetch_config, NULL);
1038+
10101039
argc = parse_options(argc, argv, prefix,
10111040
builtin_fetch_options, builtin_fetch_usage, 0);
10121041

remote.c

Lines changed: 3 additions & 0 deletions
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)
@@ -404,6 +405,8 @@ static int handle_config(const char *key, const char *value, void *cb)
404405
remote->skip_default_update = git_config_bool(key, value);
405406
else if (!strcmp(subkey, ".skipfetchall"))
406407
remote->skip_default_update = git_config_bool(key, value);
408+
else if (!strcmp(subkey, ".prune"))
409+
remote->prune = git_config_bool(key, value);
407410
else if (!strcmp(subkey, ".url")) {
408411
const char *v;
409412
if (git_config_string(&v, key, value))

remote.h

Lines changed: 1 addition & 0 deletions
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

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,88 @@ test_expect_success "should be able to fetch with duplicate refspecs" '
497497
)
498498
'
499499

500+
# configured prune tests
501+
502+
set_config_tristate () {
503+
# var=$1 val=$2
504+
case "$2" in
505+
unset) test_unconfig "$1" ;;
506+
*) git config "$1" "$2" ;;
507+
esac
508+
}
509+
510+
test_configured_prune () {
511+
fetch_prune=$1 remote_origin_prune=$2 cmdline=$3 expected=$4
512+
513+
test_expect_success "prune fetch.prune=$1 remote.origin.prune=$2${3:+ $3}; $4" '
514+
# make sure a newbranch is there in . and also in one
515+
git branch -f newbranch &&
516+
(
517+
cd one &&
518+
test_unconfig fetch.prune &&
519+
test_unconfig remote.origin.prune &&
520+
git fetch &&
521+
git rev-parse --verify refs/remotes/origin/newbranch
522+
)
523+
524+
# now remove it
525+
git branch -d newbranch &&
526+
527+
# then test
528+
(
529+
cd one &&
530+
set_config_tristate fetch.prune $fetch_prune &&
531+
set_config_tristate remote.origin.prune $remote_origin_prune &&
532+
533+
git fetch $cmdline &&
534+
case "$expected" in
535+
pruned)
536+
test_must_fail git rev-parse --verify refs/remotes/origin/newbranch
537+
;;
538+
kept)
539+
git rev-parse --verify refs/remotes/origin/newbranch
540+
;;
541+
esac
542+
)
543+
'
544+
}
545+
546+
test_configured_prune unset unset "" kept
547+
test_configured_prune unset unset "--no-prune" kept
548+
test_configured_prune unset unset "--prune" pruned
549+
550+
test_configured_prune false unset "" kept
551+
test_configured_prune false unset "--no-prune" kept
552+
test_configured_prune false unset "--prune" pruned
553+
554+
test_configured_prune true unset "" pruned
555+
test_configured_prune true unset "--prune" pruned
556+
test_configured_prune true unset "--no-prune" kept
557+
558+
test_configured_prune unset false "" kept
559+
test_configured_prune unset false "--no-prune" kept
560+
test_configured_prune unset false "--prune" pruned
561+
562+
test_configured_prune false false "" kept
563+
test_configured_prune false false "--no-prune" kept
564+
test_configured_prune false false "--prune" pruned
565+
566+
test_configured_prune true false "" kept
567+
test_configured_prune true false "--prune" pruned
568+
test_configured_prune true false "--no-prune" kept
569+
570+
test_configured_prune unset true "" pruned
571+
test_configured_prune unset true "--no-prune" kept
572+
test_configured_prune unset true "--prune" pruned
573+
574+
test_configured_prune false true "" pruned
575+
test_configured_prune false true "--no-prune" kept
576+
test_configured_prune false true "--prune" pruned
577+
578+
test_configured_prune true true "" pruned
579+
test_configured_prune true true "--prune" pruned
580+
test_configured_prune true true "--no-prune" kept
581+
500582
test_expect_success 'all boundary commits are excluded' '
501583
test_commit base &&
502584
test_commit oneside &&

0 commit comments

Comments
 (0)