Skip to contentSkip to conversation

diff --no-index: allow pathspec after --

PATCH In review Nguyễn Thái Ngọc Duy 2 participants 5 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 diff --no-index: allow pathspec after --

Another patch to test the water before I put more effort into it.

Commit d516c2d (Teach git-diff-files the new option --no-index - 2007-02-22) brings the bells and whistles of git-diff to the world outside a git repository. This patch continues that direction and adds a new syntax

    git diff --no-index [--] <path> <path> -- <pathspec>

It's easy to guess that the two directories will be filtered by pathspec, which could be useful for filtering out generated files (negative pathspec to rescue!), or simply to limit diff output to a selection of files.

---
 builtin/diff.c  | 26 ++++++++++++++------------
 diff-no-index.c | 49 ++++++++++++++++++++++++++++++++++++++-----------
 2 files changed, 52 insertions(+), 23 deletions(-)
builtin/diff.c +14 -12
index 0f247d2..4e37876 100644
@@ -321,23 +321,25 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
 
 	init_revisions(&rev, prefix);
 
-	if (no_index && argc != i + 2) {
-		if (no_index == DIFF_NO_INDEX_IMPLICIT) {
-			/*
-			 * There was no --no-index and there were not two
-			 * paths. It is possible that the user intended
-			 * to do an inside-repository operation.
-			 */
-			fprintf(stderr, "Not a git repository\n");
-			fprintf(stderr,
-				"To compare two paths outside a working tree:\n");
-		}
+	if (no_index == DIFF_NO_INDEX_IMPLICIT && argc != i + 2) {
+		/*
+		 * There was no --no-index and there were not two
+		 * paths. It is possible that the user intended
+		 * to do an inside-repository operation.
+		 */
+		fprintf(stderr, "Not a git repository\n");
+		fprintf(stderr,
+			"To compare two paths outside a working tree:\n");
 		/* Give the usage message for non-repository usage and exit. */
 		usagef("git diff %s <path> <path>",
 		       no_index == DIFF_NO_INDEX_EXPLICIT ?
 		       "--no-index" : "[--no-index]");
-
 	}
+	if (no_index == DIFF_NO_INDEX_EXPLICIT && argc < i + 2)
+		/* Give the usage message for non-repository usage and exit. */
+		usagef("git diff %s <path> <path>",
+		       no_index == DIFF_NO_INDEX_EXPLICIT ?
+		       "--no-index" : "[--no-index]");
 	if (no_index)
 		/* If this is a no-index diff, just run it and exit there. */
 		diff_no_index(&rev, argc, argv, prefix);
diff-no-index.c +38 -11

106 lines not on this page · raw message

Signature · 1 line
2.1.0.rc0.78.gc0d8480
#1 lore raw
Quoted 11 lines · Nguyễn Thái Ngọc Duy

Another patch to test the water before I put more effort into it.

Commit d516c2d (Teach git-diff-files the new option --no-index - 2007-02-22) brings the bells and whistles of git-diff to the world outside a git repository. This patch continues that direction and adds a new syntax

    git diff --no-index [--] <path> <path> -- <pathspec>

It's easy to guess that the two directories will be filtered by pathspec,...

Ugh. Nobody's diff works that way, and nowhere in our UI we use double-dashes that way, either.

So while I like the idea of "I want to tell Git to compare these two directories with these pathspec", I do not think we would want to touch such a syntax with 10 foot pole X-<.

diff-no-index.c void diff_no_index(struct rev_info *revs, · line 213

Quoted 10 lines
diff-no-index.c +3 -2
@@ -194,19 +207,23 @@ void diff_no_index(struct rev_info *revs,
 		int j;
 		if (!strcmp(argv[i], "--no-index"))
 			i++;
-		else if (!strcmp(argv[i], "--"))
+		else if (!strcmp(argv[i], "--")) {
 			i++;
-		else {
+			break;
+		} else {

I think this part is a good bugfix regardless of your new feature.

The general command line structure ought to be:

   $ git diff [--no-index] [--<diff options>...] [--] <path> <path>

but the existing code is too loose in that "--" is just ignored. The caller in builtin/diff.c makes sure "--no-index" comes before "--", the latter of which stops option processing, so it is not so grave a bug, though.

Coming back to the command line syntax for the new feature, if I had to choose, I would say

	git diff --no-index [-<options>] [--] <path> <path> <pathspec>

perhaps? As we never compare anything other than two things, we can do the following:

* Implicit --no-index heuristics will kick in _ONLY_ when we have two things at the end after parsing options in builtin/diff.c, as before;

* In diff_no_index() after parsing options at its beginning,

- if we have only two things left, they may be

. two files;
. a file and "-" or "-" and a file; or
. two directories (fully traversed without pathspecs)

just as before.

- if we have more than two things left, the first two of these _MUST_ be directories, and the remainder is pathspec to filter the result of directory traversal.

Wluldn't that be cleaner and easier to understand?

author
#2 lore raw
Quoted 13 lines · Junio C Hamano

diff-no-index.c void diff_no_index(struct rev_info *revs, · line 213

Quoted 10 lines
diff-no-index.c +3 -2
@@ -194,19 +207,23 @@ void diff_no_index(struct rev_info *revs,
              int j;
              if (!strcmp(argv[i], "--no-index"))
                      i++;
-             else if (!strcmp(argv[i], "--"))
+             else if (!strcmp(argv[i], "--")) {
                      i++;
-             else {
+                     break;
+             } else {

I think this part is a good bugfix regardless of your new feature.

Quoted 8 lines

The general command line structure ought to be:

   $ git diff [--no-index] [--<diff options>...] [--] <path> <path>

but the existing code is too loose in that "--" is just ignored. The caller in builtin/diff.c makes sure "--no-index" comes before "--", the latter of which stops option processing, so it is not so grave a bug, though.

Yeah I pack everything in one patch because this is more of a design question. Will make separate cleanup patches.

Quoted 20 lines

Coming back to the command line syntax for the new feature, if I had to choose, I would say

        git diff --no-index [-<options>] [--] <path> <path> <pathspec>

perhaps? As we never compare anything other than two things, we can do the following:

* Implicit --no-index heuristics will kick in _ONLY_ when we have two things at the end after parsing options in builtin/diff.c, as before;

* In diff_no_index() after parsing options at its beginning,

- if we have only two things left, they may be

. two files;
. a file and "-" or "-" and a file; or
. two directories (fully traversed without pathspecs)

Quoted 7 lines

just as before.

- if we have more than two things left, the first two of these _MUST_ be directories, and the remainder is pathspec to filter the result of directory traversal.

Wluldn't that be cleaner and easier to understand?

Looks good.

Signature · 1 line
Duy
#3 lore raw
In reply to Junio C Hamano
Quoted 6 lines · Junio C Hamano

Coming back to the command line syntax for the new feature, if I had to choose, I would say

	git diff --no-index [-<options>] [--] <path> <path> <pathspec>

perhaps? As we never compare anything other than two things,...

Actually, I am not so sure about this anymore.

It is not entirely fair to say that

git diff --no-index [--options] A B C D

as comparing A and B while using C and D as pathspec and declare a person who expects otherwise a moron. To a person whose brain is not rotten by years of use of Git, "we never compare anything other than two things with --no-index" is not a given, I am afraid.

It is equally plausible that the same command line may be asking to compare A with B and C with D, i.e. producing

	diff --no-git a/A b/B
        ...
        diff --no-git a/C b/D
        ...

It also equally plausible (taking a cue from "mv A B C Dir/") that it is comparing A, B and C with D/A, D/B and D/C, respectively, producing

	diff --no-git a/A b/D/A
        ...
        diff --no-git a/B b/D/B
        ...
        diff --no-git a/C b/D/C
        ...

The only unambiguous syntax I can think of that avoids such alternative interpretations is something ugly like

    git diff --no-index [-<options>] --src=<path> --dst=<path> [--] <pathspec>

where "src" and "dst" are in line with the existing src/dst-prefix options.

Perhaps we could declare that this is the "canonical" way to spell pathspec-filtered no-index comparison of two directories, but we allow the syntax suggested in the message I am responding to as a short-hand, but I am not sure if that would fly well.

This --src/--dst thing could lead to even uglier tangent. We could use these options to specify what is compared with what else

    --{src,dst}-path=<directory in the working tree>
    --{src,dst}-index
    --{src,dst}-worktree
    --{src,dst}-tree=<tree object>

which allows us to express funky combinations like

	git diff --src-index --dst-worktree [--] <pathspec>
	git diff --src-tree=HEAD --dst-index [--] <pathspec>
	git diff --src-tree=maint --dst-tree=master [--] <pathspec>

The above three would be ugly ways to spell the traditional "short hands", e.g.

git diff [--] <pathspec>
git diff --cached [--] <pathspec>
git diff maint master [--] <pathspec>

respectively. And an obvious fallout of the above is this command line:

	git diff --src-path=A --dst-path=B [--] <pathspec>

Because this does not involve any index, tree or worktree, it cannot be a Git operation, so we know that is --no-index mode; the command line does not need to say --no-index anywhere if we go that route.

author
#4 lore raw
Quoted 20 lines · Junio C Hamano
Quoted 6 lines · Junio C Hamano

Coming back to the command line syntax for the new feature, if I had to choose, I would say

      git diff --no-index [-<options>] [--] <path> <path> <pathspec>

perhaps? As we never compare anything other than two things,...

Actually, I am not so sure about this anymore.

It is not entirely fair to say that

git diff --no-index [--options] A B C D

as comparing A and B while using C and D as pathspec and declare a person who expects otherwise a moron. To a person whose brain is not rotten by years of use of Git, "we never compare anything other than two things with --no-index" is not a given, I am afraid.

Quoted 8 lines

It is equally plausible that the same command line may be asking to compare A with B and C with D, i.e. producing

        diff --no-git a/A b/B
        ...
        diff --no-git a/C b/D
        ...

Heh.. I wrote something like that [1]. An extension of this is feed all filename pairs via stdin.

[1] http://thread.gmane.org/gmane.comp.version-control.git/188435

Quoted 10 lines

It also equally plausible (taking a cue from "mv A B C Dir/") that it is comparing A, B and C with D/A, D/B and D/C, respectively, producing

        diff --no-git a/A b/D/A
        ...
        diff --no-git a/B b/D/B
        ...
        diff --no-git a/C b/D/C
        ...

This mode is weird... In real world, "A" may match to D/some-path/A not just D/A. Which brings it back to the previous mode.

Quoted 7 lines

The only unambiguous syntax I can think of that avoids such alternative interpretations is something ugly like

    git diff --no-index [-<options>] --src=<path> --dst=<path> [--] <pathspec>

where "src" and "dst" are in line with the existing src/dst-prefix options.

Or let the user define a "separator", e.g.

git diff --no-index --pathspec-separator=<sep> [--] <paths...> [<sep> <pathspec>]

Another option is pathspec magic (I'm not entirely sure how it looks like at the implementaiton level yet), --src=<path> could become :(source)path, --dst=path -> :(dst)path and so on..

Quoted 26 lines

Perhaps we could declare that this is the "canonical" way to spell pathspec-filtered no-index comparison of two directories, but we allow the syntax suggested in the message I am responding to as a short-hand, but I am not sure if that would fly well.

This --src/--dst thing could lead to even uglier tangent. We could use these options to specify what is compared with what else

    --{src,dst}-path=<directory in the working tree>
    --{src,dst}-index
    --{src,dst}-worktree
    --{src,dst}-tree=<tree object>

which allows us to express funky combinations like

        git diff --src-index --dst-worktree [--] <pathspec>
        git diff --src-tree=HEAD --dst-index [--] <pathspec>
        git diff --src-tree=maint --dst-tree=master [--] <pathspec>

The above three would be ugly ways to spell the traditional "short hands", e.g.

git diff [--] <pathspec>
git diff --cached [--] <pathspec>
git diff maint master [--] <pathspec>

Quoted 8 lines

respectively. And an obvious fallout of the above is this command line:

        git diff --src-path=A --dst-path=B [--] <pathspec>

Because this does not involve any index, tree or worktree, it cannot be a Git operation, so we know that is --no-index mode; the command line does not need to say --no-index anywhere if we go that route.

Sounds a lot like Jeff's "git-put", except with -- syntax instead of a special one, and you only look at the diff, not move any content. Also people who would go with "git diff --no-index A B C D E F..." usually script it, not type it manually. And stdin would fit better than command line arguments.

Signature · 1 line
Duy