2012-05-16 18:28:31 +04:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
test_description='diff --no-index'
|
|
|
|
|
|
|
|
. ./test-lib.sh
|
|
|
|
|
|
|
|
test_expect_success 'setup' '
|
|
|
|
mkdir a &&
|
|
|
|
mkdir b &&
|
|
|
|
echo 1 >a/1 &&
|
2012-06-21 22:09:50 +04:00
|
|
|
echo 2 >a/2 &&
|
|
|
|
git init repo &&
|
|
|
|
echo 1 >repo/a &&
|
|
|
|
mkdir -p non/git &&
|
|
|
|
echo 1 >non/git/a &&
|
|
|
|
echo 1 >non/git/b
|
2012-05-16 18:28:31 +04:00
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'git diff --no-index directories' '
|
2015-03-20 13:11:46 +03:00
|
|
|
test_expect_code 1 git diff --no-index a b >cnt &&
|
|
|
|
test_line_count = 14 cnt
|
2012-05-16 18:28:31 +04:00
|
|
|
'
|
|
|
|
|
2012-06-21 22:09:50 +04:00
|
|
|
test_expect_success 'git diff --no-index relative path outside repo' '
|
|
|
|
(
|
|
|
|
cd repo &&
|
|
|
|
test_expect_code 0 git diff --no-index a ../non/git/a &&
|
|
|
|
test_expect_code 0 git diff --no-index ../non/git/a ../non/git/b
|
|
|
|
)
|
|
|
|
'
|
|
|
|
|
2013-12-11 13:58:43 +04:00
|
|
|
test_expect_success 'git diff --no-index with broken index' '
|
|
|
|
(
|
|
|
|
cd repo &&
|
|
|
|
echo broken >.git/index &&
|
|
|
|
git diff --no-index a ../non/git/a
|
|
|
|
)
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'git diff outside repo with broken index' '
|
|
|
|
(
|
|
|
|
cd repo &&
|
|
|
|
git diff ../non/git/a ../non/git/b
|
|
|
|
)
|
|
|
|
'
|
|
|
|
|
2013-12-17 00:19:23 +04:00
|
|
|
test_expect_success 'git diff --no-index executed outside repo gives correct error message' '
|
|
|
|
(
|
|
|
|
GIT_CEILING_DIRECTORIES=$TRASH_DIRECTORY/non &&
|
|
|
|
export GIT_CEILING_DIRECTORIES &&
|
|
|
|
cd non/git &&
|
|
|
|
test_must_fail git diff --no-index a 2>actual.err &&
|
2019-03-24 11:20:13 +03:00
|
|
|
test_i18ngrep "usage: git diff --no-index" actual.err
|
2013-12-17 00:19:23 +04:00
|
|
|
)
|
|
|
|
'
|
|
|
|
|
2015-03-26 02:11:39 +03:00
|
|
|
test_expect_success 'diff D F and diff F D' '
|
|
|
|
(
|
|
|
|
cd repo &&
|
|
|
|
echo in-repo >a &&
|
|
|
|
echo non-repo >../non/git/a &&
|
|
|
|
mkdir sub &&
|
|
|
|
echo sub-repo >sub/a &&
|
|
|
|
|
|
|
|
test_must_fail git diff --no-index sub/a ../non/git/a >expect &&
|
|
|
|
test_must_fail git diff --no-index sub/a ../non/git/ >actual &&
|
|
|
|
test_cmp expect actual &&
|
|
|
|
|
|
|
|
test_must_fail git diff --no-index a ../non/git/a >expect &&
|
|
|
|
test_must_fail git diff --no-index a ../non/git/ >actual &&
|
|
|
|
test_cmp expect actual &&
|
|
|
|
|
|
|
|
test_must_fail git diff --no-index ../non/git/a a >expect &&
|
|
|
|
test_must_fail git diff --no-index ../non/git a >actual &&
|
|
|
|
test_cmp expect actual
|
|
|
|
)
|
|
|
|
'
|
|
|
|
|
2015-03-22 08:11:27 +03:00
|
|
|
test_expect_success 'turning a file into a directory' '
|
|
|
|
(
|
|
|
|
cd non/git &&
|
|
|
|
mkdir d e e/sub &&
|
|
|
|
echo 1 >d/sub &&
|
|
|
|
echo 2 >e/sub/file &&
|
|
|
|
printf "D\td/sub\nA\te/sub/file\n" >expect &&
|
|
|
|
test_must_fail git diff --no-index --name-status d e >actual &&
|
|
|
|
test_cmp expect actual
|
|
|
|
)
|
|
|
|
'
|
|
|
|
|
diff: handle --no-index prefixes consistently
If we see an explicit "git diff --no-index ../foo ../bar",
then we do not set up the git repository at all (we already
know we are in --no-index mode, so do not have to check "are
we in a repository?"), and hence have no "prefix" within the
repository. A patch generated by this command will have the
filenames "a/../foo" and "b/../bar", no matter which
directory we are in with respect to any repository.
However, in the implicit case, where we notice that the
files are outside the repository, we will have chdir()'d to
the top-level of the repository. We then feed the prefix
back to the diff machinery. As a result, running the same
diff from a subdirectory will result in paths that look like
"a/subdir/../../foo".
Besides being unnecessarily long, this may also be confusing
to the user: they don't care about the subdir or the
repository at all; it's just where they happened to be when
running the command. We should treat this the same as the
explicit --no-index case.
One way to address this would be to chdir() back to the
original path before running our diff. However, that's a bit
hacky, as we would also need to adjust $GIT_DIR, which could
be a relative path from our top-level.
Instead, we can reuse the diff machinery's RELATIVE_NAME
option, which automatically strips off the prefix. Note that
this _also_ restricts the diff to this relative prefix, but
that's OK for our purposes: we queue our own diff pairs
manually, and do not rely on that part of the diff code.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2016-09-13 06:23:32 +03:00
|
|
|
test_expect_success 'diff from repo subdir shows real paths (explicit)' '
|
|
|
|
echo "diff --git a/../../non/git/a b/../../non/git/b" >expect &&
|
|
|
|
test_expect_code 1 \
|
|
|
|
git -C repo/sub \
|
|
|
|
diff --no-index ../../non/git/a ../../non/git/b >actual &&
|
|
|
|
head -n 1 <actual >actual.head &&
|
|
|
|
test_cmp expect actual.head
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'diff from repo subdir shows real paths (implicit)' '
|
|
|
|
echo "diff --git a/../../non/git/a b/../../non/git/b" >expect &&
|
|
|
|
test_expect_code 1 \
|
|
|
|
git -C repo/sub \
|
|
|
|
diff ../../non/git/a ../../non/git/b >actual &&
|
|
|
|
head -n 1 <actual >actual.head &&
|
|
|
|
test_cmp expect actual.head
|
|
|
|
'
|
|
|
|
|
diff: always try to set up the repository
If we see an explicit "--no-index", we do not bother calling
setup_git_directory_gently() at all. This means that we may
miss out on reading repo-specific config.
It's arguable whether this is correct or not. If we were
designing from scratch, making "git diff --no-index"
completely ignore the repository makes some sense. But we
are nowhere near scratch, so let's look at the existing
behavior:
1. If you're in the top-level of a repository and run an
explicit "diff --no-index", the config subsystem falls
back to reading ".git/config", and we will respect repo
config.
2. If you're in a subdirectory of a repository, then we
still try to read ".git/config", but it generally
doesn't exist. So "diff --no-index" there does not
respect repo config.
3. If you have $GIT_DIR set in the environment, we read
and respect $GIT_DIR/config,
4. If you run "git diff /tmp/foo /tmp/bar" to get an
implicit no-index, we _do_ run the repository setup,
and set $GIT_DIR (or respect an existing $GIT_DIR
variable). We find the repo config no matter where we
started, and respect it.
So we already respect the repository config in a number of
common cases, and case (2) is the only one that does not.
And at least one of our tests, t4034, depends on case (1)
behaving as it does now (though it is just incidental, not
an explicit test for this behavior).
So let's bring case (2) in line with the others by always
running the repository setup, even with an explicit
"--no-index". We shouldn't need to change anything else, as the
implicit case already handles the prefix.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2016-09-13 06:23:36 +03:00
|
|
|
test_expect_success 'diff --no-index from repo subdir respects config (explicit)' '
|
|
|
|
echo "diff --git ../../non/git/a ../../non/git/b" >expect &&
|
|
|
|
test_config -C repo diff.noprefix true &&
|
|
|
|
test_expect_code 1 \
|
|
|
|
git -C repo/sub \
|
|
|
|
diff --no-index ../../non/git/a ../../non/git/b >actual &&
|
|
|
|
head -n 1 <actual >actual.head &&
|
|
|
|
test_cmp expect actual.head
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'diff --no-index from repo subdir respects config (implicit)' '
|
|
|
|
echo "diff --git ../../non/git/a ../../non/git/b" >expect &&
|
|
|
|
test_config -C repo diff.noprefix true &&
|
|
|
|
test_expect_code 1 \
|
|
|
|
git -C repo/sub \
|
|
|
|
diff ../../non/git/a ../../non/git/b >actual &&
|
|
|
|
head -n 1 <actual >actual.head &&
|
|
|
|
test_cmp expect actual.head
|
|
|
|
'
|
|
|
|
|
2018-10-19 19:58:07 +03:00
|
|
|
test_expect_success 'diff --no-index from repo subdir with absolute paths' '
|
|
|
|
cat <<-EOF >expect &&
|
|
|
|
1 1 $(pwd)/non/git/{a => b}
|
|
|
|
EOF
|
|
|
|
test_expect_code 1 \
|
|
|
|
git -C repo/sub diff --numstat \
|
|
|
|
"$(pwd)/non/git/a" "$(pwd)/non/git/b" >actual &&
|
|
|
|
test_cmp expect actual
|
|
|
|
'
|
|
|
|
|
2019-02-16 09:57:56 +03:00
|
|
|
test_expect_success 'diff --no-index allows external diff' '
|
|
|
|
test_expect_code 1 \
|
|
|
|
env GIT_EXTERNAL_DIFF="echo external ;:" \
|
|
|
|
git diff --no-index non/git/a non/git/b >actual &&
|
|
|
|
echo external >expect &&
|
|
|
|
test_cmp expect actual
|
|
|
|
'
|
|
|
|
|
2012-05-16 18:28:31 +04:00
|
|
|
test_done
|