Skip to content

Commit cad1db2

Browse files
derrickstoleedscho
authored andcommitted
fetch: Warn about forced updates after branch list
Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
1 parent 69f45e6 commit cad1db2

1 file changed

Lines changed: 25 additions & 9 deletions

File tree

builtin/fetch.c

Lines changed: 25 additions & 9 deletions
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>"),
@@ -41,6 +43,7 @@ enum {
4143
static int fetch_prune_config = -1; /* unspecified */
4244
static int fetch_show_forced_updates = 1;
4345
static int fetch_show_forced_updates_warning = 0;
46+
static uint64_t forced_updates_ms = 0;
4447
static int prune = -1; /* unspecified */
4548
#define PRUNE_BY_DEFAULT 0 /* do we prune by default? */
4649

@@ -708,6 +711,7 @@ static int update_local_ref(struct ref *ref,
708711
enum object_type type;
709712
struct branch *current_branch = branch_get(NULL);
710713
const char *pretty_ref = prettify_refname(ref->name);
714+
int fast_forward = 0;
711715

712716
type = oid_object_info(the_repository, &ref->new_oid, NULL);
713717
if (type < 0)
@@ -782,18 +786,19 @@ static int update_local_ref(struct ref *ref,
782786
return r;
783787
}
784788

785-
if (!fetch_show_forced_updates || in_merge_bases(current, updated)) {
789+
if (fetch_show_forced_updates) {
790+
uint64_t t_before = getnanotime();
791+
fast_forward = in_merge_bases(current, updated);
792+
forced_updates_ms += (getnanotime() - t_before) / 1000000;
793+
} else {
794+
fetch_show_forced_updates_warning = 1;
795+
fast_forward = 1;
796+
}
797+
798+
if (fast_forward) {
786799
struct strbuf quickref = STRBUF_INIT;
787800
int r;
788801

789-
if (!fetch_show_forced_updates && !fetch_show_forced_updates_warning) {
790-
fetch_show_forced_updates_warning = 1;
791-
warning(_("Fetch normally indicates which branches had a forced update, but that check has been disabled."));
792-
warning(_("To re-enable, use '--show-forced-updates' flag or run 'git config fetch.showForcedUpdates true'."));
793-
warning(_("OR if you just want to check if a branch with name <branch> was force-updated after fetching, run:"));
794-
warning(_(" git rev-list --left-right --count <branch>...<branch>@{1}"));
795-
}
796-
797802
strbuf_add_unique_abbrev(&quickref, &current->object.oid, DEFAULT_ABBREV);
798803
strbuf_addstr(&quickref, "..");
799804
strbuf_add_unique_abbrev(&quickref, &ref->new_oid, DEFAULT_ABBREV);
@@ -989,6 +994,17 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
989994
" 'git remote prune %s' to remove any old, conflicting "
990995
"branches"), remote_name);
991996

997+
if (!fetch_show_forced_updates && fetch_show_forced_updates_warning) {
998+
warning(_("Fetch normally indicates which branches had a forced update, but that check has been disabled."));
999+
warning(_("To re-enable, use '--show-forced-updates' flag or run 'git config fetch.showForcedUpdates true'."));
1000+
}
1001+
if (fetch_show_forced_updates &&
1002+
forced_updates_ms > FORCED_UPDATES_DELAY_WARNING_IN_MS) {
1003+
warning(_("It took %.2f seconds to check forced updates. You can use '--no-show-forced-updates'\n"),
1004+
forced_updates_ms / 1000.0);
1005+
warning(_("or run 'git config fetch.showForcedUpdates false' to avoid this check.\n"));
1006+
}
1007+
9921008
abort:
9931009
strbuf_release(&note);
9941010
free(url);

0 commit comments

Comments
 (0)