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

[RFC PATCH v3 3/4] branch: introduce dont_fail parameter for branchname validation

From
Kaartic Sivaraam <kaartic.sivaraam@gmail.com>
Date
Nov 2, 2017, 06:54 UTC
Message-ID
<20171102065407.25404-4-kaartic.sivaraam@gmail.com>
In-Reply-To
<20171102065407.25404-1-kaartic.sivaraam@gmail.com>

This parameter allows the branchname validation functions to optionally return a flag specifying the reason for failure, when requested. This allows the caller to know why it was about to die. This allows more useful error messages to be given to the user when trying to rename a branch.

The flags are specified in the form of an enum and values for success flags have been assigned explicitly to clearly express that certain callers rely on those values and they cannot be arbitrary.

Only the logic has been added but no caller has been made to use it, yet. So, no functional changes.

Signed-off-by: Kaartic Sivaraam <kaarticsivaraam91196@gmail.com>
Signed-off-by: Kaartic Sivaraam <kaartic.sivaraam@gmail.com>
---
 branch.c           | 62 +++++++++++++++++++++++++++++++-----------------------
 branch.h           | 60 ++++++++++++++++++++++++++++++++++++++++++----------
 builtin/branch.c   |  4 ++--
 builtin/checkout.c |  5 +++--
 4 files changed, 90 insertions(+), 41 deletions(-)
diff --git a/branch.c b/branch.c
index 7c8093041..7db2e3296 100644
--- a/branch.c
+++ b/branch.c
@@ -178,41 +178,51 @@ int read_branch_desc(struct strbuf *buf, const char *branch_name)
 	return 0;
 }
 
-/*
- * Check if 'name' can be a valid name for a branch; die otherwise.
- * Return 1 if the named branch already exists; return 0 otherwise.
- * Fill ref with the full refname for the branch.
- */
-int validate_branchname(const char *name, struct strbuf *ref)
+enum branch_validation_result validate_branchname(const char *name, struct strbuf *ref, unsigned dont_fail)
 {
-	if (strbuf_check_branch_ref(ref, name))
-		die(_("'%s' is not a valid branch name."), name);
+	if (strbuf_check_branch_ref(ref, name)) {
+		if (dont_fail)
+			return INVALID_BRANCH_NAME;
+		else
+			die(_("'%s' is not a valid branch name."), name);
+	}
 
-	return ref_exists(ref->buf);
+	if(ref_exists(ref->buf))
+		return BRANCH_EXISTS;
+	else
+		return BRANCH_DOESNT_EXIST;
 }
 
-/*
- * Check if a branch 'name' can be created as a new branch; die otherwise.
- * 'force' can be used when it is OK for the named branch already exists.
- * Return 1 if the named branch already exists; return 0 otherwise.
- * Fill ref with the full refname for the branch.
- */
-int validate_new_branchname(const char *name, struct strbuf *ref, int force)
+enum branch_validation_result validate_new_branchname(const char *name, struct strbuf *ref, int force, unsigned dont_fail)
 {
 	const char *head;
 
-	if (!validate_branchname(name, ref))
-		return 0;
+	if(dont_fail) {
+		enum branch_validation_result res = validate_branchname(name, ref, 1);
+		if (res == INVALID_BRANCH_NAME || res == BRANCH_DOESNT_EXIST)
+				return res;
+	} else {
+		if(validate_branchname(name, ref, 0) == BRANCH_DOESNT_EXIST)
+			return BRANCH_DOESNT_EXIST;
+	}
 
-	if (!force)
-		die(_("A branch named '%s' already exists."),
-		    ref->buf + strlen("refs/heads/"));
+	if (!force) {
+		if (dont_fail)
+			return BRANCH_EXISTS_NO_FORCE;
+		else
+			die(_("A branch named '%s' already exists."),
+				ref->buf + strlen("refs/heads/"));
+	}
 
 	head = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
-	if (!is_bare_repository() && head && !strcmp(head, ref->buf))
-		die(_("Cannot force update the current branch."));
+	if (!is_bare_repository() && head && !strcmp(head, ref->buf)) {
+		if (dont_fail)
+			return CANNOT_FORCE_UPDATE_CURRENT_BRANCH;
+		else
+			die(_("Cannot force update the current branch."));
+	}
 
-	return 1;
+	return BRANCH_EXISTS;
 }
 
 static int check_tracking_branch(struct remote *remote, void *cb_data)
@@ -259,8 +269,8 @@ void create_branch(const char *name, const char *start_name,
 		explicit_tracking = 1;
 
 	if ((track == BRANCH_TRACK_OVERRIDE || clobber_head_ok)
-	    ? validate_branchname(name, &ref)
-	    : validate_new_branchname(name, &ref, force)) {
+	    ? validate_branchname(name, &ref, 0)
+	    : validate_new_branchname(name, &ref, force, 0)) {
 		if (!force)
 			dont_change_ref = 1;
 		else
diff --git a/branch.h b/branch.h
index 85052628b..0c178ec5a 100644
--- a/branch.h
+++ b/branch.h
@@ -27,20 +27,58 @@ void create_branch(const char *name, const char *start_name,
 		   enum branch_track track, int force, int clobber_head_ok,
 		   int reflog, int quiet);
 
-/*
- * Check if 'name' can be a valid name for a branch; die otherwise.
- * Return 1 if the named branch already exists; return 0 otherwise.
- * Fill ref with the full refname for the branch.
- */
-extern int validate_branchname(const char *name, struct strbuf *ref);
+enum branch_validation_result {
+	/* Flags that convey that validation FAILED */
+	BRANCH_EXISTS_NO_FORCE = -3,
+	CANNOT_FORCE_UPDATE_CURRENT_BRANCH,
+	INVALID_BRANCH_NAME,
+	/* Flags that convey that validation SUCCEEDED */
+	BRANCH_DOESNT_EXIST = 0,
+	BRANCH_EXISTS = 1,
+};
 
 /*
- * Check if a branch 'name' can be created as a new branch; die otherwise.
- * 'force' can be used when it is OK for the named branch already exists.
- * Return 1 if the named branch already exists; return 0 otherwise.
- * Fill ref with the full refname for the branch.
+ * Check if 'name' can be a valid name for a branch; die otherwise.
+ *
+ *   - name is the new branch name
+ *
+ *   - ref is used to return the full refname for the branch
+ *
+ * The return values have the following meaning,
+ *
+ *   - If dont_fail is 0, the function dies in case of failure and returns flags of
+ *     'branch_validation_result' that indicate status of the given branch. The positive
+ *     non-zero flag implies that the branch exists.
+ *
+ *   - If dont_fail is 1, the function doesn't die in case of failure but returns flags
+ *     of 'branch_validaton_result' that specify the reason for failure. The behaviour in case of
+ *     success is same as above.
+ *
  */
-extern int validate_new_branchname(const char *name, struct strbuf *ref, int force);
+extern enum branch_validation_result validate_branchname(const char *name, struct strbuf *ref, unsigned dont_fail);
+
+/*
+ * Check if a branch 'name' can be created as a new branch.
+ *
+ *   - name is the new branch name
+ *
+ *   - ref is used to return the full refname for the branch
+ *
+ *   - force can be used when it is OK if the named branch already exists.
+ *     the currently checkout branch; with 'shouldnt_exist', it has no effect.
+ *
+ * The return values have the following meaning,
+ *
+ *   - If dont_fail is 0, the function dies in case of failure and returns flags of
+ *     'branch_validation_result' that indicate that convey status of given branch. The positive
+ *     non-zero flag implies that the branch can be force updated.
+ *
+ *   - If dont_fail is 1, the function doesn't die in case of failure but returns flags
+ *     of 'branch_validaton_result' that specify the reason for failure. The behaviour in case of
+ *     success is same as above.
+ *
+ */
+extern enum branch_validation_result validate_new_branchname(const char *name, struct strbuf *ref, int force, unsigned dont_fail);
 
 /*
  * Remove information about the state of working on the current
diff --git a/builtin/branch.c b/builtin/branch.c
index df06ac968..7018e5d75 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -487,9 +487,9 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
 	 * cause the worktree to become inconsistent with HEAD, so allow it.
 	 */
 	if (!strcmp(oldname, newname))
-		validate_branchname(newname, &newref);
+		validate_branchname(newname, &newref, 0);
 	else
-		validate_new_branchname(newname, &newref, force);
+		validate_new_branchname(newname, &newref, force, 0);
 
 	reject_rebase_or_bisect_branch(oldref.buf);
 
diff --git a/builtin/checkout.c b/builtin/checkout.c
index 5c34a9a0d..4adab3814 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -1288,10 +1288,11 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
 		struct strbuf buf = STRBUF_INIT;
 
 		if (opts.new_branch_force)
-			opts.branch_exists = validate_branchname(opts.new_branch, &buf);
+			opts.branch_exists = validate_branchname(opts.new_branch, &buf, 0);
 		else
 			opts.branch_exists =
-				validate_new_branchname(opts.new_branch, &buf, 0);
+				validate_new_branchname(opts.new_branch, &buf, 0, 0);
+
 		strbuf_release(&buf);
 	}
 
-- 
2.15.0.461.gf957c703b.dirty
Previous: Kaartic SivaraamNext: Kaartic Sivaraam
Message 89 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.