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