Skip to content

Commit 860e652

Browse files
committed
Merge branch 'difftool-no-index'
This fixes #2123 Signed-off-by: Johannes Schindelin <[email protected]>
2 parents 59a40ee + 89dfd5d commit 860e652

File tree

7 files changed

+29
-9
lines changed

7 files changed

+29
-9
lines changed

Documentation/technical/api-parse-options.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,10 @@ There are some macros to easily define options:
198198
The filename will be prefixed by passing the filename along with
199199
the prefix argument of `parse_options()` to `prefix_filename()`.
200200

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

204206
`OPT_NUMBER_CALLBACK(&var, description, func_ptr)`::
205207
Recognize numerical options like -123 and feed the integer as

builtin/difftool.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ static int run_file_diff(int prompt, const char *prefix,
690690
int cmd_difftool(int argc, const char **argv, const char *prefix)
691691
{
692692
int use_gui_tool = 0, dir_diff = 0, prompt = -1, symlinks = 0,
693-
tool_help = 0;
693+
tool_help = 0, no_index = 0;
694694
static char *difftool_cmd = NULL, *extcmd = NULL;
695695
struct option builtin_difftool_options[] = {
696696
OPT_BOOL('g', "gui", &use_gui_tool,
@@ -714,6 +714,7 @@ int cmd_difftool(int argc, const char **argv, const char *prefix)
714714
"tool returns a non - zero exit code")),
715715
OPT_STRING('x', "extcmd", &extcmd, N_("command"),
716716
N_("specify a custom command for viewing diffs")),
717+
OPT_ARGUMENT("no-index", &no_index, N_("passed to `diff`")),
717718
OPT_END()
718719
};
719720

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

730-
/* NEEDSWORK: once we no longer spawn anything, remove this */
731-
setenv(GIT_DIR_ENVIRONMENT, absolute_path(get_git_dir()), 1);
732-
setenv(GIT_WORK_TREE_ENVIRONMENT, absolute_path(get_git_work_tree()), 1);
731+
if (!no_index && !startup_info->have_repository)
732+
die(_("difftool requires worktree or --no-index"));
733+
734+
if (!no_index){
735+
setup_work_tree();
736+
setenv(GIT_DIR_ENVIRONMENT, absolute_path(get_git_dir()), 1);
737+
setenv(GIT_WORK_TREE_ENVIRONMENT, absolute_path(get_git_work_tree()), 1);
738+
}
733739

734740
if (use_gui_tool && diff_gui_tool && *diff_gui_tool)
735741
setenv("GIT_DIFF_TOOL", diff_gui_tool, 1);

git.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ static struct cmd_struct commands[] = {
476476
{ "diff-files", cmd_diff_files, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT },
477477
{ "diff-index", cmd_diff_index, RUN_SETUP | NO_PARSEOPT },
478478
{ "diff-tree", cmd_diff_tree, RUN_SETUP | NO_PARSEOPT },
479-
{ "difftool", cmd_difftool, RUN_SETUP | NEED_WORK_TREE },
479+
{ "difftool", cmd_difftool, RUN_SETUP_GENTLY },
480480
{ "fast-export", cmd_fast_export, RUN_SETUP },
481481
{ "fetch", cmd_fetch, RUN_SETUP },
482482
{ "fetch-pack", cmd_fetch_pack, RUN_SETUP | NO_PARSEOPT },

parse-options.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
261261
optname(options, flags));
262262
if (*rest)
263263
continue;
264+
if (options->value)
265+
*(int *)options->value = options->defval;
264266
p->out[p->cpidx++] = arg - 2;
265267
return 0;
266268
}

parse-options.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ struct option {
128128
#define OPT_BOOL_F(s, l, v, h, f) OPT_SET_INT_F(s, l, v, h, 1, f)
129129

130130
#define OPT_END() { OPTION_END }
131-
#define OPT_ARGUMENT(l, h) { OPTION_ARGUMENT, 0, (l), NULL, NULL, \
132-
(h), PARSE_OPT_NOARG}
131+
#define OPT_ARGUMENT(l, v, h) { OPTION_ARGUMENT, 0, (l), (v), NULL, \
132+
(h), PARSE_OPT_NOARG, NULL, 1 }
133133
#define OPT_GROUP(h) { OPTION_GROUP, 0, NULL, NULL, NULL, (h) }
134134
#define OPT_BIT(s, l, v, h, b) OPT_BIT_F(s, l, v, h, b, 0)
135135
#define OPT_NEGBIT(s, l, v, h, b) { OPTION_NEGBIT, (s), (l), (v), NULL, \

t/helper/test-parse-options.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ int cmd__parse_options(int argc, const char **argv)
131131
OPT_NOOP_NOARG(0, "obsolete"),
132132
OPT_STRING_LIST(0, "list", &list, "str", "add str to list"),
133133
OPT_GROUP("Magic arguments"),
134-
OPT_ARGUMENT("quux", "means --quux"),
134+
OPT_ARGUMENT("quux", NULL, "means --quux"),
135135
OPT_NUMBER_CALLBACK(&integer, "set integer to NUM",
136136
number_callback),
137137
{ OPTION_COUNTUP, '+', NULL, &boolean, NULL, "same as -b",

t/t7800-difftool.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,4 +705,14 @@ test_expect_success SYMLINKS 'difftool --dir-diff handles modified symlinks' '
705705
test_cmp expect actual
706706
'
707707

708+
test_expect_success 'outside worktree' '
709+
echo 1 >1 &&
710+
echo 2 >2 &&
711+
test_expect_code 1 nongit git \
712+
-c diff.tool=echo -c difftool.echo.cmd="echo \$LOCAL \$REMOTE" \
713+
difftool --no-prompt --no-index --ext-diff ../1 ../2 >actual &&
714+
echo "../1 ../2" >expect &&
715+
test_cmp expect actual
716+
'
717+
708718
test_done

0 commit comments

Comments
 (0)