2 * Copyright (C) 2005 Junio C Hamano
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
28 if (getenv("GIT_EXTERNAL_DIFF"))
29 external_diff_cmd = getenv("GIT_EXTERNAL_DIFF");
31 /* In case external diff fails... */
32 diff_opts = getenv("GIT_DIFF_OPTS") ? : diff_opts;
35 return external_diff_cmd;
38 /* Help to copy the thing properly quoted for the shell safety.
39 * any single quote is replaced with '\'', and the caller is
40 * expected to enclose the result within a single quote pair.
43 * original sq_expand result
44 * name ==> name ==> 'name'
45 * a b ==> a b ==> 'a b'
46 * a'b ==> a'\''b ==> 'a'\''b'
48 static char *sq_expand(const char *src)
50 static char *buf = NULL;
55 /* count bytes needed to store the quoted string. */
56 for (cnt = 1, cp = src; *cp; cnt++, cp++)
62 while ((c = *src++)) {
66 bp = strcpy(bp, "'\\''");
74 static struct diff_tempfile {
81 static void builtin_diff(const char *name,
82 struct diff_tempfile *temp)
85 const char *diff_cmd = "diff -L'%s%s' -L'%s%s'";
86 const char *diff_arg = "'%s' '%s'";
87 const char *input_name_sq[2];
90 const char *name_sq = sq_expand(name);
93 /* diff_cmd and diff_arg have 6 %s in total which makes
94 * the sum of these strings 12 bytes larger than required.
95 * we use 2 spaces around diff-opts, and we need to count
96 * terminating NUL, so we subtract 9 here.
98 int cmd_size = (strlen(diff_cmd) + strlen(diff_opts) +
99 strlen(diff_arg) - 9);
100 for (i = 0; i < 2; i++) {
101 input_name_sq[i] = sq_expand(temp[i].name);
102 if (!strcmp(temp[i].name, "/dev/null")) {
103 path0[i] = "/dev/null";
106 path0[i] = i ? "b/" : "a/";
109 cmd_size += (strlen(path0[i]) + strlen(path1[i]) +
110 strlen(input_name_sq[i]));
113 cmd = xmalloc(cmd_size);
116 next_at += snprintf(cmd+next_at, cmd_size-next_at,
118 path0[0], path1[0], path0[1], path1[1]);
119 next_at += snprintf(cmd+next_at, cmd_size-next_at,
121 next_at += snprintf(cmd+next_at, cmd_size-next_at,
122 diff_arg, input_name_sq[0], input_name_sq[1]);
125 printf("Created: %s (mode:%s)\n", name, temp[1].mode);
126 else if (!path1[1][0])
127 printf("Deleted: %s\n", name);
128 else if (strcmp(temp[0].mode, temp[1].mode))
129 printf("Mode changed: %s (%s->%s)\n", name,
130 temp[0].mode, temp[1].mode);
132 execlp("/bin/sh","sh", "-c", cmd, NULL);
135 static void prepare_temp_file(const char *name,
136 struct diff_tempfile *temp,
137 struct diff_spec *one)
139 static unsigned char null_sha1[20] = { 0, };
141 if (!one->file_valid) {
143 /* A '-' entry produces this for file-2, and
144 * a '+' entry produces this for file-1.
146 temp->name = "/dev/null";
147 strcpy(temp->hex, ".");
148 strcpy(temp->mode, ".");
152 if (one->sha1_valid &&
153 !memcmp(one->u.sha1, null_sha1, sizeof(null_sha1))) {
158 if (!one->sha1_valid) {
160 temp->name = one->u.name;
161 if (stat(temp->name, &st) < 0) {
163 goto not_a_valid_file;
164 die("stat(%s): %s", temp->name, strerror(errno));
166 strcpy(temp->hex, sha1_to_hex(null_sha1));
167 sprintf(temp->mode, "%06o",
168 S_IFREG |ce_permissions(st.st_mode));
176 blob = read_sha1_file(one->u.sha1, type, &size);
177 if (!blob || strcmp(type, "blob"))
178 die("unable to read blob object for %s (%s)",
179 name, sha1_to_hex(one->u.sha1));
181 strcpy(temp->tmp_path, ".diff_XXXXXX");
182 fd = mkstemp(temp->tmp_path);
184 die("unable to create temp-file");
185 if (write(fd, blob, size) != size)
186 die("unable to write temp-file");
189 temp->name = temp->tmp_path;
190 strcpy(temp->hex, sha1_to_hex(one->u.sha1));
192 sprintf(temp->mode, "%06o", one->mode);
196 static void remove_tempfile(void)
200 for (i = 0; i < 2; i++)
201 if (diff_temp[i].name == diff_temp[i].tmp_path) {
202 unlink(diff_temp[i].name);
203 diff_temp[i].name = NULL;
207 static void remove_tempfile_on_signal(int signo)
212 /* An external diff command takes:
214 * diff-cmd name infile1 infile1-sha1 infile1-mode \
215 * infile2 infile2-sha1 infile2-mode.
218 void run_external_diff(const char *name,
219 struct diff_spec *one,
220 struct diff_spec *two)
222 struct diff_tempfile *temp = diff_temp;
225 static int atexit_asked = 0;
228 prepare_temp_file(name, &temp[0], one);
229 prepare_temp_file(name, &temp[1], two);
230 if (! atexit_asked &&
231 (temp[0].name == temp[0].tmp_path ||
232 temp[1].name == temp[1].tmp_path)) {
234 atexit(remove_tempfile);
236 signal(SIGINT, remove_tempfile_on_signal);
242 die("unable to fork");
244 const char *pgm = external_diff();
249 temp[0].name, temp[0].hex, temp[0].mode,
250 temp[1].name, temp[1].hex, temp[1].mode,
253 execlp(pgm, pgm, name, NULL);
256 * otherwise we use the built-in one.
259 builtin_diff(name, temp);
261 printf("* Unmerged path %s\n", name);
264 if (waitpid(pid, &status, 0) < 0 || !WIFEXITED(status)) {
265 /* We do not check the exit status because typically
266 * diff exits non-zero if files are different, and
267 * we are not interested in knowing that. We *knew*
268 * they are different and that's why we ran diff
269 * in the first place! However if it dies by a signal,
270 * we stop processing immediately.
273 die("external diff died unexpectedly.\n");
278 void diff_addremove(int addremove, unsigned mode,
279 const unsigned char *sha1,
280 const char *base, const char *path)
282 char concatpath[PATH_MAX];
283 struct diff_spec spec[2], *one, *two;
285 memcpy(spec[0].u.sha1, sha1, 20);
287 spec[0].sha1_valid = spec[0].file_valid = 1;
288 spec[1].file_valid = 0;
290 if (addremove == '+') {
291 one = spec + 1; two = spec;
293 one = spec; two = one + 1;
297 strcpy(concatpath, base);
298 strcat(concatpath, path);
300 run_external_diff(path ? concatpath : base, one, two);
303 void diff_change(unsigned old_mode, unsigned new_mode,
304 const unsigned char *old_sha1,
305 const unsigned char *new_sha1,
306 const char *base, const char *path) {
307 char concatpath[PATH_MAX];
308 struct diff_spec spec[2];
310 memcpy(spec[0].u.sha1, old_sha1, 20);
311 spec[0].mode = old_mode;
312 memcpy(spec[1].u.sha1, new_sha1, 20);
313 spec[1].mode = new_mode;
314 spec[0].sha1_valid = spec[0].file_valid = 1;
315 spec[1].sha1_valid = spec[1].file_valid = 1;
318 strcpy(concatpath, base);
319 strcat(concatpath, path);
321 run_external_diff(path ? concatpath : base, &spec[0], &spec[1]);
324 void diff_unmerge(const char *path)
326 run_external_diff(path, NULL, NULL);