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