Skip to contentSkip to conversation

diff-no-index: exit(1) if 'diff --quiet <repo file> <external file>' finds changes

PATCH In review Tim Henigan 3 participants 13 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 diff-no-index: exit(1) if 'diff --quiet <repo file> <external file>' finds changes

When running 'git diff --quiet <file1> <file2>', if file1 or file2 is outside the repository, it will exit(0) even if the files differ. It should exit(1) when they differ.

---

v3 improves the test coverage to include variations of 'diff --quiet' where one or both of the files are outside the repository. Tests for '--ignore-space-at-eol' and '--ignore-all-space' are included as well.

 diff-no-index.c       |  3 +-
 t/t4035-diff-quiet.sh | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 80 insertions(+), 2 deletions(-)
diff-no-index.c +2 -1
index f0b0010..b935d2a 100644
@@ -273,5 +273,6 @@ void diff_no_index(struct rev_info *revs,
 	 * The return code for --no-index imitates diff(1):
 	 * 0 = no changes, 1 = changes, else error
 	 */
-	exit(revs->diffopt.found_changes);
+	int result = diff_result_code(&revs->diffopt, 0);
+	exit(result);
 }
t/t4035-diff-quiet.sh +78 -1

89 lines not on this page · raw message

Signature · 1 line
1.7.11.rc3.6.g5532165
#1 lore raw
Quoted 3 lines · Tim Henigan

When running 'git diff --quiet <file1> <file2>', if file1 or file2 is outside the repository, it will exit(0) even if the files differ. It should exit(1) when they differ.

Can we explain in the commit message a bit about why the patch works? If I hadn't just dug into this a few days ago, the patch would be somewhat confusing. Maybe something like:

The problem comes from checking diff_options's found_changes member to see whether any changes were found. This flag is set only when we actually run xdiff and it finds a change. However, the diff machinery will optimize out the actual xdiff call when it is not necessary (i.e., when we are doing a straight byte-for-byte comparison and do not care about the output). As a result, this flag was never set, and we must check the HAS_CHANGES flag instead, just like the regular index-aware diff code paths do.

I am also tempted to suggest that found_changes be renamed to xdiff_found_changes or something similar, because it really is quite misleading as-is.

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

Quoted 11 lines
diff-no-index.c +2 -1
index f0b0010..b935d2a 100644
@@ -273,5 +273,6 @@ void diff_no_index(struct rev_info *revs,
 	 * The return code for --no-index imitates diff(1):
 	 * 0 = no changes, 1 = changes, else error
 	 */
-	exit(revs->diffopt.found_changes);
+	int result = diff_result_code(&revs->diffopt, 0);
+	exit(result);

This is a declaration-after-statement, no? We try to stick to C89, which does not allow this.

I don't see any reason why the extra variable could not be removed entirely:

  exit(diff_result_code(&revs->diffopt, 0));

-Peff

#2 lore raw
In reply to Tim Henigan
Quoted 22 lines · Tim Henigan

When running 'git diff --quiet <file1> <file2>', if file1 or file2 is outside the repository, it will exit(0) even if the files differ. It should exit(1) when they differ.

---

v3 improves the test coverage to include variations of 'diff --quiet' where one or both of the files are outside the repository. Tests for '--ignore-space-at-eol' and '--ignore-all-space' are included as well.

 diff-no-index.c       |  3 +-
 t/t4035-diff-quiet.sh | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 80 insertions(+), 2 deletions(-)
diff-no-index.c +0 -0
index f0b0010..b935d2a 100644

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

Quoted 7 lines
diff-no-index.c +2 -1
@@ -273,5 +273,6 @@ void diff_no_index(struct rev_info *revs,
 	 * The return code for --no-index imitates diff(1):
 	 * 0 = no changes, 1 = changes, else error
 	 */
-	exit(revs->diffopt.found_changes);
+	int result = diff_result_code(&revs->diffopt, 0);
+	exit(result);
 }

Decl-after-stmt.

Quoted 5 lines
t/t4035-diff-quiet.sh +0 -0
index cdb9202..33d8980 100755

t/t4035-diff-quiet.sh test_expect_success 'setup' ' · line 17

Quoted 9 lines
t/t4035-diff-quiet.sh +5 -1
@@ -10,7 +10,22 @@ test_expect_success 'setup' '
 	git commit -m first &&
 	echo 2 >b &&
 	git add . &&
-	git commit -a -m second
+	git commit -a -m second &&
+	mkdir -p test-outside/repo && (
+		cd test-outside/repo &&
+		git init &&
+		echo "1 1" > a &&

Please drop extra SP between ">" and "a".

t/t4035-diff-quiet.sh test_expect_success 'setup' ' · line 28

Quoted 11 lines
t/t4035-diff-quiet.sh +11 -0
+		git add . &&
+		git commit -m 1
+	) &&
+	mkdir -p test-outside/no-repo && (
+		cd test-outside/no-repo &&
+		echo "1 1" >a &&
+		echo "1 1" >matching-file &&
+		echo "1 1 " >trailing-space &&
+		echo "1   1" >extra-space &&
+		echo "2" >never-match
+	)

The inspiration of using CEILING comes from the existing t7810-grep test, and I would have preferred if you used the same non/git for a non-git repository for easier greppability ("git grep CEIL t/" to notice the use of the technique and then "git grep non/git t/" to verify, for example).

t/t4035-diff-quiet.sh test_expect_success 'git diff-index --cached HEAD' ' · line 98

Quoted 11 lines
t/t4035-diff-quiet.sh +4 -0
 '
 
 test_expect_success 'git diff-tree HEAD^ HEAD' '
@@ -77,4 +92,66 @@ test_expect_success 'git diff-index --cached HEAD' '
 	}
 '
 
+test_expect_success 'git diff, one file outside repo' '
+	(
+		GIT_CEILING_DIRECTORIES="$TRASH_DIRECTORY/test-outside" &&
+		export GIT_CEILING_DIRECTORIES &&

Do you even need these two lines for this test? your test runs inside test-outside/repo that _is_ a git repository, and that repository knows that ../no-repo is not part of it already.

t/t4035-diff-quiet.sh test_expect_success 'git diff-index --cached HEAD' ' · line 111

Quoted 13 lines
t/t4035-diff-quiet.sh +13 -0
+		cd test-outside/repo &&
+		test_expect_code 0 git diff --quiet a "$TRASH_DIRECTORY/test-outside/no-repo/matching-file" &&
+		test_expect_code 1 git diff --quiet a "$TRASH_DIRECTORY/test-outside/no-repo/extra-space"
+	)
+'
+
+test_expect_success 'git diff, both files outside repo' '
+	(
+		GIT_CEILING_DIRECTORIES="$TRASH_DIRECTORY/test-outside" &&
+		export GIT_CEILING_DIRECTORIES &&
+		cd test-outside/no-repo &&
+		test_expect_code 0 git diff --quiet a matching-file &&
+		test_expect_code 1 git diff --quiet a extra-space

This one does need the ceiling to prevent git from finding the trash directory.

t/t4035-diff-quiet.sh test_expect_success 'git diff-index --cached HEAD' ' · line 118

Quoted 7 lines
t/t4035-diff-quiet.sh +7 -0
+	)
+'
+
+test_expect_success 'git diff --ignore-space-at-eol, one file outside repo' '
+	(
+		GIT_CEILING_DIRECTORIES="$TRASH_DIRECTORY/test-outside" &&
+		export GIT_CEILING_DIRECTORIES &&

This one does not, I think (please correct me; I am not being very careful).

Quoted 10 lines
t/t4035-diff-quiet.sh +10 -0
+		cd test-outside/repo &&
+		test_expect_code 0 git diff --quiet --ignore-space-at-eol a "$TRASH_DIRECTORY/test-outside/no-repo/trailing-space" &&
+		test_expect_code 1 git diff --quiet --ignore-space-at-eol a "$TRASH_DIRECTORY/test-outside/no-repo/extra-space"
+	)
+'
+
+test_expect_success 'git diff --ignore-space-at-eol, both files outside repo' '
+	(
+		GIT_CEILING_DIRECTORIES="$TRASH_DIRECTORY/test-outside" &&
+		export GIT_CEILING_DIRECTORIES &&

t/t4035-diff-quiet.sh test_expect_success 'git diff-index --cached HEAD' ' · line 138

Quoted 10 lines
t/t4035-diff-quiet.sh +10 -0
+		cd test-outside/no-repo &&
+		test_expect_code 0 git diff --quiet --ignore-space-at-eol a trailing-space &&
+		test_expect_code 1 git diff --quiet --ignore-space-at-eol a extra-space
+	)
+'
+
+test_expect_success 'git diff --ignore-all-space, one file outside repo' '
+	(
+		GIT_CEILING_DIRECTORIES="$TRASH_DIRECTORY/test-outside" &&
+		export GIT_CEILING_DIRECTORIES &&

This one does not, I think (please correct me; I am not being very careful).

Quoted 19 lines
t/t4035-diff-quiet.sh +18 -0
+		cd test-outside/repo &&
+		test_expect_code 0 git diff --quiet --ignore-all-space a "$TRASH_DIRECTORY/test-outside/no-repo/trailing-space" &&
+		test_expect_code 0 git diff --quiet --ignore-all-space a "$TRASH_DIRECTORY/test-outside/no-repo/extra-space" &&
+		test_expect_code 1 git diff --quiet --ignore-all-space a "$TRASH_DIRECTORY/test-outside/no-repo/never-match"
+	)
+'
+
+test_expect_success 'git diff --ignore-all-space, both files outside repo' '
+	(
+		GIT_CEILING_DIRECTORIES="$TRASH_DIRECTORY/test-outside" &&
+		export GIT_CEILING_DIRECTORIES &&
+		cd test-outside/no-repo &&
+		test_expect_code 0 git diff --quiet --ignore-all-space a trailing-space &&
+		test_expect_code 0 git diff --quiet --ignore-all-space a extra-space &&
+		test_expect_code 1 git diff --quiet --ignore-all-space a never-match
+	)
+'
+
 test_done
#3 lore raw
Quoted 11 lines · Junio C Hamano
Quoted 7 lines · Tim Henigan

When running 'git diff --quiet <file1> <file2>', if file1 or file2 is outside the repository, it will exit(0) even if the files differ. It should exit(1) when they differ.
-     exit(revs->diffopt.found_changes);
+     int result = diff_result_code(&revs->diffopt, 0);
+     exit(result);
 }

Decl-after-stmt.

Will eliminate intermediate variable in v4. Thanks to both you and Jeff for pointing this out.

Quoted 3 lines
Quoted 1 line

+             echo "1 1" > a &&

Please drop extra SP between ">" and "a".

Will fix in v4.

Quoted 12 lines
Quoted 11 lines

+             git add . &&
+             git commit -m 1
+     ) &&
+     mkdir -p test-outside/no-repo && (
+             cd test-outside/no-repo &&
+             echo "1 1" >a &&
+             echo "1 1" >matching-file &&
+             echo "1 1 " >trailing-space &&
+             echo "1   1" >extra-space &&
+             echo "2" >never-match
+     )

Quoted 5 lines

The inspiration of using CEILING comes from the existing t7810-grep test, and I would have preferred if you used the same non/git for a non-git repository for easier greppability ("git grep CEIL t/" to notice the use of the technique and then "git grep non/git t/" to verify, for example).

Okay. I still need the non/git directory to be outside the test repo's path, so the new layout will be:

$TRASH_DIRECTORY/
test-outside/
repo/
non/git/

This adds an extra layer to the non git paths, but won't cause any problems.

Quoted 8 lines
Quoted 4 lines

+test_expect_success 'git diff, one file outside repo' '
+     (
+             GIT_CEILING_DIRECTORIES="$TRASH_DIRECTORY/test-outside" &&
+             export GIT_CEILING_DIRECTORIES &&

Do you even need these two lines for this test?  your test runs inside test-outside/repo that _is_ a git repository, and that repository knows that ../no-repo is not part of it already.

You are correct, CEILING does not need to be set for tests where one file is inside 'test-outside/repo'.

As a side note, I found that these tests fail if a relative path is used for the file in 'non/git'. In other words, this passes:

    test_expect_code 0 git diff --quiet a

"$TRASH_DIRECTORY/test-outside/non/git/matching-file"

but this fails:

    test_expect_code 0 git diff --quiet a ../non/git/matching-file

This surprised me, but I have not investigated any further.

#4 lore raw
Quoted 11 lines · Tim Henigan

As a side note, I found that these tests fail if a relative path is used for the file in 'non/git'. In other words, this passes:

    test_expect_code 0 git diff --quiet a

"$TRASH_DIRECTORY/test-outside/non/git/matching-file"

but this fails:

    test_expect_code 0 git diff --quiet a ../non/git/matching-file

This surprised me, but I have not investigated any further.

The problem is that path_outside_repo in diff-no-index.c does not bother handling relative paths at all, and just assumes they are inside the repository. This is obviously not true if the path starts with "..", in which case you would need to compare the number of ".." with the current depth in the repository.

prefix_path already does this (and is what generates the later "../non/git/matching-file is not in the repository" message). We could perhaps get rid of path_outside_repo and just re-use prefix_path's logic, something like (not tested):

cache.h +1 -0
index 0b7ddee..0736bfb 100644
@@ -411,6 +411,7 @@ extern const char *prefix_filename(const char *prefix, int len, const char *path
 extern int check_filename(const char *prefix, const char *name);
 extern void verify_filename(const char *prefix, const char *name);
 extern void verify_non_filename(const char *prefix, const char *name);
+extern int path_inside_repo(const char *prefix, const char *path);
 
 #define INIT_DB_QUIET 0x0001
 
diff-no-index.c +2 -19
index 6911196..9a1b459 100644
@@ -151,23 +151,6 @@ static int queue_diff(struct diff_options *o,
 	}
 }
 
-static int path_outside_repo(const char *path)
-{
-	const char *work_tree;
-	size_t len;
-
-	if (!is_absolute_path(path))
-		return 0;
-	work_tree = get_git_work_tree();
-	if (!work_tree)
-		return 1;
-	len = strlen(work_tree);
-	if (strncmp(path, work_tree, len) ||
-	    (path[len] != '\0' && path[len] != '/'))
-		return 1;
-	return 0;
-}
-
 void diff_no_index(struct rev_info *revs,
 		   int argc, const char **argv,
 		   int nongit, const char *prefix)
@@ -197,8 +180,8 @@ void diff_no_index(struct rev_info *revs,
 		 * a colourful "diff" replacement.
 		 */
 		if ((argc != i + 2) ||
-		    (!path_outside_repo(argv[i]) &&
-		     !path_outside_repo(argv[i+1])))
+		    (path_inside_repo(prefix, argv[i]) &&
+		     path_inside_repo(prefix, argv[i+1])))
 			return;
 	}
 	if (argc != i + 2)
setup.c +22 -2
index 731851a..2cfa037 100644
@@ -4,7 +4,7 @@
 static int inside_git_dir = -1;
 static int inside_work_tree = -1;
 
-char *prefix_path(const char *prefix, int len, const char *path)
+static char *prefix_path_gently(const char *prefix, int len, const char *path)
 {
 	const char *orig = path;
 	char *sanitized;
@@ -31,7 +31,8 @@ char *prefix_path(const char *prefix, int len, const char *path)
 		if (strncmp(sanitized, work_tree, len) ||
 		    (len > root_len && sanitized[len] != '\0' && sanitized[len] != '/')) {
 		error_out:
-			die("'%s' is outside repository", orig);
+			free(sanitized);
+			return NULL;
 		}
 		if (sanitized[len] == '/')
 			len++;
@@ -40,6 +41,25 @@ char *prefix_path(const char *prefix, int len, const char *path)
 	return sanitized;
 }
 
+char *prefix_path(const char *prefix, int len, const char *path)
+{
+	char *r = prefix_path_gently(prefix, len, path);
+	if (!r)
+		die("'%s' is outside repository", path);
+	return r;
+}
+
+int path_inside_repo(const char *prefix, const char *path)
+{
+	int len = prefix ? strlen(prefix) : 0;
+	char *r = prefix_path_gently(prefix, len, path);
+	if (r) {
+		free(r);
+		return 1;
+	}
+	return 0;
+}
+
 int check_filename(const char *prefix, const char *arg)
 {
 	const char *name;
#5 lore raw
Quoted 20 lines · Jeff King
Quoted 11 lines · Tim Henigan

As a side note, I found that these tests fail if a relative path is used for the file in 'non/git'.  In other words, this passes:

    test_expect_code 0 git diff --quiet a "$TRASH_DIRECTORY/test-outside/non/git/matching-file"

but this fails:

    test_expect_code 0 git diff --quiet a ../non/git/matching-file

This surprised me, but I have not investigated any further.

The problem is that path_outside_repo in diff-no-index.c does not bother handling relative paths at all, and just assumes they are inside the repository. This is obviously not true if the path starts with "..", in which case you would need to compare the number of ".." with the current depth in the repository.

Quoted 4 lines

prefix_path already does this (and is what generates the later "../non/git/matching-file is not in the repository" message). We could perhaps get rid of path_outside_repo and just re-use prefix_path's logic, something like (not tested):

With your patch applied, I was able to use relative paths in my tests.
I also confirmed that all the t4*.sh tests pass.

For what its worth, your patch looks correct to me. Existing consumers of 'prefix_path' should get the same results as before and the one added xmalloc is paired with a free.

#6 lore raw
Quoted 26 lines · Tim Henigan
Quoted 20 lines · Jeff King
Quoted 11 lines · Tim Henigan

As a side note, I found that these tests fail if a relative path is used for the file in 'non/git'.  In other words, this passes:

    test_expect_code 0 git diff --quiet a "$TRASH_DIRECTORY/test-outside/non/git/matching-file"

but this fails:

    test_expect_code 0 git diff --quiet a ../non/git/matching-file

This surprised me, but I have not investigated any further.

The problem is that path_outside_repo in diff-no-index.c does not bother handling relative paths at all, and just assumes they are inside the repository. This is obviously not true if the path starts with "..", in which case you would need to compare the number of ".." with the current depth in the repository.

Quoted 4 lines

prefix_path already does this (and is what generates the later "../non/git/matching-file is not in the repository" message). We could perhaps get rid of path_outside_repo and just re-use prefix_path's logic, something like (not tested):

Quoted 6 lines

With your patch applied, I was able to use relative paths in my tests. I also confirmed that all the t4*.sh tests pass.

For what its worth, your patch looks correct to me.  Existing consumers of 'prefix_path' should get the same results as before and the one added xmalloc is paired with a free.

Jeff,

Are you planning to send this patch to the list? If not, can I include it as 1 of 2 before my patch? If we go that route, I'm not sure how to properly show you as the author...

Also, in an earlier email [1] you mentioned that it may be a good idea to rename 'found_changes' to something like 'xdiff_found_changes'. I like the idea...I could submit this change as another patch in the series, if you have no objections.

Thanks again for your review and help.

[1]: http://thread.gmane.org/gmane.comp.version-control.git/200160/focus=200163

#7 lore raw
Quoted 7 lines · Tim Henigan
Quoted 6 lines

With your patch applied, I was able to use relative paths in my tests. I also confirmed that all the t4*.sh tests pass.

For what its worth, your patch looks correct to me.  Existing consumers of 'prefix_path' should get the same results as before and the one added xmalloc is paired with a free.

Quoted 5 lines

Jeff,

Are you planning to send this patch to the list? If not, can I include it as 1 of 2 before my patch? If we go that route, I'm not sure how to properly show you as the author...

I'd probably get to it eventually, but I haven't touched it since I sent it. If you want to include some tests and package it with a commit message, that would make me very happy.

You can override the author by including a "From: " header as the first line in the body of the email (which git-am will use rather than the identity in the email's From header). If you use git-send-email, it will do this automatically when the patch author does not match your identity.

I didn't sign-off the original, but please feel free to include my sign-off, as well as add your own. And note your own contributions in the commit message. So the resulting email would be something like:

From: Tim Henigan <[email protected]>
Date: ...
Subject: [PATCH 1/2] diff: handle relative paths in no-index

From: Jeff King <[email protected]>

... some commit message body ...

Tests and commit message by Tim Henigan.

---
... the actual patch ...

Quoted 4 lines

Also, in an earlier email [1] you mentioned that it may be a good idea to rename 'found_changes' to something like 'xdiff_found_changes'. I like the idea...I could submit this change as another patch in the series, if you have no objections.

Fine by me. I think "xdiff_found_changes" is not quite accurate; it is really "did builtin_diff find any changes?" since we might never call into xdiff (e.g., for binary files). I'm not sure what the best name is.

-Peff

#8 lore raw
Quoted 3 lines · Jeff King

Fine by me. I think "xdiff_found_changes" is not quite accurate; it is really "did builtin_diff find any changes?" since we might never call into xdiff (e.g., for binary files). I'm not sure what the best name is.

"diffopt.found_changes" is clear enough for me.

#9 lore raw
Quoted 7 lines · Junio C Hamano
Quoted 3 lines · Jeff King

Fine by me. I think "xdiff_found_changes" is not quite accurate; it is really "did builtin_diff find any changes?" since we might never call into xdiff (e.g., for binary files). I'm not sure what the best name is.

"diffopt.found_changes" is clear enough for me.

I thought this bug would be enough to show that diffopt.found_changes is not clear enough. It is the source of the original bug (the code should have been using HAS_CHANGES instead of found_changes), and it gave at least one of the bug investigators (i.e., me) quite a bit of confusion to understand why found_changes was not being set when diff_flush found changes.

IOW, as a naive reader of the "struct diff_options", how do I understand the difference between HAS_CHANGES and found_changes?

-Peff

#10 lore raw
Quoted 6 lines · Jeff King

I thought this bug would be enough to show that diffopt.found_changes is not clear enough. It is the source of the original bug (the code should have been using HAS_CHANGES instead of found_changes), and it gave at least one of the bug investigators (i.e., me) quite a bit of confusion to understand why found_changes was not being set when diff_flush found changes.

I think when found_changes was introduced so that diff can indicate more than what HAS_CHANGES (i.e. there is a blob-level difference exists) can represent, the patch forgot to update the no-index codepath.

Quoted 2 lines

IOW, as a naive reader of the "struct diff_options", how do I understand the difference between HAS_CHANGES and found_changes?

HAS_CHANGES and found_changes should be implementation detail of diff_result_code() and as long as we do not add outside users of it, the names should not matter too much. If we were to rename them, HAS_CHANGES should also be made more descriptive to hint what it means ("object level difference exists"), I would think. Given the recent discussion on "diff/log -L <bottom>,<top>", found_changes would mean "content level change that the caller cares about exists".

#11 lore raw
In reply to Jeff King
Quoted 9 lines · Jeff King
Quoted 4 lines · Tim Henigan

Are you planning to send this patch to the list?  If not, can I include it as 1 of 2 before my patch?  If we go that route, I'm not sure how to properly show you as the author...

I'd probably get to it eventually, but I haven't touched it since I sent it. If you want to include some tests and package it with a commit message, that would make me very happy.

Thanks, I will do that.

It looks like the best place to add tests is t4010-diff-pathspec.sh. The only cases not tested through other means appear to be:

git diff <file in repo> <relative path outside repo> git diff <relative path outside repo> <relative path outside repo>

Other pathspec variations seem to be covered extensively by other tests (mostly as a side effect). Am I missing other variations that should be checked?

In both cases shown above, we are simply verifying that giving a relative path to diff does not cause it to abort. So it may be sufficient to only test one of the above.

The tests that I added to t4035-diff-quiet.sh already cover both of the cases listed above. Is it worthwhile to duplicate some of those tests in t4010?

#12 lore raw
Quoted 1 line · Tim Henigan

It looks like the best place to add tests is t4010-diff-pathspec.sh.

Well, pathspecs are all about limiting the changes in the repository by patterns, while "diff --no-index" is about exact pathnames, so I am not sure if that is a good place to put them. Isn't there a test script that is dedicated to the "diff --no-index" codepath (perhaps 4053) that is more appropriate for doing this?