Skip to content

Commit c167a96

Browse files
committed
Merge branch 'nd/diff-with-path-params'
A few options of "git diff" did not work well when the command was run from a subdirectory. * nd/diff-with-path-params: diff: make -O and --output work in subdirectory diff-no-index: do not take a redundant prefix argument
2 parents 465c30a + a97262c commit c167a96

File tree

7 files changed

+25
-12
lines changed

7 files changed

+25
-12
lines changed

builtin/am.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1642,7 +1642,7 @@ static int fall_back_threeway(const struct am_state *state, const char *index_pa
16421642

16431643
init_revisions(&rev_info, NULL);
16441644
rev_info.diffopt.output_format = DIFF_FORMAT_NAME_STATUS;
1645-
diff_opt_parse(&rev_info.diffopt, &diff_filter_str, 1);
1645+
diff_opt_parse(&rev_info.diffopt, &diff_filter_str, 1, rev_info.prefix);
16461646
add_pending_sha1(&rev_info, "HEAD", our_tree, 0);
16471647
diff_setup_done(&rev_info.diffopt);
16481648
run_diff_index(&rev_info, 1);

builtin/diff.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
341341
}
342342
if (no_index)
343343
/* If this is a no-index diff, just run it and exit there. */
344-
diff_no_index(&rev, argc, argv, prefix);
344+
diff_no_index(&rev, argc, argv);
345345

346346
/* Otherwise, we are doing the usual "git" diff */
347347
rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;

diff-no-index.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,12 +237,12 @@ static void fixup_paths(const char **path, struct strbuf *replacement)
237237
}
238238

239239
void diff_no_index(struct rev_info *revs,
240-
int argc, const char **argv,
241-
const char *prefix)
240+
int argc, const char **argv)
242241
{
243242
int i, prefixlen;
244243
const char *paths[2];
245244
struct strbuf replacement = STRBUF_INIT;
245+
const char *prefix = revs->prefix;
246246

247247
diff_setup(&revs->diffopt);
248248
for (i = 1; i < argc - 2; ) {
@@ -252,7 +252,8 @@ void diff_no_index(struct rev_info *revs,
252252
else if (!strcmp(argv[i], "--"))
253253
i++;
254254
else {
255-
j = diff_opt_parse(&revs->diffopt, argv + i, argc - i);
255+
j = diff_opt_parse(&revs->diffopt, argv + i, argc - i,
256+
revs->prefix);
256257
if (j <= 0)
257258
die("invalid diff option/value: %s", argv[i]);
258259
i += j;

diff.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3693,12 +3693,16 @@ static int parse_ws_error_highlight(struct diff_options *opt, const char *arg)
36933693
return 1;
36943694
}
36953695

3696-
int diff_opt_parse(struct diff_options *options, const char **av, int ac)
3696+
int diff_opt_parse(struct diff_options *options,
3697+
const char **av, int ac, const char *prefix)
36973698
{
36983699
const char *arg = av[0];
36993700
const char *optarg;
37003701
int argcount;
37013702

3703+
if (!prefix)
3704+
prefix = "";
3705+
37023706
/* Output format options */
37033707
if (!strcmp(arg, "-p") || !strcmp(arg, "-u") || !strcmp(arg, "--patch")
37043708
|| opt_arg(arg, 'U', "unified", &options->context))
@@ -3915,7 +3919,8 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
39153919
else if (!strcmp(arg, "--pickaxe-regex"))
39163920
options->pickaxe_opts |= DIFF_PICKAXE_REGEX;
39173921
else if ((argcount = short_opt('O', av, &optarg))) {
3918-
options->orderfile = optarg;
3922+
const char *path = prefix_filename(prefix, strlen(prefix), optarg);
3923+
options->orderfile = xstrdup(path);
39193924
return argcount;
39203925
}
39213926
else if ((argcount = parse_long_opt("diff-filter", av, &optarg))) {
@@ -3954,9 +3959,10 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
39543959
else if (!strcmp(arg, "--no-function-context"))
39553960
DIFF_OPT_CLR(options, FUNCCONTEXT);
39563961
else if ((argcount = parse_long_opt("output", av, &optarg))) {
3957-
options->file = fopen(optarg, "w");
3962+
const char *path = prefix_filename(prefix, strlen(prefix), optarg);
3963+
options->file = fopen(path, "w");
39583964
if (!options->file)
3959-
die_errno("Could not open '%s'", optarg);
3965+
die_errno("Could not open '%s'", path);
39603966
options->close_file = 1;
39613967
return argcount;
39623968
} else

diff.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ extern int parse_long_opt(const char *opt, const char **argv,
268268
extern int git_diff_basic_config(const char *var, const char *value, void *cb);
269269
extern int git_diff_ui_config(const char *var, const char *value, void *cb);
270270
extern void diff_setup(struct diff_options *);
271-
extern int diff_opt_parse(struct diff_options *, const char **, int);
271+
extern int diff_opt_parse(struct diff_options *, const char **, int, const char *);
272272
extern void diff_setup_done(struct diff_options *);
273273

274274
#define DIFF_DETECT_RENAME 1
@@ -345,7 +345,7 @@ extern int diff_flush_patch_id(struct diff_options *, unsigned char *);
345345

346346
extern int diff_result_code(struct diff_options *, int);
347347

348-
extern void diff_no_index(struct rev_info *, int, const char **, const char *);
348+
extern void diff_no_index(struct rev_info *, int, const char **);
349349

350350
extern int index_differs_from(const char *def, int diff_flags);
351351

revision.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2044,7 +2044,7 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
20442044
} else if (!strcmp(arg, "--ignore-missing")) {
20452045
revs->ignore_missing = 1;
20462046
} else {
2047-
int opts = diff_opt_parse(&revs->diffopt, argv, argc);
2047+
int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
20482048
if (!opts)
20492049
unkv[(*unkc)++] = arg;
20502050
return opts;

t/t4056-diff-order.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ test_expect_success POSIXPERM,SANITY 'unreadable orderfile' '
6868
test_must_fail git diff -Ounreadable_file --name-only HEAD^..HEAD
6969
'
7070

71+
test_expect_success "orderfile using option from subdir with --output" '
72+
mkdir subdir &&
73+
git -C subdir diff -O../order_file_1 --output ../actual --name-only HEAD^..HEAD &&
74+
test_cmp expect_1 actual
75+
'
76+
7177
for i in 1 2
7278
do
7379
test_expect_success "orderfile using option ($i)" '

0 commit comments

Comments
 (0)