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