Skip to content
Permalink
Browse files

Merge pull request #2175 from dscho/difftool-no-index

Allow `git difftool --no-index` to run outside of a worktree
  • Loading branch information...
dscho committed May 8, 2019
2 parents a930c5a + 290a666 commit 65f15706241a2714deb2999b0b5e70d1dc3d3ce6
Showing with 30 additions and 9 deletions.
  1. +3 −1 Documentation/technical/api-parse-options.txt
  2. +11 −4 builtin/difftool.c
  3. +1 −1 git.c
  4. +2 −0 parse-options.c
  5. +2 −2 parse-options.h
  6. +1 −1 t/helper/test-parse-options.c
  7. +10 −0 t/t7800-difftool.sh
@@ -198,8 +198,10 @@ There are some macros to easily define options:
The filename will be prefixed by passing the filename along with
the prefix argument of `parse_options()` to `prefix_filename()`.

`OPT_ARGUMENT(long, description)`::
`OPT_ARGUMENT(long, &int_var, description)`::
Introduce a long-option argument that will be kept in `argv[]`.
If this option was seen, `int_var` will be set to one (except
if a `NULL` pointer was passed).

`OPT_NUMBER_CALLBACK(&var, description, func_ptr)`::
Recognize numerical options like -123 and feed the integer as
@@ -690,7 +690,7 @@ static int run_file_diff(int prompt, const char *prefix,
int cmd_difftool(int argc, const char **argv, const char *prefix)
{
int use_gui_tool = 0, dir_diff = 0, prompt = -1, symlinks = 0,
tool_help = 0;
tool_help = 0, no_index = 0;
static char *difftool_cmd = NULL, *extcmd = NULL;
struct option builtin_difftool_options[] = {
OPT_BOOL('g', "gui", &use_gui_tool,
@@ -714,6 +714,7 @@ int cmd_difftool(int argc, const char **argv, const char *prefix)
"tool returns a non - zero exit code")),
OPT_STRING('x', "extcmd", &extcmd, N_("command"),
N_("specify a custom command for viewing diffs")),
OPT_ARGUMENT("no-index", &no_index, N_("passed to `diff`")),
OPT_END()
};

@@ -727,9 +728,15 @@ int cmd_difftool(int argc, const char **argv, const char *prefix)
if (tool_help)
return print_tool_help();

/* NEEDSWORK: once we no longer spawn anything, remove this */
setenv(GIT_DIR_ENVIRONMENT, absolute_path(get_git_dir()), 1);
setenv(GIT_WORK_TREE_ENVIRONMENT, absolute_path(get_git_work_tree()), 1);
if (!no_index && !startup_info->have_repository)
die(_("difftool requires worktree or --no-index"));

if (!no_index){
setup_work_tree();
setenv(GIT_DIR_ENVIRONMENT, absolute_path(get_git_dir()), 1);
setenv(GIT_WORK_TREE_ENVIRONMENT, absolute_path(get_git_work_tree()), 1);
} else if (dir_diff)
die(_("--dir-diff is incompatible with --no-index"));

if (use_gui_tool && diff_gui_tool && *diff_gui_tool)
setenv("GIT_DIFF_TOOL", diff_gui_tool, 1);
2 git.c
@@ -476,7 +476,7 @@ static struct cmd_struct commands[] = {
{ "diff-files", cmd_diff_files, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT },
{ "diff-index", cmd_diff_index, RUN_SETUP | NO_PARSEOPT },
{ "diff-tree", cmd_diff_tree, RUN_SETUP | NO_PARSEOPT },
{ "difftool", cmd_difftool, RUN_SETUP | NEED_WORK_TREE },
{ "difftool", cmd_difftool, RUN_SETUP_GENTLY },
{ "fast-export", cmd_fast_export, RUN_SETUP },
{ "fetch", cmd_fetch, RUN_SETUP },
{ "fetch-pack", cmd_fetch_pack, RUN_SETUP | NO_PARSEOPT },
@@ -261,6 +261,8 @@ static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
optname(options, flags));
if (*rest)
continue;
if (options->value)
*(int *)options->value = options->defval;
p->out[p->cpidx++] = arg - 2;
return 0;
}
@@ -128,8 +128,8 @@ struct option {
#define OPT_BOOL_F(s, l, v, h, f) OPT_SET_INT_F(s, l, v, h, 1, f)

#define OPT_END() { OPTION_END }
#define OPT_ARGUMENT(l, h) { OPTION_ARGUMENT, 0, (l), NULL, NULL, \
(h), PARSE_OPT_NOARG}
#define OPT_ARGUMENT(l, v, h) { OPTION_ARGUMENT, 0, (l), (v), NULL, \
(h), PARSE_OPT_NOARG, NULL, 1 }
#define OPT_GROUP(h) { OPTION_GROUP, 0, NULL, NULL, NULL, (h) }
#define OPT_BIT(s, l, v, h, b) OPT_BIT_F(s, l, v, h, b, 0)
#define OPT_NEGBIT(s, l, v, h, b) { OPTION_NEGBIT, (s), (l), (v), NULL, \
@@ -131,7 +131,7 @@ int cmd__parse_options(int argc, const char **argv)
OPT_NOOP_NOARG(0, "obsolete"),
OPT_STRING_LIST(0, "list", &list, "str", "add str to list"),
OPT_GROUP("Magic arguments"),
OPT_ARGUMENT("quux", "means --quux"),
OPT_ARGUMENT("quux", NULL, "means --quux"),
OPT_NUMBER_CALLBACK(&integer, "set integer to NUM",
number_callback),
{ OPTION_COUNTUP, '+', NULL, &boolean, NULL, "same as -b",
@@ -705,4 +705,14 @@ test_expect_success SYMLINKS 'difftool --dir-diff handles modified symlinks' '
test_cmp expect actual
'

test_expect_success 'outside worktree' '
echo 1 >1 &&
echo 2 >2 &&
test_expect_code 1 nongit git \
-c diff.tool=echo -c difftool.echo.cmd="echo \$LOCAL \$REMOTE" \
difftool --no-prompt --no-index --ext-diff ../1 ../2 >actual &&
echo "../1 ../2" >expect &&
test_cmp expect actual
'

test_done

0 comments on commit 65f1570

Please sign in to comment.
You can’t perform that action at this time.