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

Re: [PATCH] branch: allow "-" as a short-hand for "previous branch"

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Aug 19, 2022, 13:05 UTC
Message-ID
<55n449n3-71r9-28n9-094q-6r61545r7505@tzk.qr>
In-Reply-To
<5194s6qn-570s-6053-2104-9s22qo1874sn@tzk.qr>
Hi Junio & Rubén,
On Tue, 16 Aug 2022, Johannes Schindelin wrote:
Show 33 quoted lines
> On Mon, 8 Aug 2022, Junio C Hamano wrote:
>
> > Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:
> >
> > > @@ -1420,6 +1420,12 @@ static int interpret_nth_prior_checkout(struct repository *r,
> > >  	const char *brace;
> > >  	char *num_end;
> > >
> > > +	if (namelen == 1 && *name == '-') {
> > > +		brace = name;
> > > +		nth = 1;
> > > +		goto find_nth_checkout;
> > > +	}
> > > +
> > >  	if (namelen < 4)
> > >  		return -1;
> > >  	if (name[0] != '@' || name[1] != '{' || name[2] != '-')
> >
> > If a solution along this line works, it would be far cleaner design
> > than the various hacks we have done in the past, noticing "-" and
> > replacing with "@{-1}".
>
> Indeed, but it does not work as-is: `interpret_nth_prior_checkout()` is
> used on prefixes of a rev, and for the special handling of `-` we cannot
> have that.
>
> [...]
>
> One thing we could do, however, would be to patch only
> `repo_interpret_branch_name()`, i.e. only allow `-` to imply the
> previous branch name in invocations where a branch name is asked for
> _explicitly_. I.e. not any random revision, but specifically a branch
> name.
This patch does that:
-- snip --
diff --git a/object-name.c b/object-name.c
index 4d2746574cd..a2732be3b71 100644
--- a/object-name.c
+++ b/object-name.c
@@ -1616,6 +1616,20 @@ int repo_interpret_branch_name(struct repository *r,
 	if (!namelen)
 		namelen = strlen(name);

+	if (namelen == 1 && *name == '-') {
+		struct grab_nth_branch_switch_cbdata cb = {
+			.remaining = 1,
+			.sb = buf
+		};
+
+		if (refs_for_each_reflog_ent_reverse(get_main_ref_store(r),
+						     "HEAD",
+						     grab_nth_branch_switch,
+						     &cb) <= 0)
+			return -1;
+		return namelen;
+	}
+
 	if (!options->allowed || (options->allowed & INTERPRET_BRANCH_LOCAL)) {
 		len = interpret_nth_prior_checkout(r, name, namelen, buf);
 		if (!len) {
diff --git a/t/t3204-branch-name-interpretation.sh b/t/t3204-branch-name-interpretation.sh
index 993a6b5eff7..5acbef95913 100755
--- a/t/t3204-branch-name-interpretation.sh
+++ b/t/t3204-branch-name-interpretation.sh
@@ -67,6 +67,22 @@ test_expect_success 'delete branch via @{-1}' '
 	expect_deleted previous-del
 '

+test_expect_success 'delete branch via -' '
+	git checkout -b previous-del &&
+	git checkout - &&
+
+	git branch -d - &&
+	expect_deleted previous-del &&
+
+	git branch previous-del2 &&
+	git checkout -b previous-del &&
+	git checkout - &&
+
+	git branch -d previous-del2 - &&
+	expect_deleted previous-del &&
+	expect_deleted previous-del2
+'
+
 test_expect_success 'delete branch via local @{upstream}' '
 	git branch local-del &&
 	git branch --set-upstream-to=local-del &&
-- snap --

This adds support for things like `git branch -d -`, and it even verifies
that that works.

What does not work with this patch is `git show -`. I am not sure whether
we want to make that work, although I have to admit that I would use it.
Often. And this patch would make it work, the test suite even passes with
it:

-- snip --
diff --git a/revision.c b/revision.c
index f4eee11cc8b..207b554aef1 100644
--- a/revision.c
+++ b/revision.c
@@ -2802,7 +2802,7 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s
 		revarg_opt |= REVARG_CANNOT_BE_FILENAME;
 	for (left = i = 1; i < argc; i++) {
 		const char *arg = argv[i];
-		if (!seen_end_of_options && *arg == '-') {
+		if (!seen_end_of_options && *arg == '-' && arg[1]) {
 			int opts;

 			opts = handle_revision_pseudo_opt(
-- snap --

That latter patch obviously needs at least one accompanying test case, and
the patch series would then need to drop the special-casings we already
have for "-".

Rubén, do you want to take this a bit further?

Ciao,
Dscho

P.S.: For convenient fetching, I opened a draft PR here:
https://github.com/gitgitgadget/git/pull/1329
Previous: Johannes SchindelinNext: Junio C Hamano
Message 7 of 24 in “branch: allow "-" as a short-hand for "previous branch"”
  1. branch: allow "-" as a short-hand for "previous branch"Rubén Justo via GitGitGadget, Aug 7, 2022
  2. Johannes SchindelinAug 8, 2022
  3. Junio C HamanoAug 8, 2022
  4. Rubén JustoAug 13, 2022
  5. Junio C HamanoAug 13, 2022
  6. Johannes SchindelinAug 16, 2022
  7. Johannes SchindelinAug 19, 2022
  8. Junio C HamanoAug 19, 2022
  9. Rubén JustoAug 25, 2022
  10. Junio C HamanoAug 25, 2022
  11. Rubén JustoAug 25, 2022
  12. Rubén JustoAug 13, 2022
  13. Junio C HamanoAug 8, 2022
  14. Rubén JustoAug 13, 2022
  15. Johannes SchindelinAug 16, 2022
  16. Rubén JustoAug 16, 2022
  17. Johannes SchindelinAug 19, 2022
  18. allow "-" as short-hand for "@{-1}" in "branch -d"Rubén Justo via GitGitGadget, Aug 16, 2022
  19. Junio C HamanoAug 16, 2022
  20. Rubén JustoAug 16, 2022
  21. branch: allow "-" as a short-hand for "previous branch"Rubén Justo, Aug 16, 2022
  22. branch: allow "-" as a shortcut for "previous branch"Rubén Justo, Sep 11, 2022
  23. Junio C HamanoSep 12, 2022
  24. Rubén JustoSep 12, 2022

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.