Commit | Line | Data |
---|---|---|
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" | |
427dcb4b | 10 | #include "diffcore.h" |
86436c28 | 11 | |
d19938ab | 12 | static const char *diff_opts = "-pu"; |
5c97558c | 13 | static unsigned char null_sha1[20] = { 0, }; |
427dcb4b | 14 | |
427dcb4b JH |
15 | static int reverse_diff; |
16 | static int diff_raw_output = -1; | |
17 | static const char **pathspec; | |
18 | static int speccnt; | |
86436c28 | 19 | |
be3cfa85 | 20 | static const char *external_diff(void) |
86436c28 | 21 | { |
d19938ab | 22 | static const char *external_diff_cmd = NULL; |
be3cfa85 JH |
23 | static int done_preparing = 0; |
24 | ||
25 | if (done_preparing) | |
26 | return external_diff_cmd; | |
27 | ||
86436c28 JH |
28 | /* |
29 | * Default values above are meant to match the | |
30 | * Linux kernel development style. Examples of | |
31 | * alternative styles you can specify via environment | |
32 | * variables are: | |
33 | * | |
86436c28 JH |
34 | * GIT_DIFF_OPTS="-c"; |
35 | */ | |
d19938ab JH |
36 | if (gitenv("GIT_EXTERNAL_DIFF")) |
37 | external_diff_cmd = gitenv("GIT_EXTERNAL_DIFF"); | |
be3cfa85 JH |
38 | |
39 | /* In case external diff fails... */ | |
d19938ab | 40 | diff_opts = gitenv("GIT_DIFF_OPTS") ? : diff_opts; |
be3cfa85 JH |
41 | |
42 | done_preparing = 1; | |
43 | return external_diff_cmd; | |
86436c28 JH |
44 | } |
45 | ||
46 | /* Help to copy the thing properly quoted for the shell safety. | |
47 | * any single quote is replaced with '\'', and the caller is | |
48 | * expected to enclose the result within a single quote pair. | |
49 | * | |
50 | * E.g. | |
51 | * original sq_expand result | |
52 | * name ==> name ==> 'name' | |
53 | * a b ==> a b ==> 'a b' | |
54 | * a'b ==> a'\''b ==> 'a'\''b' | |
55 | */ | |
56 | static char *sq_expand(const char *src) | |
57 | { | |
58 | static char *buf = NULL; | |
59 | int cnt, c; | |
60 | const char *cp; | |
61 | char *bp; | |
62 | ||
57fe64a4 | 63 | /* count bytes needed to store the quoted string. */ |
86436c28 JH |
64 | for (cnt = 1, cp = src; *cp; cnt++, cp++) |
65 | if (*cp == '\'') | |
66 | cnt += 3; | |
67 | ||
812666c8 | 68 | buf = xmalloc(cnt); |
86436c28 JH |
69 | bp = buf; |
70 | while ((c = *src++)) { | |
71 | if (c != '\'') | |
72 | *bp++ = c; | |
73 | else { | |
74 | bp = strcpy(bp, "'\\''"); | |
75 | bp += 4; | |
76 | } | |
77 | } | |
78 | *bp = 0; | |
79 | return buf; | |
80 | } | |
81 | ||
be3cfa85 | 82 | static struct diff_tempfile { |
427dcb4b | 83 | const char *name; /* filename external diff should read from */ |
be3cfa85 JH |
84 | char hex[41]; |
85 | char mode[10]; | |
86 | char tmp_path[50]; | |
87 | } diff_temp[2]; | |
88 | ||
915838c3 JH |
89 | static void builtin_diff(const char *name_a, |
90 | const char *name_b, | |
57fe64a4 | 91 | struct diff_tempfile *temp, |
427dcb4b | 92 | const char *xfrm_msg) |
86436c28 | 93 | { |
9669e17a | 94 | int i, next_at, cmd_size; |
c983370e | 95 | const char *diff_cmd = "diff -L'%s%s' -L'%s%s'"; |
6fa28064 | 96 | const char *diff_arg = "'%s' '%s'||:"; /* "||:" is to return 0 */ |
2f978138 JH |
97 | const char *input_name_sq[2]; |
98 | const char *path0[2]; | |
99 | const char *path1[2]; | |
915838c3 | 100 | const char *name_sq[2]; |
2f978138 | 101 | char *cmd; |
915838c3 JH |
102 | |
103 | name_sq[0] = sq_expand(name_a); | |
104 | name_sq[1] = sq_expand(name_b); | |
105 | ||
c983370e JH |
106 | /* diff_cmd and diff_arg have 6 %s in total which makes |
107 | * the sum of these strings 12 bytes larger than required. | |
be3cfa85 | 108 | * we use 2 spaces around diff-opts, and we need to count |
c983370e | 109 | * terminating NUL, so we subtract 9 here. |
be3cfa85 | 110 | */ |
9669e17a | 111 | cmd_size = (strlen(diff_cmd) + strlen(diff_opts) + |
c983370e | 112 | strlen(diff_arg) - 9); |
2f978138 JH |
113 | for (i = 0; i < 2; i++) { |
114 | input_name_sq[i] = sq_expand(temp[i].name); | |
115 | if (!strcmp(temp[i].name, "/dev/null")) { | |
116 | path0[i] = "/dev/null"; | |
117 | path1[i] = ""; | |
2f978138 | 118 | } else { |
0980d9b3 | 119 | path0[i] = i ? "b/" : "a/"; |
915838c3 | 120 | path1[i] = name_sq[i]; |
2f978138 JH |
121 | } |
122 | cmd_size += (strlen(path0[i]) + strlen(path1[i]) + | |
c983370e | 123 | strlen(input_name_sq[i])); |
2f978138 | 124 | } |
be3cfa85 | 125 | |
2f978138 JH |
126 | cmd = xmalloc(cmd_size); |
127 | ||
128 | next_at = 0; | |
be3cfa85 | 129 | next_at += snprintf(cmd+next_at, cmd_size-next_at, |
2f978138 | 130 | diff_cmd, |
c983370e | 131 | path0[0], path1[0], path0[1], path1[1]); |
be3cfa85 JH |
132 | next_at += snprintf(cmd+next_at, cmd_size-next_at, |
133 | " %s ", diff_opts); | |
134 | next_at += snprintf(cmd+next_at, cmd_size-next_at, | |
2f978138 JH |
135 | diff_arg, input_name_sq[0], input_name_sq[1]); |
136 | ||
915838c3 | 137 | printf("diff --git a/%s b/%s\n", name_a, name_b); |
c983370e | 138 | if (!path1[0][0]) |
b58f23b3 | 139 | printf("new file mode %s\n", temp[1].mode); |
c983370e | 140 | else if (!path1[1][0]) |
b58f23b3 | 141 | printf("deleted file mode %s\n", temp[0].mode); |
273b9834 | 142 | else { |
b58f23b3 JH |
143 | if (strcmp(temp[0].mode, temp[1].mode)) { |
144 | printf("old mode %s\n", temp[0].mode); | |
145 | printf("new mode %s\n", temp[1].mode); | |
146 | } | |
427dcb4b JH |
147 | if (xfrm_msg && xfrm_msg[0]) |
148 | fputs(xfrm_msg, stdout); | |
149 | ||
273b9834 JH |
150 | if (strncmp(temp[0].mode, temp[1].mode, 3)) |
151 | /* we do not run diff between different kind | |
152 | * of objects. | |
153 | */ | |
b28858bf JH |
154 | exit(0); |
155 | } | |
c983370e | 156 | fflush(NULL); |
be3cfa85 | 157 | execlp("/bin/sh","sh", "-c", cmd, NULL); |
86436c28 JH |
158 | } |
159 | ||
427dcb4b JH |
160 | struct diff_filespec *alloc_filespec(const char *path) |
161 | { | |
162 | int namelen = strlen(path); | |
163 | struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1); | |
164 | spec->path = (char *)(spec + 1); | |
165 | strcpy(spec->path, path); | |
166 | spec->should_free = spec->should_munmap = spec->file_valid = 0; | |
167 | spec->xfrm_flags = 0; | |
168 | spec->size = 0; | |
169 | spec->data = 0; | |
170 | return spec; | |
171 | } | |
172 | ||
173 | void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1, | |
174 | unsigned short mode) | |
175 | { | |
176 | spec->mode = mode; | |
177 | memcpy(spec->sha1, sha1, 20); | |
178 | spec->sha1_valid = !!memcmp(sha1, null_sha1, 20); | |
179 | spec->file_valid = 1; | |
180 | } | |
181 | ||
b46f0b6d JH |
182 | /* |
183 | * Given a name and sha1 pair, if the dircache tells us the file in | |
184 | * the work tree has that object contents, return true, so that | |
185 | * prepare_temp_file() does not have to inflate and extract. | |
186 | */ | |
187 | static int work_tree_matches(const char *name, const unsigned char *sha1) | |
188 | { | |
189 | struct cache_entry *ce; | |
190 | struct stat st; | |
191 | int pos, len; | |
5c97558c | 192 | |
b46f0b6d JH |
193 | /* We do not read the cache ourselves here, because the |
194 | * benchmark with my previous version that always reads cache | |
195 | * shows that it makes things worse for diff-tree comparing | |
196 | * two linux-2.6 kernel trees in an already checked out work | |
915838c3 | 197 | * tree. This is because most diff-tree comparisons deal with |
b46f0b6d JH |
198 | * only a small number of files, while reading the cache is |
199 | * expensive for a large project, and its cost outweighs the | |
200 | * savings we get by not inflating the object to a temporary | |
201 | * file. Practically, this code only helps when we are used | |
202 | * by diff-cache --cached, which does read the cache before | |
203 | * calling us. | |
57fe64a4 | 204 | */ |
b46f0b6d JH |
205 | if (!active_cache) |
206 | return 0; | |
207 | ||
208 | len = strlen(name); | |
209 | pos = cache_name_pos(name, len); | |
210 | if (pos < 0) | |
211 | return 0; | |
212 | ce = active_cache[pos]; | |
b28858bf | 213 | if ((lstat(name, &st) < 0) || |
427dcb4b | 214 | !S_ISREG(st.st_mode) || /* careful! */ |
5d728c84 | 215 | ce_match_stat(ce, &st) || |
b46f0b6d JH |
216 | memcmp(sha1, ce->sha1, 20)) |
217 | return 0; | |
427dcb4b JH |
218 | /* we return 1 only when we can stat, it is a regular file, |
219 | * stat information matches, and sha1 recorded in the cache | |
220 | * matches. I.e. we know the file in the work tree really is | |
221 | * the same as the <name, sha1> pair. | |
222 | */ | |
b46f0b6d JH |
223 | return 1; |
224 | } | |
225 | ||
427dcb4b JH |
226 | /* |
227 | * While doing rename detection and pickaxe operation, we may need to | |
228 | * grab the data for the blob (or file) for our own in-core comparison. | |
229 | * diff_filespec has data and size fields for this purpose. | |
230 | */ | |
231 | int diff_populate_filespec(struct diff_filespec *s) | |
232 | { | |
233 | int err = 0; | |
234 | if (!s->file_valid) | |
235 | die("internal error: asking to populate invalid file."); | |
236 | if (S_ISDIR(s->mode)) | |
237 | return -1; | |
238 | ||
239 | if (s->data) | |
240 | return err; | |
241 | if (!s->sha1_valid || | |
242 | work_tree_matches(s->path, s->sha1)) { | |
243 | struct stat st; | |
244 | int fd; | |
245 | if (lstat(s->path, &st) < 0) { | |
246 | if (errno == ENOENT) { | |
247 | err_empty: | |
248 | err = -1; | |
249 | empty: | |
250 | s->data = ""; | |
251 | s->size = 0; | |
252 | return err; | |
253 | } | |
254 | } | |
255 | s->size = st.st_size; | |
256 | if (!s->size) | |
257 | goto empty; | |
258 | if (S_ISLNK(st.st_mode)) { | |
259 | int ret; | |
260 | s->data = xmalloc(s->size); | |
261 | s->should_free = 1; | |
262 | ret = readlink(s->path, s->data, s->size); | |
263 | if (ret < 0) { | |
264 | free(s->data); | |
265 | goto err_empty; | |
266 | } | |
267 | return 0; | |
268 | } | |
269 | fd = open(s->path, O_RDONLY); | |
270 | if (fd < 0) | |
271 | goto err_empty; | |
272 | s->data = mmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0); | |
273 | s->should_munmap = 1; | |
274 | close(fd); | |
275 | } | |
276 | else { | |
277 | char type[20]; | |
278 | s->data = read_sha1_file(s->sha1, type, &s->size); | |
279 | s->should_free = 1; | |
280 | } | |
281 | return 0; | |
282 | } | |
283 | ||
284 | void diff_free_filespec_data(struct diff_filespec *s) | |
285 | { | |
286 | if (s->should_free) | |
287 | free(s->data); | |
288 | else if (s->should_munmap) | |
289 | munmap(s->data, s->size); | |
290 | s->should_free = s->should_munmap = 0; | |
291 | s->data = 0; | |
292 | } | |
293 | ||
b28858bf JH |
294 | static void prep_temp_blob(struct diff_tempfile *temp, |
295 | void *blob, | |
296 | unsigned long size, | |
297 | unsigned char *sha1, | |
298 | int mode) | |
299 | { | |
300 | int fd; | |
301 | ||
302 | strcpy(temp->tmp_path, ".diff_XXXXXX"); | |
303 | fd = mkstemp(temp->tmp_path); | |
304 | if (fd < 0) | |
305 | die("unable to create temp-file"); | |
306 | if (write(fd, blob, size) != size) | |
307 | die("unable to write temp-file"); | |
308 | close(fd); | |
309 | temp->name = temp->tmp_path; | |
310 | strcpy(temp->hex, sha1_to_hex(sha1)); | |
311 | temp->hex[40] = 0; | |
312 | sprintf(temp->mode, "%06o", mode); | |
313 | } | |
314 | ||
be3cfa85 JH |
315 | static void prepare_temp_file(const char *name, |
316 | struct diff_tempfile *temp, | |
427dcb4b | 317 | struct diff_filespec *one) |
86436c28 | 318 | { |
be3cfa85 JH |
319 | if (!one->file_valid) { |
320 | not_a_valid_file: | |
532149d7 JH |
321 | /* A '-' entry produces this for file-2, and |
322 | * a '+' entry produces this for file-1. | |
323 | */ | |
be3cfa85 JH |
324 | temp->name = "/dev/null"; |
325 | strcpy(temp->hex, "."); | |
326 | strcpy(temp->mode, "."); | |
86436c28 JH |
327 | return; |
328 | } | |
be3cfa85 | 329 | |
5c97558c | 330 | if (!one->sha1_valid || |
427dcb4b | 331 | work_tree_matches(name, one->sha1)) { |
be3cfa85 | 332 | struct stat st; |
427dcb4b | 333 | if (lstat(name, &st) < 0) { |
be3cfa85 JH |
334 | if (errno == ENOENT) |
335 | goto not_a_valid_file; | |
427dcb4b | 336 | die("stat(%s): %s", name, strerror(errno)); |
be3cfa85 | 337 | } |
b28858bf JH |
338 | if (S_ISLNK(st.st_mode)) { |
339 | int ret; | |
340 | char *buf, buf_[1024]; | |
341 | buf = ((sizeof(buf_) < st.st_size) ? | |
342 | xmalloc(st.st_size) : buf_); | |
343 | ret = readlink(name, buf, st.st_size); | |
344 | if (ret < 0) | |
345 | die("readlink(%s)", name); | |
346 | prep_temp_blob(temp, buf, st.st_size, | |
347 | (one->sha1_valid ? | |
427dcb4b | 348 | one->sha1 : null_sha1), |
b28858bf JH |
349 | (one->sha1_valid ? |
350 | one->mode : S_IFLNK)); | |
351 | } | |
352 | else { | |
427dcb4b JH |
353 | /* we can borrow from the file in the work tree */ |
354 | temp->name = name; | |
b28858bf JH |
355 | if (!one->sha1_valid) |
356 | strcpy(temp->hex, sha1_to_hex(null_sha1)); | |
357 | else | |
427dcb4b | 358 | strcpy(temp->hex, sha1_to_hex(one->sha1)); |
b28858bf JH |
359 | sprintf(temp->mode, "%06o", |
360 | S_IFREG |ce_permissions(st.st_mode)); | |
361 | } | |
362 | return; | |
be3cfa85 JH |
363 | } |
364 | else { | |
427dcb4b JH |
365 | if (diff_populate_filespec(one)) |
366 | die("cannot read data blob for %s", one->path); | |
367 | prep_temp_blob(temp, one->data, one->size, | |
368 | one->sha1, one->mode); | |
be3cfa85 JH |
369 | } |
370 | } | |
371 | ||
372 | static void remove_tempfile(void) | |
373 | { | |
374 | int i; | |
375 | ||
376 | for (i = 0; i < 2; i++) | |
377 | if (diff_temp[i].name == diff_temp[i].tmp_path) { | |
378 | unlink(diff_temp[i].name); | |
379 | diff_temp[i].name = NULL; | |
380 | } | |
381 | } | |
382 | ||
532149d7 JH |
383 | static void remove_tempfile_on_signal(int signo) |
384 | { | |
385 | remove_tempfile(); | |
386 | } | |
387 | ||
5c97558c JH |
388 | static int matches_pathspec(const char *name) |
389 | { | |
390 | int i; | |
391 | int namelen; | |
392 | ||
393 | if (speccnt == 0) | |
394 | return 1; | |
395 | ||
396 | namelen = strlen(name); | |
397 | for (i = 0; i < speccnt; i++) { | |
398 | int speclen = strlen(pathspec[i]); | |
399 | if (! strncmp(pathspec[i], name, speclen) && | |
400 | speclen <= namelen && | |
401 | (name[speclen] == 0 || name[speclen] == '/')) | |
402 | return 1; | |
403 | } | |
404 | return 0; | |
405 | } | |
406 | ||
be3cfa85 JH |
407 | /* An external diff command takes: |
408 | * | |
409 | * diff-cmd name infile1 infile1-sha1 infile1-mode \ | |
5c97558c | 410 | * infile2 infile2-sha1 infile2-mode [ rename-to ] |
be3cfa85 JH |
411 | * |
412 | */ | |
5c97558c JH |
413 | static void run_external_diff(const char *name, |
414 | const char *other, | |
427dcb4b JH |
415 | struct diff_filespec *one, |
416 | struct diff_filespec *two, | |
417 | const char *xfrm_msg) | |
be3cfa85 JH |
418 | { |
419 | struct diff_tempfile *temp = diff_temp; | |
532149d7 JH |
420 | pid_t pid; |
421 | int status; | |
be3cfa85 JH |
422 | static int atexit_asked = 0; |
423 | ||
5c97558c JH |
424 | if (!matches_pathspec(name) && (!other || !matches_pathspec(other))) |
425 | return; | |
426 | ||
77eb2720 JH |
427 | if (one && two) { |
428 | prepare_temp_file(name, &temp[0], one); | |
915838c3 | 429 | prepare_temp_file(other ? : name, &temp[1], two); |
77eb2720 JH |
430 | if (! atexit_asked && |
431 | (temp[0].name == temp[0].tmp_path || | |
432 | temp[1].name == temp[1].tmp_path)) { | |
433 | atexit_asked = 1; | |
434 | atexit(remove_tempfile); | |
435 | } | |
532149d7 | 436 | signal(SIGINT, remove_tempfile_on_signal); |
be3cfa85 JH |
437 | } |
438 | ||
439 | fflush(NULL); | |
440 | pid = fork(); | |
441 | if (pid < 0) | |
442 | die("unable to fork"); | |
443 | if (!pid) { | |
444 | const char *pgm = external_diff(); | |
5c97558c JH |
445 | if (pgm) { |
446 | if (one && two) { | |
427dcb4b | 447 | const char *exec_arg[10]; |
5c97558c JH |
448 | const char **arg = &exec_arg[0]; |
449 | *arg++ = pgm; | |
450 | *arg++ = name; | |
451 | *arg++ = temp[0].name; | |
452 | *arg++ = temp[0].hex; | |
453 | *arg++ = temp[0].mode; | |
454 | *arg++ = temp[1].name; | |
455 | *arg++ = temp[1].hex; | |
456 | *arg++ = temp[1].mode; | |
427dcb4b | 457 | if (other) { |
5c97558c | 458 | *arg++ = other; |
427dcb4b JH |
459 | *arg++ = xfrm_msg; |
460 | } | |
461 | *arg = 0; | |
5c97558c JH |
462 | execvp(pgm, (char *const*) exec_arg); |
463 | } | |
77eb2720 JH |
464 | else |
465 | execlp(pgm, pgm, name, NULL); | |
466 | } | |
be3cfa85 JH |
467 | /* |
468 | * otherwise we use the built-in one. | |
469 | */ | |
77eb2720 | 470 | if (one && two) |
427dcb4b | 471 | builtin_diff(name, other ? : name, temp, xfrm_msg); |
77eb2720 JH |
472 | else |
473 | printf("* Unmerged path %s\n", name); | |
be3cfa85 JH |
474 | exit(0); |
475 | } | |
6fa28064 JH |
476 | if (waitpid(pid, &status, 0) < 0 || |
477 | !WIFEXITED(status) || WEXITSTATUS(status)) { | |
478 | /* Earlier we did not check the exit status because | |
532149d7 | 479 | * diff exits non-zero if files are different, and |
6fa28064 JH |
480 | * we are not interested in knowing that. It was a |
481 | * mistake which made it harder to quit a diff-* | |
482 | * session that uses the git-apply-patch-script as | |
483 | * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF | |
484 | * should also exit non-zero only when it wants to | |
485 | * abort the entire diff-* session. | |
532149d7 JH |
486 | */ |
487 | remove_tempfile(); | |
6fa28064 JH |
488 | fprintf(stderr, "external diff died, stopping at %s.\n", name); |
489 | exit(1); | |
532149d7 | 490 | } |
be3cfa85 JH |
491 | remove_tempfile(); |
492 | } | |
493 | ||
427dcb4b | 494 | int diff_scoreopt_parse(const char *opt) |
5c97558c | 495 | { |
427dcb4b JH |
496 | int diglen, num, scale, i; |
497 | if (opt[0] != '-' || (opt[1] != 'M' && opt[1] != 'C')) | |
498 | return -1; /* that is not a -M nor -C option */ | |
499 | diglen = strspn(opt+2, "0123456789"); | |
500 | if (diglen == 0 || strlen(opt+2) != diglen) | |
501 | return 0; /* use default */ | |
502 | sscanf(opt+2, "%d", &num); | |
503 | for (i = 0, scale = 1; i < diglen; i++) | |
504 | scale *= 10; | |
5c97558c | 505 | |
427dcb4b JH |
506 | /* user says num divided by scale and we say internally that |
507 | * is MAX_SCORE * num / scale. | |
508 | */ | |
509 | return MAX_SCORE * num / scale; | |
5c97558c JH |
510 | } |
511 | ||
38c6f780 | 512 | void diff_setup(int reverse_diff_, int diff_raw_output_) |
5c97558c | 513 | { |
427dcb4b | 514 | reverse_diff = reverse_diff_; |
427dcb4b | 515 | diff_raw_output = diff_raw_output_; |
5c97558c JH |
516 | } |
517 | ||
38c6f780 | 518 | struct diff_queue_struct diff_queued_diff; |
5c97558c | 519 | |
52e95789 | 520 | struct diff_filepair *diff_queue(struct diff_queue_struct *queue, |
427dcb4b JH |
521 | struct diff_filespec *one, |
522 | struct diff_filespec *two) | |
5c97558c | 523 | { |
52e95789 | 524 | struct diff_filepair *dp = xmalloc(sizeof(*dp)); |
427dcb4b JH |
525 | dp->one = one; |
526 | dp->two = two; | |
527 | dp->xfrm_msg = 0; | |
528 | dp->orig_order = queue->nr; | |
529 | dp->xfrm_work = 0; | |
530 | if (queue->alloc <= queue->nr) { | |
531 | queue->alloc = alloc_nr(queue->alloc); | |
532 | queue->queue = xrealloc(queue->queue, | |
533 | sizeof(dp) * queue->alloc); | |
5c97558c | 534 | } |
427dcb4b JH |
535 | queue->queue[queue->nr++] = dp; |
536 | return dp; | |
5c97558c JH |
537 | } |
538 | ||
427dcb4b | 539 | static const char *git_object_type(unsigned mode) |
5c97558c | 540 | { |
427dcb4b | 541 | return S_ISDIR(mode) ? "tree" : "blob"; |
5c97558c JH |
542 | } |
543 | ||
52e95789 | 544 | static void diff_flush_raw(struct diff_filepair *p) |
5c97558c | 545 | { |
427dcb4b JH |
546 | struct diff_filespec *it; |
547 | int addremove; | |
5c97558c | 548 | |
427dcb4b JH |
549 | /* raw output does not have a way to express rename nor copy */ |
550 | if (strcmp(p->one->path, p->two->path)) | |
551 | return; | |
57fe64a4 | 552 | |
427dcb4b JH |
553 | if (p->one->file_valid && p->two->file_valid) { |
554 | char hex[41]; | |
555 | strcpy(hex, sha1_to_hex(p->one->sha1)); | |
556 | printf("*%06o->%06o %s %s->%s %s%c", | |
557 | p->one->mode, p->two->mode, | |
558 | git_object_type(p->one->mode), | |
559 | hex, sha1_to_hex(p->two->sha1), | |
560 | p->one->path, diff_raw_output); | |
561 | return; | |
562 | } | |
5c97558c | 563 | |
427dcb4b JH |
564 | if (p->one->file_valid) { |
565 | it = p->one; | |
566 | addremove = '-'; | |
567 | } else { | |
568 | it = p->two; | |
569 | addremove = '+'; | |
570 | } | |
5c97558c | 571 | |
427dcb4b JH |
572 | printf("%c%06o %s %s %s%c", |
573 | addremove, | |
574 | it->mode, git_object_type(it->mode), | |
575 | sha1_to_hex(it->sha1), it->path, diff_raw_output); | |
5c97558c JH |
576 | } |
577 | ||
52e95789 | 578 | static void diff_flush_patch(struct diff_filepair *p) |
5c97558c | 579 | { |
427dcb4b | 580 | const char *name, *other; |
5c97558c | 581 | |
427dcb4b JH |
582 | name = p->one->path; |
583 | other = (strcmp(name, p->two->path) ? p->two->path : NULL); | |
584 | if ((p->one->file_valid && S_ISDIR(p->one->mode)) || | |
585 | (p->two->file_valid && S_ISDIR(p->two->mode))) | |
586 | return; /* no tree diffs in patch format */ | |
5c97558c | 587 | |
427dcb4b | 588 | run_external_diff(name, other, p->one, p->two, p->xfrm_msg); |
5c97558c JH |
589 | } |
590 | ||
427dcb4b | 591 | static int identical(struct diff_filespec *one, struct diff_filespec *two) |
5c97558c | 592 | { |
427dcb4b JH |
593 | /* This function is written stricter than necessary to support |
594 | * the currently implemented transformers, but the idea is to | |
52e95789 | 595 | * let transformers to produce diff_filepairs any way they want, |
427dcb4b | 596 | * and filter and clean them up here before producing the output. |
5c97558c | 597 | */ |
5c97558c | 598 | |
427dcb4b JH |
599 | if (!one->file_valid && !two->file_valid) |
600 | return 1; /* not interesting */ | |
5c97558c | 601 | |
427dcb4b JH |
602 | /* deletion, addition, mode change and renames are all interesting. */ |
603 | if ((one->file_valid != two->file_valid) || (one->mode != two->mode) || | |
604 | strcmp(one->path, two->path)) | |
605 | return 0; | |
57fe64a4 | 606 | |
427dcb4b JH |
607 | /* both are valid and point at the same path. that is, we are |
608 | * dealing with a change. | |
57fe64a4 | 609 | */ |
427dcb4b JH |
610 | if (one->sha1_valid && two->sha1_valid && |
611 | !memcmp(one->sha1, two->sha1, sizeof(one->sha1))) | |
612 | return 1; /* no change */ | |
613 | if (!one->sha1_valid && !two->sha1_valid) | |
614 | return 1; /* both look at the same file on the filesystem. */ | |
615 | return 0; | |
57fe64a4 JH |
616 | } |
617 | ||
52e95789 | 618 | static void diff_flush_one(struct diff_filepair *p) |
5c97558c | 619 | { |
427dcb4b JH |
620 | if (identical(p->one, p->two)) |
621 | return; | |
622 | if (0 <= diff_raw_output) | |
623 | diff_flush_raw(p); | |
624 | else | |
625 | diff_flush_patch(p); | |
57fe64a4 JH |
626 | } |
627 | ||
38c6f780 | 628 | int diff_queue_is_empty(void) |
57fe64a4 | 629 | { |
38c6f780 | 630 | struct diff_queue_struct *q = &diff_queued_diff; |
427dcb4b JH |
631 | int i; |
632 | ||
38c6f780 JH |
633 | for (i = 0; i < q->nr; i++) { |
634 | struct diff_filepair *p = q->queue[i]; | |
635 | if (!identical(p->one, p->two)) | |
636 | return 0; | |
637 | } | |
638 | return 1; | |
639 | } | |
640 | ||
641 | void diff_flush(const char **pathspec_, int speccnt_) | |
642 | { | |
643 | struct diff_queue_struct *q = &diff_queued_diff; | |
644 | int i; | |
645 | ||
646 | pathspec = pathspec_; | |
647 | speccnt = speccnt_; | |
648 | ||
427dcb4b JH |
649 | for (i = 0; i < q->nr; i++) |
650 | diff_flush_one(q->queue[i]); | |
651 | ||
652 | for (i = 0; i < q->nr; i++) { | |
52e95789 | 653 | struct diff_filepair *p = q->queue[i]; |
427dcb4b JH |
654 | diff_free_filespec_data(p->one); |
655 | diff_free_filespec_data(p->two); | |
656 | free(p->xfrm_msg); | |
657 | free(p); | |
658 | } | |
659 | free(q->queue); | |
660 | q->queue = NULL; | |
661 | q->nr = q->alloc = 0; | |
5c97558c JH |
662 | } |
663 | ||
77eb2720 JH |
664 | void diff_addremove(int addremove, unsigned mode, |
665 | const unsigned char *sha1, | |
666 | const char *base, const char *path) | |
be3cfa85 | 667 | { |
77eb2720 | 668 | char concatpath[PATH_MAX]; |
427dcb4b JH |
669 | struct diff_filespec *one, *two; |
670 | ||
671 | /* This may look odd, but it is a preparation for | |
672 | * feeding "there are unchanged files which should | |
673 | * not produce diffs, but when you are doing copy | |
674 | * detection you would need them, so here they are" | |
675 | * entries to the diff-core. They will be prefixed | |
676 | * with something like '=' or '*' (I haven't decided | |
677 | * which but should not make any difference). | |
678 | * Feeding the same new and old to diff_change() should | |
679 | * also have the same effect. diff_flush() should | |
680 | * filter the identical ones out at the final output | |
681 | * stage. | |
682 | */ | |
7ca45252 | 683 | if (reverse_diff) |
427dcb4b JH |
684 | addremove = (addremove == '+' ? '-' : |
685 | addremove == '-' ? '+' : addremove); | |
7ca45252 | 686 | |
427dcb4b JH |
687 | if (!path) path = ""; |
688 | sprintf(concatpath, "%s%s", base, path); | |
689 | one = alloc_filespec(concatpath); | |
690 | two = alloc_filespec(concatpath); | |
be3cfa85 | 691 | |
427dcb4b JH |
692 | if (addremove != '+') |
693 | fill_filespec(one, sha1, mode); | |
694 | if (addremove != '-') | |
695 | fill_filespec(two, sha1, mode); | |
5c97558c | 696 | |
38c6f780 | 697 | diff_queue(&diff_queued_diff, one, two); |
be3cfa85 JH |
698 | } |
699 | ||
77eb2720 JH |
700 | void diff_change(unsigned old_mode, unsigned new_mode, |
701 | const unsigned char *old_sha1, | |
702 | const unsigned char *new_sha1, | |
703 | const char *base, const char *path) { | |
704 | char concatpath[PATH_MAX]; | |
427dcb4b | 705 | struct diff_filespec *one, *two; |
77eb2720 | 706 | |
7ca45252 JH |
707 | if (reverse_diff) { |
708 | unsigned tmp; | |
709 | const unsigned char *tmp_c; | |
710 | tmp = old_mode; old_mode = new_mode; new_mode = tmp; | |
711 | tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c; | |
712 | } | |
427dcb4b JH |
713 | if (!path) path = ""; |
714 | sprintf(concatpath, "%s%s", base, path); | |
715 | one = alloc_filespec(concatpath); | |
716 | two = alloc_filespec(concatpath); | |
717 | fill_filespec(one, old_sha1, old_mode); | |
718 | fill_filespec(two, new_sha1, new_mode); | |
719 | ||
38c6f780 | 720 | diff_queue(&diff_queued_diff, one, two); |
77eb2720 | 721 | } |
be3cfa85 | 722 | |
77eb2720 JH |
723 | void diff_unmerge(const char *path) |
724 | { | |
57fe64a4 JH |
725 | if (0 <= diff_raw_output) { |
726 | printf("U %s%c", path, diff_raw_output); | |
727 | return; | |
728 | } | |
427dcb4b | 729 | run_external_diff(path, NULL, NULL, NULL, NULL); |
86436c28 | 730 | } |