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 <davvid@gmail.com> Acked-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

David Aguilar committed Feb 5, 2017 at 13:23 UTC d81345ce09c121fb0d18dec5c2535ec8d2a67542
2 files changed +17 -4
builtin/difftool.c
+4 -4
@@ -647,10 +647,6 @@ int cmd_difftool(int argc, const char **argv, const char *prefix)
647 OPT_END()
648 };
649
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 -
650 git_config(difftool_config, NULL);
651 symlinks = has_symlinks;
652
@@ -661,6 +657,10 @@ int cmd_difftool(int argc, const char **argv, const char *prefix)
657 if (tool_help)
658 return print_tool_help();
659
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 +
664 if (use_gui_tool && diff_gui_tool && *diff_gui_tool)
665 setenv("GIT_DIFF_TOOL", diff_gui_tool, 1);
666 else if (difftool_cmd) {
t/t7800-difftool.sh
+13
@@ -23,6 +23,19 @@ prompt_given ()
23 test "$prompt" = "Launch 'test-tool' [Y/n]? branch"
24 }
25
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 +
39 # Create a file on master and change it on branch
40 test_expect_success 'setup' '
41 echo master >file &&