The three cases where "git diff" operates outside of a repository are 1)
when we run it outside of a repository, 2) when one of the files we're
comparing is outside of the repository we're in, and 3) the --no-index
option. Commit 4f03666 ("diff: handle sha1 abbreviations outside of
repository", 2016-10-20) only worked in the first case.
---
 builtin/diff.c                  |  4 +++-
 t/t4063-diff-no-index-abbrev.sh | 50 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+), 1 deletion(-)
 create mode 100755 t/t4063-diff-no-index-abbrev.sh

diff --git a/builtin/diff.c b/builtin/diff.c
index 7f91f6d..ec7c432 100644
--- a/builtin/diff.c
+++ b/builtin/diff.c
@@ -342,9 +342,11 @@ int cmd_diff(int argc, const char **argv, const char 
*prefix)
                       "--no-index" : "[--no-index]");
 
        }
-       if (no_index)
+       if (no_index) {
                /* If this is a no-index diff, just run it and exit there. */
+               startup_info->have_repository = 0;
                diff_no_index(&rev, argc, argv);
+       }
 
        /* Otherwise, we are doing the usual "git" diff */
        rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
diff --git a/t/t4063-diff-no-index-abbrev.sh b/t/t4063-diff-no-index-abbrev.sh
new file mode 100755
index 0000000..d1d6302
--- /dev/null
+++ b/t/t4063-diff-no-index-abbrev.sh
@@ -0,0 +1,50 @@
+#!/bin/sh
+
+test_description='don'\'' peek into .git when operating outside of a repository
+
+When abbreviating SHA-1s, if another object in the repository has the
+same abbreviation, we normally lengthen the abbreviation until it'\''s
+unique. Commit 4f03666 ("diff: handle sha1 abbreviations outside of
+repository", 2016-10-20) addressed the case of abbreviating SHA-1s
+outside the context of a repository. In that case we shouldn'\''t peek
+into a .git directory to make an abbreviation unique.
+
+To check that we don'\''t, create an blob with a SHA-1 that starts with
+0000. (Outside of a repository, SHA-1s are all zeros.) Then make an
+abbreviation and check that Git doesn'\''t lengthen it.
+
+The three cases where "git diff" operates outside of a repository are
+1) when we run it outside of a repository, 2) when one of the files
+we'\''re comparing is outside of the repository we'\''re in,
+and 3) the --no-index option.
+'
+
+. ./test-lib.sh
+
+test_expect_success setup '
+       echo 1 >a &&
+       echo 2 >b &&
+       git init repo &&
+       (
+               cd repo &&
+
+               # Create a blob
+               # 00002e907f44c3881822c473d8842405cfd96362
+               echo 119132 >collision &&
+               git add collision
+       )
+'
+
+cat >expect <<EOF
+:100644 100644 0000... 0000... M       ../a
+EOF
+
+test_expect_success 'don'\''t peek into .git when operating outside of a 
repository' '
+       (
+               cd repo &&
+               test_must_fail git diff --raw --abbrev=4 ../a ../b >actual &&
+               test_cmp ../expect actual
+       )
+'
+
+test_done
-- 
2.10.2

Reply via email to