1/*
2 * Copyright (C) 2005 Junio C Hamano
3 */
4#include <sys/types.h>
5#include <sys/wait.h>
6#include <signal.h>
7#include "cache.h"
8#include "diff.h"
9
10static char *diff_opts = "-pu";
11
12static const char *external_diff(void)
13{
14 static char *external_diff_cmd = NULL;
15 static int done_preparing = 0;
16
17 if (done_preparing)
18 return external_diff_cmd;
19
20 /*
21 * Default values above are meant to match the
22 * Linux kernel development style. Examples of
23 * alternative styles you can specify via environment
24 * variables are:
25 *
26 * GIT_DIFF_OPTS="-c";
27 */
28 if (getenv("GIT_EXTERNAL_DIFF"))
29 external_diff_cmd = getenv("GIT_EXTERNAL_DIFF");
30
31 /* In case external diff fails... */
32 diff_opts = getenv("GIT_DIFF_OPTS") ? : diff_opts;
33
34 done_preparing = 1;
35 return external_diff_cmd;
36}
37
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.
41 *
42 * E.g.
43 * original sq_expand result
44 * name ==> name ==> 'name'
45 * a b ==> a b ==> 'a b'
46 * a'b ==> a'\''b ==> 'a'\''b'
47 */
48static char *sq_expand(const char *src)
49{
50 static char *buf = NULL;
51 int cnt, c;
52 const char *cp;
53 char *bp;
54
55 /* count bytes needed to store the quoted string. */
56 for (cnt = 1, cp = src; *cp; cnt++, cp++)
57 if (*cp == '\'')
58 cnt += 3;
59
60 buf = xmalloc(cnt);
61 bp = buf;
62 while ((c = *src++)) {
63 if (c != '\'')
64 *bp++ = c;
65 else {
66 bp = strcpy(bp, "'\\''");
67 bp += 4;
68 }
69 }
70 *bp = 0;
71 return buf;
72}
73
74static struct diff_tempfile {
75 const char *name;
76 char hex[41];
77 char mode[10];
78 char tmp_path[50];
79} diff_temp[2];
80
81static void builtin_diff(const char *name,
82 struct diff_tempfile *temp)
83{
84 int i, next_at;
85 const char *diff_cmd = "diff -L'%s%s%s' -L'%s%s%s'";
86 const char *diff_arg = "'%s' '%s'";
87 const char *input_name_sq[2];
88 const char *path0[2];
89 const char *path1[2];
90 char mode[2][20];
91 const char *name_sq = sq_expand(name);
92 char *cmd;
93
94 /* diff_cmd and diff_arg have 8 %s in total which makes
95 * the sum of these strings 16 bytes larger than required.
96 * we use 2 spaces around diff-opts, and we need to count
97 * terminating NUL, so we subtract 13 here.
98 */
99 int cmd_size = (strlen(diff_cmd) + strlen(diff_opts) +
100 strlen(diff_arg) - 13);
101 for (i = 0; i < 2; i++) {
102 input_name_sq[i] = sq_expand(temp[i].name);
103 if (!strcmp(temp[i].name, "/dev/null")) {
104 path0[i] = "/dev/null";
105 path1[i] = "";
106 mode[i][0] = 0;
107 } else {
108 path0[i] = i ? "l/" : "k/";
109 path1[i] = name_sq;
110 sprintf(mode[i], " (mode:%s)", temp[i].mode);
111 }
112 cmd_size += (strlen(path0[i]) + strlen(path1[i]) +
113 strlen(mode[i]) + strlen(input_name_sq[i]));
114 }
115
116 cmd = xmalloc(cmd_size);
117
118 next_at = 0;
119 next_at += snprintf(cmd+next_at, cmd_size-next_at,
120 diff_cmd,
121 path0[0], path1[0], mode[0],
122 path0[1], path1[1], mode[1]);
123 next_at += snprintf(cmd+next_at, cmd_size-next_at,
124 " %s ", diff_opts);
125 next_at += snprintf(cmd+next_at, cmd_size-next_at,
126 diff_arg, input_name_sq[0], input_name_sq[1]);
127
128 execlp("/bin/sh","sh", "-c", cmd, NULL);
129}
130
131static void prepare_temp_file(const char *name,
132 struct diff_tempfile *temp,
133 struct diff_spec *one)
134{
135 static unsigned char null_sha1[20] = { 0, };
136
137 if (!one->file_valid) {
138 not_a_valid_file:
139 /* A '-' entry produces this for file-2, and
140 * a '+' entry produces this for file-1.
141 */
142 temp->name = "/dev/null";
143 strcpy(temp->hex, ".");
144 strcpy(temp->mode, ".");
145 return;
146 }
147
148 if (one->sha1_valid &&
149 !memcmp(one->u.sha1, null_sha1, sizeof(null_sha1))) {
150 one->sha1_valid = 0;
151 one->u.name = name;
152 }
153
154 if (!one->sha1_valid) {
155 struct stat st;
156 temp->name = one->u.name;
157 if (stat(temp->name, &st) < 0) {
158 if (errno == ENOENT)
159 goto not_a_valid_file;
160 die("stat(%s): %s", temp->name, strerror(errno));
161 }
162 strcpy(temp->hex, sha1_to_hex(null_sha1));
163 sprintf(temp->mode, "%06o",
164 S_IFREG |ce_permissions(st.st_mode));
165 }
166 else {
167 int fd;
168 void *blob;
169 char type[20];
170 unsigned long size;
171
172 blob = read_sha1_file(one->u.sha1, type, &size);
173 if (!blob || strcmp(type, "blob"))
174 die("unable to read blob object for %s (%s)",
175 name, sha1_to_hex(one->u.sha1));
176
177 strcpy(temp->tmp_path, ".diff_XXXXXX");
178 fd = mkstemp(temp->tmp_path);
179 if (fd < 0)
180 die("unable to create temp-file");
181 if (write(fd, blob, size) != size)
182 die("unable to write temp-file");
183 close(fd);
184 free(blob);
185 temp->name = temp->tmp_path;
186 strcpy(temp->hex, sha1_to_hex(one->u.sha1));
187 temp->hex[40] = 0;
188 sprintf(temp->mode, "%06o", one->mode);
189 }
190}
191
192static void remove_tempfile(void)
193{
194 int i;
195
196 for (i = 0; i < 2; i++)
197 if (diff_temp[i].name == diff_temp[i].tmp_path) {
198 unlink(diff_temp[i].name);
199 diff_temp[i].name = NULL;
200 }
201}
202
203static void remove_tempfile_on_signal(int signo)
204{
205 remove_tempfile();
206}
207
208/* An external diff command takes:
209 *
210 * diff-cmd name infile1 infile1-sha1 infile1-mode \
211 * infile2 infile2-sha1 infile2-mode.
212 *
213 */
214void run_external_diff(const char *name,
215 struct diff_spec *one,
216 struct diff_spec *two)
217{
218 struct diff_tempfile *temp = diff_temp;
219 pid_t pid;
220 int status;
221 static int atexit_asked = 0;
222
223 if (one && two) {
224 prepare_temp_file(name, &temp[0], one);
225 prepare_temp_file(name, &temp[1], two);
226 if (! atexit_asked &&
227 (temp[0].name == temp[0].tmp_path ||
228 temp[1].name == temp[1].tmp_path)) {
229 atexit_asked = 1;
230 atexit(remove_tempfile);
231 }
232 signal(SIGINT, remove_tempfile_on_signal);
233 }
234
235 fflush(NULL);
236 pid = fork();
237 if (pid < 0)
238 die("unable to fork");
239 if (!pid) {
240 const char *pgm = external_diff();
241 if (pgm) {
242 if (one && two)
243 execlp(pgm, pgm,
244 name,
245 temp[0].name, temp[0].hex, temp[0].mode,
246 temp[1].name, temp[1].hex, temp[1].mode,
247 NULL);
248 else
249 execlp(pgm, pgm, name, NULL);
250 }
251 /*
252 * otherwise we use the built-in one.
253 */
254 if (one && two)
255 builtin_diff(name, temp);
256 else
257 printf("* Unmerged path %s\n", name);
258 exit(0);
259 }
260 if (waitpid(pid, &status, 0) < 0 || !WIFEXITED(status)) {
261 /* We do not check the exit status because typically
262 * diff exits non-zero if files are different, and
263 * we are not interested in knowing that. We *knew*
264 * they are different and that's why we ran diff
265 * in the first place! However if it dies by a signal,
266 * we stop processing immediately.
267 */
268 remove_tempfile();
269 die("external diff died unexpectedly.\n");
270 }
271 remove_tempfile();
272}
273
274void diff_addremove(int addremove, unsigned mode,
275 const unsigned char *sha1,
276 const char *base, const char *path)
277{
278 char concatpath[PATH_MAX];
279 struct diff_spec spec[2], *one, *two;
280
281 memcpy(spec[0].u.sha1, sha1, 20);
282 spec[0].mode = mode;
283 spec[0].sha1_valid = spec[0].file_valid = 1;
284 spec[1].file_valid = 0;
285
286 if (addremove == '+') {
287 one = spec + 1; two = spec;
288 } else {
289 one = spec; two = one + 1;
290 }
291
292 if (path) {
293 strcpy(concatpath, base);
294 strcat(concatpath, path);
295 }
296 run_external_diff(path ? concatpath : base, one, two);
297}
298
299void diff_change(unsigned old_mode, unsigned new_mode,
300 const unsigned char *old_sha1,
301 const unsigned char *new_sha1,
302 const char *base, const char *path) {
303 char concatpath[PATH_MAX];
304 struct diff_spec spec[2];
305
306 memcpy(spec[0].u.sha1, old_sha1, 20);
307 spec[0].mode = old_mode;
308 memcpy(spec[1].u.sha1, new_sha1, 20);
309 spec[1].mode = new_mode;
310 spec[0].sha1_valid = spec[0].file_valid = 1;
311 spec[1].sha1_valid = spec[1].file_valid = 1;
312
313 if (path) {
314 strcpy(concatpath, base);
315 strcat(concatpath, path);
316 }
317 run_external_diff(path ? concatpath : base, &spec[0], &spec[1]);
318}
319
320void diff_unmerge(const char *path)
321{
322 run_external_diff(path, NULL, NULL);
323}