difftool: add a skeleton for the upcoming builtin

This adds a builtin difftool that still falls back to the legacy Perl version, which has been renamed to `legacy-difftool`. The idea is that the new, experimental, builtin difftool immediately hands off to the legacy difftool for now, unless the config variable difftool.useBuiltin is set to true. This feature flag will be used in the upcoming Git for Windows v2.11.0 release, to allow early testers to opt-in to use the builtin difftool and flesh out any bugs. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Jan 17, 2017 at 16:54 UTC be8a90e59ce4c7603207a8255284fdbbffff1a2e
7 files changed +75 -1
.gitignore
+1
@@ -76,6 +76,7 @@
76 /git-init-db
77 /git-interpret-trailers
78 /git-instaweb
79 +/git-legacy-difftool
80 /git-log
81 /git-ls-files
82 /git-ls-remote
Makefile
+2 -1
@@ -527,7 +527,7 @@ SCRIPT_LIB += git-sh-setup
527 SCRIPT_LIB += git-sh-i18n
528
529 SCRIPT_PERL += git-add--interactive.perl
530 -SCRIPT_PERL += git-difftool.perl
530 +SCRIPT_PERL += git-legacy-difftool.perl
531 SCRIPT_PERL += git-archimport.perl
532 SCRIPT_PERL += git-cvsexportcommit.perl
533 SCRIPT_PERL += git-cvsimport.perl
@@ -888,6 +888,7 @@ BUILTIN_OBJS += builtin/diff-files.o
888 BUILTIN_OBJS += builtin/diff-index.o
889 BUILTIN_OBJS += builtin/diff-tree.o
890 BUILTIN_OBJS += builtin/diff.o
891 +BUILTIN_OBJS += builtin/difftool.o
892 BUILTIN_OBJS += builtin/fast-export.o
893 BUILTIN_OBJS += builtin/fetch-pack.o
894 BUILTIN_OBJS += builtin/fetch.o
builtin.h
+1
@@ -60,6 +60,7 @@ extern int cmd_diff_files(int argc, const char **argv, const char *prefix);
60 extern int cmd_diff_index(int argc, const char **argv, const char *prefix);
61 extern int cmd_diff(int argc, const char **argv, const char *prefix);
62 extern int cmd_diff_tree(int argc, const char **argv, const char *prefix);
63 +extern int cmd_difftool(int argc, const char **argv, const char *prefix);
64 extern int cmd_fast_export(int argc, const char **argv, const char *prefix);
65 extern int cmd_fetch(int argc, const char **argv, const char *prefix);
66 extern int cmd_fetch_pack(int argc, const char **argv, const char *prefix);
builtin/difftool.c new
+63
@@ -0,0 +1,63 @@
1 +/*
2 + * "git difftool" builtin command
3 + *
4 + * This is a wrapper around the GIT_EXTERNAL_DIFF-compatible
5 + * git-difftool--helper script.
6 + *
7 + * This script exports GIT_EXTERNAL_DIFF and GIT_PAGER for use by git.
8 + * The GIT_DIFF* variables are exported for use by git-difftool--helper.
9 + *
10 + * Any arguments that are unknown to this script are forwarded to 'git diff'.
11 + *
12 + * Copyright (C) 2016 Johannes Schindelin
13 + */
14 +#include "builtin.h"
15 +#include "run-command.h"
16 +#include "exec_cmd.h"
17 +
18 +/*
19 + * NEEDSWORK: this function can go once the legacy-difftool Perl script is
20 + * retired.
21 + *
22 + * We intentionally avoid reading the config directly here, to avoid messing up
23 + * the GIT_* environment variables when we need to fall back to exec()ing the
24 + * Perl script.
25 + */
26 +static int use_builtin_difftool(void) {
27 + struct child_process cp = CHILD_PROCESS_INIT;
28 + struct strbuf out = STRBUF_INIT;
29 + int ret;
30 +
31 + argv_array_pushl(&cp.args,
32 + "config", "--bool", "difftool.usebuiltin", NULL);
33 + cp.git_cmd = 1;
34 + if (capture_command(&cp, &out, 6))
35 + return 0;
36 + strbuf_trim(&out);
37 + ret = !strcmp("true", out.buf);
38 + strbuf_release(&out);
39 + return ret;
40 +}
41 +
42 +int cmd_difftool(int argc, const char **argv, const char *prefix)
43 +{
44 + /*
45 + * NEEDSWORK: Once the builtin difftool has been tested enough
46 + * and git-legacy-difftool.perl is retired to contrib/, this preamble
47 + * can be removed.
48 + */
49 + if (!use_builtin_difftool()) {
50 + const char *path = mkpath("%s/git-legacy-difftool",
51 + git_exec_path());
52 +
53 + if (sane_execvp(path, (char **)argv) < 0)
54 + die_errno("could not exec %s", path);
55 +
56 + return 0;
57 + }
58 + prefix = setup_git_directory();
59 + trace_repo_setup(prefix);
60 + setup_work_tree();
61 +
62 + die("TODO");
63 +}
git-legacy-difftool.perl renamed
git.c
+6
@@ -424,6 +424,12 @@ static struct cmd_struct commands[] = {
424 { "diff-files", cmd_diff_files, RUN_SETUP | NEED_WORK_TREE },
425 { "diff-index", cmd_diff_index, RUN_SETUP },
426 { "diff-tree", cmd_diff_tree, RUN_SETUP },
427 + /*
428 + * NEEDSWORK: Once the redirection to git-legacy-difftool.perl in
429 + * builtin/difftool.c has been removed, this entry should be changed to
430 + * RUN_SETUP | NEED_WORK_TREE
431 + */
432 + { "difftool", cmd_difftool },
433 { "fast-export", cmd_fast_export, RUN_SETUP },
434 { "fetch", cmd_fetch, RUN_SETUP },
435 { "fetch-pack", cmd_fetch_pack, RUN_SETUP },
t/t7800-difftool.sh
+2
@@ -23,6 +23,8 @@ prompt_given ()
23 test "$prompt" = "Launch 'test-tool' [Y/n]? branch"
24 }
25
26 +# NEEDSWORK: lose all the PERL prereqs once legacy-difftool is retired.
27 +
28 # Create a file on master and change it on branch
29 test_expect_success PERL 'setup' '
30 echo master >file &&