Make run_diff_index() use unpack_trees(), not read_tree()
[git/git.git] / diff-lib.c
CommitLineData
be3cfa85
JH
1/*
2 * Copyright (C) 2005 Junio C Hamano
3 */
86436c28 4#include "cache.h"
6fb737be 5#include "quote.h"
6973dcae 6#include "commit.h"
86436c28 7#include "diff.h"
427dcb4b 8#include "diffcore.h"
6973dcae 9#include "revision.h"
1cfe7733 10#include "cache-tree.h"
d516c2d1 11#include "path-list.h"
d1f2d7e8 12#include "unpack-trees.h"
427dcb4b 13
b46f0b6d 14/*
6973dcae 15 * diff-files
b46f0b6d 16 */
f0c6b2a2 17
d516c2d1
JS
18static int read_directory(const char *path, struct path_list *list)
19{
20 DIR *dir;
21 struct dirent *e;
22
23 if (!(dir = opendir(path)))
24 return error("Could not open directory %s", path);
25
26 while ((e = readdir(dir)))
27 if (strcmp(".", e->d_name) && strcmp("..", e->d_name))
4d3f4b80 28 path_list_insert(e->d_name, list);
d516c2d1
JS
29
30 closedir(dir);
31 return 0;
32}
33
0c725f1b
JS
34static int get_mode(const char *path, int *mode)
35{
36 struct stat st;
37
38 if (!path || !strcmp(path, "/dev/null"))
39 *mode = 0;
40 else if (!strcmp(path, "-"))
7a51ed66 41 *mode = create_ce_mode(0666);
0c725f1b
JS
42 else if (stat(path, &st))
43 return error("Could not access '%s'", path);
44 else
45 *mode = st.st_mode;
46 return 0;
47}
48
d516c2d1
JS
49static int queue_diff(struct diff_options *o,
50 const char *name1, const char *name2)
51{
d516c2d1
JS
52 int mode1 = 0, mode2 = 0;
53
0c725f1b
JS
54 if (get_mode(name1, &mode1) || get_mode(name2, &mode2))
55 return -1;
d516c2d1
JS
56
57 if (mode1 && mode2 && S_ISDIR(mode1) != S_ISDIR(mode2))
58 return error("file/directory conflict: %s, %s", name1, name2);
59
60 if (S_ISDIR(mode1) || S_ISDIR(mode2)) {
61 char buffer1[PATH_MAX], buffer2[PATH_MAX];
62 struct path_list p1 = {NULL, 0, 0, 1}, p2 = {NULL, 0, 0, 1};
63 int len1 = 0, len2 = 0, i1, i2, ret = 0;
64
65 if (name1 && read_directory(name1, &p1))
66 return -1;
67 if (name2 && read_directory(name2, &p2)) {
68 path_list_clear(&p1, 0);
69 return -1;
70 }
71
72 if (name1) {
73 len1 = strlen(name1);
74 if (len1 > 0 && name1[len1 - 1] == '/')
75 len1--;
76 memcpy(buffer1, name1, len1);
77 buffer1[len1++] = '/';
78 }
79
80 if (name2) {
81 len2 = strlen(name2);
82 if (len2 > 0 && name2[len2 - 1] == '/')
83 len2--;
84 memcpy(buffer2, name2, len2);
85 buffer2[len2++] = '/';
86 }
87
88 for (i1 = i2 = 0; !ret && (i1 < p1.nr || i2 < p2.nr); ) {
89 const char *n1, *n2;
90 int comp;
91
92 if (i1 == p1.nr)
93 comp = 1;
94 else if (i2 == p2.nr)
95 comp = -1;
96 else
97 comp = strcmp(p1.items[i1].path,
98 p2.items[i2].path);
99
100 if (comp > 0)
101 n1 = NULL;
102 else {
103 n1 = buffer1;
104 strncpy(buffer1 + len1, p1.items[i1++].path,
105 PATH_MAX - len1);
106 }
107
108 if (comp < 0)
109 n2 = NULL;
110 else {
111 n2 = buffer2;
112 strncpy(buffer2 + len2, p2.items[i2++].path,
113 PATH_MAX - len2);
114 }
115
116 ret = queue_diff(o, n1, n2);
117 }
118 path_list_clear(&p1, 0);
119 path_list_clear(&p2, 0);
120
121 return ret;
122 } else {
123 struct diff_filespec *d1, *d2;
124
8f67f8ae 125 if (DIFF_OPT_TST(o, REVERSE_DIFF)) {
d516c2d1
JS
126 unsigned tmp;
127 const char *tmp_c;
128 tmp = mode1; mode1 = mode2; mode2 = tmp;
129 tmp_c = name1; name1 = name2; name2 = tmp_c;
130 }
131
132 if (!name1)
133 name1 = "/dev/null";
134 if (!name2)
135 name2 = "/dev/null";
136 d1 = alloc_filespec(name1);
137 d2 = alloc_filespec(name2);
138 fill_filespec(d1, null_sha1, mode1);
139 fill_filespec(d2, null_sha1, mode2);
140
141 diff_queue(&diff_queued_diff, d1, d2);
142 return 0;
143 }
144}
145
1ad029b6
JH
146/*
147 * Does the path name a blob in the working tree, or a directory
148 * in the working tree?
149 */
d516c2d1
JS
150static int is_in_index(const char *path)
151{
1ad029b6
JH
152 int len, pos;
153 struct cache_entry *ce;
154
155 len = strlen(path);
156 while (path[len-1] == '/')
157 len--;
158 if (!len)
159 return 1; /* "." */
160 pos = cache_name_pos(path, len);
161 if (0 <= pos)
162 return 1;
163 pos = -1 - pos;
164 while (pos < active_nr) {
165 ce = active_cache[pos++];
166 if (ce_namelen(ce) <= len ||
167 strncmp(ce->name, path, len) ||
168 (ce->name[len] > '/'))
169 break; /* path cannot be a prefix */
170 if (ce->name[len] == '/')
171 return 1;
172 }
173 return 0;
d516c2d1
JS
174}
175
176static int handle_diff_files_args(struct rev_info *revs,
4bd5b7da
JH
177 int argc, const char **argv,
178 unsigned int *options)
d516c2d1 179{
4bd5b7da 180 *options = 0;
d516c2d1
JS
181
182 /* revs->max_count == -2 means --no-index */
183 while (1 < argc && argv[1][0] == '-') {
184 if (!strcmp(argv[1], "--base"))
185 revs->max_count = 1;
186 else if (!strcmp(argv[1], "--ours"))
187 revs->max_count = 2;
188 else if (!strcmp(argv[1], "--theirs"))
189 revs->max_count = 3;
190 else if (!strcmp(argv[1], "-n") ||
41bbf9d5 191 !strcmp(argv[1], "--no-index")) {
d516c2d1 192 revs->max_count = -2;
8f67f8ae
PH
193 DIFF_OPT_SET(&revs->diffopt, EXIT_WITH_STATUS);
194 DIFF_OPT_SET(&revs->diffopt, NO_INDEX);
41bbf9d5 195 }
d516c2d1 196 else if (!strcmp(argv[1], "-q"))
4bd5b7da 197 *options |= DIFF_SILENT_ON_REMOVED;
d516c2d1
JS
198 else
199 return error("invalid option: %s", argv[1]);
200 argv++; argc--;
201 }
202
203 if (revs->max_count == -1 && revs->diffopt.nr_paths == 2) {
204 /*
205 * If two files are specified, and at least one is untracked,
206 * default to no-index.
207 */
208 read_cache();
209 if (!is_in_index(revs->diffopt.paths[0]) ||
6d2d9e86 210 !is_in_index(revs->diffopt.paths[1])) {
d516c2d1 211 revs->max_count = -2;
8f67f8ae 212 DIFF_OPT_SET(&revs->diffopt, NO_INDEX);
6d2d9e86 213 }
d516c2d1
JS
214 }
215
216 /*
217 * Make sure there are NO revision (i.e. pending object) parameter,
218 * rev.max_count is reasonable (0 <= n <= 3),
219 * there is no other revision filtering parameters.
220 */
221 if (revs->pending.nr || revs->max_count > 3 ||
222 revs->min_age != -1 || revs->max_age != -1)
223 return error("no revision allowed with diff-files");
224
225 if (revs->max_count == -1 &&
226 (revs->diffopt.output_format & DIFF_FORMAT_PATCH))
227 revs->combine_merges = revs->dense_combined_merges = 1;
228
229 return 0;
230}
231
fcfa33ec
JS
232static int is_outside_repo(const char *path, int nongit, const char *prefix)
233{
234 int i;
ecf4831d 235 if (nongit || !strcmp(path, "-") || is_absolute_path(path))
fcfa33ec
JS
236 return 1;
237 if (prefixcmp(path, "../"))
238 return 0;
239 if (!prefix)
240 return 1;
241 for (i = strlen(prefix); !prefixcmp(path, "../"); ) {
242 while (i > 0 && prefix[i - 1] != '/')
243 i--;
244 if (--i < 0)
245 return 1;
246 path += 3;
247 }
248 return 0;
249}
250
251int setup_diff_no_index(struct rev_info *revs,
252 int argc, const char ** argv, int nongit, const char *prefix)
253{
254 int i;
255 for (i = 1; i < argc; i++)
5332b2af 256 if (argv[i][0] != '-' || argv[i][1] == '\0')
fcfa33ec
JS
257 break;
258 else if (!strcmp(argv[i], "--")) {
259 i++;
260 break;
261 } else if (i < argc - 3 && !strcmp(argv[i], "--no-index")) {
262 i = argc - 3;
8f67f8ae 263 DIFF_OPT_SET(&revs->diffopt, EXIT_WITH_STATUS);
fcfa33ec
JS
264 break;
265 }
266 if (argc != i + 2 || (!is_outside_repo(argv[i + 1], nongit, prefix) &&
267 !is_outside_repo(argv[i], nongit, prefix)))
268 return -1;
269
270 diff_setup(&revs->diffopt);
271 for (i = 1; i < argc - 2; )
272 if (!strcmp(argv[i], "--no-index"))
273 i++;
274 else {
275 int j = diff_opt_parse(&revs->diffopt,
276 argv + i, argc - i);
277 if (!j)
278 die("invalid diff option/value: %s", argv[i]);
279 i += j;
280 }
ae792aa5
JH
281
282 if (prefix) {
283 int len = strlen(prefix);
284
285 revs->diffopt.paths = xcalloc(2, sizeof(char*));
286 for (i = 0; i < 2; i++) {
3afaa72d
JH
287 const char *p = argv[argc - 2 + i];
288 /*
289 * stdin should be spelled as '-'; if you have
290 * path that is '-', spell it as ./-.
291 */
292 p = (strcmp(p, "-")
293 ? xstrdup(prefix_filename(prefix, len, p))
294 : p);
295 revs->diffopt.paths[i] = p;
ae792aa5
JH
296 }
297 }
298 else
299 revs->diffopt.paths = argv + argc - 2;
fcfa33ec 300 revs->diffopt.nr_paths = 2;
8f67f8ae 301 DIFF_OPT_SET(&revs->diffopt, NO_INDEX);
fcfa33ec 302 revs->max_count = -2;
b78281f7
JH
303 if (diff_setup_done(&revs->diffopt) < 0)
304 die("diff_setup_done failed");
fcfa33ec
JS
305 return 0;
306}
307
d516c2d1
JS
308int run_diff_files_cmd(struct rev_info *revs, int argc, const char **argv)
309{
4bd5b7da 310 unsigned int options;
d516c2d1 311
4bd5b7da 312 if (handle_diff_files_args(revs, argc, argv, &options))
d516c2d1
JS
313 return -1;
314
8f67f8ae 315 if (DIFF_OPT_TST(&revs->diffopt, NO_INDEX)) {
d516c2d1
JS
316 if (revs->diffopt.nr_paths != 2)
317 return error("need two files/directories with --no-index");
34a5e1a2
JS
318 if (queue_diff(&revs->diffopt, revs->diffopt.paths[0],
319 revs->diffopt.paths[1]))
320 return -1;
d516c2d1
JS
321 diffcore_std(&revs->diffopt);
322 diff_flush(&revs->diffopt);
34a5e1a2
JS
323 /*
324 * The return code for --no-index imitates diff(1):
325 * 0 = no changes, 1 = changes, else error
326 */
327 return revs->diffopt.found_changes;
d516c2d1
JS
328 }
329
efdfd6c8
JH
330 if (read_cache() < 0) {
331 perror("read_cache");
332 return -1;
333 }
4bd5b7da 334 return run_diff_files(revs, options);
d516c2d1
JS
335}
336
4bd5b7da 337int run_diff_files(struct rev_info *revs, unsigned int option)
f0c6b2a2 338{
6973dcae
JH
339 int entries, i;
340 int diff_unmerged_stage = revs->max_count;
4bd5b7da 341 int silent_on_removed = option & DIFF_SILENT_ON_REMOVED;
fb63d7f8
JH
342 unsigned ce_option = ((option & DIFF_RACY_IS_MODIFIED)
343 ? CE_MATCH_RACY_IS_DIRTY : 0);
f0c6b2a2 344
6973dcae
JH
345 if (diff_unmerged_stage < 0)
346 diff_unmerged_stage = 2;
b4e1e4a7 347 entries = active_nr;
6973dcae 348 for (i = 0; i < entries; i++) {
be3cfa85 349 struct stat st;
6973dcae
JH
350 unsigned int oldmode, newmode;
351 struct cache_entry *ce = active_cache[i];
352 int changed;
8676eb43 353
8f67f8ae
PH
354 if (DIFF_OPT_TST(&revs->diffopt, QUIET) &&
355 DIFF_OPT_TST(&revs->diffopt, HAS_CHANGES))
822cac01
JH
356 break;
357
6973dcae 358 if (!ce_path_match(ce, revs->prune_data))
8676eb43 359 continue;
be3cfa85 360
6973dcae 361 if (ce_stage(ce)) {
b4b15503 362 struct combine_diff_path *dpath;
6973dcae 363 int num_compare_stages = 0;
b4b15503 364 size_t path_len;
6973dcae 365
b4b15503
FF
366 path_len = ce_namelen(ce);
367
4fc970c4 368 dpath = xmalloc(combine_diff_path_size(5, path_len));
b4b15503
FF
369 dpath->path = (char *) &(dpath->parent[5]);
370
371 dpath->next = NULL;
372 dpath->len = path_len;
373 memcpy(dpath->path, ce->name, path_len);
374 dpath->path[path_len] = '\0';
a8e0d16d 375 hashclr(dpath->sha1);
b4b15503 376 memset(&(dpath->parent[0]), 0,
4fc970c4
JH
377 sizeof(struct combine_diff_parent)*5);
378
379 if (lstat(ce->name, &st) < 0) {
380 if (errno != ENOENT && errno != ENOTDIR) {
381 perror(ce->name);
382 continue;
383 }
384 if (silent_on_removed)
385 continue;
386 }
387 else
7a51ed66 388 dpath->mode = ce_mode_from_stat(ce, st.st_mode);
6973dcae
JH
389
390 while (i < entries) {
391 struct cache_entry *nce = active_cache[i];
392 int stage;
393
394 if (strcmp(ce->name, nce->name))
395 break;
396
397 /* Stage #2 (ours) is the first parent,
398 * stage #3 (theirs) is the second.
399 */
400 stage = ce_stage(nce);
401 if (2 <= stage) {
7a51ed66 402 int mode = nce->ce_mode;
6973dcae 403 num_compare_stages++;
e702496e 404 hashcpy(dpath->parent[stage-2].sha1, nce->sha1);
7a51ed66 405 dpath->parent[stage-2].mode = ce_mode_from_stat(nce, mode);
b4b15503 406 dpath->parent[stage-2].status =
6973dcae
JH
407 DIFF_STATUS_MODIFIED;
408 }
409
410 /* diff against the proper unmerged stage */
411 if (stage == diff_unmerged_stage)
412 ce = nce;
413 i++;
414 }
415 /*
416 * Compensate for loop update
f0c6b2a2 417 */
6973dcae 418 i--;
6b5ee137 419
6973dcae 420 if (revs->combine_merges && num_compare_stages == 2) {
b4b15503 421 show_combined_diff(dpath, 2,
6973dcae
JH
422 revs->dense_combined_merges,
423 revs);
b4b15503 424 free(dpath);
6973dcae 425 continue;
1b1480ff 426 }
b4b15503
FF
427 free(dpath);
428 dpath = NULL;
0e3994fa 429
6973dcae
JH
430 /*
431 * Show the diff for the 'ce' if we found the one
432 * from the desired stage.
433 */
e9c84099 434 diff_unmerge(&revs->diffopt, ce->name, 0, null_sha1);
6973dcae
JH
435 if (ce_stage(ce) != diff_unmerged_stage)
436 continue;
eeaa4603 437 }
6b14d7fa 438
d1f2d7e8
LT
439 if (ce_uptodate(ce))
440 continue;
6973dcae
JH
441 if (lstat(ce->name, &st) < 0) {
442 if (errno != ENOENT && errno != ENOTDIR) {
443 perror(ce->name);
15d061b4
JH
444 continue;
445 }
6973dcae
JH
446 if (silent_on_removed)
447 continue;
7a51ed66 448 diff_addremove(&revs->diffopt, '-', ce->ce_mode,
6973dcae
JH
449 ce->sha1, ce->name, NULL);
450 continue;
f2ce9fde 451 }
fb63d7f8 452 changed = ce_match_stat(ce, &st, ce_option);
8f67f8ae 453 if (!changed && !DIFF_OPT_TST(&revs->diffopt, FIND_COPIES_HARDER))
6973dcae 454 continue;
7a51ed66
LT
455 oldmode = ce->ce_mode;
456 newmode = ce_mode_from_stat(ce, st.st_mode);
6973dcae
JH
457 diff_change(&revs->diffopt, oldmode, newmode,
458 ce->sha1, (changed ? null_sha1 : ce->sha1),
459 ce->name, NULL);
77eb2720 460
7ca45252 461 }
6973dcae
JH
462 diffcore_std(&revs->diffopt);
463 diff_flush(&revs->diffopt);
464 return 0;
77eb2720 465}
be3cfa85 466
e09ad6e1
JH
467/*
468 * diff-index
469 */
470
471/* A file entry went away or appeared */
472static void diff_index_show_file(struct rev_info *revs,
473 const char *prefix,
474 struct cache_entry *ce,
475 unsigned char *sha1, unsigned int mode)
476{
7a51ed66 477 diff_addremove(&revs->diffopt, prefix[0], mode,
e09ad6e1
JH
478 sha1, ce->name, NULL);
479}
480
481static int get_stat_data(struct cache_entry *ce,
482 unsigned char **sha1p,
483 unsigned int *modep,
484 int cached, int match_missing)
485{
486 unsigned char *sha1 = ce->sha1;
487 unsigned int mode = ce->ce_mode;
488
489 if (!cached) {
490 static unsigned char no_sha1[20];
491 int changed;
492 struct stat st;
493 if (lstat(ce->name, &st) < 0) {
494 if (errno == ENOENT && match_missing) {
495 *sha1p = sha1;
496 *modep = mode;
497 return 0;
498 }
499 return -1;
500 }
501 changed = ce_match_stat(ce, &st, 0);
502 if (changed) {
185c975f 503 mode = ce_mode_from_stat(ce, st.st_mode);
e09ad6e1
JH
504 sha1 = no_sha1;
505 }
506 }
507
508 *sha1p = sha1;
509 *modep = mode;
510 return 0;
511}
512
513static void show_new_file(struct rev_info *revs,
514 struct cache_entry *new,
515 int cached, int match_missing)
516{
517 unsigned char *sha1;
518 unsigned int mode;
519
520 /* New file in the index: it might actually be different in
521 * the working copy.
522 */
523 if (get_stat_data(new, &sha1, &mode, cached, match_missing) < 0)
524 return;
525
526 diff_index_show_file(revs, "+", new, sha1, mode);
527}
528
529static int show_modified(struct rev_info *revs,
530 struct cache_entry *old,
531 struct cache_entry *new,
532 int report_missing,
533 int cached, int match_missing)
534{
535 unsigned int mode, oldmode;
536 unsigned char *sha1;
537
538 if (get_stat_data(new, &sha1, &mode, cached, match_missing) < 0) {
539 if (report_missing)
540 diff_index_show_file(revs, "-", old,
541 old->sha1, old->ce_mode);
542 return -1;
543 }
544
cb2b9f5e
PM
545 if (revs->combine_merges && !cached &&
546 (hashcmp(sha1, old->sha1) || hashcmp(old->sha1, new->sha1))) {
547 struct combine_diff_path *p;
548 int pathlen = ce_namelen(new);
549
550 p = xmalloc(combine_diff_path_size(2, pathlen));
551 p->path = (char *) &p->parent[2];
552 p->next = NULL;
553 p->len = pathlen;
554 memcpy(p->path, new->name, pathlen);
555 p->path[pathlen] = 0;
7a51ed66 556 p->mode = mode;
cb2b9f5e
PM
557 hashclr(p->sha1);
558 memset(p->parent, 0, 2 * sizeof(struct combine_diff_parent));
559 p->parent[0].status = DIFF_STATUS_MODIFIED;
7a51ed66 560 p->parent[0].mode = new->ce_mode;
cb2b9f5e
PM
561 hashcpy(p->parent[0].sha1, new->sha1);
562 p->parent[1].status = DIFF_STATUS_MODIFIED;
7a51ed66 563 p->parent[1].mode = old->ce_mode;
cb2b9f5e
PM
564 hashcpy(p->parent[1].sha1, old->sha1);
565 show_combined_diff(p, 2, revs->dense_combined_merges, revs);
566 free(p);
567 return 0;
568 }
569
e09ad6e1 570 oldmode = old->ce_mode;
a89fccd2 571 if (mode == oldmode && !hashcmp(sha1, old->sha1) &&
8f67f8ae 572 !DIFF_OPT_TST(&revs->diffopt, FIND_COPIES_HARDER))
e09ad6e1
JH
573 return 0;
574
e09ad6e1
JH
575 diff_change(&revs->diffopt, oldmode, mode,
576 old->sha1, sha1, old->name, NULL);
577 return 0;
578}
579
580static int diff_cache(struct rev_info *revs,
581 struct cache_entry **ac, int entries,
582 const char **pathspec,
583 int cached, int match_missing)
584{
585 while (entries) {
586 struct cache_entry *ce = *ac;
587 int same = (entries > 1) && ce_same_name(ce, ac[1]);
588
8f67f8ae
PH
589 if (DIFF_OPT_TST(&revs->diffopt, QUIET) &&
590 DIFF_OPT_TST(&revs->diffopt, HAS_CHANGES))
822cac01
JH
591 break;
592
e09ad6e1
JH
593 if (!ce_path_match(ce, pathspec))
594 goto skip_entry;
595
596 switch (ce_stage(ce)) {
597 case 0:
598 /* No stage 1 entry? That means it's a new file */
599 if (!same) {
600 show_new_file(revs, ce, cached, match_missing);
601 break;
602 }
603 /* Show difference between old and new */
1cfe7733 604 show_modified(revs, ac[1], ce, 1,
e09ad6e1
JH
605 cached, match_missing);
606 break;
607 case 1:
608 /* No stage 3 (merge) entry?
609 * That means it's been deleted.
610 */
611 if (!same) {
612 diff_index_show_file(revs, "-", ce,
613 ce->sha1, ce->ce_mode);
614 break;
615 }
616 /* We come here with ce pointing at stage 1
617 * (original tree) and ac[1] pointing at stage
618 * 3 (unmerged). show-modified with
619 * report-missing set to false does not say the
620 * file is deleted but reports true if work
621 * tree does not have it, in which case we
622 * fall through to report the unmerged state.
623 * Otherwise, we show the differences between
624 * the original tree and the work tree.
625 */
626 if (!cached &&
627 !show_modified(revs, ce, ac[1], 0,
628 cached, match_missing))
629 break;
e9c84099 630 diff_unmerge(&revs->diffopt, ce->name,
7a51ed66 631 ce->ce_mode, ce->sha1);
e9c84099 632 break;
e09ad6e1 633 case 3:
e9c84099
JH
634 diff_unmerge(&revs->diffopt, ce->name,
635 0, null_sha1);
e09ad6e1
JH
636 break;
637
638 default:
639 die("impossible cache entry stage");
640 }
641
642skip_entry:
643 /*
644 * Ignore all the different stages for this file,
645 * we've handled the relevant cases now.
646 */
647 do {
648 ac++;
649 entries--;
650 } while (entries && ce_same_name(ce, ac[0]));
651 }
652 return 0;
653}
654
655/*
656 * This turns all merge entries into "stage 3". That guarantees that
657 * when we read in the new tree (into "stage 1"), we won't lose sight
658 * of the fact that we had unmerged entries.
659 */
660static void mark_merge_entries(void)
661{
662 int i;
663 for (i = 0; i < active_nr; i++) {
664 struct cache_entry *ce = active_cache[i];
665 if (!ce_stage(ce))
666 continue;
7a51ed66 667 ce->ce_flags |= CE_STAGEMASK;
e09ad6e1
JH
668 }
669}
670
d1f2d7e8
LT
671/*
672 * This gets a mix of an existing index and a tree, one pathname entry
673 * at a time. The index entry may be a single stage-0 one, but it could
674 * also be multiple unmerged entries (in which case idx_pos/idx_nr will
675 * give you the position and number of entries in the index).
676 */
677static void do_oneway_diff(struct unpack_trees_options *o,
678 struct cache_entry *idx,
679 struct cache_entry *tree,
680 int idx_pos, int idx_nr)
e09ad6e1 681{
d1f2d7e8
LT
682 struct rev_info *revs = o->unpack_data;
683 int match_missing, cached;
5c21ac0e 684
a6080a0a 685 /*
5c21ac0e 686 * Backward compatibility wart - "diff-index -m" does
d1f2d7e8
LT
687 * not mean "do not ignore merges", but "match_missing".
688 *
689 * But with the revision flag parsing, that's found in
690 * "!revs->ignore_merges".
691 */
692 cached = o->index_only;
693 match_missing = !revs->ignore_merges;
694
695 if (cached && idx && ce_stage(idx)) {
696 if (tree)
697 diff_unmerge(&revs->diffopt, idx->name, idx->ce_mode, idx->sha1);
698 return;
699 }
700
701 /*
702 * Something added to the tree?
703 */
704 if (!tree) {
705 show_new_file(revs, idx, cached, match_missing);
706 return;
707 }
708
709 /*
710 * Something removed from the tree?
5c21ac0e 711 */
d1f2d7e8
LT
712 if (!idx) {
713 diff_index_show_file(revs, "-", tree, tree->sha1, tree->ce_mode);
714 return;
715 }
716
717 /* Show difference between old and new */
718 show_modified(revs, tree, idx, 1, cached, match_missing);
719}
720
721/*
722 * Count how many index entries go with the first one
723 */
724static inline int count_skip(const struct cache_entry *src, int pos)
725{
726 int skip = 1;
727
728 /* We can only have multiple entries if the first one is not stage-0 */
729 if (ce_stage(src)) {
730 struct cache_entry **p = active_cache + pos;
731 int namelen = ce_namelen(src);
732
733 for (;;) {
734 const struct cache_entry *ce;
735 pos++;
736 if (pos >= active_nr)
737 break;
738 ce = *++p;
739 if (ce_namelen(ce) != namelen)
740 break;
741 if (memcmp(ce->name, src->name, namelen))
742 break;
743 skip++;
744 }
745 }
746 return skip;
747}
748
749/*
750 * The unpack_trees() interface is designed for merging, so
751 * the different source entries are designed primarily for
752 * the source trees, with the old index being really mainly
753 * used for being replaced by the result.
754 *
755 * For diffing, the index is more important, and we only have a
756 * single tree.
757 *
758 * We're supposed to return how many index entries we want to skip.
759 *
760 * This wrapper makes it all more readable, and takes care of all
761 * the fairly complex unpack_trees() semantic requirements, including
762 * the skipping, the path matching, the type conflict cases etc.
763 */
764static int oneway_diff(struct cache_entry **src,
765 struct unpack_trees_options *o,
766 int index_pos)
767{
768 int skip = 0;
769 struct cache_entry *idx = src[0];
770 struct cache_entry *tree = src[1];
771 struct rev_info *revs = o->unpack_data;
772
773 if (index_pos >= 0)
774 skip = count_skip(idx, index_pos);
775
776 /*
777 * Unpack-trees generates a DF/conflict entry if
778 * there was a directory in the index and a tree
779 * in the tree. From a diff standpoint, that's a
780 * delete of the tree and a create of the file.
781 */
782 if (tree == o->df_conflict_entry)
783 tree = NULL;
784
785 if (ce_path_match(idx ? idx : tree, revs->prune_data))
786 do_oneway_diff(o, idx, tree, index_pos, skip);
787
788 return skip;
789}
790
791int run_diff_index(struct rev_info *revs, int cached)
792{
793 struct object *ent;
794 struct tree *tree;
795 const char *tree_name;
796 struct unpack_trees_options opts;
797 struct tree_desc t;
e09ad6e1 798
e09ad6e1
JH
799 mark_merge_entries();
800
1f1e895f
LT
801 ent = revs->pending.objects[0].item;
802 tree_name = revs->pending.objects[0].name;
e09ad6e1
JH
803 tree = parse_tree_indirect(ent->sha1);
804 if (!tree)
805 return error("bad tree object %s", tree_name);
d1f2d7e8
LT
806
807 memset(&opts, 0, sizeof(opts));
808 opts.head_idx = 1;
809 opts.index_only = cached;
810 opts.merge = 1;
811 opts.fn = oneway_diff;
812 opts.unpack_data = revs;
813
814 init_tree_desc(&t, tree->buffer, tree->size);
815 unpack_trees(1, &t, &opts);
816
e09ad6e1
JH
817 diffcore_std(&revs->diffopt);
818 diff_flush(&revs->diffopt);
d1f2d7e8 819 return 0;
e09ad6e1 820}
1cfe7733
JH
821
822int do_diff_cache(const unsigned char *tree_sha1, struct diff_options *opt)
823{
824 struct tree *tree;
825 struct rev_info revs;
826 int i;
827 struct cache_entry **dst;
828 struct cache_entry *last = NULL;
829
830 /*
831 * This is used by git-blame to run diff-cache internally;
832 * it potentially needs to repeatedly run this, so we will
833 * start by removing the higher order entries the last round
834 * left behind.
835 */
836 dst = active_cache;
837 for (i = 0; i < active_nr; i++) {
838 struct cache_entry *ce = active_cache[i];
839 if (ce_stage(ce)) {
840 if (last && !strcmp(ce->name, last->name))
841 continue;
842 cache_tree_invalidate_path(active_cache_tree,
843 ce->name);
844 last = ce;
7a51ed66 845 ce->ce_flags |= CE_REMOVE;
1cfe7733
JH
846 }
847 *dst++ = ce;
848 }
849 active_nr = dst - active_cache;
850
851 init_revisions(&revs, NULL);
852 revs.prune_data = opt->paths;
853 tree = parse_tree_indirect(tree_sha1);
854 if (!tree)
855 die("bad tree object %s", sha1_to_hex(tree_sha1));
856 if (read_tree(tree, 1, opt->paths))
857 return error("unable to read tree %s", sha1_to_hex(tree_sha1));
858 return diff_cache(&revs, active_cache, active_nr, revs.prune_data,
859 1, 0);
860}