fetch: warn about forced updates in branch listing

The --[no-]show-forced-updates option in 'git fetch' can be confusing for some users, especially if it is enabled via config setting and not by argument. Add advice to warn the user that the (forced update) messages were not listed. Additionally, warn users when the forced update check takes longer than ten seconds, and recommend that they disable the check. These messages can be disabled by the advice.fetchShowForcedUpdates config setting. Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Jun 18, 2019 at 13:25 UTC 377444b440eab3b6ef636df092f7f8299acf75bb
4 files changed +31 -1
Documentation/config/advice.txt
+4
@@ -4,6 +4,10 @@ advice.*::
4 can tell Git that you do not need help by setting these to 'false':
5 +
6 --
7 + fetchShowForcedUpdates::
8 + Advice shown when linkgit:git-fetch[1] takes a long time
9 + to calculate forced updates after ref updates, or to warn
10 + that the check is disabled.
11 pushUpdateRejected::
12 Set this variable to 'false' if you want to disable
13 'pushNonFFCurrent',
advice.c
+2
@@ -3,6 +3,7 @@
3 #include "color.h"
4 #include "help.h"
5
6 +int advice_fetch_show_forced_updates = 1;
7 int advice_push_update_rejected = 1;
8 int advice_push_non_ff_current = 1;
9 int advice_push_non_ff_matching = 1;
@@ -59,6 +60,7 @@ static struct {
60 const char *name;
61 int *preference;
62 } advice_config[] = {
63 + { "fetchShowForcedUpdates", &advice_fetch_show_forced_updates },
64 { "pushUpdateRejected", &advice_push_update_rejected },
65 { "pushNonFFCurrent", &advice_push_non_ff_current },
66 { "pushNonFFMatching", &advice_push_non_ff_matching },
advice.h
+1
@@ -3,6 +3,7 @@
3
4 #include "git-compat-util.h"
5
6 +extern int advice_fetch_show_forced_updates;
7 extern int advice_push_update_rejected;
8 extern int advice_push_non_ff_current;
9 extern int advice_push_non_ff_matching;
builtin/fetch.c
+24 -1
@@ -24,6 +24,8 @@
24 #include "list-objects-filter-options.h"
25 #include "commit-reach.h"
26
27 +#define FORCED_UPDATES_DELAY_WARNING_IN_MS (10 * 1000)
28 +
29 static const char * const builtin_fetch_usage[] = {
30 N_("git fetch [<options>] [<repository> [<refspec>...]]"),
31 N_("git fetch [<options>] <group>"),
@@ -40,6 +42,7 @@ enum {
42
43 static int fetch_prune_config = -1; /* unspecified */
44 static int fetch_show_forced_updates = 1;
45 +static uint64_t forced_updates_ms = 0;
46 static int prune = -1; /* unspecified */
47 #define PRUNE_BY_DEFAULT 0 /* do we prune by default? */
48
@@ -707,6 +710,7 @@ static int update_local_ref(struct ref *ref,
710 enum object_type type;
711 struct branch *current_branch = branch_get(NULL);
712 const char *pretty_ref = prettify_refname(ref->name);
713 + int fast_forward = 0;
714
715 type = oid_object_info(the_repository, &ref->new_oid, NULL);
716 if (type < 0)
@@ -781,7 +785,15 @@ static int update_local_ref(struct ref *ref,
785 return r;
786 }
787
784 - if (!fetch_show_forced_updates || in_merge_bases(current, updated)) {
788 + if (fetch_show_forced_updates) {
789 + uint64_t t_before = getnanotime();
790 + fast_forward = in_merge_bases(current, updated);
791 + forced_updates_ms += (getnanotime() - t_before) / 1000000;
792 + } else {
793 + fast_forward = 1;
794 + }
795 +
796 + if (fast_forward) {
797 struct strbuf quickref = STRBUF_INIT;
798 int r;
799
@@ -980,6 +992,17 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
992 " 'git remote prune %s' to remove any old, conflicting "
993 "branches"), remote_name);
994
995 + if (advice_fetch_show_forced_updates) {
996 + if (!fetch_show_forced_updates) {
997 + warning(_("Fetch normally indicates which branches had a forced update, but that check has been disabled."));
998 + warning(_("To re-enable, use '--show-forced-updates' flag or run 'git config fetch.showForcedUpdates true'."));
999 + } else if (forced_updates_ms > FORCED_UPDATES_DELAY_WARNING_IN_MS) {
1000 + warning(_("It took %.2f seconds to check forced updates. You can use '--no-show-forced-updates'\n"),
1001 + forced_updates_ms / 1000.0);
1002 + warning(_("or run 'git config fetch.showForcedUpdates false' to avoid this check.\n"));
1003 + }
1004 + }
1005 +
1006 abort:
1007 strbuf_release(&note);
1008 free(url);