git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/2] branch: improve error log on branch not found by checking remotes refs

From
CGctmbl via GitGitGadget <gitgitgadget@gmail.com>
Date
Mar 22, 2023, 09:42 UTC
Message-ID
<91cb506968a11fbceba856a40b14bed0c4e647bc.1679478126.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.1476.git.git.1679478126.gitgitgadget@gmail.com>
From: ctmbl <mabileau.clement@gmail.com>

when failing to delete a branch with `git branch -d <branch>` because of branch not found, try to find a remote refs matching `<branch>` and if so add an hint: `Did you forget --remote?` to the error message

Signed-off-by: Clement Mabileau <mabileau.clement@gmail.com>
---
 builtin/branch.c | 28 +++++++++++++++++++++++-----
 1 file changed, 23 insertions(+), 5 deletions(-)
diff --git a/builtin/branch.c b/builtin/branch.c
index f63fd45edb9..8e768761b9b 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -216,10 +216,12 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
 	struct string_list refs_to_delete = STRING_LIST_INIT_DUP;
 	struct string_list_item *item;
 	int branch_name_pos;
+	char* FMT_REMOTES = "refs/remotes/%s";
+	char* FMT_BRANCHES = "refs/heads/%s";
 
 	switch (kinds) {
 	case FILTER_REFS_REMOTES:
-		fmt = "refs/remotes/%s";
+		fmt = FMT_REMOTES;
 		/* For subsequent UI messages */
 		remote_branch = 1;
 		allowed_interpret = INTERPRET_BRANCH_REMOTE;
@@ -227,7 +229,7 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
 		force = 1;
 		break;
 	case FILTER_REFS_BRANCHES:
-		fmt = "refs/heads/%s";
+		fmt = FMT_BRANCHES;
 		allowed_interpret = INTERPRET_BRANCH_LOCAL;
 		break;
 	default:
@@ -263,9 +265,25 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
 					| RESOLVE_REF_ALLOW_BAD_NAME,
 					&oid, &flags);
 		if (!target) {
-			error(remote_branch
-			      ? _("remote-tracking branch '%s' not found.")
-			      : _("branch '%s' not found."), bname.buf);
+			char* MISSING_REMOTE_REF_ERROR_MSG = "remote-tracking branch '%s' not found.";
+			char* MISSING_BRANCH_ERROR_MSG = "branch '%s' not found.";
+			char* MISSING_BRANCH_HINT_MSG = "branch '%s' not found.\n"
+											"Did you forget --remote?";
+
+			if (remote_branch) {
+				error(_(MISSING_REMOTE_REF_ERROR_MSG), bname.buf);
+			} else {
+				char* virtual_name = mkpathdup(FMT_REMOTES, bname.buf);
+				char* virtual_target = resolve_refdup(virtual_name,
+							RESOLVE_REF_READING
+							| RESOLVE_REF_NO_RECURSE
+							| RESOLVE_REF_ALLOW_BAD_NAME,
+							&oid, &flags);
+				if (virtual_target)
+					error(_(MISSING_BRANCH_HINT_MSG), bname.buf);
+				else
+					error(_(MISSING_BRANCH_ERROR_MSG), bname.buf);
+			}
 			ret = 1;
 			continue;
 		}
-- 
gitgitgadget
Previous: Junio C HamanoNext: ClementMabileau via GitGitGadget
Message 6 of 13 in “branch: improve error log on branch not found by checking remotes refs”
  1. 0/2 branch: improve error log on branch not found by checking remotes refsClementMabileau via GitGitGadget, Mar 22, 2023
  2. 2/2 Fix mem leak in branch.c due to not-free newly added virtual_name variablectmbl via GitGitGadget, Mar 22, 2023
  3. Junio C HamanoMar 22, 2023
  4. Clement MabileauMar 22, 2023
  5. Junio C HamanoMar 22, 2023
  6. 1/2 branch: improve error log on branch not found by checking remotes refsctmbl via GitGitGadget, Mar 22, 2023
  7. branch: improve error log on branch not found by checking remotes refsClementMabileau via GitGitGadget, Mar 22, 2023
  8. Junio C HamanoMar 22, 2023
  9. Clement MabileauMar 23, 2023
  10. Clement MabileauApr 4, 2023
  11. Junio C HamanoApr 4, 2023
  12. Clement MabileauApr 5, 2023
  13. branch: improve error log on branch not found by checking remotes refsClementMabileau via GitGitGadget, Apr 5, 2023

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.