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

[PATCH v4 2/3] builtin/branch: give more useful error messages when renaming

From
Kaartic Sivaraam <kaartic.sivaraam@gmail.com>
Date
Mar 10, 2018, 15:54 UTC
Message-ID
<20180310155416.21802-3-kaartic.sivaraam@gmail.com>
In-Reply-To
<20180310155416.21802-1-kaartic.sivaraam@gmail.com>

When trying to rename an "inexistent" branch name to a branch name that "already exists" the rename failed stating that the new branch name exists rather than stating that the branch trying to be renamed doesn't exist.

    $ git branch -m tset master
    fatal: A branch named 'master' already exists.

It's conventional to report that 'tset' doesn't exist rather than reporting that 'master' exists, the same way the 'mv' command does.

    (hypothetical)
    $ git branch -m tset master
    fatal: branch 'tset' doesn't exist.

That has the problem that the error about an existing branch is shown only after the user corrects the error about inexistent branch.

    $ git branch -m test master
    fatal: A branch named 'master' already exists.

This isn't useful either because the user would have corrected this error in a single go if he had been told this alongside the first error. So, give more useful error messages by giving errors about old branch name and new branch name at the same time. This is possible as the branch name validation functions now return the reason they were about to die, when requested.

    $ git branch -m tset master
    fatal: branch 'tset' doesn't exist; branch 'master' already exists
Signed-off-by: Kaartic Sivaraam <kaartic.sivaraam@gmail.com>
---
 builtin/branch.c | 111 +++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 94 insertions(+), 17 deletions(-)
diff --git a/builtin/branch.c b/builtin/branch.c
index 5412aa78f..ab78a7f45 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -55,6 +55,13 @@ enum color_branch {
 	BRANCH_COLOR_UPSTREAM = 5
 };
 
+enum old_branch_validation_result {
+	VALIDATION_1_FATAL_OLD_BRANCH_DOESNT_EXIST = -2,
+	VALIDATION_1_FATAL_INVALID_OLD_BRANCH_NAME = -1,
+	VALIDATION_1_PASS_OLD_BRANCH_EXISTS = 0,
+	VALIDATION_1_WARN_BAD_OLD_BRANCH_NAME = 1
+};
+
 static struct string_list output = STRING_LIST_INIT_DUP;
 static unsigned int colopts;
 
@@ -458,13 +465,86 @@ static void reject_rebase_or_bisect_branch(const char *target)
 	free_worktrees(worktrees);
 }
 
+static void get_error_msg(struct strbuf* error_msg,
+			  const char* oldname, enum old_branch_validation_result old_branch_name_res,
+			  const char* newname, enum branch_validation_result new_branch_name_res)
+{
+	const char* connector_string = "; ";
+	unsigned append_connector = 0;
+
+	switch (old_branch_name_res) {
+	case VALIDATION_1_FATAL_INVALID_OLD_BRANCH_NAME:
+		strbuf_addf(error_msg,
+			    _("old branch name '%s' is invalid"), oldname);
+		append_connector = 1;
+		break;
+	case VALIDATION_1_FATAL_OLD_BRANCH_DOESNT_EXIST:
+		strbuf_addf(error_msg,
+			    _("branch '%s' doesn't exist"), oldname);
+		append_connector = 1;
+		break;
+
+	/* not necessary to handle nonfatal cases */
+	case VALIDATION_1_PASS_OLD_BRANCH_EXISTS:
+	case VALIDATION_1_WARN_BAD_OLD_BRANCH_NAME:
+		break;
+	}
+
+	switch (new_branch_name_res) {
+	case VALIDATION_FATAL_BRANCH_EXISTS_NO_FORCE:
+		strbuf_addf(error_msg, "%s",
+			    (append_connector) ? connector_string : "");
+		strbuf_addf(error_msg,
+			    _("branch '%s' already exists"), newname);
+		break;
+	case VALIDATION_FATAL_CANNOT_FORCE_UPDATE_CURRENT_BRANCH:
+		strbuf_addf(error_msg, "%s",
+			    (append_connector) ? connector_string : "");
+		strbuf_addstr(error_msg,
+				_("cannot force update the current branch"));
+		break;
+	case VALIDATION_FATAL_INVALID_BRANCH_NAME:
+		strbuf_addf(error_msg, "%s",
+			    (append_connector) ? connector_string : "");
+		strbuf_addf(error_msg,
+			    _("new branch name '%s' is invalid"), newname);
+		break;
+
+	/* not necessary to handle nonfatal cases */
+	case VALIDATION_PASS_BRANCH_DOESNT_EXIST:
+	case VALIDATION_PASS_BRANCH_EXISTS:
+	case VALIDATION_WARN_BRANCH_EXISTS:
+		break;
+	}
+}
+
+/* Validate the old branch name and return the result */
+static enum old_branch_validation_result validate_old_branchname (const char* name, struct strbuf *oldref) {
+	int bad_ref = strbuf_check_branch_ref(oldref, name);
+	int branch_exists = ref_exists(oldref->buf);
+
+	if (bad_ref) {
+		if(branch_exists)
+			return VALIDATION_1_WARN_BAD_OLD_BRANCH_NAME;
+		else
+			return VALIDATION_1_FATAL_INVALID_OLD_BRANCH_NAME;
+	}
+
+	if (branch_exists)
+		return VALIDATION_1_PASS_OLD_BRANCH_EXISTS;
+	else
+		return VALIDATION_1_FATAL_OLD_BRANCH_DOESNT_EXIST;
+}
+
 static void copy_or_rename_branch(const char *oldname, const char *newname, int copy, int force)
 {
 	struct strbuf oldref = STRBUF_INIT, newref = STRBUF_INIT, logmsg = STRBUF_INIT;
 	struct strbuf oldsection = STRBUF_INIT, newsection = STRBUF_INIT;
 	const char *interpreted_oldname = NULL;
 	const char *interpreted_newname = NULL;
-	int recovery = 0;
+	struct strbuf error_msg = STRBUF_INIT, empty = STRBUF_INIT;
+	enum branch_validation_result new_branch_name_res;
+	enum old_branch_validation_result old_branch_name_res;
 
 	if (!oldname) {
 		if (copy)
@@ -473,33 +553,28 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
 			die(_("cannot rename the current branch while not on any."));
 	}
 
-	if (strbuf_check_branch_ref(&oldref, oldname)) {
-		/*
-		 * Bad name --- this could be an attempt to rename a
-		 * ref that we used to allow to be created by accident.
-		 */
-		if (ref_exists(oldref.buf))
-			recovery = 1;
-		else
-			die(_("Invalid branch name: '%s'"), oldname);
-	}
-
+	old_branch_name_res = validate_old_branchname(oldname, &oldref);
 	/*
 	 * A command like "git branch -M currentbranch currentbranch" cannot
 	 * cause the worktree to become inconsistent with HEAD, so allow it.
 	 */
 	if (!strcmp(oldname, newname))
-		validate_branchname(newname, &newref, 0);
+		new_branch_name_res = validate_branchname(newname, &newref, 1);
 	else
-		validate_new_branchname(newname, &newref, force, 0);
-
-	reject_rebase_or_bisect_branch(oldref.buf);
+		new_branch_name_res = validate_new_branchname(newname, &newref, force, 1);
 
 	if (!skip_prefix(oldref.buf, "refs/heads/", &interpreted_oldname) ||
 	    !skip_prefix(newref.buf, "refs/heads/", &interpreted_newname)) {
 		die("BUG: expected prefix missing for refs");
 	}
 
+	get_error_msg(&error_msg, interpreted_oldname, old_branch_name_res,
+				  interpreted_newname, new_branch_name_res);
+	if (strbuf_cmp(&error_msg, &empty))
+		die("%s", error_msg.buf);
+
+	reject_rebase_or_bisect_branch(oldref.buf);
+
 	if (copy)
 		strbuf_addf(&logmsg, "Branch: copied %s to %s",
 			    oldref.buf, newref.buf);
@@ -512,7 +587,7 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
 	if (copy && copy_existing_ref(oldref.buf, newref.buf, logmsg.buf))
 		die(_("Branch copy failed"));
 
-	if (recovery) {
+	if (old_branch_name_res == VALIDATION_1_WARN_BAD_OLD_BRANCH_NAME) {
 		if (copy)
 			warning(_("Created a copy of a misnamed branch '%s'"),
 				interpreted_oldname);
@@ -537,6 +612,8 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
 		die(_("Branch is copied, but update of config-file failed"));
 	strbuf_release(&oldsection);
 	strbuf_release(&newsection);
+	strbuf_release(&error_msg);
+	strbuf_release(&empty);
 }
 
 static GIT_PATH_FUNC(edit_description, "EDIT_DESCRIPTION")
-- 
2.16.1.291.g4437f3f13
Previous: Kaartic SivaraamNext: Junio C Hamano
Message 123 of 127 in “branch: warn user about non-existent branch”
  1. branch: warn user about non-existent branchKaartic Sivaraam, Jul 24, 2017
  2. Change in output as a result of patchKaartic Sivaraam, Jul 24, 2017
  3. Junio C HamanoJul 24, 2017
  4. Kaartic SivaraamJul 25, 2017
  5. Junio C HamanoJul 26, 2017
  6. Can the '--set-upstream' option of branch be removed ?Kaartic Sivaraam, Aug 7, 2017
  7. 1/2 builtin/branch: remove the deprecated '--set-upstream' optionKaartic Sivaraam, Aug 7, 2017
  8. 2/2 branch: quote branch/ref names to improve readabilityKaartic Sivaraam, Aug 7, 2017
  9. Junio C HamanoAug 7, 2017
  10. Kaartic SivaraamAug 8, 2017
  11. Junio C HamanoAug 8, 2017
  12. 1/2 builtin/branch: stop supporting the use of --set-upstream optionKaartic Sivaraam, Aug 8, 2017
  13. 2/2 branch: quote branch/ref names to improve readabilityKaartic Sivaraam, Aug 8, 2017
  14. Stefan BellerAug 8, 2017
  15. Junio C HamanoAug 8, 2017
  16. Stefan BellerAug 8, 2017
  17. Martin ÅgrenAug 8, 2017
  18. Kaartic SivaraamAug 14, 2017
  19. 1/2 builtin/branch: stop supporting the use of --set-upstream optionKaartic Sivaraam, Aug 14, 2017
  20. Martin ÅgrenAug 14, 2017
  21. Kaartic SivaraamAug 15, 2017
  22. Junio C HamanoAug 14, 2017
  23. Kaartic SivaraamAug 15, 2017
  24. Junio C HamanoAug 15, 2017
  25. Kaartic SivaraamAug 16, 2017
  26. Junio C HamanoAug 16, 2017
  27. Kaartic SivaraamAug 17, 2017
  28. Junio C HamanoSep 12, 2017
  29. Kaartic SivaraamSep 12, 2017
  30. branch: strictly don't allow a branch with name 'HEAD'Kaartic Sivaraam, Sep 12, 2017
  31. 1/3 test: cleanup cruft of a testKaartic Sivaraam, Aug 17, 2017
  32. 2/3 builtin/branch: stop supporting the use of --set-upstream optionKaartic Sivaraam, Aug 17, 2017
  33. Martin ÅgrenAug 17, 2017
  34. Junio C HamanoAug 17, 2017
  35. Kaartic SivaraamAug 18, 2017
  36. Junio C HamanoAug 18, 2017
  37. Martin ÅgrenAug 18, 2017
  38. Junio C HamanoAug 17, 2017
  39. Kaartic SivaraamAug 18, 2017
  40. Junio C HamanoAug 18, 2017
  41. 3/3 branch: quote branch/ref names to improve readabilityKaartic Sivaraam, Aug 17, 2017
  42. Kaartic SivaraamAug 7, 2017
  43. 0/5 branch: improve error messages of branch renamingKaartic Sivaraam, Sep 19, 2017
  44. 1/5 builtin/checkout: avoid usage of '!!'Kaartic Sivaraam, Sep 19, 2017
  45. Junio C HamanoSep 20, 2017
  46. Kaartic SivaraamSep 20, 2017
  47. Kaartic SivaraamSep 20, 2017
  48. Junio C HamanoSep 21, 2017
  49. Kaartic SivaraamSep 23, 2017
  50. 2/5 branch: document the usage of certain parametersKaartic Sivaraam, Sep 19, 2017
  51. Junio C HamanoSep 20, 2017
  52. Kaartic SivaraamSep 20, 2017
  53. Junio C HamanoSep 21, 2017
  54. 3/5 branch: cleanup branch name validationKaartic Sivaraam, Sep 19, 2017
  55. Junio C HamanoSep 20, 2017
  56. Kaartic SivaraamSep 20, 2017
  57. Junio C HamanoSep 21, 2017
  58. Kaartic SivaraamSep 23, 2017
  59. Kaartic SivaraamSep 20, 2017
  60. 4/5 branch: introduce dont_fail parameter for update validationKaartic Sivaraam, Sep 19, 2017
  61. 5/5 builtin/branch: give more useful error messages when renamingKaartic Sivaraam, Sep 19, 2017
  62. [RFC SAMPLE] builtin/branch: give more useful error messages when renamingKaartic Sivaraam, Sep 19, 2017
  63. Kaartic SivaraamSep 19, 2017
  64. Stefan BellerSep 20, 2017
  65. Kaartic SivaraamSep 23, 2017
  66. 0/5 Give more useful error messages when renaming a branchKaartic Sivaraam, Sep 25, 2017
  67. 1/5 branch: improve documentation and naming of certain parametersKaartic Sivaraam, Sep 25, 2017
  68. Stefan BellerOct 20, 2017
  69. Eric SunshineOct 20, 2017
  70. Kaartic SivaraamOct 21, 2017
  71. Kaartic SivaraamOct 21, 2017
  72. 2/5 branch: re-order function arguments to group related argumentsKaartic Sivaraam, Sep 25, 2017
  73. Stefan BellerOct 20, 2017
  74. Kaartic SivaraamOct 21, 2017
  75. Stefan BellerOct 23, 2017
  76. 3/5 branch: cleanup branch name validationKaartic Sivaraam, Sep 25, 2017
  77. 4/5 branch: introduce dont_fail parameter for create validationKaartic Sivaraam, Sep 25, 2017
  78. 5/5 builtin/branch: give more useful error messages when renamingKaartic Sivaraam, Sep 25, 2017
  79. Stefan BellerOct 23, 2017
  80. Kaartic SivaraamOct 24, 2017
  81. Kaartic SivaraamOct 20, 2017
  82. Stefan BellerOct 20, 2017
  83. 0/4 give more useful error messages while renaming branchKaartic Sivaraam, Nov 2, 2017
  84. 2/4 branch: re-order function arguments to group related argumentsKaartic Sivaraam, Nov 2, 2017
  85. Junio C HamanoNov 6, 2017
  86. Kaartic SivaraamNov 12, 2017
  87. Junio C HamanoNov 13, 2017
  88. Kaartic SivaraamNov 13, 2017
  89. 3/4 branch: introduce dont_fail parameter for branchname validationKaartic Sivaraam, Nov 2, 2017
  90. Kaartic SivaraamNov 2, 2017
  91. Stefan BellerNov 2, 2017
  92. Kaartic SivaraamNov 3, 2017
  93. Junio C HamanoNov 6, 2017
  94. Kaartic SivaraamNov 12, 2017
  95. 4/4 builtin/branch: give more useful error messages when renamingKaartic Sivaraam, Nov 2, 2017
  96. Eric SunshineNov 2, 2017
  97. Kaartic SivaraamNov 3, 2017
  98. Junio C HamanoNov 6, 2017
  99. Kaartic SivaraamNov 12, 2017
  100. Kevin DaudtNov 12, 2017
  101. Kaartic SivaraamNov 13, 2017
  102. Kevin DaudtNov 13, 2017
  103. Kaartic SivaraamNov 14, 2017
  104. 1/4 branch: improve documentation and naming of 'create_branch()'Kaartic Sivaraam, Nov 2, 2017
  105. 0/4 cleanups surrounding branchKaartic Sivaraam, Nov 18, 2017
  106. 1/4 branch: improve documentation and naming of create_branch() parametersKaartic Sivaraam, Nov 18, 2017
  107. 2/4 branch: group related arguments of create_branch()Kaartic Sivaraam, Nov 18, 2017
  108. 3/4 branch: update warning message shown when copying a misnamed branchKaartic Sivaraam, Nov 18, 2017
  109. 4/4 builtin/branch: strip refs/heads/ using skip_prefixKaartic Sivaraam, Nov 18, 2017
  110. Eric SunshineNov 19, 2017
  111. Kaartic SivaraamNov 19, 2017
  112. Eric SunshineNov 19, 2017
  113. 4/4 builtin/branch: strip refs/heads/ using skip_prefixKaartic Sivaraam, Nov 29, 2017
  114. 4/4 builtin/branch: strip refs/heads/ using skip_prefixKaartic Sivaraam, Dec 1, 2017
  115. SZEDER GáborDec 4, 2017
  116. Junio C HamanoDec 7, 2017
  117. Junio C HamanoDec 7, 2017
  118. Kaartic SivaraamDec 8, 2017
  119. 0/3 give more useful error messages while renaming branch (reboot)Kaartic Sivaraam, Mar 10, 2018
  120. 1/3 branch: introduce dont_fail parameter for branchname validationKaartic Sivaraam, Mar 10, 2018
  121. Junio C HamanoMar 15, 2018
  122. Kaartic SivaraamMar 16, 2018
  123. 2/3 builtin/branch: give more useful error messages when renamingKaartic Sivaraam, Mar 10, 2018
  124. Junio C HamanoMar 15, 2018
  125. Kaartic SivaraamMar 24, 2018
  126. 3/3 t/t3200: fix a typo in a test descriptionKaartic Sivaraam, Mar 10, 2018
  127. Junio C HamanoMar 15, 2018

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.