diff mbox series

[09/11] fetch: warn about forced updates after branch list

Message ID 6a80cfa5a5604eb4560e8ad7254a982caa894678.1559593097.git.gitgitgadget@gmail.com (mailing list archive)
State New, archived
Headers show
Series Create 'core.size=large' setting to update config defaults | expand

Commit Message

Philippe Blain via GitGitGadget June 3, 2019, 8:18 p.m. UTC
From: Derrick Stolee <dstolee@microsoft.com>

Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 builtin/fetch.c | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/builtin/fetch.c b/builtin/fetch.c
index 571c255218..f8ff98fdaf 100644
--- a/builtin/fetch.c
+++ b/builtin/fetch.c
@@ -24,6 +24,8 @@ 
 #include "list-objects-filter-options.h"
 #include "commit-reach.h"
 
+#define FORCED_UPDATES_DELAY_WARNING_IN_MS (10 * 1000)
+
 static const char * const builtin_fetch_usage[] = {
 	N_("git fetch [<options>] [<repository> [<refspec>...]]"),
 	N_("git fetch [<options>] <group>"),
@@ -40,6 +42,7 @@  enum {
 
 static int fetch_prune_config = -1; /* unspecified */
 static int fetch_show_forced_updates = 1;
+static uint64_t forced_updates_ms = 0;
 static int prune = -1; /* unspecified */
 #define PRUNE_BY_DEFAULT 0 /* do we prune by default? */
 
@@ -707,6 +710,7 @@  static int update_local_ref(struct ref *ref,
 	enum object_type type;
 	struct branch *current_branch = branch_get(NULL);
 	const char *pretty_ref = prettify_refname(ref->name);
+	int fast_forward = 0;
 
 	type = oid_object_info(the_repository, &ref->new_oid, NULL);
 	if (type < 0)
@@ -781,7 +785,15 @@  static int update_local_ref(struct ref *ref,
 		return r;
 	}
 
-	if (!fetch_show_forced_updates || in_merge_bases(current, updated)) {
+	if (fetch_show_forced_updates) {
+		uint64_t t_before = getnanotime();
+		fast_forward = in_merge_bases(current, updated);
+		forced_updates_ms += (getnanotime() - t_before) / 1000000;
+	} else {
+		fast_forward = 1;
+	}
+
+	if (fast_forward) {
 		struct strbuf quickref = STRBUF_INIT;
 		int r;
 
@@ -980,6 +992,17 @@  static int store_updated_refs(const char *raw_url, const char *remote_name,
 		      " 'git remote prune %s' to remove any old, conflicting "
 		      "branches"), remote_name);
 
+	if (!fetch_show_forced_updates) {
+		warning(_("Fetch normally indicates which branches had a forced update, but that check has been disabled."));
+		warning(_("To re-enable, use '--show-forced-updates' flag or run 'git config fetch.showForcedUpdates true'."));
+	}
+	if (fetch_show_forced_updates &&
+	    forced_updates_ms > FORCED_UPDATES_DELAY_WARNING_IN_MS) {
+		warning(_("It took %.2f seconds to check forced updates. You can use '--no-show-forced-updates'\n"),
+			forced_updates_ms / 1000.0);
+		warning(_("or run 'git config fetch.showForcedUpdates false' to avoid this check.\n"));
+	}
+
  abort:
 	strbuf_release(&note);
 	free(url);