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

[PATCH 7/7 (v2)] checkout -: make "-" to mean "previous branch" everywhere

From
Junio C Hamano <gitster@pobox.com>
Date
Mar 22, 2009, 00:58 UTC
Message-ID
<7vskl69xw3.fsf_-_@gitster.siamese.dyndns.org>
In-Reply-To
<1237673619-12608-8-git-send-email-gitster@pobox.com>

This is iffy, in that it teaches the very low level machinery to interpret it as "the tip of the previous branch" when "-" is fed to it, and has a high risk of unintended side effects.

This makes "git log ..-" to work as expected, which is marginally useful because the revision parameter parser misinterprets the other direction "git log -..". It also makes "git check-ref-format --branch -" to work, which is not very useful because Porcelains can always ask for @{-1}.

It also makes a refname whose last component is "-" forbidden.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin-checkout.c |    8 +++++---
 refs.c             |    3 +++
 sha1_name.c        |   21 +++++++++++++--------
 3 files changed, 21 insertions(+), 11 deletions(-)
diff --git a/builtin-checkout.c b/builtin-checkout.c
index 66df0c0..6b3b450 100644
--- a/builtin-checkout.c
+++ b/builtin-checkout.c
@@ -666,9 +666,11 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
 		arg = argv[0];
 		has_dash_dash = (argc > 1) && !strcmp(argv[1], "--");
 
-		if (!strcmp(arg, "-"))
-			arg = "@{-1}";
-
+		{
+			struct strbuf sb = STRBUF_INIT;
+			strbuf_branchname(&sb, arg);
+			arg = strbuf_detach(&sb, NULL);
+		}
 		if (get_sha1(arg, rev)) {
 			if (has_dash_dash)          /* case (1) */
 				die("invalid reference: %s", arg);
diff --git a/refs.c b/refs.c
index e355489..7e27537 100644
--- a/refs.c
+++ b/refs.c
@@ -735,6 +735,9 @@ int check_ref_format(const char *ref)
 		if (!ch) {
 			if (ref <= cp - 2 && cp[-2] == '.')
 				return CHECK_REF_FORMAT_ERROR;
+			if (ref <= cp - 2 && cp[-2] == '-' &&
+			    (cp - 3 < ref || cp[-3] == '/'))
+				return CHECK_REF_FORMAT_ERROR;
 			if (level < 2)
 				return CHECK_REF_FORMAT_ONELEVEL;
 			return ret;
diff --git a/sha1_name.c b/sha1_name.c
index 904bcd9..3972f4c 100644
--- a/sha1_name.c
+++ b/sha1_name.c
@@ -758,14 +758,19 @@ int interpret_branch_name(const char *name, struct strbuf *buf)
 	const char *brace;
 	char *num_end;
 
-	if (name[0] != '@' || name[1] != '{' || name[2] != '-')
-		return -1;
-	brace = strchr(name, '}');
-	if (!brace)
-		return -1;
-	nth = strtol(name+3, &num_end, 10);
-	if (num_end != brace)
-		return -1;
+	if (name[0] == '-' && !name[1]) {
+		nth = 1;
+		brace = name; /* "end of branch name expression" */
+	} else {
+		if (name[0] != '@' || name[1] != '{' || name[2] != '-')
+			return -1;
+		brace = strchr(name, '}');
+		if (!brace)
+			return -1;
+		nth = strtol(name+3, &num_end, 10);
+		if (num_end != brace)
+			return -1;
+	}
 	if (nth <= 0)
 		return -1;
 	cb.alloc = nth;
-- 
1.6.2.1.299.gda643a
Previous: Junio C HamanoNext: Bert Wesarg
Message 9 of 14 in “Clean up interpret_nth_last_branch feature”
  1. 0/7 Clean up interpret_nth_last_branch featureJunio C Hamano, Mar 21, 2009
  2. 1/7 check_ref_format(): tighten refname rulesJunio C Hamano, Mar 21, 2009
  3. 2/7 Rename interpret/substitute nth_last_branch functionsJunio C Hamano, Mar 21, 2009
  4. 3/7 check-ref-format --branch: give Porcelain a way to grok branch shorthandJunio C Hamano, Mar 21, 2009
  5. 4/7 strbuf_branchname(): a wrapper for branch name shorthandsJunio C Hamano, Mar 21, 2009
  6. 5/7 Fix "branch -m @{-1} newname"Junio C Hamano, Mar 21, 2009
  7. 6/7 strbuf_check_branch_ref(): a helper to check a refname for a branchJunio C Hamano, Mar 21, 2009
  8. 7/7 checkout -: make "-" to mean "previous branch" everywhereJunio C Hamano, Mar 21, 2009
  9. 7/7 checkout -: make "-" to mean "previous branch" everywhereJunio C Hamano, Mar 22, 2009
  10. Bert WesargMar 22, 2009
  11. Junio C HamanoMar 22, 2009
  12. Junio C HamanoMar 21, 2009
  13. Johannes SchindelinMar 22, 2009
  14. Junio C HamanoMar 22, 2009

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.