Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit 6779e43

Browse files
committed
Merge branch 'jk/external-diff-use-argv-array'
Code clean-up (and a bugfix which has been merged for 2.0). * jk/external-diff-use-argv-array: run_external_diff: refactor cmdline setup logic run_external_diff: hoist common bits out of conditional run_external_diff: drop fflush(NULL) run_external_diff: clean up error handling run_external_diff: use an argv_array for the environment
2 parents 06b2a0f + f3efe78 commit 6779e43

File tree

2 files changed

+26
-32
lines changed

2 files changed

+26
-32
lines changed

diff.c

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2880,6 +2880,16 @@ static struct diff_tempfile *prepare_temp_file(const char *name,
28802880
return temp;
28812881
}
28822882

2883+
static void add_external_diff_name(struct argv_array *argv,
2884+
const char *name,
2885+
struct diff_filespec *df)
2886+
{
2887+
struct diff_tempfile *temp = prepare_temp_file(name, df);
2888+
argv_array_push(argv, temp->name);
2889+
argv_array_push(argv, temp->hex);
2890+
argv_array_push(argv, temp->mode);
2891+
}
2892+
28832893
/* An external diff command takes:
28842894
*
28852895
* diff-cmd name infile1 infile1-sha1 infile1-mode \
@@ -2896,48 +2906,32 @@ static void run_external_diff(const char *pgm,
28962906
struct diff_options *o)
28972907
{
28982908
struct argv_array argv = ARGV_ARRAY_INIT;
2899-
int retval;
2909+
struct argv_array env = ARGV_ARRAY_INIT;
29002910
struct diff_queue_struct *q = &diff_queued_diff;
2901-
const char *env[3] = { NULL };
2902-
char env_counter[50];
2903-
char env_total[50];
2911+
2912+
argv_array_push(&argv, pgm);
2913+
argv_array_push(&argv, name);
29042914

29052915
if (one && two) {
2906-
struct diff_tempfile *temp_one, *temp_two;
2907-
const char *othername = (other ? other : name);
2908-
temp_one = prepare_temp_file(name, one);
2909-
temp_two = prepare_temp_file(othername, two);
2910-
argv_array_push(&argv, pgm);
2911-
argv_array_push(&argv, name);
2912-
argv_array_push(&argv, temp_one->name);
2913-
argv_array_push(&argv, temp_one->hex);
2914-
argv_array_push(&argv, temp_one->mode);
2915-
argv_array_push(&argv, temp_two->name);
2916-
argv_array_push(&argv, temp_two->hex);
2917-
argv_array_push(&argv, temp_two->mode);
2918-
if (other) {
2916+
add_external_diff_name(&argv, name, one);
2917+
if (!other)
2918+
add_external_diff_name(&argv, name, two);
2919+
else {
2920+
add_external_diff_name(&argv, other, two);
29192921
argv_array_push(&argv, other);
29202922
argv_array_push(&argv, xfrm_msg);
29212923
}
2922-
} else {
2923-
argv_array_push(&argv, pgm);
2924-
argv_array_push(&argv, name);
29252924
}
2926-
fflush(NULL);
29272925

2928-
env[0] = env_counter;
2929-
snprintf(env_counter, sizeof(env_counter), "GIT_DIFF_PATH_COUNTER=%d",
2930-
++o->diff_path_counter);
2931-
env[1] = env_total;
2932-
snprintf(env_total, sizeof(env_total), "GIT_DIFF_PATH_TOTAL=%d", q->nr);
2926+
argv_array_pushf(&env, "GIT_DIFF_PATH_COUNTER=%d", ++o->diff_path_counter);
2927+
argv_array_pushf(&env, "GIT_DIFF_PATH_TOTAL=%d", q->nr);
2928+
2929+
if (run_command_v_opt_cd_env(argv.argv, RUN_USING_SHELL, NULL, env.argv))
2930+
die(_("external diff died, stopping at %s"), name);
29332931

2934-
retval = run_command_v_opt_cd_env(argv.argv, RUN_USING_SHELL, NULL, env);
29352932
remove_tempfile();
29362933
argv_array_clear(&argv);
2937-
if (retval) {
2938-
fprintf(stderr, "external diff died, stopping at %s.\n", name);
2939-
exit(1);
2940-
}
2934+
argv_array_clear(&env);
29412935
}
29422936

29432937
static int similarity_index(struct diff_filepair *p)

t/t7800-difftool.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ test_expect_success PERL 'custom tool commands override built-ins' '
5858

5959
test_expect_success PERL 'difftool ignores bad --tool values' '
6060
: >expect &&
61-
test_expect_code 1 \
61+
test_must_fail \
6262
git difftool --no-prompt --tool=bad-tool branch >actual &&
6363
test_cmp expect actual
6464
'

0 commit comments

Comments
 (0)