difftool: avoid $GIT_DIR and $GIT_WORK_TREE
Environment variables are global and hard to reason about. Use the `--git-dir` and `--work-tree` arguments when invoking `git` instead of relying on the environment. Add a test to ensure that difftool's dir-diff feature works when these variables are present in the environment. Signed-off-by: David Aguilar <davvid@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:

committed by
Junio C Hamano

parent
9ec26e7977
commit
98f917ed42
@ -83,20 +83,17 @@ sub changed_files
|
|||||||
{
|
{
|
||||||
my ($repo_path, $index, $worktree) = @_;
|
my ($repo_path, $index, $worktree) = @_;
|
||||||
$ENV{GIT_INDEX_FILE} = $index;
|
$ENV{GIT_INDEX_FILE} = $index;
|
||||||
$ENV{GIT_WORK_TREE} = $worktree;
|
|
||||||
my $must_unset_git_dir = 0;
|
|
||||||
if (not defined($ENV{GIT_DIR})) {
|
|
||||||
$must_unset_git_dir = 1;
|
|
||||||
$ENV{GIT_DIR} = $repo_path;
|
|
||||||
}
|
|
||||||
|
|
||||||
my @refreshargs = qw/update-index --really-refresh -q --unmerged/;
|
my @gitargs = ('--git-dir', $repo_path, '--work-tree', $worktree);
|
||||||
my @gitargs = qw/diff-files --name-only -z/;
|
my @refreshargs = (
|
||||||
|
@gitargs, 'update-index',
|
||||||
|
'--really-refresh', '-q', '--unmerged');
|
||||||
try {
|
try {
|
||||||
Git::command_oneline(@refreshargs);
|
Git::command_oneline(@refreshargs);
|
||||||
} catch Git::Error::Command with {};
|
} catch Git::Error::Command with {};
|
||||||
|
|
||||||
my $line = Git::command_oneline(@gitargs);
|
my @diffargs = (@gitargs, 'diff-files', '--name-only', '-z');
|
||||||
|
my $line = Git::command_oneline(@diffargs);
|
||||||
my @files;
|
my @files;
|
||||||
if (defined $line) {
|
if (defined $line) {
|
||||||
@files = split('\0', $line);
|
@files = split('\0', $line);
|
||||||
@ -105,8 +102,6 @@ sub changed_files
|
|||||||
}
|
}
|
||||||
|
|
||||||
delete($ENV{GIT_INDEX_FILE});
|
delete($ENV{GIT_INDEX_FILE});
|
||||||
delete($ENV{GIT_WORK_TREE});
|
|
||||||
delete($ENV{GIT_DIR}) if ($must_unset_git_dir);
|
|
||||||
|
|
||||||
return map { $_ => 1 } @files;
|
return map { $_ => 1 } @files;
|
||||||
}
|
}
|
||||||
@ -204,15 +199,6 @@ EOF
|
|||||||
mkpath($ldir) or exit_cleanup($tmpdir, 1);
|
mkpath($ldir) or exit_cleanup($tmpdir, 1);
|
||||||
mkpath($rdir) or exit_cleanup($tmpdir, 1);
|
mkpath($rdir) or exit_cleanup($tmpdir, 1);
|
||||||
|
|
||||||
# If $GIT_DIR is not set prior to calling 'git update-index' and
|
|
||||||
# 'git checkout-index', then those commands will fail if difftool
|
|
||||||
# is called from a directory other than the repo root.
|
|
||||||
my $must_unset_git_dir = 0;
|
|
||||||
if (not defined($ENV{GIT_DIR})) {
|
|
||||||
$must_unset_git_dir = 1;
|
|
||||||
$ENV{GIT_DIR} = $repo_path;
|
|
||||||
}
|
|
||||||
|
|
||||||
# Populate the left and right directories based on each index file
|
# Populate the left and right directories based on each index file
|
||||||
my ($inpipe, $ctx);
|
my ($inpipe, $ctx);
|
||||||
$ENV{GIT_INDEX_FILE} = "$tmpdir/lindex";
|
$ENV{GIT_INDEX_FILE} = "$tmpdir/lindex";
|
||||||
@ -241,7 +227,6 @@ EOF
|
|||||||
|
|
||||||
# If $GIT_DIR was explicitly set just for the update/checkout
|
# If $GIT_DIR was explicitly set just for the update/checkout
|
||||||
# commands, then it should be unset before continuing.
|
# commands, then it should be unset before continuing.
|
||||||
delete($ENV{GIT_DIR}) if ($must_unset_git_dir);
|
|
||||||
delete($ENV{GIT_INDEX_FILE});
|
delete($ENV{GIT_INDEX_FILE});
|
||||||
|
|
||||||
# Changes in the working tree need special treatment since they are
|
# Changes in the working tree need special treatment since they are
|
||||||
|
@ -412,6 +412,20 @@ run_dir_diff_test 'difftool --dir-diff from subdirectory' '
|
|||||||
)
|
)
|
||||||
'
|
'
|
||||||
|
|
||||||
|
run_dir_diff_test 'difftool --dir-diff from subdirectory with GIT_DIR set' '
|
||||||
|
(
|
||||||
|
GIT_DIR=$(pwd)/.git &&
|
||||||
|
export GIT_DIR &&
|
||||||
|
GIT_WORK_TREE=$(pwd) &&
|
||||||
|
export GIT_WORK_TREE &&
|
||||||
|
cd sub &&
|
||||||
|
git difftool --dir-diff $symlinks --extcmd ls \
|
||||||
|
branch -- sub >output &&
|
||||||
|
grep sub output &&
|
||||||
|
! grep file output
|
||||||
|
)
|
||||||
|
'
|
||||||
|
|
||||||
run_dir_diff_test 'difftool --dir-diff when worktree file is missing' '
|
run_dir_diff_test 'difftool --dir-diff when worktree file is missing' '
|
||||||
test_when_finished git reset --hard &&
|
test_when_finished git reset --hard &&
|
||||||
rm file2 &&
|
rm file2 &&
|
||||||
|
Reference in New Issue
Block a user