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 <gitster@pobox.com> Signed-off-by: Michael Schubert <mschub@elegosoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Schubert committed Jul 13, 2013 at 11:36 UTC 737c5a9cde708d6995c765b7c2e95033edd0a896
5 files changed +130 -5
Documentation/config.txt
+10
@@ -1049,6 +1049,10 @@ fetch.unpackLimit::
1049 especially on slow filesystems. If not set, the value of
1050 `transfer.unpackLimit` is used instead.
1051
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 +
1056 format.attach::
1057 Enable multipart/mixed attachments as the default for
1058 'format-patch'. The value can also be a double quoted string
@@ -1984,6 +1988,12 @@ remote.<name>.vcs::
1988 Setting this to a value <vcs> will cause Git to interact with
1989 the remote with the git-remote-<vcs> helper.
1990
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 +
1997 remotes.<group>::
1998 The list of remotes which are fetched by "git remote update
1999 <group>". See linkgit:git-remote[1].
builtin/fetch.c
+34 -5
@@ -30,7 +30,11 @@ enum {
30 TAGS_SET = 2
31 };
32
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;
38 static int progress = -1, recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
39 static int tags = TAGS_DEFAULT, unshallow;
40 static const char *depth;
@@ -54,6 +58,15 @@ static int option_parse_recurse_submodules(const struct option *opt,
58 return 0;
59 }
60
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 +
70 static struct option builtin_fetch_options[] = {
71 OPT__VERBOSITY(&verbosity),
72 OPT_BOOLEAN(0, "all", &all,
@@ -69,8 +82,8 @@ static struct option builtin_fetch_options[] = {
82 N_("fetch all tags and associated objects"), TAGS_SET),
83 OPT_SET_INT('n', NULL, &tags,
84 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")),
87 { OPTION_CALLBACK, 0, "recurse-submodules", NULL, N_("on-demand"),
88 N_("control recursive fetching of submodules"),
89 PARSE_OPT_OPTARG, option_parse_recurse_submodules },
@@ -739,7 +752,10 @@ static int do_fetch(struct transport *transport,
752 return 1;
753 }
754 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 + */
759 if (tags == TAGS_SET) {
760 const char *tags_str = "refs/tags/*:refs/tags/*";
761 struct refspec *tags_refspec, *refspec;
@@ -848,7 +864,7 @@ static void add_options_to_argv(struct argv_array *argv)
864 {
865 if (dry_run)
866 argv_array_push(argv, "--dry-run");
851 - if (prune)
867 + if (prune > 0)
868 argv_array_push(argv, "--prune");
869 if (update_head_ok)
870 argv_array_push(argv, "--update-head-ok");
@@ -916,6 +932,17 @@ static int fetch_one(struct remote *remote, int argc, const char **argv)
932 "remote name from which new revisions should be fetched."));
933
934 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 +
946 transport_set_verbosity(transport, verbosity, progress);
947 if (upload_pack)
948 set_option(TRANS_OPT_UPLOADPACK, upload_pack);
@@ -973,6 +1000,8 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
1000 for (i = 1; i < argc; i++)
1001 strbuf_addf(&default_rla, " %s", argv[i]);
1002
1003 + git_config(git_fetch_config, NULL);
1004 +
1005 argc = parse_options(argc, argv, prefix,
1006 builtin_fetch_options, builtin_fetch_usage, 0);
1007
remote.c
+3
@@ -148,6 +148,7 @@ static struct remote *make_remote(const char *name, int len)
148 }
149
150 ret = xcalloc(1, sizeof(struct remote));
151 + ret->prune = -1; /* unspecified */
152 ALLOC_GROW(remotes, remotes_nr + 1, remotes_alloc);
153 remotes[remotes_nr++] = ret;
154 if (len)
@@ -419,6 +420,8 @@ static int handle_config(const char *key, const char *value, void *cb)
420 remote->skip_default_update = git_config_bool(key, value);
421 else if (!strcmp(subkey, ".skipfetchall"))
422 remote->skip_default_update = git_config_bool(key, value);
423 + else if (!strcmp(subkey, ".prune"))
424 + remote->prune = git_config_bool(key, value);
425 else if (!strcmp(subkey, ".url")) {
426 const char *v;
427 if (git_config_string(&v, key, value))
remote.h
+1
@@ -40,6 +40,7 @@ struct remote {
40 int fetch_tags;
41 int skip_default_update;
42 int mirror;
43 + int prune;
44
45 const char *receivepack;
46 const char *uploadpack;
t/t5510-fetch.sh
+82
@@ -471,6 +471,88 @@ test_expect_success "should be able to fetch with duplicate refspecs" '
471 )
472 '
473
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 +
556 test_expect_success 'all boundary commits are excluded' '
557 test_commit base &&
558 test_commit oneside &&