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

[PATCH 1/2] range-diff: reorder argument handling

From
Johannes Schindelin via GitGitGadget <gitgitgadget@gmail.com>
Date
Aug 23, 2022, 12:35 UTC
Message-ID
<7fe4f228ae0a88b99b489018c076a7008642610e.1661258122.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.1335.git.1661258122.gitgitgadget@gmail.com>
From: Johannes Schindelin <johannes.schindelin@gmx.de>

In d9c66f0b5bf (range-diff: first rudimentary implementation, 2018-08-13), we introduced the argument handling of the `range-diff` command, special-casing three different stanzas based on the argument count.

The somewhat unorthodox order (first handling the case of 2 arguments, then 3, then 1) was chosen for clarity: the natural argument number is 2 because that is how many revision ranges are used internally. The code to handle three arguments is relatively trivial, so it was added next. And finally, the code to ungarble a single symmetric range into two separate ones was added, because it was the most complicated (the most inelegant part being about interpreting empty sides of the symmetric range as `HEAD`).

In preparation for allowing pathspecs in `git range-diff` invocations, where we no longer have the luxury of using the number of arguments to disambiguate between these three different ways to specify the commit ranges, we need to order these cases by argument count, in ascending order.

This patch is best viewed with `--color-moved`.
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 builtin/range-diff.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
diff --git a/builtin/range-diff.c b/builtin/range-diff.c
index 50318849d65..c8ffcd35aea 100644
--- a/builtin/range-diff.c
+++ b/builtin/range-diff.c
@@ -55,18 +55,7 @@ int cmd_range_diff(int argc, const char **argv, const char *prefix)
 	if (!simple_color)
 		diffopt.use_color = 1;
 
-	if (argc == 2) {
-		if (!is_range_diff_range(argv[0]))
-			die(_("not a commit range: '%s'"), argv[0]);
-		strbuf_addstr(&range1, argv[0]);
-
-		if (!is_range_diff_range(argv[1]))
-			die(_("not a commit range: '%s'"), argv[1]);
-		strbuf_addstr(&range2, argv[1]);
-	} else if (argc == 3) {
-		strbuf_addf(&range1, "%s..%s", argv[0], argv[1]);
-		strbuf_addf(&range2, "%s..%s", argv[0], argv[2]);
-	} else if (argc == 1) {
+	if (argc == 1) {
 		const char *b = strstr(argv[0], "..."), *a = argv[0];
 		int a_len;
 
@@ -85,6 +74,17 @@ int cmd_range_diff(int argc, const char **argv, const char *prefix)
 			b = "HEAD";
 		strbuf_addf(&range1, "%s..%.*s", b, a_len, a);
 		strbuf_addf(&range2, "%.*s..%s", a_len, a, b);
+	} else if (argc == 2) {
+		if (!is_range_diff_range(argv[0]))
+			die(_("not a commit range: '%s'"), argv[0]);
+		strbuf_addstr(&range1, argv[0]);
+
+		if (!is_range_diff_range(argv[1]))
+			die(_("not a commit range: '%s'"), argv[1]);
+		strbuf_addstr(&range2, argv[1]);
+	} else if (argc == 3) {
+		strbuf_addf(&range1, "%s..%s", argv[0], argv[1]);
+		strbuf_addf(&range2, "%s..%s", argv[0], argv[2]);
 	} else {
 		error(_("need two commit ranges"));
 		usage_with_options(builtin_range_diff_usage, options);
-- 
gitgitgadget
Previous: Johannes Schindelin via GitGitGadgetNext: Johannes Schindelin via GitGitGadget
Message 2 of 10 in “Allow passing pathspecs to git range-diff”
  1. 0/2 Allow passing pathspecs to git range-diffJohannes Schindelin via GitGitGadget, Aug 23, 2022
  2. 1/2 range-diff: reorder argument handlingJohannes Schindelin via GitGitGadget, Aug 23, 2022
  3. 2/2 range-diff: optionally accept a pathspecJohannes Schindelin via GitGitGadget, Aug 23, 2022
  4. Junio C HamanoAug 24, 2022
  5. 0/3 Allow passing pathspecs to git range-diffJohannes Schindelin via GitGitGadget, Aug 26, 2022
  6. 1/3 range-diff: reorder argument handlingJohannes Schindelin via GitGitGadget, Aug 26, 2022
  7. 2/3 range-diff: consistently validate the argumentsJohannes Schindelin via GitGitGadget, Aug 26, 2022
  8. Junio C HamanoAug 26, 2022
  9. 3/3 range-diff: optionally accept pathspecsJohannes Schindelin via GitGitGadget, Aug 26, 2022
  10. Junio C HamanoAug 26, 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.