Skip to contentSkip to conversation

diff: allow "-" as a short-hand for "last branch"

PATCH Superseded mash 2 participants 4 messages Mar 8, 2017 – Mar 10, 2017

lore
Share & tools
lore

mash

author
#0 lore raw

PATCH 1/1 diff: allow "-" as a short-hand for "last branch"

Just like "git merge -" is a short-hand for "git merge @{-1}" to conveniently merge the previous branch, "git diff -" is a short-hand for "git diff @{-1}" to conveniently diff against the previous branch.

Allow the usage of "-" in the dot dot notation to allow the use of "git diff -..HEAD^" as a short-hand for "git diff @{-1}..HEAD^".

---
This is a GSoC microproject. I'm not sure how useful this change is. Please review it and take it apart.

I'm not very happy with the change of handle_revision_arg. Maybe I should teach sha1_name.c:get_sha1_basic how to handle a dash instead.

Documentation was not updated. I could only think of updating revisions.txt but that might be misleading since the use of dash does not work everywhere.

 revision.c           | 22 ++++++++++++++++++--
 t/t4063-diff-last.sh | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 78 insertions(+), 2 deletions(-)
 create mode 100755 t/t4063-diff-last.sh
revision.c +20 -2
index b37dbec..c331bd5 100644
@@ -1439,6 +1439,7 @@ int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsi
 	const char *arg = arg_;
 	int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
 	unsigned get_sha1_flags = 0;
+	static const char previous_branch[] = "@{-1}";
 
 	flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
 
@@ -1457,6 +1458,8 @@ int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsi
 
 		if (!*next)
 			next = head_by_default;
+		else if (!strcmp(next, "-"))
+			next = previous_branch;
 		if (dotdot == arg)
 			this = head_by_default;
 		if (this == head_by_default && next == head_by_default &&
@@ -1469,6 +1472,8 @@ int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsi
 				*dotdot = '.';
 				return -1;
 			}
+		} else if (!strcmp(this, "-")) {
+			this = previous_branch;
 		}
 		if (!get_sha1_committish(this, from_sha1) &&
 		    !get_sha1_committish(next, sha1)) {
@@ -1568,6 +1573,8 @@ int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsi
 	if (revarg_opt & REVARG_COMMITTISH)
 		get_sha1_flags = GET_SHA1_COMMITTISH;
 
+	if (!strcmp(arg, "-"))
+		arg = previous_branch;
 	if (get_sha1_with_context(arg, get_sha1_flags, sha1, &oc))
 		return revs->ignore_missing ? 0 : -1;
 	if (!cant_be_filename)
@@ -1578,6 +1585,15 @@ int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsi
 	return 0;
 }
 
+/*
+ * Check if the argument is supposed to be a revision argument instead of an
+ * option even though it starts with a dash.
+ */
+static int is_revision_arg(const char *arg)
+{
+	return *arg == '\0' || starts_with(arg, "..");
+}
+
 struct cmdline_pathspec {
 	int alloc;
 	int nr;
@@ -1621,7 +1637,9 @@ static void read_revisions_from_stdin(struct rev_info *revs,
 				seen_dashdash = 1;
 				break;
 			}
-			die("options not supported in --stdin mode");
+			if (!is_revision_arg(sb.buf + 1)) {
+				die("options not supported in --stdin mode");
+			}
 		}
 		if (handle_revision_arg(sb.buf, revs, 0,
 					REVARG_CANNOT_BE_FILENAME))
@@ -2205,7 +2223,7 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s
 	read_from_stdin = 0;
 	for (left = i = 1; i < argc; i++) {
 		const char *arg = argv[i];
-		if (*arg == '-') {
+		if (*arg == '-' && !is_revision_arg(arg + 1)) {
 			int opts;
 
 			opts = handle_revision_pseudo_opt(submodule,
t/t4063-diff-last.sh new file +58 -0

58 lines not on this page · raw message

Signature · 1 line
2.9.3
author
#1 lore raw

RFC PATCH v2 1/1 diff: allow "-" as a short-hand for "last branch"

Just like "git merge -" is a short-hand for "git merge @{-1}" to conveniently merge the previous branch, "git diff -" is a short-hand for "git diff @{-1}" to conveniently diff against the previous branch.

Allow the usage of "-" in the dot dot notation to allow the use of "git diff -..HEAD^" as a short-hand for "git diff @{-1}..HEAD^".

---
Add tests to confirm that passing in this short-hand from stdin works.

Handling the dash in sha1_name:get_sha1_basic is not an issue but git was designed with the dash in mind for options not for this weird short-hand so as long as there's no decision made that git should actually have this short-hand everywhere it does not seem like a good idea to change anything in there because it would probably have unwanted side-effects.

For example for now just handle_revision_arg was modified which is mainly used by git diff but also used in builtin/pack-objects.c:get_object_list#2785 which is terrible since this may have already introduced an unwanted the side-effect.

Bypassing the whatever starts with a dash is always an option filters is not a nice thing to do either.

Overall I see the benefit of the dash short-hand when doing checkouts since it's very similar to "cd -" and switching between two branches is something one would commonly do but for every git command where executing it twice results into the same action achieving the same result twice it seems like a not that useful short-hand.

Example:
master# g co maint
maint# g co - # Switch to master
master# g co - # Switch to maint (different result) maint# g d - # diff master..HEAD
maint# g d - # diff master..HEAD (same result - less useful)

This is obviously only my own option.
Because of what was stated above I've now marked this as open for discussion since I'm currently not convinced that applying the patch is a good idea.

 revision.c           | 22 +++++++++++++++--
 t/t4063-diff-last.sh | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 88 insertions(+), 2 deletions(-)
 create mode 100755 t/t4063-diff-last.sh
revision.c +20 -2
index b37dbec..c331bd5 100644
@@ -1439,6 +1439,7 @@ int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsi
 	const char *arg = arg_;
 	int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
 	unsigned get_sha1_flags = 0;
+	static const char previous_branch[] = "@{-1}";
 
 	flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
 
@@ -1457,6 +1458,8 @@ int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsi
 
 		if (!*next)
 			next = head_by_default;
+		else if (!strcmp(next, "-"))
+			next = previous_branch;
 		if (dotdot == arg)
 			this = head_by_default;
 		if (this == head_by_default && next == head_by_default &&
@@ -1469,6 +1472,8 @@ int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsi
 				*dotdot = '.';
 				return -1;
 			}
+		} else if (!strcmp(this, "-")) {
+			this = previous_branch;
 		}
 		if (!get_sha1_committish(this, from_sha1) &&
 		    !get_sha1_committish(next, sha1)) {
@@ -1568,6 +1573,8 @@ int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsi
 	if (revarg_opt & REVARG_COMMITTISH)
 		get_sha1_flags = GET_SHA1_COMMITTISH;
 
+	if (!strcmp(arg, "-"))
+		arg = previous_branch;
 	if (get_sha1_with_context(arg, get_sha1_flags, sha1, &oc))
 		return revs->ignore_missing ? 0 : -1;
 	if (!cant_be_filename)
@@ -1578,6 +1585,15 @@ int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsi
 	return 0;
 }
 
+/*
+ * Check if the argument is supposed to be a revision argument instead of an
+ * option even though it starts with a dash.
+ */
+static int is_revision_arg(const char *arg)
+{
+	return *arg == '\0' || starts_with(arg, "..");
+}
+
 struct cmdline_pathspec {
 	int alloc;
 	int nr;
@@ -1621,7 +1637,9 @@ static void read_revisions_from_stdin(struct rev_info *revs,
 				seen_dashdash = 1;
 				break;
 			}
-			die("options not supported in --stdin mode");
+			if (!is_revision_arg(sb.buf + 1)) {
+				die("options not supported in --stdin mode");
+			}
 		}
 		if (handle_revision_arg(sb.buf, revs, 0,
 					REVARG_CANNOT_BE_FILENAME))
@@ -2205,7 +2223,7 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s
 	read_from_stdin = 0;
 	for (left = i = 1; i < argc; i++) {
 		const char *arg = argv[i];
-		if (*arg == '-') {
+		if (*arg == '-' && !is_revision_arg(arg + 1)) {
 			int opts;
 
 			opts = handle_revision_pseudo_opt(submodule,
t/t4063-diff-last.sh new file +68 -0

68 lines not on this page · raw message

Signature · 1 line
2.9.3
author
#2 lore raw
Quoted 3 lines

From the discussion over the different versions of my patch, I get the feeling that enabling this shorthand for all the commands is the direction that git wants to move in.

Interesting.

Quoted 1 line

Sorry about the time you spent on this patch.

Don't worry about it. I just seem to be too stupid to search through the mailing list archive properly.

Maybe you can reuse the diff tests. I'll do another microproject then.

mash

The original message doesn't seem to cc the mailing list:

Quoted 40 lines

Hey, I have already worked on this, and I made the change inside sha1_name.c.

The final version of my patch is here[1].

Quoted 6 lines

Handling the dash in sha1_name:get_sha1_basic is not an issue but git was designed with the dash in mind for options not for this weird short-hand so as long as there's no decision made that git should actually have this short-hand everywhere it does not seem like a good idea to change anything in there because it would probably have unwanted side-effects.

Actually, this was discussed even when I was working on this patch.

I said [2]

Quoted 4 lines

Making a change in sha1_name.c will touch a lot of commands (setup_revisions is called from everywhere in the codebase), so, I am still trying to figure out how to do this such that the rest of the codepath remains unchanged.

Matthieu replied to this [3]

Quoted 4 lines

I don't have strong opinion on this: I tend to favor consistency and supporting "-" everywhere goes in this direction, but I think the downsides should be considered too. A large part of the exercice here is to write a good commit message!

From the discussion over the different versions of my patch, I get the feeling that enabling this shorthand for all the commands is the direction that git wants to move in.

Sorry about the time you spent on this patch.

[1]: http://public-inbox.org/git/[email protected]/
[2]: https://public-inbox.org/git/[email protected]/
[3]: https://public-inbox.org/git/[email protected]/

Thanks,
Siddharth.

#3 lore raw
Quoted 1 line · mash

Maybe you can reuse the diff tests. I'll do another microproject then.

Yeah, definitely. If there are more tests required, then I will reuse your ones!

Quoted 4 lines

mash

The original message doesn't seem to cc the mailing list:

Thanks! It was rather daft of me to not realise this. I was waiting for it to appear on public-inbox.

I re-sent it with the CC. The timestamp is a little bit skewed, but I think it should make sense.

Thanks,
Siddharth.