Subject: [PATCH v2] RFC: tracking branches: add advice to ambiguous refspec error

From: "Tao Klerks via GitGitGadget" <[email-masked]>
Date: Tue, 22 Mar 2022 09:18:06 +0000

From: Tao Klerks <[email-masked]>

The error "not tracking: ambiguous information for ref" is raised
when we are evaluating what tracking information to set on a branch,
and find that the ref to be added as tracking branch is mapped
under multiple remotes' fetch refspecs.

This can easily happen when a user copy-pastes a remote definition
in their git config, and forgets to change the tracking path.

Add advice in this situation, explicitly highlighting which remotes
are involved and suggesting how to correct the situation.

Signed-off-by: Tao Klerks <[email-masked]>
---
    RFC: tracking branches: add advice to ambiguous refspec error
    
    Second go at adding some advice around ambiguous remote refspecs,
    incorporating Ævar's suggestions.

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1183%2FTaoK%2Fadvise-ambiguous-tracking-refspec-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1183/TaoK/advise-ambiguous-tracking-refspec-v2
Pull-Request: https://github.com/gitgitgadget/git/pull/1183

Range-diff vs v1:

 1:  9ec19e1c48e ! 1:  6c1cd885dda tracking branches: add advice to ambiguous refspec error
     @@ Metadata
      Author: Tao Klerks <[email-masked]>
      
       ## Commit message ##
     -    tracking branches: add advice to ambiguous refspec error
     +    RFC: tracking branches: add advice to ambiguous refspec error
      
          The error "not tracking: ambiguous information for ref" is raised
          when we are evaluating what tracking information to set on a branch,
     @@ Commit message
      
          Signed-off-by: Tao Klerks <[email-masked]>
      
     + ## Documentation/config/advice.txt ##
     +@@ Documentation/config/advice.txt: advice.*::
     + 		Advice shown when either linkgit:git-add[1] or linkgit:git-rm[1]
     + 		is asked to update index entries outside the current sparse
     + 		checkout.
     ++	ambiguousFetchRefspec::
     ++		Advice shown when branch tracking relationship setup fails due
     ++		to multiple remotes' refspecs mapping to the same remote
     ++		tracking namespace in the repo.
     + --
     +
       ## advice.c ##
      @@ advice.c: static struct {
       	[ADVICE_ADD_EMPTY_PATHSPEC]			= { "addEmptyPathspec", 1 },
     @@ advice.h: struct string_list;
       	ADVICE_DETACHED_HEAD,
      
       ## branch.c ##
     -@@
     - struct tracking {
     - 	struct refspec_item spec;
     - 	struct string_list *srcs;
     -+	struct string_list *remotes;
     - 	const char *remote;
     +@@ branch.c: struct tracking {
       	int matches;
       };
     + 
     ++struct find_tracked_branch_cb {
     ++	struct tracking *tracking;
     ++	struct strbuf remotes_advice;
     ++};
     ++
     + static int find_tracked_branch(struct remote *remote, void *priv)
     + {
     +-	struct tracking *tracking = priv;
     ++	struct find_tracked_branch_cb *ftb = priv;
     ++	struct tracking *tracking = ftb->tracking;
     + 
     + 	if (!remote_find_tracking(remote, &tracking->spec)) {
     + 		if (++tracking->matches == 1) {
      @@ branch.c: static int find_tracked_branch(struct remote *remote, void *priv)
       			free(tracking->spec.src);
       			string_list_clear(tracking->srcs, 0);
       		}
     -+		string_list_append(tracking->remotes, remote->name);
     ++		strbuf_addf(&ftb->remotes_advice, "  %s\n", remote->name);
       		tracking->spec.src = NULL;
       	}
       
     @@ branch.c: static int inherit_tracking(struct tracking *tracking, const char *ori
      +
      +static const char ambiguous_refspec_advice_pre[] =
      +N_("\n"
     -+"There are multiple remotes with fetch refspecs mapping to\n"
     -+"the tracking ref %s:\n";)
     ++"There are multiple remotes whose fetch refspecs map to the remote\n"
     ++"tracking ref";)
      +static const char ambiguous_refspec_advice_post[] =
      +N_("This is typically a configuration error.\n"
      +"\n"
     @@ branch.c: static int inherit_tracking(struct tracking *tracking, const char *ori
        * This is called when new_ref is branched off of orig_ref, and tries
        * to infer the settings for branch.<new_ref>.{remote,merge} from the
      @@ branch.c: static void setup_tracking(const char *new_ref, const char *orig_ref,
     - {
       	struct tracking tracking;
       	struct string_list tracking_srcs = STRING_LIST_INIT_DUP;
     -+	struct string_list tracking_remotes = STRING_LIST_INIT_DUP;
       	int config_flags = quiet ? 0 : BRANCH_CONFIG_VERBOSE;
     -+	struct string_list_item *item;
     ++	struct find_tracked_branch_cb ftb_cb = {
     ++		.tracking = &tracking,
     ++		.remotes_advice = STRBUF_INIT,
     ++	};
       
       	memset(&tracking, 0, sizeof(tracking));
       	tracking.spec.dst = (char *)orig_ref;
       	tracking.srcs = &tracking_srcs;
     -+	tracking.remotes = &tracking_remotes;
       	if (track != BRANCH_TRACK_INHERIT)
     - 		for_each_remote(find_tracked_branch, &tracking);
     +-		for_each_remote(find_tracked_branch, &tracking);
     ++		for_each_remote(find_tracked_branch, &ftb_cb);
       	else if (inherit_tracking(&tracking, orig_ref))
     + 		return;
     + 
      @@ branch.c: static void setup_tracking(const char *new_ref, const char *orig_ref,
       			return;
       		}
       
      -	if (tracking.matches > 1)
     +-		die(_("not tracking: ambiguous information for ref %s"),
     +-		    orig_ref);
      +	if (tracking.matches > 1) {
     -+		if (advice_enabled(ADVICE_AMBIGUOUS_FETCH_REFSPEC)) {
     -+			advise(_(ambiguous_refspec_advice_pre), orig_ref);
     -+			for_each_string_list_item(item, &tracking_remotes) {
     -+				advise("  %s", item->string);
     -+			}
     -+			advise(_(ambiguous_refspec_advice_post));
     -+		}
     - 		die(_("not tracking: ambiguous information for ref %s"),
     - 		    orig_ref);
     ++		int status = die_message(_("not tracking: ambiguous information for ref %s"),
     ++					    orig_ref);
     ++		if (advice_enabled(ADVICE_AMBIGUOUS_FETCH_REFSPEC))
     ++			advise("%s %s:\n%s\n%s",
     ++			       _(ambiguous_refspec_advice_pre),
     ++			       orig_ref,
     ++			       ftb_cb.remotes_advice.buf,
     ++			       _(ambiguous_refspec_advice_post)
     ++			       );
     ++		exit(status);
      +	}
       
       	if (tracking.srcs->nr < 1)

 Documentation/config/advice.txt |  4 ++++
 advice.c                        |  1 +
 advice.h                        |  1 +
 branch.c                        | 42 +++++++++++++++++++++++++++++----
 4 files changed, 43 insertions(+), 5 deletions(-)

diff --git a/Documentation/config/advice.txt b/Documentation/config/advice.txt
index 063eec2511d..abfac4f664b 100644
--- a/Documentation/config/advice.txt
+++ b/Documentation/config/advice.txt
@@ -126,4 +126,8 @@ advice.*::
 		Advice shown when either linkgit:git-add[1] or linkgit:git-rm[1]
 		is asked to update index entries outside the current sparse
 		checkout.
+	ambiguousFetchRefspec::
+		Advice shown when branch tracking relationship setup fails due
+		to multiple remotes' refspecs mapping to the same remote
+		tracking namespace in the repo.
 --
diff --git a/advice.c b/advice.c
index 1dfc91d1767..686612590ec 100644
--- a/advice.c
+++ b/advice.c
@@ -39,6 +39,7 @@ static struct {
 	[ADVICE_ADD_EMPTY_PATHSPEC]			= { "addEmptyPathspec", 1 },
 	[ADVICE_ADD_IGNORED_FILE]			= { "addIgnoredFile", 1 },
 	[ADVICE_AM_WORK_DIR] 				= { "amWorkDir", 1 },
+	[ADVICE_AMBIGUOUS_FETCH_REFSPEC]		= { "ambiguousFetchRefspec", 1 },
 	[ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME] 	= { "checkoutAmbiguousRemoteBranchName", 1 },
 	[ADVICE_COMMIT_BEFORE_MERGE]			= { "commitBeforeMerge", 1 },
 	[ADVICE_DETACHED_HEAD]				= { "detachedHead", 1 },
diff --git a/advice.h b/advice.h
index 601265fd107..3d68c1a6cb4 100644
--- a/advice.h
+++ b/advice.h
@@ -17,6 +17,7 @@ struct string_list;
 	ADVICE_ADD_EMPTY_PATHSPEC,
 	ADVICE_ADD_IGNORED_FILE,
 	ADVICE_AM_WORK_DIR,
+	ADVICE_AMBIGUOUS_FETCH_REFSPEC,
 	ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME,
 	ADVICE_COMMIT_BEFORE_MERGE,
 	ADVICE_DETACHED_HEAD,
diff --git a/branch.c b/branch.c
index 5d20a2e8484..4a3a77aa338 100644
--- a/branch.c
+++ b/branch.c
@@ -16,9 +16,15 @@ struct tracking {
 	int matches;
 };
 
+struct find_tracked_branch_cb {
+	struct tracking *tracking;
+	struct strbuf remotes_advice;
+};
+
 static int find_tracked_branch(struct remote *remote, void *priv)
 {
-	struct tracking *tracking = priv;
+	struct find_tracked_branch_cb *ftb = priv;
+	struct tracking *tracking = ftb->tracking;
 
 	if (!remote_find_tracking(remote, &tracking->spec)) {
 		if (++tracking->matches == 1) {
@@ -28,6 +34,7 @@ static int find_tracked_branch(struct remote *remote, void *priv)
 			free(tracking->spec.src);
 			string_list_clear(tracking->srcs, 0);
 		}
+		strbuf_addf(&ftb->remotes_advice, "  %s\n", remote->name);
 		tracking->spec.src = NULL;
 	}
 
@@ -217,6 +224,18 @@ static int inherit_tracking(struct tracking *tracking, const char *orig_ref)
 	return 0;
 }
 
+
+static const char ambiguous_refspec_advice_pre[] =
+N_("\n"
+"There are multiple remotes whose fetch refspecs map to the remote\n"
+"tracking ref";)
+static const char ambiguous_refspec_advice_post[] =
+N_("This is typically a configuration error.\n"
+"\n"
+"To support setting up tracking branches, ensure that\n"
+"different remotes' fetch refspecs map into different\n"
+"tracking namespaces.\n");
+
 /*
  * This is called when new_ref is branched off of orig_ref, and tries
  * to infer the settings for branch.<new_ref>.{remote,merge} from the
@@ -228,12 +247,16 @@ static void setup_tracking(const char *new_ref, const char *orig_ref,
 	struct tracking tracking;
 	struct string_list tracking_srcs = STRING_LIST_INIT_DUP;
 	int config_flags = quiet ? 0 : BRANCH_CONFIG_VERBOSE;
+	struct find_tracked_branch_cb ftb_cb = {
+		.tracking = &tracking,
+		.remotes_advice = STRBUF_INIT,
+	};
 
 	memset(&tracking, 0, sizeof(tracking));
 	tracking.spec.dst = (char *)orig_ref;
 	tracking.srcs = &tracking_srcs;
 	if (track != BRANCH_TRACK_INHERIT)
-		for_each_remote(find_tracked_branch, &tracking);
+		for_each_remote(find_tracked_branch, &ftb_cb);
 	else if (inherit_tracking(&tracking, orig_ref))
 		return;
 
@@ -248,9 +271,18 @@ static void setup_tracking(const char *new_ref, const char *orig_ref,
 			return;
 		}
 
-	if (tracking.matches > 1)
-		die(_("not tracking: ambiguous information for ref %s"),
-		    orig_ref);
+	if (tracking.matches > 1) {
+		int status = die_message(_("not tracking: ambiguous information for ref %s"),
+					    orig_ref);
+		if (advice_enabled(ADVICE_AMBIGUOUS_FETCH_REFSPEC))
+			advise("%s %s:\n%s\n%s",
+			       _(ambiguous_refspec_advice_pre),
+			       orig_ref,
+			       ftb_cb.remotes_advice.buf,
+			       _(ambiguous_refspec_advice_post)
+			       );
+		exit(status);
+	}
 
 	if (tracking.srcs->nr < 1)
 		string_list_append(tracking.srcs, orig_ref);

base-commit: 4c53a8c20f8984adb226293a3ffd7b88c3f4ac1a
-- 
gitgitgadget