]> git.ipfire.org Git - thirdparty/git.git/blame - diff.c
Terminate diff-* on non-zero exit from GIT_EXTERNAL_DIFF
[thirdparty/git.git] / diff.c
CommitLineData
be3cfa85
JH
1/*
2 * Copyright (C) 2005 Junio C Hamano
3 */
4#include <sys/types.h>
5#include <sys/wait.h>
532149d7 6#include <signal.h>
86436c28
JH
7#include "cache.h"
8#include "diff.h"
9
be3cfa85 10static char *diff_opts = "-pu";
86436c28 11
be3cfa85 12static const char *external_diff(void)
86436c28 13{
be3cfa85
JH
14 static char *external_diff_cmd = NULL;
15 static int done_preparing = 0;
16
17 if (done_preparing)
18 return external_diff_cmd;
19
86436c28
JH
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 *
86436c28
JH
26 * GIT_DIFF_OPTS="-c";
27 */
be3cfa85
JH
28 if (getenv("GIT_EXTERNAL_DIFF"))
29 external_diff_cmd = getenv("GIT_EXTERNAL_DIFF");
30
31 /* In case external diff fails... */
86436c28 32 diff_opts = getenv("GIT_DIFF_OPTS") ? : diff_opts;
be3cfa85
JH
33
34 done_preparing = 1;
35 return external_diff_cmd;
86436c28
JH
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
812666c8 60 buf = xmalloc(cnt);
86436c28
JH
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
be3cfa85
JH
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)
86436c28 83{
2f978138 84 int i, next_at;
c983370e 85 const char *diff_cmd = "diff -L'%s%s' -L'%s%s'";
6fa28064 86 const char *diff_arg = "'%s' '%s'||:"; /* "||:" is to return 0 */
2f978138
JH
87 const char *input_name_sq[2];
88 const char *path0[2];
89 const char *path1[2];
be3cfa85 90 const char *name_sq = sq_expand(name);
2f978138
JH
91 char *cmd;
92
c983370e
JH
93 /* diff_cmd and diff_arg have 6 %s in total which makes
94 * the sum of these strings 12 bytes larger than required.
be3cfa85 95 * we use 2 spaces around diff-opts, and we need to count
c983370e 96 * terminating NUL, so we subtract 9 here.
be3cfa85 97 */
2f978138 98 int cmd_size = (strlen(diff_cmd) + strlen(diff_opts) +
c983370e 99 strlen(diff_arg) - 9);
2f978138
JH
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";
104 path1[i] = "";
2f978138 105 } else {
0980d9b3 106 path0[i] = i ? "b/" : "a/";
2f978138 107 path1[i] = name_sq;
2f978138
JH
108 }
109 cmd_size += (strlen(path0[i]) + strlen(path1[i]) +
c983370e 110 strlen(input_name_sq[i]));
2f978138 111 }
be3cfa85 112
2f978138
JH
113 cmd = xmalloc(cmd_size);
114
115 next_at = 0;
be3cfa85 116 next_at += snprintf(cmd+next_at, cmd_size-next_at,
2f978138 117 diff_cmd,
c983370e 118 path0[0], path1[0], path0[1], path1[1]);
be3cfa85
JH
119 next_at += snprintf(cmd+next_at, cmd_size-next_at,
120 " %s ", diff_opts);
121 next_at += snprintf(cmd+next_at, cmd_size-next_at,
2f978138
JH
122 diff_arg, input_name_sq[0], input_name_sq[1]);
123
c983370e
JH
124 if (!path1[0][0])
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);
131 fflush(NULL);
be3cfa85 132 execlp("/bin/sh","sh", "-c", cmd, NULL);
86436c28
JH
133}
134
be3cfa85
JH
135static void prepare_temp_file(const char *name,
136 struct diff_tempfile *temp,
137 struct diff_spec *one)
86436c28 138{
be3cfa85
JH
139 static unsigned char null_sha1[20] = { 0, };
140
141 if (!one->file_valid) {
142 not_a_valid_file:
532149d7
JH
143 /* A '-' entry produces this for file-2, and
144 * a '+' entry produces this for file-1.
145 */
be3cfa85
JH
146 temp->name = "/dev/null";
147 strcpy(temp->hex, ".");
148 strcpy(temp->mode, ".");
86436c28
JH
149 return;
150 }
be3cfa85
JH
151
152 if (one->sha1_valid &&
153 !memcmp(one->u.sha1, null_sha1, sizeof(null_sha1))) {
154 one->sha1_valid = 0;
155 one->u.name = name;
156 }
157
158 if (!one->sha1_valid) {
159 struct stat st;
160 temp->name = one->u.name;
161 if (stat(temp->name, &st) < 0) {
162 if (errno == ENOENT)
163 goto not_a_valid_file;
164 die("stat(%s): %s", temp->name, strerror(errno));
165 }
532149d7 166 strcpy(temp->hex, sha1_to_hex(null_sha1));
be3cfa85
JH
167 sprintf(temp->mode, "%06o",
168 S_IFREG |ce_permissions(st.st_mode));
169 }
170 else {
171 int fd;
172 void *blob;
173 char type[20];
174 unsigned long size;
175
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));
180
181 strcpy(temp->tmp_path, ".diff_XXXXXX");
182 fd = mkstemp(temp->tmp_path);
183 if (fd < 0)
184 die("unable to create temp-file");
185 if (write(fd, blob, size) != size)
186 die("unable to write temp-file");
187 close(fd);
188 free(blob);
189 temp->name = temp->tmp_path;
190 strcpy(temp->hex, sha1_to_hex(one->u.sha1));
191 temp->hex[40] = 0;
192 sprintf(temp->mode, "%06o", one->mode);
193 }
194}
195
196static void remove_tempfile(void)
197{
198 int i;
199
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;
204 }
205}
206
532149d7
JH
207static void remove_tempfile_on_signal(int signo)
208{
209 remove_tempfile();
210}
211
be3cfa85
JH
212/* An external diff command takes:
213 *
214 * diff-cmd name infile1 infile1-sha1 infile1-mode \
215 * infile2 infile2-sha1 infile2-mode.
216 *
217 */
218void run_external_diff(const char *name,
219 struct diff_spec *one,
220 struct diff_spec *two)
221{
222 struct diff_tempfile *temp = diff_temp;
532149d7
JH
223 pid_t pid;
224 int status;
be3cfa85
JH
225 static int atexit_asked = 0;
226
77eb2720
JH
227 if (one && two) {
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)) {
233 atexit_asked = 1;
234 atexit(remove_tempfile);
235 }
532149d7 236 signal(SIGINT, remove_tempfile_on_signal);
be3cfa85
JH
237 }
238
239 fflush(NULL);
240 pid = fork();
241 if (pid < 0)
242 die("unable to fork");
243 if (!pid) {
244 const char *pgm = external_diff();
77eb2720
JH
245 if (pgm) {
246 if (one && two)
247 execlp(pgm, pgm,
248 name,
249 temp[0].name, temp[0].hex, temp[0].mode,
250 temp[1].name, temp[1].hex, temp[1].mode,
251 NULL);
252 else
253 execlp(pgm, pgm, name, NULL);
254 }
be3cfa85
JH
255 /*
256 * otherwise we use the built-in one.
257 */
77eb2720
JH
258 if (one && two)
259 builtin_diff(name, temp);
260 else
261 printf("* Unmerged path %s\n", name);
be3cfa85
JH
262 exit(0);
263 }
6fa28064
JH
264 if (waitpid(pid, &status, 0) < 0 ||
265 !WIFEXITED(status) || WEXITSTATUS(status)) {
266 /* Earlier we did not check the exit status because
532149d7 267 * diff exits non-zero if files are different, and
6fa28064
JH
268 * we are not interested in knowing that. It was a
269 * mistake which made it harder to quit a diff-*
270 * session that uses the git-apply-patch-script as
271 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
272 * should also exit non-zero only when it wants to
273 * abort the entire diff-* session.
532149d7
JH
274 */
275 remove_tempfile();
6fa28064
JH
276 fprintf(stderr, "external diff died, stopping at %s.\n", name);
277 exit(1);
532149d7 278 }
be3cfa85
JH
279 remove_tempfile();
280}
281
77eb2720
JH
282void diff_addremove(int addremove, unsigned mode,
283 const unsigned char *sha1,
284 const char *base, const char *path)
be3cfa85 285{
77eb2720 286 char concatpath[PATH_MAX];
be3cfa85
JH
287 struct diff_spec spec[2], *one, *two;
288
77eb2720
JH
289 memcpy(spec[0].u.sha1, sha1, 20);
290 spec[0].mode = mode;
be3cfa85
JH
291 spec[0].sha1_valid = spec[0].file_valid = 1;
292 spec[1].file_valid = 0;
293
77eb2720 294 if (addremove == '+') {
be3cfa85
JH
295 one = spec + 1; two = spec;
296 } else {
297 one = spec; two = one + 1;
298 }
77eb2720
JH
299
300 if (path) {
301 strcpy(concatpath, base);
77eb2720
JH
302 strcat(concatpath, path);
303 }
304 run_external_diff(path ? concatpath : base, one, two);
be3cfa85
JH
305}
306
77eb2720
JH
307void diff_change(unsigned old_mode, unsigned new_mode,
308 const unsigned char *old_sha1,
309 const unsigned char *new_sha1,
310 const char *base, const char *path) {
311 char concatpath[PATH_MAX];
312 struct diff_spec spec[2];
313
314 memcpy(spec[0].u.sha1, old_sha1, 20);
315 spec[0].mode = old_mode;
316 memcpy(spec[1].u.sha1, new_sha1, 20);
317 spec[1].mode = new_mode;
be3cfa85 318 spec[0].sha1_valid = spec[0].file_valid = 1;
77eb2720 319 spec[1].sha1_valid = spec[1].file_valid = 1;
be3cfa85 320
77eb2720
JH
321 if (path) {
322 strcpy(concatpath, base);
77eb2720 323 strcat(concatpath, path);
be3cfa85 324 }
77eb2720
JH
325 run_external_diff(path ? concatpath : base, &spec[0], &spec[1]);
326}
be3cfa85 327
77eb2720
JH
328void diff_unmerge(const char *path)
329{
330 run_external_diff(path, NULL, NULL);
86436c28 331}