[PATCH] Diffcore updates.
[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"
427dcb4b 10#include "diffcore.h"
86436c28 11
d19938ab 12static const char *diff_opts = "-pu";
5c97558c 13static unsigned char null_sha1[20] = { 0, };
427dcb4b 14
427dcb4b 15static int reverse_diff;
81e50eab
JH
16static int generate_patch;
17static int line_termination = '\n';
18static int inter_name_termination = '\t';
86436c28 19
be3cfa85 20static 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 */
56static 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 82static 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
89static 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
160struct 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);
81e50eab 166 spec->should_free = spec->should_munmap = 0;
427dcb4b
JH
167 spec->xfrm_flags = 0;
168 spec->size = 0;
169 spec->data = 0;
81e50eab
JH
170 spec->mode = 0;
171 memset(spec->sha1, 0, 20);
427dcb4b
JH
172 return spec;
173}
174
175void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
176 unsigned short mode)
177{
81e50eab
JH
178 if (mode) { /* just playing defensive */
179 spec->mode = mode;
180 memcpy(spec->sha1, sha1, 20);
181 spec->sha1_valid = !!memcmp(sha1, null_sha1, 20);
182 }
427dcb4b
JH
183}
184
b46f0b6d
JH
185/*
186 * Given a name and sha1 pair, if the dircache tells us the file in
187 * the work tree has that object contents, return true, so that
188 * prepare_temp_file() does not have to inflate and extract.
189 */
190static int work_tree_matches(const char *name, const unsigned char *sha1)
191{
192 struct cache_entry *ce;
193 struct stat st;
194 int pos, len;
5c97558c 195
b46f0b6d
JH
196 /* We do not read the cache ourselves here, because the
197 * benchmark with my previous version that always reads cache
198 * shows that it makes things worse for diff-tree comparing
199 * two linux-2.6 kernel trees in an already checked out work
915838c3 200 * tree. This is because most diff-tree comparisons deal with
b46f0b6d
JH
201 * only a small number of files, while reading the cache is
202 * expensive for a large project, and its cost outweighs the
203 * savings we get by not inflating the object to a temporary
204 * file. Practically, this code only helps when we are used
205 * by diff-cache --cached, which does read the cache before
206 * calling us.
57fe64a4 207 */
b46f0b6d
JH
208 if (!active_cache)
209 return 0;
210
211 len = strlen(name);
212 pos = cache_name_pos(name, len);
213 if (pos < 0)
214 return 0;
215 ce = active_cache[pos];
b28858bf 216 if ((lstat(name, &st) < 0) ||
427dcb4b 217 !S_ISREG(st.st_mode) || /* careful! */
5d728c84 218 ce_match_stat(ce, &st) ||
b46f0b6d
JH
219 memcmp(sha1, ce->sha1, 20))
220 return 0;
427dcb4b
JH
221 /* we return 1 only when we can stat, it is a regular file,
222 * stat information matches, and sha1 recorded in the cache
223 * matches. I.e. we know the file in the work tree really is
224 * the same as the <name, sha1> pair.
225 */
b46f0b6d
JH
226 return 1;
227}
228
427dcb4b
JH
229/*
230 * While doing rename detection and pickaxe operation, we may need to
231 * grab the data for the blob (or file) for our own in-core comparison.
232 * diff_filespec has data and size fields for this purpose.
233 */
234int diff_populate_filespec(struct diff_filespec *s)
235{
236 int err = 0;
81e50eab 237 if (!DIFF_FILE_VALID(s))
427dcb4b
JH
238 die("internal error: asking to populate invalid file.");
239 if (S_ISDIR(s->mode))
240 return -1;
241
242 if (s->data)
243 return err;
244 if (!s->sha1_valid ||
245 work_tree_matches(s->path, s->sha1)) {
246 struct stat st;
247 int fd;
248 if (lstat(s->path, &st) < 0) {
249 if (errno == ENOENT) {
250 err_empty:
251 err = -1;
252 empty:
253 s->data = "";
254 s->size = 0;
255 return err;
256 }
257 }
258 s->size = st.st_size;
259 if (!s->size)
260 goto empty;
261 if (S_ISLNK(st.st_mode)) {
262 int ret;
263 s->data = xmalloc(s->size);
264 s->should_free = 1;
265 ret = readlink(s->path, s->data, s->size);
266 if (ret < 0) {
267 free(s->data);
268 goto err_empty;
269 }
270 return 0;
271 }
272 fd = open(s->path, O_RDONLY);
273 if (fd < 0)
274 goto err_empty;
275 s->data = mmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
276 s->should_munmap = 1;
277 close(fd);
278 }
279 else {
280 char type[20];
281 s->data = read_sha1_file(s->sha1, type, &s->size);
282 s->should_free = 1;
283 }
284 return 0;
285}
286
6b14d7fa
JH
287void diff_free_filepair(struct diff_filepair *p)
288{
289 free(p->xfrm_msg);
290 free(p);
291}
292
427dcb4b
JH
293void diff_free_filespec_data(struct diff_filespec *s)
294{
295 if (s->should_free)
296 free(s->data);
297 else if (s->should_munmap)
298 munmap(s->data, s->size);
299 s->should_free = s->should_munmap = 0;
300 s->data = 0;
301}
302
b28858bf
JH
303static void prep_temp_blob(struct diff_tempfile *temp,
304 void *blob,
305 unsigned long size,
306 unsigned char *sha1,
307 int mode)
308{
309 int fd;
310
311 strcpy(temp->tmp_path, ".diff_XXXXXX");
312 fd = mkstemp(temp->tmp_path);
313 if (fd < 0)
314 die("unable to create temp-file");
315 if (write(fd, blob, size) != size)
316 die("unable to write temp-file");
317 close(fd);
318 temp->name = temp->tmp_path;
319 strcpy(temp->hex, sha1_to_hex(sha1));
320 temp->hex[40] = 0;
321 sprintf(temp->mode, "%06o", mode);
322}
323
be3cfa85
JH
324static void prepare_temp_file(const char *name,
325 struct diff_tempfile *temp,
427dcb4b 326 struct diff_filespec *one)
86436c28 327{
81e50eab 328 if (!DIFF_FILE_VALID(one)) {
be3cfa85 329 not_a_valid_file:
532149d7
JH
330 /* A '-' entry produces this for file-2, and
331 * a '+' entry produces this for file-1.
332 */
be3cfa85
JH
333 temp->name = "/dev/null";
334 strcpy(temp->hex, ".");
335 strcpy(temp->mode, ".");
86436c28
JH
336 return;
337 }
be3cfa85 338
5c97558c 339 if (!one->sha1_valid ||
427dcb4b 340 work_tree_matches(name, one->sha1)) {
be3cfa85 341 struct stat st;
427dcb4b 342 if (lstat(name, &st) < 0) {
be3cfa85
JH
343 if (errno == ENOENT)
344 goto not_a_valid_file;
427dcb4b 345 die("stat(%s): %s", name, strerror(errno));
be3cfa85 346 }
b28858bf
JH
347 if (S_ISLNK(st.st_mode)) {
348 int ret;
349 char *buf, buf_[1024];
350 buf = ((sizeof(buf_) < st.st_size) ?
351 xmalloc(st.st_size) : buf_);
352 ret = readlink(name, buf, st.st_size);
353 if (ret < 0)
354 die("readlink(%s)", name);
355 prep_temp_blob(temp, buf, st.st_size,
356 (one->sha1_valid ?
427dcb4b 357 one->sha1 : null_sha1),
b28858bf
JH
358 (one->sha1_valid ?
359 one->mode : S_IFLNK));
360 }
361 else {
427dcb4b
JH
362 /* we can borrow from the file in the work tree */
363 temp->name = name;
b28858bf
JH
364 if (!one->sha1_valid)
365 strcpy(temp->hex, sha1_to_hex(null_sha1));
366 else
427dcb4b 367 strcpy(temp->hex, sha1_to_hex(one->sha1));
b28858bf
JH
368 sprintf(temp->mode, "%06o",
369 S_IFREG |ce_permissions(st.st_mode));
370 }
371 return;
be3cfa85
JH
372 }
373 else {
427dcb4b
JH
374 if (diff_populate_filespec(one))
375 die("cannot read data blob for %s", one->path);
376 prep_temp_blob(temp, one->data, one->size,
377 one->sha1, one->mode);
be3cfa85
JH
378 }
379}
380
381static void remove_tempfile(void)
382{
383 int i;
384
385 for (i = 0; i < 2; i++)
386 if (diff_temp[i].name == diff_temp[i].tmp_path) {
387 unlink(diff_temp[i].name);
388 diff_temp[i].name = NULL;
389 }
390}
391
532149d7
JH
392static void remove_tempfile_on_signal(int signo)
393{
394 remove_tempfile();
395}
396
be3cfa85
JH
397/* An external diff command takes:
398 *
399 * diff-cmd name infile1 infile1-sha1 infile1-mode \
5c97558c 400 * infile2 infile2-sha1 infile2-mode [ rename-to ]
be3cfa85
JH
401 *
402 */
5c97558c
JH
403static void run_external_diff(const char *name,
404 const char *other,
427dcb4b
JH
405 struct diff_filespec *one,
406 struct diff_filespec *two,
407 const char *xfrm_msg)
be3cfa85
JH
408{
409 struct diff_tempfile *temp = diff_temp;
532149d7
JH
410 pid_t pid;
411 int status;
be3cfa85
JH
412 static int atexit_asked = 0;
413
77eb2720
JH
414 if (one && two) {
415 prepare_temp_file(name, &temp[0], one);
915838c3 416 prepare_temp_file(other ? : name, &temp[1], two);
77eb2720
JH
417 if (! atexit_asked &&
418 (temp[0].name == temp[0].tmp_path ||
419 temp[1].name == temp[1].tmp_path)) {
420 atexit_asked = 1;
421 atexit(remove_tempfile);
422 }
532149d7 423 signal(SIGINT, remove_tempfile_on_signal);
be3cfa85
JH
424 }
425
426 fflush(NULL);
427 pid = fork();
428 if (pid < 0)
429 die("unable to fork");
430 if (!pid) {
431 const char *pgm = external_diff();
5c97558c
JH
432 if (pgm) {
433 if (one && two) {
427dcb4b 434 const char *exec_arg[10];
5c97558c
JH
435 const char **arg = &exec_arg[0];
436 *arg++ = pgm;
437 *arg++ = name;
438 *arg++ = temp[0].name;
439 *arg++ = temp[0].hex;
440 *arg++ = temp[0].mode;
441 *arg++ = temp[1].name;
442 *arg++ = temp[1].hex;
443 *arg++ = temp[1].mode;
427dcb4b 444 if (other) {
5c97558c 445 *arg++ = other;
427dcb4b
JH
446 *arg++ = xfrm_msg;
447 }
448 *arg = 0;
5c97558c
JH
449 execvp(pgm, (char *const*) exec_arg);
450 }
77eb2720
JH
451 else
452 execlp(pgm, pgm, name, NULL);
453 }
be3cfa85
JH
454 /*
455 * otherwise we use the built-in one.
456 */
77eb2720 457 if (one && two)
427dcb4b 458 builtin_diff(name, other ? : name, temp, xfrm_msg);
77eb2720
JH
459 else
460 printf("* Unmerged path %s\n", name);
be3cfa85
JH
461 exit(0);
462 }
6fa28064
JH
463 if (waitpid(pid, &status, 0) < 0 ||
464 !WIFEXITED(status) || WEXITSTATUS(status)) {
465 /* Earlier we did not check the exit status because
532149d7 466 * diff exits non-zero if files are different, and
6fa28064
JH
467 * we are not interested in knowing that. It was a
468 * mistake which made it harder to quit a diff-*
469 * session that uses the git-apply-patch-script as
470 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
471 * should also exit non-zero only when it wants to
472 * abort the entire diff-* session.
532149d7
JH
473 */
474 remove_tempfile();
6fa28064
JH
475 fprintf(stderr, "external diff died, stopping at %s.\n", name);
476 exit(1);
532149d7 477 }
be3cfa85
JH
478 remove_tempfile();
479}
480
6b14d7fa 481void diff_setup(int reverse_diff_)
5c97558c 482{
6b14d7fa 483 reverse_diff = reverse_diff_;
5c97558c
JH
484}
485
6b14d7fa
JH
486struct diff_queue_struct diff_queued_diff;
487
488void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
5c97558c 489{
6b14d7fa
JH
490 if (queue->alloc <= queue->nr) {
491 queue->alloc = alloc_nr(queue->alloc);
492 queue->queue = xrealloc(queue->queue,
493 sizeof(dp) * queue->alloc);
81e50eab 494 }
6b14d7fa 495 queue->queue[queue->nr++] = dp;
5c97558c
JH
496}
497
52e95789 498struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
6b14d7fa
JH
499 struct diff_filespec *one,
500 struct diff_filespec *two)
5c97558c 501{
52e95789 502 struct diff_filepair *dp = xmalloc(sizeof(*dp));
427dcb4b
JH
503 dp->one = one;
504 dp->two = two;
505 dp->xfrm_msg = 0;
506 dp->orig_order = queue->nr;
507 dp->xfrm_work = 0;
6b14d7fa 508 diff_q(queue, dp);
427dcb4b 509 return dp;
5c97558c
JH
510}
511
52e95789 512static void diff_flush_raw(struct diff_filepair *p)
5c97558c 513{
6b14d7fa
JH
514 if (DIFF_PAIR_UNMERGED(p)) {
515 printf("U %s%c", p->one->path, line_termination);
516 return;
517 }
81e50eab
JH
518 printf(":%06o %06o %s ",
519 p->one->mode, p->two->mode, sha1_to_hex(p->one->sha1));
520 printf("%s%c%s%c%s%c",
521 sha1_to_hex(p->two->sha1), inter_name_termination,
522 p->one->path, inter_name_termination,
523 p->two->path, line_termination);
5c97558c
JH
524}
525
52e95789 526static void diff_flush_patch(struct diff_filepair *p)
5c97558c 527{
427dcb4b 528 const char *name, *other;
5c97558c 529
427dcb4b
JH
530 name = p->one->path;
531 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
81e50eab
JH
532 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
533 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
427dcb4b 534 return; /* no tree diffs in patch format */
5c97558c 535
6b14d7fa
JH
536 if (DIFF_PAIR_UNMERGED(p))
537 run_external_diff(name, NULL, NULL, NULL, NULL);
538 else
539 run_external_diff(name, other, p->one, p->two, p->xfrm_msg);
5c97558c
JH
540}
541
6b14d7fa 542static int uninteresting(struct diff_filepair *p)
5c97558c 543{
427dcb4b
JH
544 /* This function is written stricter than necessary to support
545 * the currently implemented transformers, but the idea is to
52e95789 546 * let transformers to produce diff_filepairs any way they want,
427dcb4b 547 * and filter and clean them up here before producing the output.
5c97558c 548 */
6b14d7fa
JH
549 struct diff_filespec *one, *two;
550
551 if (DIFF_PAIR_UNMERGED(p))
552 return 0; /* unmerged is interesting */
5c97558c 553
6b14d7fa
JH
554 one = p->one;
555 two = p->two;
5c97558c 556
427dcb4b 557 /* deletion, addition, mode change and renames are all interesting. */
81e50eab
JH
558 if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
559 (one->mode != two->mode) ||
427dcb4b
JH
560 strcmp(one->path, two->path))
561 return 0;
57fe64a4 562
427dcb4b
JH
563 /* both are valid and point at the same path. that is, we are
564 * dealing with a change.
57fe64a4 565 */
427dcb4b
JH
566 if (one->sha1_valid && two->sha1_valid &&
567 !memcmp(one->sha1, two->sha1, sizeof(one->sha1)))
568 return 1; /* no change */
569 if (!one->sha1_valid && !two->sha1_valid)
570 return 1; /* both look at the same file on the filesystem. */
571 return 0;
57fe64a4
JH
572}
573
6b14d7fa
JH
574void diffcore_prune(void)
575{
576 /*
577 * Although rename/copy detection wants to have "no-change"
578 * entries fed into them, the downstream do not need to see
579 * them. This function removes such entries.
580 *
581 * The applications that use rename/copy should:
582 *
583 * (1) feed change and "no-change" entries via diff_queue().
584 * (2) call diffcore_rename, and any other future diffcore_xxx
585 * that would benefit by still having "no-change" entries.
586 * (3) call diffcore_prune
587 * (4) call other diffcore_xxx that do not need to see
588 * "no-change" entries.
589 */
590 struct diff_queue_struct *q = &diff_queued_diff;
591 struct diff_queue_struct outq;
592 int i;
593
594 outq.queue = NULL;
595 outq.nr = outq.alloc = 0;
596
597 for (i = 0; i < q->nr; i++) {
598 struct diff_filepair *p = q->queue[i];
599 if (!uninteresting(p))
600 diff_q(&outq, p);
601 else
602 diff_free_filepair(p);
603 }
604 free(q->queue);
605 *q = outq;
606 return;
607}
608
52e95789 609static void diff_flush_one(struct diff_filepair *p)
5c97558c 610{
6b14d7fa 611 if (uninteresting(p))
427dcb4b 612 return;
81e50eab 613 if (generate_patch)
427dcb4b 614 diff_flush_patch(p);
81e50eab
JH
615 else
616 diff_flush_raw(p);
57fe64a4
JH
617}
618
38c6f780 619int diff_queue_is_empty(void)
57fe64a4 620{
38c6f780 621 struct diff_queue_struct *q = &diff_queued_diff;
427dcb4b
JH
622 int i;
623
38c6f780
JH
624 for (i = 0; i < q->nr; i++) {
625 struct diff_filepair *p = q->queue[i];
6b14d7fa 626 if (!uninteresting(p))
38c6f780
JH
627 return 0;
628 }
629 return 1;
630}
631
6b14d7fa 632void diff_flush(int diff_output_style)
38c6f780
JH
633{
634 struct diff_queue_struct *q = &diff_queued_diff;
635 int i;
636
6b14d7fa
JH
637 generate_patch = 0;
638 switch (diff_output_style) {
639 case DIFF_FORMAT_HUMAN:
640 line_termination = '\n';
641 inter_name_termination = '\t';
642 break;
643 case DIFF_FORMAT_MACHINE:
644 line_termination = inter_name_termination = 0;
645 break;
646 case DIFF_FORMAT_PATCH:
647 generate_patch = 1;
648 break;
649 }
427dcb4b
JH
650 for (i = 0; i < q->nr; i++)
651 diff_flush_one(q->queue[i]);
427dcb4b 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
664void 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
6b14d7fa 680 * filter uninteresting ones out at the final output
427dcb4b
JH
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
81e50eab
JH
700void diff_guif(unsigned old_mode,
701 unsigned new_mode,
702 const unsigned char *old_sha1,
703 const unsigned char *new_sha1,
704 const char *old_path,
705 const char *new_path)
706{
707 struct diff_filespec *one, *two;
708
709 if (reverse_diff) {
710 unsigned tmp;
711 const unsigned char *tmp_c;
712 tmp = old_mode; old_mode = new_mode; new_mode = tmp;
713 tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
714 }
715 one = alloc_filespec(old_path);
716 two = alloc_filespec(new_path);
717 if (old_mode)
718 fill_filespec(one, old_sha1, old_mode);
719 if (new_mode)
720 fill_filespec(two, new_sha1, new_mode);
721 diff_queue(&diff_queued_diff, one, two);
722}
723
77eb2720
JH
724void diff_change(unsigned old_mode, unsigned new_mode,
725 const unsigned char *old_sha1,
726 const unsigned char *new_sha1,
81e50eab
JH
727 const char *base, const char *path)
728{
77eb2720 729 char concatpath[PATH_MAX];
427dcb4b 730 struct diff_filespec *one, *two;
77eb2720 731
7ca45252
JH
732 if (reverse_diff) {
733 unsigned tmp;
734 const unsigned char *tmp_c;
735 tmp = old_mode; old_mode = new_mode; new_mode = tmp;
736 tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
737 }
427dcb4b
JH
738 if (!path) path = "";
739 sprintf(concatpath, "%s%s", base, path);
740 one = alloc_filespec(concatpath);
741 two = alloc_filespec(concatpath);
742 fill_filespec(one, old_sha1, old_mode);
743 fill_filespec(two, new_sha1, new_mode);
744
38c6f780 745 diff_queue(&diff_queued_diff, one, two);
77eb2720 746}
be3cfa85 747
77eb2720
JH
748void diff_unmerge(const char *path)
749{
6b14d7fa
JH
750 struct diff_filespec *one, *two;
751 one = alloc_filespec(path);
752 two = alloc_filespec(path);
753 diff_queue(&diff_queued_diff, one, two);
86436c28 754}