Skip to content

Commit d81345c

Browse files
davvidgitster
authored andcommitted
difftool: fix bug when printing usage
"git difftool -h" reports an error: fatal: BUG: setup_git_env called without repository Defer repository setup so that the help option processing happens before the repository is initialized. Add tests to ensure that the basic usage works inside and outside of a repository. Signed-off-by: David Aguilar <[email protected]> Acked-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 94d3997 commit d81345c

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

builtin/difftool.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -647,10 +647,6 @@ int cmd_difftool(int argc, const char **argv, const char *prefix)
647647
OPT_END()
648648
};
649649

650-
/* NEEDSWORK: once we no longer spawn anything, remove this */
651-
setenv(GIT_DIR_ENVIRONMENT, absolute_path(get_git_dir()), 1);
652-
setenv(GIT_WORK_TREE_ENVIRONMENT, absolute_path(get_git_work_tree()), 1);
653-
654650
git_config(difftool_config, NULL);
655651
symlinks = has_symlinks;
656652

@@ -661,6 +657,10 @@ int cmd_difftool(int argc, const char **argv, const char *prefix)
661657
if (tool_help)
662658
return print_tool_help();
663659

660+
/* NEEDSWORK: once we no longer spawn anything, remove this */
661+
setenv(GIT_DIR_ENVIRONMENT, absolute_path(get_git_dir()), 1);
662+
setenv(GIT_WORK_TREE_ENVIRONMENT, absolute_path(get_git_work_tree()), 1);
663+
664664
if (use_gui_tool && diff_gui_tool && *diff_gui_tool)
665665
setenv("GIT_DIFF_TOOL", diff_gui_tool, 1);
666666
else if (difftool_cmd) {

t/t7800-difftool.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@ prompt_given ()
2323
test "$prompt" = "Launch 'test-tool' [Y/n]? branch"
2424
}
2525

26+
test_expect_success 'basic usage requires no repo' '
27+
lines=$(git difftool -h | grep ^usage: | wc -l) &&
28+
test "$lines" -eq 1 &&
29+
# create a ceiling directory to prevent Git from finding a repo
30+
mkdir -p not/repo &&
31+
ceiling="$PWD/not" &&
32+
lines=$(cd not/repo &&
33+
GIT_CEILING_DIRECTORIES="$ceiling" git difftool -h |
34+
grep ^usage: | wc -l) &&
35+
test "$lines" -eq 1 &&
36+
rmdir -p not/repo
37+
'
38+
2639
# Create a file on master and change it on branch
2740
test_expect_success 'setup' '
2841
echo master >file &&

0 commit comments

Comments
 (0)