Skip to content

Commit

Permalink
Merge branch 'rs/diff-no-index-cleanup'
Browse files Browse the repository at this point in the history
"git diff --no-index A B" managed its the pathnames of its two
input files rather haphazardly, sometimes leaking them.  The
command line argument processing has been straightened out to clean
it up.

* rs/diff-no-index-cleanup:
  diff-no-index: simplify argv index calculation
  diff-no-index: release prefixed filenames
  diff-no-index: release strbuf on queue error
  • Loading branch information
gitster committed Sep 13, 2022
2 parents f322e9f + 2b43dd0 commit e0574c4
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions diff-no-index.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,9 @@ int diff_no_index(struct rev_info *revs,
int argc, const char **argv)
{
int i, no_index;
int ret = 1;
const char *paths[2];
char *to_free[ARRAY_SIZE(paths)] = { 0 };
struct strbuf replacement = STRBUF_INIT;
const char *prefix = revs->prefix;
struct option no_index_options[] = {
Expand All @@ -265,15 +267,15 @@ int diff_no_index(struct rev_info *revs,
}
FREE_AND_NULL(options);
for (i = 0; i < 2; i++) {
const char *p = argv[argc - 2 + i];
const char *p = argv[i];
if (!strcmp(p, "-"))
/*
* stdin should be spelled as "-"; if you have
* path that is "-", spell it as "./-".
*/
p = file_from_standard_input;
else if (prefix)
p = prefix_filename(prefix, p);
p = to_free[i] = prefix_filename(prefix, p);
paths[i] = p;
}

Expand All @@ -295,16 +297,20 @@ int diff_no_index(struct rev_info *revs,
revs->diffopt.flags.exit_with_status = 1;

if (queue_diff(&revs->diffopt, paths[0], paths[1]))
return 1;
goto out;
diff_set_mnemonic_prefix(&revs->diffopt, "1/", "2/");
diffcore_std(&revs->diffopt);
diff_flush(&revs->diffopt);

strbuf_release(&replacement);

/*
* The return code for --no-index imitates diff(1):
* 0 = no changes, 1 = changes, else error
*/
return diff_result_code(&revs->diffopt, 0);
ret = diff_result_code(&revs->diffopt, 0);

out:
for (i = 0; i < ARRAY_SIZE(to_free); i++)
free(to_free[i]);
strbuf_release(&replacement);
return ret;
}

0 comments on commit e0574c4

Please sign in to comment.