[PATCH] Deltification library work by Nicolas Pitre.
[git/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>
a1df57ab 7#include <limits.h>
86436c28
JH
8#include "cache.h"
9#include "diff.h"
10
d19938ab 11static const char *diff_opts = "-pu";
86436c28 12
be3cfa85 13static const char *external_diff(void)
86436c28 14{
d19938ab 15 static const char *external_diff_cmd = NULL;
be3cfa85
JH
16 static int done_preparing = 0;
17
18 if (done_preparing)
19 return external_diff_cmd;
20
86436c28
JH
21 /*
22 * Default values above are meant to match the
23 * Linux kernel development style. Examples of
24 * alternative styles you can specify via environment
25 * variables are:
26 *
86436c28
JH
27 * GIT_DIFF_OPTS="-c";
28 */
d19938ab
JH
29 if (gitenv("GIT_EXTERNAL_DIFF"))
30 external_diff_cmd = gitenv("GIT_EXTERNAL_DIFF");
be3cfa85
JH
31
32 /* In case external diff fails... */
d19938ab 33 diff_opts = gitenv("GIT_DIFF_OPTS") ? : diff_opts;
be3cfa85
JH
34
35 done_preparing = 1;
36 return external_diff_cmd;
86436c28
JH
37}
38
39/* Help to copy the thing properly quoted for the shell safety.
40 * any single quote is replaced with '\'', and the caller is
41 * expected to enclose the result within a single quote pair.
42 *
43 * E.g.
44 * original sq_expand result
45 * name ==> name ==> 'name'
46 * a b ==> a b ==> 'a b'
47 * a'b ==> a'\''b ==> 'a'\''b'
48 */
49static char *sq_expand(const char *src)
50{
51 static char *buf = NULL;
52 int cnt, c;
53 const char *cp;
54 char *bp;
55
56 /* count bytes needed to store the quoted string. */
57 for (cnt = 1, cp = src; *cp; cnt++, cp++)
58 if (*cp == '\'')
59 cnt += 3;
60
812666c8 61 buf = xmalloc(cnt);
86436c28
JH
62 bp = buf;
63 while ((c = *src++)) {
64 if (c != '\'')
65 *bp++ = c;
66 else {
67 bp = strcpy(bp, "'\\''");
68 bp += 4;
69 }
70 }
71 *bp = 0;
72 return buf;
73}
74
be3cfa85
JH
75static struct diff_tempfile {
76 const char *name;
77 char hex[41];
78 char mode[10];
79 char tmp_path[50];
80} diff_temp[2];
81
915838c3
JH
82static void builtin_diff(const char *name_a,
83 const char *name_b,
be3cfa85 84 struct diff_tempfile *temp)
86436c28 85{
2f978138 86 int i, next_at;
c983370e 87 const char *diff_cmd = "diff -L'%s%s' -L'%s%s'";
6fa28064 88 const char *diff_arg = "'%s' '%s'||:"; /* "||:" is to return 0 */
2f978138
JH
89 const char *input_name_sq[2];
90 const char *path0[2];
91 const char *path1[2];
915838c3 92 const char *name_sq[2];
2f978138 93 char *cmd;
915838c3
JH
94
95 name_sq[0] = sq_expand(name_a);
96 name_sq[1] = sq_expand(name_b);
97
c983370e
JH
98 /* diff_cmd and diff_arg have 6 %s in total which makes
99 * the sum of these strings 12 bytes larger than required.
be3cfa85 100 * we use 2 spaces around diff-opts, and we need to count
c983370e 101 * terminating NUL, so we subtract 9 here.
be3cfa85 102 */
2f978138 103 int cmd_size = (strlen(diff_cmd) + strlen(diff_opts) +
c983370e 104 strlen(diff_arg) - 9);
2f978138
JH
105 for (i = 0; i < 2; i++) {
106 input_name_sq[i] = sq_expand(temp[i].name);
107 if (!strcmp(temp[i].name, "/dev/null")) {
108 path0[i] = "/dev/null";
109 path1[i] = "";
2f978138 110 } else {
0980d9b3 111 path0[i] = i ? "b/" : "a/";
915838c3 112 path1[i] = name_sq[i];
2f978138
JH
113 }
114 cmd_size += (strlen(path0[i]) + strlen(path1[i]) +
c983370e 115 strlen(input_name_sq[i]));
2f978138 116 }
be3cfa85 117
2f978138
JH
118 cmd = xmalloc(cmd_size);
119
120 next_at = 0;
be3cfa85 121 next_at += snprintf(cmd+next_at, cmd_size-next_at,
2f978138 122 diff_cmd,
c983370e 123 path0[0], path1[0], path0[1], path1[1]);
be3cfa85
JH
124 next_at += snprintf(cmd+next_at, cmd_size-next_at,
125 " %s ", diff_opts);
126 next_at += snprintf(cmd+next_at, cmd_size-next_at,
2f978138
JH
127 diff_arg, input_name_sq[0], input_name_sq[1]);
128
915838c3 129 printf("diff --git a/%s b/%s\n", name_a, name_b);
c983370e 130 if (!path1[0][0])
b58f23b3 131 printf("new file mode %s\n", temp[1].mode);
c983370e 132 else if (!path1[1][0])
b58f23b3 133 printf("deleted file mode %s\n", temp[0].mode);
273b9834 134 else {
b58f23b3
JH
135 if (strcmp(temp[0].mode, temp[1].mode)) {
136 printf("old mode %s\n", temp[0].mode);
137 printf("new mode %s\n", temp[1].mode);
138 }
915838c3
JH
139 if (strcmp(name_a, name_b)) {
140 printf("rename old %s\n", name_a);
141 printf("rename new %s\n", name_b);
142 }
273b9834
JH
143 if (strncmp(temp[0].mode, temp[1].mode, 3))
144 /* we do not run diff between different kind
145 * of objects.
146 */
b28858bf
JH
147 exit(0);
148 }
c983370e 149 fflush(NULL);
be3cfa85 150 execlp("/bin/sh","sh", "-c", cmd, NULL);
86436c28
JH
151}
152
b46f0b6d
JH
153/*
154 * Given a name and sha1 pair, if the dircache tells us the file in
155 * the work tree has that object contents, return true, so that
156 * prepare_temp_file() does not have to inflate and extract.
157 */
158static int work_tree_matches(const char *name, const unsigned char *sha1)
159{
160 struct cache_entry *ce;
161 struct stat st;
162 int pos, len;
163
164 /* We do not read the cache ourselves here, because the
165 * benchmark with my previous version that always reads cache
166 * shows that it makes things worse for diff-tree comparing
167 * two linux-2.6 kernel trees in an already checked out work
915838c3 168 * tree. This is because most diff-tree comparisons deal with
b46f0b6d
JH
169 * only a small number of files, while reading the cache is
170 * expensive for a large project, and its cost outweighs the
171 * savings we get by not inflating the object to a temporary
172 * file. Practically, this code only helps when we are used
173 * by diff-cache --cached, which does read the cache before
174 * calling us.
175 */
176 if (!active_cache)
177 return 0;
178
179 len = strlen(name);
180 pos = cache_name_pos(name, len);
181 if (pos < 0)
182 return 0;
183 ce = active_cache[pos];
b28858bf
JH
184 if ((lstat(name, &st) < 0) ||
185 !S_ISREG(st.st_mode) ||
5d728c84 186 ce_match_stat(ce, &st) ||
b46f0b6d
JH
187 memcmp(sha1, ce->sha1, 20))
188 return 0;
189 return 1;
190}
191
b28858bf
JH
192static void prep_temp_blob(struct diff_tempfile *temp,
193 void *blob,
194 unsigned long size,
195 unsigned char *sha1,
196 int mode)
197{
198 int fd;
199
200 strcpy(temp->tmp_path, ".diff_XXXXXX");
201 fd = mkstemp(temp->tmp_path);
202 if (fd < 0)
203 die("unable to create temp-file");
204 if (write(fd, blob, size) != size)
205 die("unable to write temp-file");
206 close(fd);
207 temp->name = temp->tmp_path;
208 strcpy(temp->hex, sha1_to_hex(sha1));
209 temp->hex[40] = 0;
210 sprintf(temp->mode, "%06o", mode);
211}
212
be3cfa85
JH
213static void prepare_temp_file(const char *name,
214 struct diff_tempfile *temp,
215 struct diff_spec *one)
86436c28 216{
be3cfa85 217 static unsigned char null_sha1[20] = { 0, };
b46f0b6d 218 int use_work_tree = 0;
be3cfa85
JH
219
220 if (!one->file_valid) {
221 not_a_valid_file:
532149d7
JH
222 /* A '-' entry produces this for file-2, and
223 * a '+' entry produces this for file-1.
224 */
be3cfa85
JH
225 temp->name = "/dev/null";
226 strcpy(temp->hex, ".");
227 strcpy(temp->mode, ".");
86436c28
JH
228 return;
229 }
be3cfa85
JH
230
231 if (one->sha1_valid &&
b46f0b6d
JH
232 (!memcmp(one->blob_sha1, null_sha1, sizeof(null_sha1)) ||
233 work_tree_matches(name, one->blob_sha1)))
234 use_work_tree = 1;
be3cfa85 235
b46f0b6d 236 if (!one->sha1_valid || use_work_tree) {
be3cfa85 237 struct stat st;
b46f0b6d 238 temp->name = name;
b28858bf 239 if (lstat(temp->name, &st) < 0) {
be3cfa85
JH
240 if (errno == ENOENT)
241 goto not_a_valid_file;
242 die("stat(%s): %s", temp->name, strerror(errno));
243 }
b28858bf
JH
244 if (S_ISLNK(st.st_mode)) {
245 int ret;
246 char *buf, buf_[1024];
247 buf = ((sizeof(buf_) < st.st_size) ?
248 xmalloc(st.st_size) : buf_);
249 ret = readlink(name, buf, st.st_size);
250 if (ret < 0)
251 die("readlink(%s)", name);
252 prep_temp_blob(temp, buf, st.st_size,
253 (one->sha1_valid ?
254 one->blob_sha1 : null_sha1),
255 (one->sha1_valid ?
256 one->mode : S_IFLNK));
257 }
258 else {
259 if (!one->sha1_valid)
260 strcpy(temp->hex, sha1_to_hex(null_sha1));
261 else
262 strcpy(temp->hex, sha1_to_hex(one->blob_sha1));
263 sprintf(temp->mode, "%06o",
264 S_IFREG |ce_permissions(st.st_mode));
265 }
266 return;
be3cfa85
JH
267 }
268 else {
be3cfa85
JH
269 void *blob;
270 char type[20];
271 unsigned long size;
272
b46f0b6d 273 blob = read_sha1_file(one->blob_sha1, type, &size);
be3cfa85
JH
274 if (!blob || strcmp(type, "blob"))
275 die("unable to read blob object for %s (%s)",
b46f0b6d 276 name, sha1_to_hex(one->blob_sha1));
b28858bf 277 prep_temp_blob(temp, blob, size, one->blob_sha1, one->mode);
be3cfa85 278 free(blob);
be3cfa85
JH
279 }
280}
281
282static void remove_tempfile(void)
283{
284 int i;
285
286 for (i = 0; i < 2; i++)
287 if (diff_temp[i].name == diff_temp[i].tmp_path) {
288 unlink(diff_temp[i].name);
289 diff_temp[i].name = NULL;
290 }
291}
292
532149d7
JH
293static void remove_tempfile_on_signal(int signo)
294{
295 remove_tempfile();
296}
297
be3cfa85
JH
298/* An external diff command takes:
299 *
300 * diff-cmd name infile1 infile1-sha1 infile1-mode \
301 * infile2 infile2-sha1 infile2-mode.
302 *
303 */
304void run_external_diff(const char *name,
915838c3 305 const char *other,
be3cfa85
JH
306 struct diff_spec *one,
307 struct diff_spec *two)
308{
309 struct diff_tempfile *temp = diff_temp;
532149d7
JH
310 pid_t pid;
311 int status;
be3cfa85
JH
312 static int atexit_asked = 0;
313
77eb2720
JH
314 if (one && two) {
315 prepare_temp_file(name, &temp[0], one);
915838c3 316 prepare_temp_file(other ? : name, &temp[1], two);
77eb2720
JH
317 if (! atexit_asked &&
318 (temp[0].name == temp[0].tmp_path ||
319 temp[1].name == temp[1].tmp_path)) {
320 atexit_asked = 1;
321 atexit(remove_tempfile);
322 }
532149d7 323 signal(SIGINT, remove_tempfile_on_signal);
be3cfa85
JH
324 }
325
326 fflush(NULL);
327 pid = fork();
328 if (pid < 0)
329 die("unable to fork");
330 if (!pid) {
331 const char *pgm = external_diff();
915838c3
JH
332 /* not passing rename patch to external ones */
333 if (!other && pgm) {
77eb2720
JH
334 if (one && two)
335 execlp(pgm, pgm,
336 name,
337 temp[0].name, temp[0].hex, temp[0].mode,
338 temp[1].name, temp[1].hex, temp[1].mode,
339 NULL);
340 else
341 execlp(pgm, pgm, name, NULL);
342 }
be3cfa85
JH
343 /*
344 * otherwise we use the built-in one.
345 */
77eb2720 346 if (one && two)
915838c3 347 builtin_diff(name, other ? : name, temp);
77eb2720
JH
348 else
349 printf("* Unmerged path %s\n", name);
be3cfa85
JH
350 exit(0);
351 }
6fa28064
JH
352 if (waitpid(pid, &status, 0) < 0 ||
353 !WIFEXITED(status) || WEXITSTATUS(status)) {
354 /* Earlier we did not check the exit status because
532149d7 355 * diff exits non-zero if files are different, and
6fa28064
JH
356 * we are not interested in knowing that. It was a
357 * mistake which made it harder to quit a diff-*
358 * session that uses the git-apply-patch-script as
359 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
360 * should also exit non-zero only when it wants to
361 * abort the entire diff-* session.
532149d7
JH
362 */
363 remove_tempfile();
6fa28064
JH
364 fprintf(stderr, "external diff died, stopping at %s.\n", name);
365 exit(1);
532149d7 366 }
be3cfa85
JH
367 remove_tempfile();
368}
369
77eb2720
JH
370void diff_addremove(int addremove, unsigned mode,
371 const unsigned char *sha1,
372 const char *base, const char *path)
be3cfa85 373{
77eb2720 374 char concatpath[PATH_MAX];
be3cfa85
JH
375 struct diff_spec spec[2], *one, *two;
376
b46f0b6d 377 memcpy(spec[0].blob_sha1, sha1, 20);
77eb2720 378 spec[0].mode = mode;
be3cfa85
JH
379 spec[0].sha1_valid = spec[0].file_valid = 1;
380 spec[1].file_valid = 0;
381
77eb2720 382 if (addremove == '+') {
be3cfa85
JH
383 one = spec + 1; two = spec;
384 } else {
385 one = spec; two = one + 1;
386 }
77eb2720
JH
387
388 if (path) {
389 strcpy(concatpath, base);
77eb2720
JH
390 strcat(concatpath, path);
391 }
915838c3 392 run_external_diff(path ? concatpath : base, NULL, one, two);
be3cfa85
JH
393}
394
77eb2720
JH
395void diff_change(unsigned old_mode, unsigned new_mode,
396 const unsigned char *old_sha1,
397 const unsigned char *new_sha1,
398 const char *base, const char *path) {
399 char concatpath[PATH_MAX];
400 struct diff_spec spec[2];
401
b46f0b6d 402 memcpy(spec[0].blob_sha1, old_sha1, 20);
77eb2720 403 spec[0].mode = old_mode;
b46f0b6d 404 memcpy(spec[1].blob_sha1, new_sha1, 20);
77eb2720 405 spec[1].mode = new_mode;
be3cfa85 406 spec[0].sha1_valid = spec[0].file_valid = 1;
77eb2720 407 spec[1].sha1_valid = spec[1].file_valid = 1;
be3cfa85 408
77eb2720
JH
409 if (path) {
410 strcpy(concatpath, base);
77eb2720 411 strcat(concatpath, path);
be3cfa85 412 }
915838c3 413 run_external_diff(path ? concatpath : base, NULL, &spec[0], &spec[1]);
77eb2720 414}
be3cfa85 415
77eb2720
JH
416void diff_unmerge(const char *path)
417{
915838c3 418 run_external_diff(path, NULL, NULL, NULL);
86436c28 419}