Skip to content

Commit e0574c4

Browse files
committed
Merge branch 'rs/diff-no-index-cleanup'
"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
2 parents f322e9f + 2b43dd0 commit e0574c4

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

diff-no-index.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,9 @@ int diff_no_index(struct rev_info *revs,
243243
int argc, const char **argv)
244244
{
245245
int i, no_index;
246+
int ret = 1;
246247
const char *paths[2];
248+
char *to_free[ARRAY_SIZE(paths)] = { 0 };
247249
struct strbuf replacement = STRBUF_INIT;
248250
const char *prefix = revs->prefix;
249251
struct option no_index_options[] = {
@@ -265,15 +267,15 @@ int diff_no_index(struct rev_info *revs,
265267
}
266268
FREE_AND_NULL(options);
267269
for (i = 0; i < 2; i++) {
268-
const char *p = argv[argc - 2 + i];
270+
const char *p = argv[i];
269271
if (!strcmp(p, "-"))
270272
/*
271273
* stdin should be spelled as "-"; if you have
272274
* path that is "-", spell it as "./-".
273275
*/
274276
p = file_from_standard_input;
275277
else if (prefix)
276-
p = prefix_filename(prefix, p);
278+
p = to_free[i] = prefix_filename(prefix, p);
277279
paths[i] = p;
278280
}
279281

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

297299
if (queue_diff(&revs->diffopt, paths[0], paths[1]))
298-
return 1;
300+
goto out;
299301
diff_set_mnemonic_prefix(&revs->diffopt, "1/", "2/");
300302
diffcore_std(&revs->diffopt);
301303
diff_flush(&revs->diffopt);
302304

303-
strbuf_release(&replacement);
304-
305305
/*
306306
* The return code for --no-index imitates diff(1):
307307
* 0 = no changes, 1 = changes, else error
308308
*/
309-
return diff_result_code(&revs->diffopt, 0);
309+
ret = diff_result_code(&revs->diffopt, 0);
310+
311+
out:
312+
for (i = 0; i < ARRAY_SIZE(to_free); i++)
313+
free(to_free[i]);
314+
strbuf_release(&replacement);
315+
return ret;
310316
}

0 commit comments

Comments
 (0)