]> git.ipfire.org Git - thirdparty/git.git/blame - diff-lib.c
typo: replace 'commitish' with 'committish'
[thirdparty/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"
f394e093 9#include "gettext.h"
41771fa4 10#include "hex.h"
dabab1d6 11#include "object-name.h"
6973dcae 12#include "revision.h"
1cfe7733 13#include "cache-tree.h"
d1f2d7e8 14#include "unpack-trees.h"
948dd346 15#include "refs.h"
ee6fc514 16#include "submodule.h"
cb2a5135 17#include "symlinks.h"
74ea5c95 18#include "trace.h"
429bb40a 19#include "dir.h"
883e248b 20#include "fsmonitor.h"
177a8302 21#include "commit-reach.h"
427dcb4b 22
b46f0b6d 23/*
6973dcae 24 * diff-files
b46f0b6d 25 */
f0c6b2a2 26
948dd346 27/*
1392a377
JH
28 * Has the work tree entity been removed?
29 *
30 * Return 1 if it was removed from the work tree, 0 if an entity to be
31 * compared with the cache entry ce still exists (the latter includes
32 * the case where a directory that is not a submodule repository
33 * exists for ce that is a submodule -- it is a submodule that is not
34 * checked out). Return negative for an error.
948dd346 35 */
0ec9949f 36static int check_removed(const struct index_state *istate, const struct cache_entry *ce, struct stat *st)
948dd346 37{
0ec9949f 38 assert(is_fsmonitor_refreshed(istate));
4f3d6d02 39 if (!(ce->ce_flags & CE_FSMONITOR_VALID) && lstat(ce->name, st) < 0) {
c7054209 40 if (!is_missing_file_error(errno))
948dd346
JH
41 return -1;
42 return 1;
43 }
57199892 44 if (has_symlink_leading_path(ce->name, ce_namelen(ce)))
948dd346
JH
45 return 1;
46 if (S_ISDIR(st->st_mode)) {
1053fe82 47 struct object_id sub;
1392a377
JH
48
49 /*
50 * If ce is already a gitlink, we can have a plain
51 * directory (i.e. the submodule is not checked out),
52 * or a checked out submodule. Either case this is not
53 * a case where something was removed from the work tree,
54 * so we will return 0.
55 *
56 * Otherwise, if the directory is not a submodule
57 * repository, that means ce which was a blob turned into
58 * a directory --- the blob was removed!
59 */
60 if (!S_ISGITLINK(ce->ce_mode) &&
a98e6101 61 resolve_gitlink_ref(ce->name, "HEAD", &sub))
948dd346
JH
62 return 1;
63 }
64 return 0;
65}
d516c2d1 66
ae6d5c1b
JL
67/*
68 * Has a file changed or has a submodule new commits or a dirty work tree?
69 *
70 * Return 1 when changes are detected, 0 otherwise. If the DIRTY_SUBMODULES
71 * option is set, the caller does not only want to know if a submodule is
72 * modified at all but wants to know all the conditions that are met (new
73 * commits, untracked content and/or modified content).
74 */
75static int match_stat_with_submodule(struct diff_options *diffopt,
eb9ae4b5
RS
76 const struct cache_entry *ce,
77 struct stat *st, unsigned ce_option,
78 unsigned *dirty_submodule)
ae6d5c1b 79{
5adbb403 80 int changed = ie_match_stat(diffopt->repo->index, ce, st, ce_option);
aee9c7d6 81 if (S_ISGITLINK(ce->ce_mode)) {
02f2f56b 82 struct diff_flags orig_flags = diffopt->flags;
0d1e0e78 83 if (!diffopt->flags.override_submodule_config)
aee9c7d6 84 set_diffopt_flags_from_submodule_config(diffopt, ce->name);
0d1e0e78 85 if (diffopt->flags.ignore_submodules)
aee9c7d6 86 changed = 0;
0d1e0e78
BW
87 else if (!diffopt->flags.ignore_dirty_submodules &&
88 (!changed || diffopt->flags.dirty_submodules))
3b69daed 89 *dirty_submodule = is_submodule_modified(ce->name,
0d1e0e78 90 diffopt->flags.ignore_untracked_in_submodules);
aee9c7d6 91 diffopt->flags = orig_flags;
ae6d5c1b
JL
92 }
93 return changed;
94}
95
4bd5b7da 96int run_diff_files(struct rev_info *revs, unsigned int option)
f0c6b2a2 97{
6973dcae
JH
98 int entries, i;
99 int diff_unmerged_stage = revs->max_count;
fb63d7f8
JH
100 unsigned ce_option = ((option & DIFF_RACY_IS_MODIFIED)
101 ? CE_MATCH_RACY_IS_DIRTY : 0);
ca54d9ba 102 uint64_t start = getnanotime();
5adbb403 103 struct index_state *istate = revs->diffopt.repo->index;
f0c6b2a2 104
a5a818ee
JH
105 diff_set_mnemonic_prefix(&revs->diffopt, "i/", "w/");
106
c9052a83
AV
107 refresh_fsmonitor(istate);
108
6973dcae
JH
109 if (diff_unmerged_stage < 0)
110 diff_unmerged_stage = 2;
5adbb403 111 entries = istate->cache_nr;
6973dcae 112 for (i = 0; i < entries; i++) {
6973dcae 113 unsigned int oldmode, newmode;
5adbb403 114 struct cache_entry *ce = istate->cache[i];
6973dcae 115 int changed;
e3d42c47 116 unsigned dirty_submodule = 0;
55497b8c 117 const struct object_id *old_oid, *new_oid;
8676eb43 118
28b9264d 119 if (diff_can_quit_early(&revs->diffopt))
822cac01
JH
120 break;
121
5adbb403 122 if (!ce_path_match(istate, ce, &revs->prune_data, NULL))
8676eb43 123 continue;
be3cfa85 124
8174627b
ĐTCD
125 if (revs->diffopt.prefix &&
126 strncmp(ce->name, revs->diffopt.prefix, revs->diffopt.prefix_length))
127 continue;
128
6973dcae 129 if (ce_stage(ce)) {
b4b15503 130 struct combine_diff_path *dpath;
095ce953
JH
131 struct diff_filepair *pair;
132 unsigned int wt_mode = 0;
6973dcae 133 int num_compare_stages = 0;
b4b15503 134 size_t path_len;
53048100 135 struct stat st;
6973dcae 136
b4b15503
FF
137 path_len = ce_namelen(ce);
138
4fc970c4 139 dpath = xmalloc(combine_diff_path_size(5, path_len));
b4b15503
FF
140 dpath->path = (char *) &(dpath->parent[5]);
141
142 dpath->next = NULL;
b4b15503
FF
143 memcpy(dpath->path, ce->name, path_len);
144 dpath->path[path_len] = '\0';
1ff57c13 145 oidclr(&dpath->oid);
b4b15503 146 memset(&(dpath->parent[0]), 0,
4fc970c4
JH
147 sizeof(struct combine_diff_parent)*5);
148
0ec9949f 149 changed = check_removed(istate, ce, &st);
f58dbf23 150 if (!changed)
095ce953 151 wt_mode = ce_mode_from_stat(ce, st.st_mode);
f58dbf23
JH
152 else {
153 if (changed < 0) {
4fc970c4
JH
154 perror(ce->name);
155 continue;
156 }
095ce953 157 wt_mode = 0;
4fc970c4 158 }
095ce953 159 dpath->mode = wt_mode;
6973dcae
JH
160
161 while (i < entries) {
5adbb403 162 struct cache_entry *nce = istate->cache[i];
6973dcae
JH
163 int stage;
164
165 if (strcmp(ce->name, nce->name))
166 break;
167
168 /* Stage #2 (ours) is the first parent,
169 * stage #3 (theirs) is the second.
170 */
171 stage = ce_stage(nce);
172 if (2 <= stage) {
7a51ed66 173 int mode = nce->ce_mode;
6973dcae 174 num_compare_stages++;
99d1a986 175 oidcpy(&dpath->parent[stage - 2].oid,
176 &nce->oid);
7a51ed66 177 dpath->parent[stage-2].mode = ce_mode_from_stat(nce, mode);
b4b15503 178 dpath->parent[stage-2].status =
6973dcae
JH
179 DIFF_STATUS_MODIFIED;
180 }
181
182 /* diff against the proper unmerged stage */
183 if (stage == diff_unmerged_stage)
184 ce = nce;
185 i++;
186 }
187 /*
188 * Compensate for loop update
f0c6b2a2 189 */
6973dcae 190 i--;
6b5ee137 191
6973dcae 192 if (revs->combine_merges && num_compare_stages == 2) {
d01141de 193 show_combined_diff(dpath, 2, revs);
b4b15503 194 free(dpath);
6973dcae 195 continue;
1b1480ff 196 }
6a83d902 197 FREE_AND_NULL(dpath);
0e3994fa 198
6973dcae
JH
199 /*
200 * Show the diff for the 'ce' if we found the one
201 * from the desired stage.
202 */
095ce953
JH
203 pair = diff_unmerge(&revs->diffopt, ce->name);
204 if (wt_mode)
205 pair->two->mode = wt_mode;
6973dcae
JH
206 if (ce_stage(ce) != diff_unmerged_stage)
207 continue;
eeaa4603 208 }
6b14d7fa 209
125fd984 210 if (ce_uptodate(ce) || ce_skip_worktree(ce))
d1f2d7e8 211 continue;
f58dbf23 212
c9052a83
AV
213 /*
214 * When CE_VALID is set (via "update-index --assume-unchanged"
215 * or via adding paths while core.ignorestat is set to true),
216 * the user has promised that the working tree file for that
217 * path will not be modified. When CE_FSMONITOR_VALID is true,
218 * the fsmonitor knows that the path hasn't been modified since
219 * we refreshed the cached stat information. In either case,
220 * we do not have to stat to see if the path has been removed
221 * or modified.
222 */
223 if (ce->ce_flags & (CE_VALID | CE_FSMONITOR_VALID)) {
53048100
JK
224 changed = 0;
225 newmode = ce->ce_mode;
226 } else {
227 struct stat st;
228
0ec9949f 229 changed = check_removed(istate, ce, &st);
53048100
JK
230 if (changed) {
231 if (changed < 0) {
232 perror(ce->name);
233 continue;
234 }
235 diff_addremove(&revs->diffopt, '-', ce->ce_mode,
c26022ea 236 &ce->oid,
99d1a986 237 !is_null_oid(&ce->oid),
53048100 238 ce->name, 0);
15d061b4 239 continue;
425a28e0
NTND
240 } else if (revs->diffopt.ita_invisible_in_index &&
241 ce_intent_to_add(ce)) {
cb0dd22b
RP
242 newmode = ce_mode_from_stat(ce, st.st_mode);
243 diff_addremove(&revs->diffopt, '+', newmode,
14228447 244 null_oid(), 0, ce->name, 0);
425a28e0 245 continue;
15d061b4 246 }
53048100
JK
247
248 changed = match_stat_with_submodule(&revs->diffopt, ce, &st,
249 ce_option, &dirty_submodule);
250 newmode = ce_mode_from_stat(ce, st.st_mode);
f2ce9fde 251 }
53048100 252
85adbf2f 253 if (!changed && !dirty_submodule) {
8fa29602 254 ce_mark_uptodate(ce);
b5a81697 255 mark_fsmonitor_valid(istate, ce);
0d1e0e78 256 if (!revs->diffopt.flags.find_copies_harder)
8fa29602
JH
257 continue;
258 }
7a51ed66 259 oldmode = ce->ce_mode;
55497b8c 260 old_oid = &ce->oid;
14228447 261 new_oid = changed ? null_oid() : &ce->oid;
6973dcae 262 diff_change(&revs->diffopt, oldmode, newmode,
94a0097a 263 old_oid, new_oid,
55497b8c
BW
264 !is_null_oid(old_oid),
265 !is_null_oid(new_oid),
e3d42c47 266 ce->name, 0, dirty_submodule);
77eb2720 267
7ca45252 268 }
6973dcae
JH
269 diffcore_std(&revs->diffopt);
270 diff_flush(&revs->diffopt);
ca54d9ba 271 trace_performance_since(start, "diff-files");
6973dcae 272 return 0;
77eb2720 273}
be3cfa85 274
e09ad6e1
JH
275/*
276 * diff-index
277 */
278
279/* A file entry went away or appeared */
280static void diff_index_show_file(struct rev_info *revs,
281 const char *prefix,
eb9ae4b5 282 const struct cache_entry *ce,
fcf2cfb5 283 const struct object_id *oid, int oid_valid,
e5450100 284 unsigned int mode,
e3d42c47 285 unsigned dirty_submodule)
e09ad6e1 286{
7a51ed66 287 diff_addremove(&revs->diffopt, prefix[0], mode,
c26022ea 288 oid, oid_valid, ce->name, dirty_submodule);
e09ad6e1
JH
289}
290
0ec9949f
NK
291static int get_stat_data(const struct index_state *istate,
292 const struct cache_entry *ce,
362d7659 293 const struct object_id **oidp,
e09ad6e1 294 unsigned int *modep,
e3d42c47 295 int cached, int match_missing,
4d34477f 296 unsigned *dirty_submodule, struct diff_options *diffopt)
e09ad6e1 297{
362d7659 298 const struct object_id *oid = &ce->oid;
e09ad6e1
JH
299 unsigned int mode = ce->ce_mode;
300
658dd48c 301 if (!cached && !ce_uptodate(ce)) {
e09ad6e1
JH
302 int changed;
303 struct stat st;
0ec9949f 304 changed = check_removed(istate, ce, &st);
948dd346
JH
305 if (changed < 0)
306 return -1;
307 else if (changed) {
308 if (match_missing) {
362d7659 309 *oidp = oid;
e09ad6e1
JH
310 *modep = mode;
311 return 0;
312 }
313 return -1;
314 }
ae6d5c1b
JL
315 changed = match_stat_with_submodule(diffopt, ce, &st,
316 0, dirty_submodule);
e09ad6e1 317 if (changed) {
185c975f 318 mode = ce_mode_from_stat(ce, st.st_mode);
14228447 319 oid = null_oid();
e09ad6e1
JH
320 }
321 }
322
362d7659 323 *oidp = oid;
e09ad6e1
JH
324 *modep = mode;
325 return 0;
326}
327
ff7e6aad 328static void show_new_file(struct rev_info *revs,
e36ede2e 329 const struct cache_entry *new_file,
e09ad6e1
JH
330 int cached, int match_missing)
331{
362d7659 332 const struct object_id *oid;
e09ad6e1 333 unsigned int mode;
e3d42c47 334 unsigned dirty_submodule = 0;
0ec9949f 335 struct index_state *istate = revs->diffopt.repo->index;
e09ad6e1 336
9eb00af5
DS
337 if (new_file && S_ISSPARSEDIR(new_file->ce_mode)) {
338 diff_tree_oid(NULL, &new_file->oid, new_file->name, &revs->diffopt);
339 return;
340 }
341
948dd346
JH
342 /*
343 * New file in the index: it might actually be different in
f7d650c0 344 * the working tree.
e09ad6e1 345 */
0ec9949f 346 if (get_stat_data(istate, new_file, &oid, &mode, cached, match_missing,
4d34477f 347 &dirty_submodule, &revs->diffopt) < 0)
e09ad6e1
JH
348 return;
349
e36ede2e 350 diff_index_show_file(revs, "+", new_file, oid, !is_null_oid(oid), mode, dirty_submodule);
e09ad6e1
JH
351}
352
ff7e6aad 353static int show_modified(struct rev_info *revs,
e36ede2e
BW
354 const struct cache_entry *old_entry,
355 const struct cache_entry *new_entry,
e09ad6e1
JH
356 int report_missing,
357 int cached, int match_missing)
358{
359 unsigned int mode, oldmode;
362d7659 360 const struct object_id *oid;
e3d42c47 361 unsigned dirty_submodule = 0;
0ec9949f 362 struct index_state *istate = revs->diffopt.repo->index;
e09ad6e1 363
9eb00af5
DS
364 assert(S_ISSPARSEDIR(old_entry->ce_mode) ==
365 S_ISSPARSEDIR(new_entry->ce_mode));
366
367 /*
368 * If both are sparse directory entries, then expand the
369 * modifications to the file level. If only one was a sparse
370 * directory, then they appear as an add and delete instead of
371 * a modification.
372 */
373 if (S_ISSPARSEDIR(new_entry->ce_mode)) {
374 diff_tree_oid(&old_entry->oid, &new_entry->oid, new_entry->name, &revs->diffopt);
375 return 0;
376 }
377
0ec9949f 378 if (get_stat_data(istate, new_entry, &oid, &mode, cached, match_missing,
4d34477f 379 &dirty_submodule, &revs->diffopt) < 0) {
e09ad6e1 380 if (report_missing)
e36ede2e
BW
381 diff_index_show_file(revs, "-", old_entry,
382 &old_entry->oid, 1, old_entry->ce_mode,
99d1a986 383 0);
e09ad6e1
JH
384 return -1;
385 }
386
cb2b9f5e 387 if (revs->combine_merges && !cached &&
9001dc2a 388 (!oideq(oid, &old_entry->oid) || !oideq(&old_entry->oid, &new_entry->oid))) {
cb2b9f5e 389 struct combine_diff_path *p;
e36ede2e 390 int pathlen = ce_namelen(new_entry);
cb2b9f5e
PM
391
392 p = xmalloc(combine_diff_path_size(2, pathlen));
393 p->path = (char *) &p->parent[2];
394 p->next = NULL;
e36ede2e 395 memcpy(p->path, new_entry->name, pathlen);
cb2b9f5e 396 p->path[pathlen] = 0;
7a51ed66 397 p->mode = mode;
1ff57c13 398 oidclr(&p->oid);
cb2b9f5e
PM
399 memset(p->parent, 0, 2 * sizeof(struct combine_diff_parent));
400 p->parent[0].status = DIFF_STATUS_MODIFIED;
e36ede2e
BW
401 p->parent[0].mode = new_entry->ce_mode;
402 oidcpy(&p->parent[0].oid, &new_entry->oid);
cb2b9f5e 403 p->parent[1].status = DIFF_STATUS_MODIFIED;
e36ede2e
BW
404 p->parent[1].mode = old_entry->ce_mode;
405 oidcpy(&p->parent[1].oid, &old_entry->oid);
d01141de 406 show_combined_diff(p, 2, revs);
cb2b9f5e
PM
407 free(p);
408 return 0;
409 }
410
e36ede2e 411 oldmode = old_entry->ce_mode;
4a7e27e9 412 if (mode == oldmode && oideq(oid, &old_entry->oid) && !dirty_submodule &&
0d1e0e78 413 !revs->diffopt.flags.find_copies_harder)
e09ad6e1
JH
414 return 0;
415
e09ad6e1 416 diff_change(&revs->diffopt, oldmode, mode,
e36ede2e
BW
417 &old_entry->oid, oid, 1, !is_null_oid(oid),
418 old_entry->name, 0, dirty_submodule);
e09ad6e1
JH
419 return 0;
420}
421
d1f2d7e8
LT
422/*
423 * This gets a mix of an existing index and a tree, one pathname entry
424 * at a time. The index entry may be a single stage-0 one, but it could
425 * also be multiple unmerged entries (in which case idx_pos/idx_nr will
426 * give you the position and number of entries in the index).
427 */
428static void do_oneway_diff(struct unpack_trees_options *o,
eb9ae4b5
RS
429 const struct cache_entry *idx,
430 const struct cache_entry *tree)
e09ad6e1 431{
ff7e6aad 432 struct rev_info *revs = o->unpack_data;
d1f2d7e8 433 int match_missing, cached;
5c21ac0e 434
ba4e3561
NTND
435 /*
436 * i-t-a entries do not actually exist in the index (if we're
437 * looking at its content)
438 */
439 if (o->index_only &&
440 revs->diffopt.ita_invisible_in_index &&
425a28e0
NTND
441 idx && ce_intent_to_add(idx)) {
442 idx = NULL;
443 if (!tree)
444 return; /* nothing to diff.. */
445 }
446
540e694b 447 /* if the entry is not checked out, don't examine work tree */
b4d1690d
NTND
448 cached = o->index_only ||
449 (idx && ((idx->ce_flags & CE_VALID) || ce_skip_worktree(idx)));
572fc9aa
SO
450
451 match_missing = revs->match_missing;
d1f2d7e8
LT
452
453 if (cached && idx && ce_stage(idx)) {
fa7b2908
JH
454 struct diff_filepair *pair;
455 pair = diff_unmerge(&revs->diffopt, idx->name);
ff00b682 456 if (tree)
f9704c2d 457 fill_filespec(pair->one, &tree->oid, 1,
99d1a986 458 tree->ce_mode);
d1f2d7e8
LT
459 return;
460 }
461
462 /*
463 * Something added to the tree?
464 */
465 if (!tree) {
ff7e6aad 466 show_new_file(revs, idx, cached, match_missing);
d1f2d7e8
LT
467 return;
468 }
469
470 /*
471 * Something removed from the tree?
5c21ac0e 472 */
d1f2d7e8 473 if (!idx) {
56d8a271
VD
474 if (S_ISSPARSEDIR(tree->ce_mode)) {
475 diff_tree_oid(&tree->oid, NULL, tree->name, &revs->diffopt);
476 return;
477 }
478
fcf2cfb5 479 diff_index_show_file(revs, "-", tree, &tree->oid, 1,
99d1a986 480 tree->ce_mode, 0);
d1f2d7e8
LT
481 return;
482 }
483
484 /* Show difference between old and new */
ff7e6aad 485 show_modified(revs, tree, idx, 1, cached, match_missing);
d1f2d7e8
LT
486}
487
d1f2d7e8
LT
488/*
489 * The unpack_trees() interface is designed for merging, so
490 * the different source entries are designed primarily for
491 * the source trees, with the old index being really mainly
492 * used for being replaced by the result.
493 *
494 * For diffing, the index is more important, and we only have a
495 * single tree.
496 *
da8ba5e7 497 * We're supposed to advance o->pos to skip what we have already processed.
d1f2d7e8
LT
498 *
499 * This wrapper makes it all more readable, and takes care of all
500 * the fairly complex unpack_trees() semantic requirements, including
501 * the skipping, the path matching, the type conflict cases etc.
502 */
5828e835
RS
503static int oneway_diff(const struct cache_entry * const *src,
504 struct unpack_trees_options *o)
d1f2d7e8 505{
eb9ae4b5
RS
506 const struct cache_entry *idx = src[0];
507 const struct cache_entry *tree = src[1];
ff7e6aad 508 struct rev_info *revs = o->unpack_data;
d1f2d7e8 509
d1f2d7e8
LT
510 /*
511 * Unpack-trees generates a DF/conflict entry if
512 * there was a directory in the index and a tree
513 * in the tree. From a diff standpoint, that's a
514 * delete of the tree and a create of the file.
515 */
516 if (tree == o->df_conflict_entry)
517 tree = NULL;
518
5adbb403
NTND
519 if (ce_path_match(revs->diffopt.repo->index,
520 idx ? idx : tree,
521 &revs->prune_data, NULL)) {
34110cd4 522 do_oneway_diff(o, idx, tree);
b4194828
JH
523 if (diff_can_quit_early(&revs->diffopt)) {
524 o->exiting_early = 1;
525 return -1;
526 }
527 }
d1f2d7e8 528
34110cd4 529 return 0;
d1f2d7e8
LT
530}
531
bf979c07 532static int diff_cache(struct rev_info *revs,
944cffbd 533 const struct object_id *tree_oid,
bf979c07
JH
534 const char *tree_name,
535 int cached)
d1f2d7e8 536{
d1f2d7e8 537 struct tree *tree;
d1f2d7e8 538 struct tree_desc t;
bf979c07 539 struct unpack_trees_options opts;
e09ad6e1 540
a9dbc179 541 tree = parse_tree_indirect(tree_oid);
e09ad6e1 542 if (!tree)
bf979c07 543 return error("bad tree object %s",
944cffbd 544 tree_name ? tree_name : oid_to_hex(tree_oid));
d1f2d7e8
LT
545 memset(&opts, 0, sizeof(opts));
546 opts.head_idx = 1;
547 opts.index_only = cached;
a0919ced 548 opts.diff_index_cached = (cached &&
0d1e0e78 549 !revs->diffopt.flags.find_copies_harder);
d1f2d7e8
LT
550 opts.merge = 1;
551 opts.fn = oneway_diff;
ff7e6aad 552 opts.unpack_data = revs;
5adbb403 553 opts.src_index = revs->diffopt.repo->index;
34110cd4 554 opts.dst_index = NULL;
2f88c197 555 opts.pathspec = &revs->diffopt.pathspec;
4838237c 556 opts.pathspec->recursive = 1;
d1f2d7e8
LT
557
558 init_tree_desc(&t, tree->buffer, tree->size);
bf979c07
JH
559 return unpack_trees(1, &t, &opts);
560}
561
177a8302
DL
562void diff_get_merge_base(const struct rev_info *revs, struct object_id *mb)
563{
564 int i;
565 struct commit *mb_child[2] = {0};
566 struct commit_list *merge_bases;
567
568 for (i = 0; i < revs->pending.nr; i++) {
569 struct object *obj = revs->pending.objects[i].item;
570 if (obj->flags)
571 die(_("--merge-base does not work with ranges"));
572 if (obj->type != OBJ_COMMIT)
573 die(_("--merge-base only works with commits"));
574 }
575
576 /*
577 * This check must go after the for loop above because A...B
578 * ranges produce three pending commits, resulting in a
579 * misleading error message.
580 */
581 if (revs->pending.nr < 1 || revs->pending.nr > 2)
582 BUG("unexpected revs->pending.nr: %d", revs->pending.nr);
583
584 for (i = 0; i < revs->pending.nr; i++)
585 mb_child[i] = lookup_commit_reference(the_repository, &revs->pending.objects[i].item->oid);
586 if (revs->pending.nr == 1) {
587 struct object_id oid;
588
d850b7a5 589 if (repo_get_oid(the_repository, "HEAD", &oid))
177a8302
DL
590 die(_("unable to get HEAD"));
591
592 mb_child[1] = lookup_commit_reference(the_repository, &oid);
593 }
594
595 merge_bases = repo_get_merge_bases(the_repository, mb_child[0], mb_child[1]);
596 if (!merge_bases)
597 die(_("no merge base found"));
598 if (merge_bases->next)
599 die(_("multiple merge bases found"));
600
601 oidcpy(mb, &merge_bases->item->object.oid);
602
603 free_commit_list(merge_bases);
604}
605
4c3fe82e 606int run_diff_index(struct rev_info *revs, unsigned int option)
bf979c07
JH
607{
608 struct object_array_entry *ent;
4c3fe82e 609 int cached = !!(option & DIFF_INDEX_CACHED);
0f5a1d44
DL
610 int merge_base = !!(option & DIFF_INDEX_MERGE_BASE);
611 struct object_id oid;
612 const char *name;
613 char merge_base_hex[GIT_MAX_HEXSZ + 1];
4f3d6d02 614 struct index_state *istate = revs->diffopt.repo->index;
bf979c07 615
3506dc94
JK
616 if (revs->pending.nr != 1)
617 BUG("run_diff_index must be passed exactly one tree");
618
c46c406a 619 trace_performance_enter();
bf979c07 620 ent = revs->pending.objects;
0f5a1d44 621
4f3d6d02
NK
622 refresh_fsmonitor(istate);
623
0f5a1d44
DL
624 if (merge_base) {
625 diff_get_merge_base(revs, &oid);
626 name = oid_to_hex_r(merge_base_hex, &oid);
627 } else {
628 oidcpy(&oid, &ent->item->oid);
629 name = ent->name;
630 }
631
632 if (diff_cache(revs, &oid, name, cached))
203a2fe1 633 exit(128);
d1f2d7e8 634
a5a818ee 635 diff_set_mnemonic_prefix(&revs->diffopt, "c/", cached ? "i/" : "w/");
784c0dae 636 diffcore_fix_diff_index();
e09ad6e1
JH
637 diffcore_std(&revs->diffopt);
638 diff_flush(&revs->diffopt);
c46c406a 639 trace_performance_leave("diff-index");
d1f2d7e8 640 return 0;
e09ad6e1 641}
1cfe7733 642
944cffbd 643int do_diff_cache(const struct object_id *tree_oid, struct diff_options *opt)
1cfe7733 644{
1cfe7733 645 struct rev_info revs;
1cfe7733 646
ffc00a48 647 repo_init_revisions(opt->repo, &revs, NULL);
9a087274 648 copy_pathspec(&revs.prune_data, &opt->pathspec);
d44e5267 649 diff_setup_done(&revs.diffopt);
bf979c07 650 revs.diffopt = *opt;
204ce979 651
944cffbd 652 if (diff_cache(&revs, tree_oid, NULL, 1))
203a2fe1 653 exit(128);
f0cb6b80 654 release_revisions(&revs);
204ce979 655 return 0;
1cfe7733 656}
75f3ff2e 657
ffc00a48
NTND
658int index_differs_from(struct repository *r,
659 const char *def, const struct diff_flags *flags,
018ec3c8 660 int ita_invisible_in_index)
75f3ff2e
SB
661{
662 struct rev_info rev;
32962c9b 663 struct setup_revision_opt opt;
54c8a7c3 664 unsigned has_changes;
75f3ff2e 665
ffc00a48 666 repo_init_revisions(r, &rev, NULL);
32962c9b
JH
667 memset(&opt, 0, sizeof(opt));
668 opt.def = def;
669 setup_revisions(0, NULL, &rev, &opt);
0d1e0e78
BW
670 rev.diffopt.flags.quick = 1;
671 rev.diffopt.flags.exit_with_status = 1;
02f2f56b
BW
672 if (flags)
673 diff_flags_or(&rev.diffopt.flags, flags);
018ec3c8 674 rev.diffopt.ita_invisible_in_index = ita_invisible_in_index;
75f3ff2e 675 run_diff_index(&rev, 1);
54c8a7c3 676 has_changes = rev.diffopt.flags.has_changes;
1878b5ed 677 release_revisions(&rev);
54c8a7c3 678 return (has_changes != 0);
75f3ff2e 679}
cdffbdc2 680
61bdc7c5 681static struct strbuf *idiff_prefix_cb(struct diff_options *opt UNUSED, void *data)
cdffbdc2
ES
682{
683 return data;
684}
685
72a72390
ES
686void show_interdiff(const struct object_id *oid1, const struct object_id *oid2,
687 int indent, struct diff_options *diffopt)
cdffbdc2
ES
688{
689 struct diff_options opts;
690 struct strbuf prefix = STRBUF_INIT;
691
72a72390 692 memcpy(&opts, diffopt, sizeof(opts));
cdffbdc2
ES
693 opts.output_format = DIFF_FORMAT_PATCH;
694 opts.output_prefix = idiff_prefix_cb;
695 strbuf_addchars(&prefix, ' ', indent);
696 opts.output_prefix_data = &prefix;
697 diff_setup_done(&opts);
698
72a72390 699 diff_tree_oid(oid1, oid2, "", &opts);
cdffbdc2
ES
700 diffcore_std(&opts);
701 diff_flush(&opts);
702
703 strbuf_release(&prefix);
704}