git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: "ClementMabileau via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Clement Mabileau <mabileau.clement@gmail.com>,
	ClementMabileau <mabileau.clement@gmail.com>,
	Clement Mabileau <mabileau.clement@gmail.com>
Subject: [PATCH v3] branch: improve error log on branch not found by checking remotes refs
Date: Wed, 05 Apr 2023 11:43:20 +0000	[thread overview]
Message-ID: <pull.1476.v3.git.git.1680695000257.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.1476.v2.git.git.1679515402379.gitgitgadget@gmail.com>

From: Clement Mabileau <mabileau.clement@gmail.com>

New git users may want to locally delete remote-tracking branches but
don't really understand how they are distinguished from branches by git.
Then one may naively try:
`git branch -d foo/bar` and get a correct error `branch foo/bar not
found` but hard to understand for a newbie, this patch aims to guide one
in such case.

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>
---
    branch: improve error log on branch not found by checking remotes refs
    
    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

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1476%2Fctmbl%2Fbranch%2Fdeletion%2Fimprove-error-msg-v3
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1476/ctmbl/branch/deletion/improve-error-msg-v3
Pull-Request: https://github.com/git/git/pull/1476

Range-diff vs v2:

 1:  eb95695ace2 ! 1:  0774f182b50 branch: improve error log on branch not found by checking remotes refs
     @@
       ## Metadata ##
     -Author: ctmbl <mabileau.clement@gmail.com>
     +Author: Clement Mabileau <mabileau.clement@gmail.com>
      
       ## Commit message ##
          branch: improve error log on branch not found by checking remotes refs
      
     +    New git users may want to locally delete remote-tracking branches but
     +    don't really understand how they are distinguished from branches by git.
     +    Then one may naively try:
     +    `git branch -d foo/bar` and get a correct error `branch foo/bar not
     +    found` but hard to understand for a newbie, this patch aims to guide one
     +    in such case.
     +
          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:
     +    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: static int delete_branches(int argc, const char **argv, int fo
       	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";
     ++	const char *fmt_remotes = "refs/remotes/%s";
       
       	switch (kinds) {
       	case FILTER_REFS_REMOTES:
      -		fmt = "refs/remotes/%s";
     -+		fmt = FMT_REMOTES;
     ++		fmt = fmt_remotes;
       		/* For subsequent UI messages */
       		remote_branch = 1;
       		allowed_interpret = INTERPRET_BRANCH_REMOTE;
     -@@ builtin/branch.c: 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:
      @@ builtin/branch.c: static int delete_branches(int argc, const char **argv, int force, int kinds,
       					| RESOLVE_REF_ALLOW_BAD_NAME,
       					&oid, &flags);
     @@ builtin/branch.c: static int delete_branches(int argc, const char **argv, int fo
      -			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);
     ++				error(_("remote-tracking branch '%s' not found."), bname.buf);
      +			} else {
     -+				char* virtual_name = mkpathdup(FMT_REMOTES, bname.buf);
     -+				char* virtual_target = resolve_refdup(virtual_name,
     ++				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);
      +				FREE_AND_NULL(virtual_name);
     ++
      +				if (virtual_target)
     -+					error(_(MISSING_BRANCH_HINT_MSG), bname.buf);
     ++					error(_("branch '%s' not found.\n"
     ++						"Did you forget --remote?"),
     ++						bname.buf);
      +				else
     -+					error(_(MISSING_BRANCH_ERROR_MSG), bname.buf);
     ++					error(_("branch '%s' not found."), bname.buf);
     ++				FREE_AND_NULL(virtual_target);
      +			}
       			ret = 1;
       			continue;


 builtin/branch.c | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)

diff --git a/builtin/branch.c b/builtin/branch.c
index f63fd45edb9..5f035dd5969 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -216,10 +216,11 @@ 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;
+	const char *fmt_remotes = "refs/remotes/%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;
@@ -263,9 +264,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);
+			if (remote_branch) {
+				error(_("remote-tracking branch '%s' not found."), 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);
+				FREE_AND_NULL(virtual_name);
+
+				if (virtual_target)
+					error(_("branch '%s' not found.\n"
+						"Did you forget --remote?"),
+						bname.buf);
+				else
+					error(_("branch '%s' not found."), bname.buf);
+				FREE_AND_NULL(virtual_target);
+			}
 			ret = 1;
 			continue;
 		}

base-commit: ae73b2c8f1da39c39335ee76a0f95857712c22a7
-- 
gitgitgadget

      parent reply	other threads:[~2023-04-05 11:44 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-22  9:42 [PATCH 0/2] branch: improve error log on branch not found by checking remotes refs ClementMabileau via GitGitGadget
2023-03-22  9:42 ` [PATCH 1/2] " ctmbl via GitGitGadget
2023-03-22  9:42 ` [PATCH 2/2] Fix mem leak in branch.c due to not-free newly added virtual_name variable ctmbl via GitGitGadget
2023-03-22 16:52   ` Junio C Hamano
2023-03-22 20:00     ` Clement Mabileau
2023-03-22 20:52       ` Junio C Hamano
2023-03-22 20:03 ` [PATCH v2] branch: improve error log on branch not found by checking remotes refs ClementMabileau via GitGitGadget
2023-03-22 22:25   ` Junio C Hamano
2023-03-23 15:51     ` Clement Mabileau
2023-04-04 13:30       ` Clement Mabileau
2023-04-04 16:24         ` Junio C Hamano
2023-04-05  9:15           ` Clement Mabileau
2023-04-05 11:43   ` ClementMabileau via GitGitGadget [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=pull.1476.v3.git.git.1680695000257.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=mabileau.clement@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).