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