2 * Copyright (C) 2005 Junio C Hamano
9 static char *diff_cmd = "diff -L'k/%s' -L'l/%s'";
10 static char *diff_opts = "-pu";
12 static const char *external_diff(void)
14 static char *external_diff_cmd = NULL;
15 static int done_preparing = 0;
18 return external_diff_cmd;
21 * Default values above are meant to match the
22 * Linux kernel development style. Examples of
23 * alternative styles you can specify via environment
26 * GIT_DIFF_CMD="diff -L '%s' -L '%s'"
29 if (getenv("GIT_EXTERNAL_DIFF"))
30 external_diff_cmd = getenv("GIT_EXTERNAL_DIFF");
32 /* In case external diff fails... */
33 diff_cmd = getenv("GIT_DIFF_CMD") ? : diff_cmd;
34 diff_opts = getenv("GIT_DIFF_OPTS") ? : diff_opts;
37 return external_diff_cmd;
40 /* Help to copy the thing properly quoted for the shell safety.
41 * any single quote is replaced with '\'', and the caller is
42 * expected to enclose the result within a single quote pair.
45 * original sq_expand result
46 * name ==> name ==> 'name'
47 * a b ==> a b ==> 'a b'
48 * a'b ==> a'\''b ==> 'a'\''b'
50 static char *sq_expand(const char *src)
52 static char *buf = NULL;
57 /* count bytes needed to store the quoted string. */
58 for (cnt = 1, cp = src; *cp; cnt++, cp++)
62 if (! (buf = malloc(cnt)))
65 while ((c = *src++)) {
69 bp = strcpy(bp, "'\\''");
77 static struct diff_tempfile {
84 static void builtin_diff(const char *name,
85 struct diff_tempfile *temp)
87 static char *diff_arg = "'%s' '%s'";
88 const char *name_1_sq = sq_expand(temp[0].name);
89 const char *name_2_sq = sq_expand(temp[1].name);
90 const char *name_sq = sq_expand(name);
92 /* diff_cmd and diff_arg have 4 %s in total which makes
93 * the sum of these strings 8 bytes larger than required.
94 * we use 2 spaces around diff-opts, and we need to count
95 * terminating NUL, so we subtract 5 here.
97 int cmd_size = (strlen(diff_cmd) +
101 strlen(name_1_sq) + strlen(name_2_sq)
103 char *cmd = malloc(cmd_size);
106 next_at += snprintf(cmd+next_at, cmd_size-next_at,
107 diff_cmd, name_sq, name_sq);
108 next_at += snprintf(cmd+next_at, cmd_size-next_at,
110 next_at += snprintf(cmd+next_at, cmd_size-next_at,
111 diff_arg, name_1_sq, name_2_sq);
112 execlp("/bin/sh","sh", "-c", cmd, NULL);
115 static void prepare_temp_file(const char *name,
116 struct diff_tempfile *temp,
117 struct diff_spec *one)
119 static unsigned char null_sha1[20] = { 0, };
121 if (!one->file_valid) {
123 temp->name = "/dev/null";
124 strcpy(temp->hex, ".");
125 strcpy(temp->mode, ".");
129 if (one->sha1_valid &&
130 !memcmp(one->u.sha1, null_sha1, sizeof(null_sha1))) {
135 if (!one->sha1_valid) {
137 temp->name = one->u.name;
138 if (stat(temp->name, &st) < 0) {
140 goto not_a_valid_file;
141 die("stat(%s): %s", temp->name, strerror(errno));
143 strcpy(temp->hex, ".");
144 sprintf(temp->mode, "%06o",
145 S_IFREG |ce_permissions(st.st_mode));
153 blob = read_sha1_file(one->u.sha1, type, &size);
154 if (!blob || strcmp(type, "blob"))
155 die("unable to read blob object for %s (%s)",
156 name, sha1_to_hex(one->u.sha1));
158 strcpy(temp->tmp_path, ".diff_XXXXXX");
159 fd = mkstemp(temp->tmp_path);
161 die("unable to create temp-file");
162 if (write(fd, blob, size) != size)
163 die("unable to write temp-file");
166 temp->name = temp->tmp_path;
167 strcpy(temp->hex, sha1_to_hex(one->u.sha1));
169 sprintf(temp->mode, "%06o", one->mode);
173 static void remove_tempfile(void)
177 for (i = 0; i < 2; i++)
178 if (diff_temp[i].name == diff_temp[i].tmp_path) {
179 unlink(diff_temp[i].name);
180 diff_temp[i].name = NULL;
184 /* An external diff command takes:
186 * diff-cmd name infile1 infile1-sha1 infile1-mode \
187 * infile2 infile2-sha1 infile2-mode.
190 void run_external_diff(const char *name,
191 struct diff_spec *one,
192 struct diff_spec *two)
194 struct diff_tempfile *temp = diff_temp;
196 static int atexit_asked = 0;
198 prepare_temp_file(name, &temp[0], one);
199 prepare_temp_file(name, &temp[1], two);
200 if (! atexit_asked &&
201 (temp[0].name == temp[0].tmp_path ||
202 temp[1].name == temp[1].tmp_path)) {
204 atexit(remove_tempfile);
210 die("unable to fork");
212 const char *pgm = external_diff();
216 temp[0].name, temp[0].hex, temp[0].mode,
217 temp[1].name, temp[1].hex, temp[1].mode,
220 * otherwise we use the built-in one.
222 builtin_diff(name, temp);
225 if (waitpid(pid, &status, 0) < 0 || !WIFEXITED(status))
226 die("diff program failed");
231 void show_diff_empty(const struct cache_entry *ce, int reverse)
233 struct diff_spec spec[2], *one, *two;
235 memcpy(spec[0].u.sha1, ce->sha1, 20);
236 spec[0].mode = ntohl(ce->ce_mode);
237 spec[0].sha1_valid = spec[0].file_valid = 1;
238 spec[1].file_valid = 0;
241 one = spec + 1; two = spec;
243 one = spec; two = one + 1;
246 run_external_diff(ce->name, one, two);
249 void show_differences(const struct cache_entry *ce, int reverse)
251 struct diff_spec spec[2], *one, *two;
253 memcpy(spec[0].u.sha1, ce->sha1, 20);
254 spec[0].mode = ntohl(ce->ce_mode);
255 spec[0].sha1_valid = spec[0].file_valid = 1;
257 spec[1].u.name = ce->name; /* the name we stated */
258 spec[1].sha1_valid = 0;
259 spec[1].file_valid = 1;
262 one = spec + 1; two = spec;
264 one = spec; two = one + 1;
267 run_external_diff(ce->name, one, two);