Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Junio C Hamano committed Feb 3, 2016
2 parents 465c30a + a97262c commit c167a96
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 12 deletions.
2 changes: 1 addition & 1 deletion builtin/am.c
Expand Up @@ -1642,7 +1642,7 @@ static int fall_back_threeway(const struct am_state *state, const char *index_pa

init_revisions(&rev_info, NULL);
rev_info.diffopt.output_format = DIFF_FORMAT_NAME_STATUS;
diff_opt_parse(&rev_info.diffopt, &diff_filter_str, 1);
diff_opt_parse(&rev_info.diffopt, &diff_filter_str, 1, rev_info.prefix);
add_pending_sha1(&rev_info, "HEAD", our_tree, 0);
diff_setup_done(&rev_info.diffopt);
run_diff_index(&rev_info, 1);
Expand Down
2 changes: 1 addition & 1 deletion builtin/diff.c
Expand Up @@ -341,7 +341,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
}
if (no_index)
/* If this is a no-index diff, just run it and exit there. */
diff_no_index(&rev, argc, argv, prefix);
diff_no_index(&rev, argc, argv);

/* Otherwise, we are doing the usual "git" diff */
rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
Expand Down
7 changes: 4 additions & 3 deletions diff-no-index.c
Expand Up @@ -237,12 +237,12 @@ static void fixup_paths(const char **path, struct strbuf *replacement)
}

void diff_no_index(struct rev_info *revs,
int argc, const char **argv,
const char *prefix)
int argc, const char **argv)
{
int i, prefixlen;
const char *paths[2];
struct strbuf replacement = STRBUF_INIT;
const char *prefix = revs->prefix;

diff_setup(&revs->diffopt);
for (i = 1; i < argc - 2; ) {
Expand All @@ -252,7 +252,8 @@ void diff_no_index(struct rev_info *revs,
else if (!strcmp(argv[i], "--"))
i++;
else {
j = diff_opt_parse(&revs->diffopt, argv + i, argc - i);
j = diff_opt_parse(&revs->diffopt, argv + i, argc - i,
revs->prefix);
if (j <= 0)
die("invalid diff option/value: %s", argv[i]);
i += j;
Expand Down
14 changes: 10 additions & 4 deletions diff.c
Expand Up @@ -3693,12 +3693,16 @@ static int parse_ws_error_highlight(struct diff_options *opt, const char *arg)
return 1;
}

int diff_opt_parse(struct diff_options *options, const char **av, int ac)
int diff_opt_parse(struct diff_options *options,
const char **av, int ac, const char *prefix)
{
const char *arg = av[0];
const char *optarg;
int argcount;

if (!prefix)
prefix = "";

/* Output format options */
if (!strcmp(arg, "-p") || !strcmp(arg, "-u") || !strcmp(arg, "--patch")
|| opt_arg(arg, 'U', "unified", &options->context))
Expand Down Expand Up @@ -3915,7 +3919,8 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
else if (!strcmp(arg, "--pickaxe-regex"))
options->pickaxe_opts |= DIFF_PICKAXE_REGEX;
else if ((argcount = short_opt('O', av, &optarg))) {
options->orderfile = optarg;
const char *path = prefix_filename(prefix, strlen(prefix), optarg);
options->orderfile = xstrdup(path);
return argcount;
}
else if ((argcount = parse_long_opt("diff-filter", av, &optarg))) {
Expand Down Expand Up @@ -3954,9 +3959,10 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
else if (!strcmp(arg, "--no-function-context"))
DIFF_OPT_CLR(options, FUNCCONTEXT);
else if ((argcount = parse_long_opt("output", av, &optarg))) {
options->file = fopen(optarg, "w");
const char *path = prefix_filename(prefix, strlen(prefix), optarg);
options->file = fopen(path, "w");
if (!options->file)
die_errno("Could not open '%s'", optarg);
die_errno("Could not open '%s'", path);
options->close_file = 1;
return argcount;
} else
Expand Down
4 changes: 2 additions & 2 deletions diff.h
Expand Up @@ -268,7 +268,7 @@ extern int parse_long_opt(const char *opt, const char **argv,
extern int git_diff_basic_config(const char *var, const char *value, void *cb);
extern int git_diff_ui_config(const char *var, const char *value, void *cb);
extern void diff_setup(struct diff_options *);
extern int diff_opt_parse(struct diff_options *, const char **, int);
extern int diff_opt_parse(struct diff_options *, const char **, int, const char *);
extern void diff_setup_done(struct diff_options *);

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

extern int diff_result_code(struct diff_options *, int);

extern void diff_no_index(struct rev_info *, int, const char **, const char *);
extern void diff_no_index(struct rev_info *, int, const char **);

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

Expand Down
2 changes: 1 addition & 1 deletion revision.c
Expand Up @@ -2044,7 +2044,7 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
} else if (!strcmp(arg, "--ignore-missing")) {
revs->ignore_missing = 1;
} else {
int opts = diff_opt_parse(&revs->diffopt, argv, argc);
int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
if (!opts)
unkv[(*unkc)++] = arg;
return opts;
Expand Down
6 changes: 6 additions & 0 deletions t/t4056-diff-order.sh
Expand Up @@ -68,6 +68,12 @@ test_expect_success POSIXPERM,SANITY 'unreadable orderfile' '
test_must_fail git diff -Ounreadable_file --name-only HEAD^..HEAD
'

test_expect_success "orderfile using option from subdir with --output" '
mkdir subdir &&
git -C subdir diff -O../order_file_1 --output ../actual --name-only HEAD^..HEAD &&
test_cmp expect_1 actual
'

for i in 1 2
do
test_expect_success "orderfile using option ($i)" '
Expand Down

0 comments on commit c167a96

Please sign in to comment.