Skip to content

Commit 377444b

Browse files
derrickstoleegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cdbd70c commit 377444b

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

Documentation/config/advice.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ advice.*::
44
can tell Git that you do not need help by setting these to 'false':
55
+
66
--
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.
711
pushUpdateRejected::
812
Set this variable to 'false' if you want to disable
913
'pushNonFFCurrent',

advice.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "color.h"
44
#include "help.h"
55

6+
int advice_fetch_show_forced_updates = 1;
67
int advice_push_update_rejected = 1;
78
int advice_push_non_ff_current = 1;
89
int advice_push_non_ff_matching = 1;
@@ -59,6 +60,7 @@ static struct {
5960
const char *name;
6061
int *preference;
6162
} advice_config[] = {
63+
{ "fetchShowForcedUpdates", &advice_fetch_show_forced_updates },
6264
{ "pushUpdateRejected", &advice_push_update_rejected },
6365
{ "pushNonFFCurrent", &advice_push_non_ff_current },
6466
{ "pushNonFFMatching", &advice_push_non_ff_matching },

advice.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "git-compat-util.h"
55

6+
extern int advice_fetch_show_forced_updates;
67
extern int advice_push_update_rejected;
78
extern int advice_push_non_ff_current;
89
extern int advice_push_non_ff_matching;

builtin/fetch.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#include "list-objects-filter-options.h"
2525
#include "commit-reach.h"
2626

27+
#define FORCED_UPDATES_DELAY_WARNING_IN_MS (10 * 1000)
28+
2729
static const char * const builtin_fetch_usage[] = {
2830
N_("git fetch [<options>] [<repository> [<refspec>...]]"),
2931
N_("git fetch [<options>] <group>"),
@@ -40,6 +42,7 @@ enum {
4042

4143
static int fetch_prune_config = -1; /* unspecified */
4244
static int fetch_show_forced_updates = 1;
45+
static uint64_t forced_updates_ms = 0;
4346
static int prune = -1; /* unspecified */
4447
#define PRUNE_BY_DEFAULT 0 /* do we prune by default? */
4548

@@ -707,6 +710,7 @@ static int update_local_ref(struct ref *ref,
707710
enum object_type type;
708711
struct branch *current_branch = branch_get(NULL);
709712
const char *pretty_ref = prettify_refname(ref->name);
713+
int fast_forward = 0;
710714

711715
type = oid_object_info(the_repository, &ref->new_oid, NULL);
712716
if (type < 0)
@@ -781,7 +785,15 @@ static int update_local_ref(struct ref *ref,
781785
return r;
782786
}
783787

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) {
785797
struct strbuf quickref = STRBUF_INIT;
786798
int r;
787799

@@ -980,6 +992,17 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
980992
" 'git remote prune %s' to remove any old, conflicting "
981993
"branches"), remote_name);
982994

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+
9831006
abort:
9841007
strbuf_release(&note);
9851008
free(url);

0 commit comments

Comments
 (0)