Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) 2005 Junio C Hamano |
3 | | */ |
4 | | |
5 | | #define USE_THE_REPOSITORY_VARIABLE |
6 | | #define DISABLE_SIGN_COMPARE_WARNINGS |
7 | | |
8 | | #include "git-compat-util.h" |
9 | | #include "commit.h" |
10 | | #include "diff.h" |
11 | | #include "diffcore.h" |
12 | | #include "gettext.h" |
13 | | #include "hash.h" |
14 | | #include "hex.h" |
15 | | #include "object-name.h" |
16 | | #include "read-cache.h" |
17 | | #include "revision.h" |
18 | | #include "cache-tree.h" |
19 | | #include "unpack-trees.h" |
20 | | #include "refs.h" |
21 | | #include "repository.h" |
22 | | #include "submodule.h" |
23 | | #include "symlinks.h" |
24 | | #include "trace.h" |
25 | | #include "dir.h" |
26 | | #include "fsmonitor.h" |
27 | | #include "commit-reach.h" |
28 | | |
29 | | /* |
30 | | * diff-files |
31 | | */ |
32 | | |
33 | | /* |
34 | | * Has the work tree entity been removed? |
35 | | * |
36 | | * Return 1 if it was removed from the work tree, 0 if an entity to be |
37 | | * compared with the cache entry ce still exists (the latter includes |
38 | | * the case where a directory that is not a submodule repository |
39 | | * exists for ce that is a submodule -- it is a submodule that is not |
40 | | * checked out). Return negative for an error. |
41 | | */ |
42 | | static int check_removed(const struct cache_entry *ce, struct stat *st) |
43 | 0 | { |
44 | 0 | if (ce->ce_flags & CE_FSMONITOR_VALID) |
45 | 0 | return fake_lstat(ce, st); |
46 | | |
47 | 0 | if (lstat(ce->name, st) < 0) { |
48 | 0 | if (!is_missing_file_error(errno)) |
49 | 0 | return -1; |
50 | 0 | return 1; |
51 | 0 | } |
52 | | |
53 | 0 | if (has_symlink_leading_path(ce->name, ce_namelen(ce))) |
54 | 0 | return 1; |
55 | 0 | if (S_ISDIR(st->st_mode)) { |
56 | 0 | struct object_id sub; |
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 | 0 | if (!S_ISGITLINK(ce->ce_mode) && |
70 | 0 | repo_resolve_gitlink_ref(the_repository, ce->name, |
71 | 0 | "HEAD", &sub)) |
72 | 0 | return 1; |
73 | 0 | } |
74 | 0 | return 0; |
75 | 0 | } |
76 | | |
77 | | /* |
78 | | * Has a file changed or has a submodule new commits or a dirty work tree? |
79 | | * |
80 | | * Return 1 when changes are detected, 0 otherwise. If the DIRTY_SUBMODULES |
81 | | * option is set, the caller does not only want to know if a submodule is |
82 | | * modified at all but wants to know all the conditions that are met (new |
83 | | * commits, untracked content and/or modified content). |
84 | | */ |
85 | | static int match_stat_with_submodule(struct diff_options *diffopt, |
86 | | const struct cache_entry *ce, |
87 | | struct stat *st, unsigned ce_option, |
88 | | unsigned *dirty_submodule) |
89 | 0 | { |
90 | 0 | int changed = ie_match_stat(diffopt->repo->index, ce, st, ce_option); |
91 | 0 | if (S_ISGITLINK(ce->ce_mode)) { |
92 | 0 | struct diff_flags orig_flags = diffopt->flags; |
93 | 0 | if (!diffopt->flags.override_submodule_config) |
94 | 0 | set_diffopt_flags_from_submodule_config(diffopt, ce->name); |
95 | 0 | if (diffopt->flags.ignore_submodules) |
96 | 0 | changed = 0; |
97 | 0 | else if (!diffopt->flags.ignore_dirty_submodules && |
98 | 0 | (!changed || diffopt->flags.dirty_submodules)) |
99 | 0 | *dirty_submodule = is_submodule_modified(ce->name, |
100 | 0 | diffopt->flags.ignore_untracked_in_submodules); |
101 | 0 | diffopt->flags = orig_flags; |
102 | 0 | } |
103 | 0 | return changed; |
104 | 0 | } |
105 | | |
106 | | void run_diff_files(struct rev_info *revs, unsigned int option) |
107 | 0 | { |
108 | 0 | int entries, i; |
109 | 0 | int diff_unmerged_stage = revs->max_count; |
110 | 0 | unsigned ce_option = ((option & DIFF_RACY_IS_MODIFIED) |
111 | 0 | ? CE_MATCH_RACY_IS_DIRTY : 0); |
112 | 0 | uint64_t start = getnanotime(); |
113 | 0 | struct index_state *istate = revs->diffopt.repo->index; |
114 | |
|
115 | 0 | if (revs->diffopt.max_depth_valid) |
116 | 0 | die(_("max-depth is not supported for worktree diffs")); |
117 | | |
118 | 0 | diff_set_mnemonic_prefix(&revs->diffopt, "i/", "w/"); |
119 | |
|
120 | 0 | refresh_fsmonitor(istate); |
121 | |
|
122 | 0 | if (diff_unmerged_stage < 0) |
123 | 0 | diff_unmerged_stage = 2; |
124 | 0 | entries = istate->cache_nr; |
125 | 0 | for (i = 0; i < entries; i++) { |
126 | 0 | unsigned int oldmode, newmode; |
127 | 0 | struct cache_entry *ce = istate->cache[i]; |
128 | 0 | int changed; |
129 | 0 | unsigned dirty_submodule = 0; |
130 | 0 | const struct object_id *old_oid, *new_oid; |
131 | |
|
132 | 0 | if (diff_can_quit_early(&revs->diffopt)) |
133 | 0 | break; |
134 | | |
135 | | /* |
136 | | * NEEDSWORK: |
137 | | * Here we filter with pathspec but the result is further |
138 | | * filtered out when --relative is in effect. To end-users, |
139 | | * a pathspec element that matched only to paths outside the |
140 | | * current directory is like not matching anything at all; |
141 | | * the handling of ps_matched[] here may become problematic |
142 | | * if/when we add the "--error-unmatch" option to "git diff". |
143 | | */ |
144 | 0 | if (!ce_path_match(istate, ce, &revs->prune_data, revs->ps_matched)) |
145 | 0 | continue; |
146 | | |
147 | 0 | if (revs->diffopt.prefix && |
148 | 0 | strncmp(ce->name, revs->diffopt.prefix, revs->diffopt.prefix_length)) |
149 | 0 | continue; |
150 | | |
151 | 0 | if (ce_stage(ce)) { |
152 | 0 | struct combine_diff_path *dpath; |
153 | 0 | struct diff_filepair *pair; |
154 | 0 | unsigned int wt_mode = 0; |
155 | 0 | int num_compare_stages = 0; |
156 | 0 | struct stat st; |
157 | |
|
158 | 0 | changed = check_removed(ce, &st); |
159 | 0 | if (!changed) |
160 | 0 | wt_mode = ce_mode_from_stat(revs->repo, ce, st.st_mode); |
161 | 0 | else { |
162 | 0 | if (changed < 0) { |
163 | 0 | perror(ce->name); |
164 | 0 | continue; |
165 | 0 | } |
166 | 0 | wt_mode = 0; |
167 | 0 | } |
168 | | |
169 | | /* |
170 | | * Allocate space for two parents, which will come from |
171 | | * index stages #2 and #3, if present. Below we'll fill |
172 | | * these from (stage - 2). |
173 | | */ |
174 | 0 | dpath = combine_diff_path_new(ce->name, ce_namelen(ce), |
175 | 0 | wt_mode, null_oid(the_hash_algo), 2); |
176 | |
|
177 | 0 | while (i < entries) { |
178 | 0 | struct cache_entry *nce = istate->cache[i]; |
179 | 0 | int stage; |
180 | |
|
181 | 0 | if (strcmp(ce->name, nce->name)) |
182 | 0 | break; |
183 | | |
184 | | /* Stage #2 (ours) is the first parent, |
185 | | * stage #3 (theirs) is the second. |
186 | | */ |
187 | 0 | stage = ce_stage(nce); |
188 | 0 | if (2 <= stage) { |
189 | 0 | int mode = nce->ce_mode; |
190 | 0 | num_compare_stages++; |
191 | 0 | oidcpy(&dpath->parent[stage - 2].oid, |
192 | 0 | &nce->oid); |
193 | 0 | dpath->parent[stage-2].mode = ce_mode_from_stat(revs->repo, nce, mode); |
194 | 0 | dpath->parent[stage-2].status = |
195 | 0 | DIFF_STATUS_MODIFIED; |
196 | 0 | } |
197 | | |
198 | | /* diff against the proper unmerged stage */ |
199 | 0 | if (stage == diff_unmerged_stage) |
200 | 0 | ce = nce; |
201 | 0 | i++; |
202 | 0 | } |
203 | | /* |
204 | | * Compensate for loop update |
205 | | */ |
206 | 0 | i--; |
207 | |
|
208 | 0 | if (revs->combine_merges && num_compare_stages == 2) { |
209 | 0 | show_combined_diff(dpath, 2, revs); |
210 | 0 | free(dpath); |
211 | 0 | continue; |
212 | 0 | } |
213 | 0 | FREE_AND_NULL(dpath); |
214 | | |
215 | | /* |
216 | | * Show the diff for the 'ce' if we found the one |
217 | | * from the desired stage. |
218 | | */ |
219 | 0 | pair = diff_unmerge(&revs->diffopt, ce->name); |
220 | 0 | if (wt_mode) |
221 | 0 | pair->two->mode = wt_mode; |
222 | 0 | if (ce_stage(ce) != diff_unmerged_stage) |
223 | 0 | continue; |
224 | 0 | } |
225 | | |
226 | 0 | if (ce_uptodate(ce) || ce_skip_worktree(ce)) { |
227 | 0 | if (revs->diffopt.flags.find_copies_harder) |
228 | 0 | diff_same(&revs->diffopt, ce->ce_mode, |
229 | 0 | &ce->oid, ce->name); |
230 | 0 | continue; |
231 | 0 | } |
232 | | |
233 | | /* |
234 | | * When CE_VALID is set (via "update-index --assume-unchanged" |
235 | | * or via adding paths while core.ignorestat is set to true), |
236 | | * the user has promised that the working tree file for that |
237 | | * path will not be modified. When CE_FSMONITOR_VALID is true, |
238 | | * the fsmonitor knows that the path hasn't been modified since |
239 | | * we refreshed the cached stat information. In either case, |
240 | | * we do not have to stat to see if the path has been removed |
241 | | * or modified. |
242 | | */ |
243 | 0 | if (ce->ce_flags & (CE_VALID | CE_FSMONITOR_VALID)) { |
244 | 0 | changed = 0; |
245 | 0 | newmode = ce->ce_mode; |
246 | 0 | } else { |
247 | 0 | struct stat st; |
248 | |
|
249 | 0 | changed = check_removed(ce, &st); |
250 | 0 | if (changed) { |
251 | 0 | if (changed < 0) { |
252 | 0 | perror(ce->name); |
253 | 0 | continue; |
254 | 0 | } |
255 | 0 | diff_addremove(&revs->diffopt, '-', ce->ce_mode, |
256 | 0 | &ce->oid, |
257 | 0 | !is_null_oid(&ce->oid), |
258 | 0 | ce->name, 0); |
259 | 0 | continue; |
260 | 0 | } else if (revs->diffopt.ita_invisible_in_index && |
261 | 0 | ce_intent_to_add(ce)) { |
262 | 0 | newmode = ce_mode_from_stat(revs->repo, ce, st.st_mode); |
263 | 0 | diff_addremove(&revs->diffopt, '+', newmode, |
264 | 0 | null_oid(the_hash_algo), 0, ce->name, 0); |
265 | 0 | continue; |
266 | 0 | } |
267 | | |
268 | 0 | changed = match_stat_with_submodule(&revs->diffopt, ce, &st, |
269 | 0 | ce_option, &dirty_submodule); |
270 | 0 | newmode = ce_mode_from_stat(revs->repo, ce, st.st_mode); |
271 | 0 | } |
272 | | |
273 | 0 | if (!changed && !dirty_submodule) { |
274 | 0 | ce_mark_uptodate(ce); |
275 | 0 | mark_fsmonitor_valid(istate, ce); |
276 | 0 | if (revs->diffopt.flags.find_copies_harder) |
277 | 0 | diff_same(&revs->diffopt, newmode, |
278 | 0 | &ce->oid, ce->name); |
279 | 0 | continue; |
280 | 0 | } |
281 | 0 | oldmode = ce->ce_mode; |
282 | 0 | old_oid = &ce->oid; |
283 | 0 | new_oid = changed ? null_oid(the_hash_algo) : &ce->oid; |
284 | 0 | diff_change(&revs->diffopt, oldmode, newmode, |
285 | 0 | old_oid, new_oid, |
286 | 0 | !is_null_oid(old_oid), |
287 | 0 | !is_null_oid(new_oid), |
288 | 0 | ce->name, 0, dirty_submodule); |
289 | |
|
290 | 0 | } |
291 | 0 | diffcore_std(&revs->diffopt); |
292 | 0 | diff_flush(&revs->diffopt); |
293 | 0 | trace_performance_since(start, "diff-files"); |
294 | 0 | } |
295 | | |
296 | | /* |
297 | | * diff-index |
298 | | */ |
299 | | |
300 | | /* A file entry went away or appeared */ |
301 | | static void diff_index_show_file(struct rev_info *revs, |
302 | | const char *prefix, |
303 | | const struct cache_entry *ce, |
304 | | const struct object_id *oid, int oid_valid, |
305 | | unsigned int mode, |
306 | | unsigned dirty_submodule) |
307 | 0 | { |
308 | 0 | diff_addremove(&revs->diffopt, prefix[0], mode, |
309 | 0 | oid, oid_valid, ce->name, dirty_submodule); |
310 | 0 | } |
311 | | |
312 | | static int get_stat_data(const struct cache_entry *ce, |
313 | | const struct object_id **oidp, |
314 | | unsigned int *modep, |
315 | | int cached, int match_missing, |
316 | | unsigned *dirty_submodule, struct diff_options *diffopt) |
317 | 0 | { |
318 | 0 | const struct object_id *oid = &ce->oid; |
319 | 0 | unsigned int mode = ce->ce_mode; |
320 | |
|
321 | 0 | if (!cached && !ce_uptodate(ce)) { |
322 | 0 | int changed; |
323 | 0 | struct stat st; |
324 | 0 | changed = check_removed(ce, &st); |
325 | 0 | if (changed < 0) |
326 | 0 | return -1; |
327 | 0 | else if (changed) { |
328 | 0 | if (match_missing) { |
329 | 0 | *oidp = oid; |
330 | 0 | *modep = mode; |
331 | 0 | return 0; |
332 | 0 | } |
333 | 0 | return -1; |
334 | 0 | } |
335 | 0 | changed = match_stat_with_submodule(diffopt, ce, &st, |
336 | 0 | 0, dirty_submodule); |
337 | 0 | if (changed) { |
338 | 0 | mode = ce_mode_from_stat(diffopt->repo, ce, st.st_mode); |
339 | 0 | oid = null_oid(the_hash_algo); |
340 | 0 | } |
341 | 0 | } |
342 | | |
343 | 0 | *oidp = oid; |
344 | 0 | *modep = mode; |
345 | 0 | return 0; |
346 | 0 | } |
347 | | |
348 | | static void show_new_file(struct rev_info *revs, |
349 | | const struct cache_entry *new_file, |
350 | | int cached, int match_missing) |
351 | 0 | { |
352 | 0 | const struct object_id *oid; |
353 | 0 | unsigned int mode; |
354 | 0 | unsigned dirty_submodule = 0; |
355 | |
|
356 | 0 | if (new_file && S_ISSPARSEDIR(new_file->ce_mode)) { |
357 | 0 | diff_tree_oid(NULL, &new_file->oid, new_file->name, &revs->diffopt); |
358 | 0 | return; |
359 | 0 | } |
360 | | |
361 | | /* |
362 | | * New file in the index: it might actually be different in |
363 | | * the working tree. |
364 | | */ |
365 | 0 | if (get_stat_data(new_file, &oid, &mode, cached, match_missing, |
366 | 0 | &dirty_submodule, &revs->diffopt) < 0) |
367 | 0 | return; |
368 | | |
369 | 0 | diff_index_show_file(revs, "+", new_file, oid, !is_null_oid(oid), mode, dirty_submodule); |
370 | 0 | } |
371 | | |
372 | | static int show_modified(struct rev_info *revs, |
373 | | const struct cache_entry *old_entry, |
374 | | const struct cache_entry *new_entry, |
375 | | int report_missing, |
376 | | int cached, int match_missing) |
377 | 0 | { |
378 | 0 | unsigned int mode, oldmode; |
379 | 0 | const struct object_id *oid; |
380 | 0 | unsigned dirty_submodule = 0; |
381 | |
|
382 | 0 | assert(S_ISSPARSEDIR(old_entry->ce_mode) == |
383 | 0 | S_ISSPARSEDIR(new_entry->ce_mode)); |
384 | | |
385 | | /* |
386 | | * If both are sparse directory entries, then expand the |
387 | | * modifications to the file level. If only one was a sparse |
388 | | * directory, then they appear as an add and delete instead of |
389 | | * a modification. |
390 | | */ |
391 | 0 | if (S_ISSPARSEDIR(new_entry->ce_mode)) { |
392 | 0 | diff_tree_oid(&old_entry->oid, &new_entry->oid, new_entry->name, &revs->diffopt); |
393 | 0 | return 0; |
394 | 0 | } |
395 | | |
396 | 0 | if (get_stat_data(new_entry, &oid, &mode, cached, match_missing, |
397 | 0 | &dirty_submodule, &revs->diffopt) < 0) { |
398 | 0 | if (report_missing) |
399 | 0 | diff_index_show_file(revs, "-", old_entry, |
400 | 0 | &old_entry->oid, 1, old_entry->ce_mode, |
401 | 0 | 0); |
402 | 0 | return -1; |
403 | 0 | } |
404 | | |
405 | 0 | if (revs->combine_merges && !cached && |
406 | 0 | (!oideq(oid, &old_entry->oid) || !oideq(&old_entry->oid, &new_entry->oid))) { |
407 | 0 | struct combine_diff_path *p; |
408 | |
|
409 | 0 | p = combine_diff_path_new(new_entry->name, |
410 | 0 | ce_namelen(new_entry), |
411 | 0 | mode, null_oid(the_hash_algo), 2); |
412 | 0 | p->parent[0].status = DIFF_STATUS_MODIFIED; |
413 | 0 | p->parent[0].mode = new_entry->ce_mode; |
414 | 0 | oidcpy(&p->parent[0].oid, &new_entry->oid); |
415 | 0 | p->parent[1].status = DIFF_STATUS_MODIFIED; |
416 | 0 | p->parent[1].mode = old_entry->ce_mode; |
417 | 0 | oidcpy(&p->parent[1].oid, &old_entry->oid); |
418 | 0 | show_combined_diff(p, 2, revs); |
419 | 0 | free(p); |
420 | 0 | return 0; |
421 | 0 | } |
422 | | |
423 | 0 | oldmode = old_entry->ce_mode; |
424 | 0 | if (mode != oldmode || !oideq(oid, &old_entry->oid) || dirty_submodule) |
425 | 0 | diff_change(&revs->diffopt, oldmode, mode, |
426 | 0 | &old_entry->oid, oid, 1, !is_null_oid(oid), |
427 | 0 | old_entry->name, 0, dirty_submodule); |
428 | 0 | else if (revs->diffopt.flags.find_copies_harder) |
429 | 0 | diff_same(&revs->diffopt, mode, oid, old_entry->name); |
430 | 0 | return 0; |
431 | 0 | } |
432 | | |
433 | | /* |
434 | | * This gets a mix of an existing index and a tree, one pathname entry |
435 | | * at a time. The index entry may be a single stage-0 one, but it could |
436 | | * also be multiple unmerged entries (in which case idx_pos/idx_nr will |
437 | | * give you the position and number of entries in the index). |
438 | | */ |
439 | | static void do_oneway_diff(struct unpack_trees_options *o, |
440 | | const struct cache_entry *idx, |
441 | | const struct cache_entry *tree) |
442 | 0 | { |
443 | 0 | struct rev_info *revs = o->unpack_data; |
444 | 0 | int match_missing, cached; |
445 | | |
446 | | /* |
447 | | * i-t-a entries do not actually exist in the index (if we're |
448 | | * looking at its content) |
449 | | */ |
450 | 0 | if (o->index_only && |
451 | 0 | revs->diffopt.ita_invisible_in_index && |
452 | 0 | idx && ce_intent_to_add(idx)) { |
453 | 0 | idx = NULL; |
454 | 0 | if (!tree) |
455 | 0 | return; /* nothing to diff.. */ |
456 | 0 | } |
457 | | |
458 | | /* if the entry is not checked out, don't examine work tree */ |
459 | 0 | cached = o->index_only || |
460 | 0 | (idx && ((idx->ce_flags & CE_VALID) || ce_skip_worktree(idx))); |
461 | |
|
462 | 0 | match_missing = revs->match_missing; |
463 | |
|
464 | 0 | if (cached && idx && ce_stage(idx)) { |
465 | 0 | struct diff_filepair *pair; |
466 | 0 | pair = diff_unmerge(&revs->diffopt, idx->name); |
467 | 0 | if (pair && tree) |
468 | 0 | fill_filespec(pair->one, &tree->oid, 1, |
469 | 0 | tree->ce_mode); |
470 | 0 | return; |
471 | 0 | } |
472 | | |
473 | | /* |
474 | | * Something added to the tree? |
475 | | */ |
476 | 0 | if (!tree) { |
477 | 0 | show_new_file(revs, idx, cached, match_missing); |
478 | 0 | return; |
479 | 0 | } |
480 | | |
481 | | /* |
482 | | * Something removed from the tree? |
483 | | */ |
484 | 0 | if (!idx) { |
485 | 0 | if (S_ISSPARSEDIR(tree->ce_mode)) { |
486 | 0 | diff_tree_oid(&tree->oid, NULL, tree->name, &revs->diffopt); |
487 | 0 | return; |
488 | 0 | } |
489 | | |
490 | 0 | diff_index_show_file(revs, "-", tree, &tree->oid, 1, |
491 | 0 | tree->ce_mode, 0); |
492 | 0 | return; |
493 | 0 | } |
494 | | |
495 | | /* Show difference between old and new */ |
496 | 0 | show_modified(revs, tree, idx, 1, cached, match_missing); |
497 | 0 | } |
498 | | |
499 | | /* |
500 | | * The unpack_trees() interface is designed for merging, so |
501 | | * the different source entries are designed primarily for |
502 | | * the source trees, with the old index being really mainly |
503 | | * used for being replaced by the result. |
504 | | * |
505 | | * For diffing, the index is more important, and we only have a |
506 | | * single tree. |
507 | | * |
508 | | * This wrapper makes it all more readable, and takes care of all |
509 | | * the fairly complex unpack_trees() semantic requirements, including |
510 | | * the path matching, the type conflict cases etc. |
511 | | */ |
512 | | static int oneway_diff(const struct cache_entry * const *src, |
513 | | struct unpack_trees_options *o) |
514 | 0 | { |
515 | 0 | const struct cache_entry *idx = src[0]; |
516 | 0 | const struct cache_entry *tree = src[1]; |
517 | 0 | struct rev_info *revs = o->unpack_data; |
518 | | |
519 | | /* |
520 | | * Unpack-trees generates a DF/conflict entry if |
521 | | * there was a directory in the index and a tree |
522 | | * in the tree. From a diff standpoint, that's a |
523 | | * delete of the tree and a create of the file. |
524 | | */ |
525 | 0 | if (tree == o->df_conflict_entry) |
526 | 0 | tree = NULL; |
527 | | |
528 | | /* |
529 | | * We should only see a NULL idx when the entry was present in the tree |
530 | | * but deleted in the idx. In which case it should be impossible |
531 | | * that a NULL tree was passed in (there would have been no entry at |
532 | | * all) or that we got a df conflict above (you need a directory and a |
533 | | * file to get such a conflict, which implies both sides are present). |
534 | | */ |
535 | 0 | if (!idx && !tree) |
536 | 0 | BUG("oneway_diff with neither idx nor tree"); |
537 | | |
538 | 0 | if (revs->diffopt.prefix && |
539 | 0 | strncmp((idx ? idx : tree)->name, revs->diffopt.prefix, |
540 | 0 | revs->diffopt.prefix_length)) |
541 | 0 | return 0; |
542 | | |
543 | 0 | if (ce_path_match(revs->diffopt.repo->index, |
544 | 0 | idx ? idx : tree, |
545 | 0 | &revs->prune_data, NULL)) { |
546 | 0 | do_oneway_diff(o, idx, tree); |
547 | 0 | if (diff_can_quit_early(&revs->diffopt)) { |
548 | 0 | o->exiting_early = 1; |
549 | 0 | return -1; |
550 | 0 | } |
551 | 0 | } |
552 | | |
553 | 0 | return 0; |
554 | 0 | } |
555 | | |
556 | | static int diff_cache(struct rev_info *revs, |
557 | | const struct object_id *tree_oid, |
558 | | const char *tree_name, |
559 | | int cached) |
560 | 0 | { |
561 | 0 | struct tree *tree; |
562 | 0 | struct tree_desc t; |
563 | 0 | struct unpack_trees_options opts; |
564 | |
|
565 | 0 | tree = repo_parse_tree_indirect(the_repository, tree_oid); |
566 | 0 | if (!tree) |
567 | 0 | return error("bad tree object %s", |
568 | 0 | tree_name ? tree_name : oid_to_hex(tree_oid)); |
569 | 0 | memset(&opts, 0, sizeof(opts)); |
570 | 0 | opts.head_idx = 1; |
571 | 0 | opts.index_only = cached; |
572 | 0 | opts.diff_index_cached = (cached && |
573 | 0 | !revs->diffopt.flags.find_copies_harder); |
574 | 0 | opts.merge = 1; |
575 | 0 | opts.fn = oneway_diff; |
576 | 0 | opts.unpack_data = revs; |
577 | 0 | opts.src_index = revs->diffopt.repo->index; |
578 | 0 | opts.dst_index = NULL; |
579 | 0 | opts.pathspec = &revs->diffopt.pathspec; |
580 | 0 | opts.pathspec->recursive = 1; |
581 | 0 | if (revs->diffopt.max_depth_valid) |
582 | 0 | die(_("max-depth is not supported for index diffs")); |
583 | | |
584 | 0 | init_tree_desc(&t, &tree->object.oid, tree->buffer, tree->size); |
585 | 0 | return unpack_trees(1, &t, &opts); |
586 | 0 | } |
587 | | |
588 | | void diff_get_merge_base(const struct rev_info *revs, struct object_id *mb) |
589 | 0 | { |
590 | 0 | int i; |
591 | 0 | struct commit *mb_child[2] = {0}; |
592 | 0 | struct commit_list *merge_bases = NULL; |
593 | |
|
594 | 0 | for (i = 0; i < revs->pending.nr; i++) { |
595 | 0 | struct object *obj = revs->pending.objects[i].item; |
596 | 0 | if (obj->flags) |
597 | 0 | die(_("--merge-base does not work with ranges")); |
598 | 0 | } |
599 | | |
600 | | /* |
601 | | * This check must go after the for loop above because A...B |
602 | | * ranges produce three pending commits, resulting in a |
603 | | * misleading error message. |
604 | | */ |
605 | 0 | if (revs->pending.nr < 1 || revs->pending.nr > 2) |
606 | 0 | BUG("unexpected revs->pending.nr: %d", revs->pending.nr); |
607 | | |
608 | 0 | for (i = 0; i < revs->pending.nr; i++) |
609 | 0 | mb_child[i] = lookup_commit_reference(the_repository, &revs->pending.objects[i].item->oid); |
610 | 0 | if (revs->pending.nr == 1) { |
611 | 0 | struct object_id oid; |
612 | |
|
613 | 0 | if (repo_get_oid(the_repository, "HEAD", &oid)) |
614 | 0 | die(_("unable to get HEAD")); |
615 | | |
616 | 0 | mb_child[1] = lookup_commit_reference(the_repository, &oid); |
617 | 0 | } |
618 | | |
619 | 0 | if (repo_get_merge_bases(the_repository, mb_child[0], mb_child[1], &merge_bases) < 0) |
620 | 0 | exit(128); |
621 | 0 | if (!merge_bases) |
622 | 0 | die(_("no merge base found")); |
623 | 0 | if (merge_bases->next) |
624 | 0 | die(_("multiple merge bases found")); |
625 | | |
626 | 0 | oidcpy(mb, &merge_bases->item->object.oid); |
627 | |
|
628 | 0 | commit_list_free(merge_bases); |
629 | 0 | } |
630 | | |
631 | | void run_diff_index(struct rev_info *revs, unsigned int option) |
632 | 0 | { |
633 | 0 | struct object_array_entry *ent; |
634 | 0 | int cached = !!(option & DIFF_INDEX_CACHED); |
635 | 0 | int merge_base = !!(option & DIFF_INDEX_MERGE_BASE); |
636 | 0 | struct object_id oid; |
637 | 0 | const char *name; |
638 | 0 | char merge_base_hex[GIT_MAX_HEXSZ + 1]; |
639 | 0 | struct index_state *istate = revs->diffopt.repo->index; |
640 | |
|
641 | 0 | if (revs->pending.nr != 1) |
642 | 0 | BUG("run_diff_index must be passed exactly one tree"); |
643 | | |
644 | 0 | trace_performance_enter(); |
645 | 0 | ent = revs->pending.objects; |
646 | |
|
647 | 0 | refresh_fsmonitor(istate); |
648 | |
|
649 | 0 | if (merge_base) { |
650 | 0 | diff_get_merge_base(revs, &oid); |
651 | 0 | name = oid_to_hex_r(merge_base_hex, &oid); |
652 | 0 | } else { |
653 | 0 | oidcpy(&oid, &ent->item->oid); |
654 | 0 | name = ent->name; |
655 | 0 | } |
656 | |
|
657 | 0 | if (diff_cache(revs, &oid, name, cached)) |
658 | 0 | exit(128); |
659 | | |
660 | 0 | diff_set_mnemonic_prefix(&revs->diffopt, "c/", cached ? "i/" : "w/"); |
661 | 0 | diffcore_fix_diff_index(); |
662 | 0 | diffcore_std(&revs->diffopt); |
663 | 0 | diff_flush(&revs->diffopt); |
664 | 0 | trace_performance_leave("diff-index"); |
665 | 0 | } |
666 | | |
667 | | int do_diff_cache(const struct object_id *tree_oid, struct diff_options *opt) |
668 | 0 | { |
669 | 0 | struct rev_info revs; |
670 | |
|
671 | 0 | repo_init_revisions(opt->repo, &revs, NULL); |
672 | 0 | copy_pathspec(&revs.prune_data, &opt->pathspec); |
673 | 0 | diff_free(&revs.diffopt); |
674 | 0 | revs.diffopt = *opt; |
675 | 0 | revs.diffopt.no_free = 1; |
676 | |
|
677 | 0 | if (diff_cache(&revs, tree_oid, NULL, 1)) |
678 | 0 | exit(128); |
679 | | |
680 | 0 | release_revisions(&revs); |
681 | 0 | return 0; |
682 | 0 | } |
683 | | |
684 | | int index_differs_from(struct repository *r, |
685 | | const char *def, const struct diff_flags *flags, |
686 | | int ita_invisible_in_index) |
687 | 0 | { |
688 | 0 | struct rev_info rev; |
689 | 0 | struct setup_revision_opt opt; |
690 | 0 | unsigned has_changes; |
691 | |
|
692 | 0 | repo_init_revisions(r, &rev, NULL); |
693 | 0 | memset(&opt, 0, sizeof(opt)); |
694 | 0 | opt.def = def; |
695 | 0 | setup_revisions(0, NULL, &rev, &opt); |
696 | 0 | rev.diffopt.flags.quick = 1; |
697 | 0 | rev.diffopt.flags.exit_with_status = 1; |
698 | 0 | if (flags) { |
699 | 0 | diff_flags_or(&rev.diffopt.flags, flags); |
700 | | /* |
701 | | * Now that flags are merged, honor override_submodule_config |
702 | | * and ignore_submodules from passed flags. |
703 | | */ |
704 | 0 | if (flags->override_submodule_config) |
705 | 0 | rev.diffopt.flags.ignore_submodules = flags->ignore_submodules; |
706 | 0 | } |
707 | 0 | rev.diffopt.ita_invisible_in_index = ita_invisible_in_index; |
708 | 0 | run_diff_index(&rev, DIFF_INDEX_CACHED); |
709 | 0 | has_changes = rev.diffopt.flags.has_changes; |
710 | 0 | release_revisions(&rev); |
711 | 0 | return (has_changes != 0); |
712 | 0 | } |
713 | | |
714 | | static const char *idiff_prefix_cb(struct diff_options *opt UNUSED, void *data) |
715 | 0 | { |
716 | 0 | return data; |
717 | 0 | } |
718 | | |
719 | | void show_interdiff(const struct object_id *oid1, const struct object_id *oid2, |
720 | | int indent, struct diff_options *diffopt) |
721 | 0 | { |
722 | 0 | struct diff_options opts; |
723 | 0 | struct strbuf prefix = STRBUF_INIT; |
724 | |
|
725 | 0 | memcpy(&opts, diffopt, sizeof(opts)); |
726 | 0 | opts.output_format = DIFF_FORMAT_PATCH; |
727 | 0 | opts.output_prefix = idiff_prefix_cb; |
728 | 0 | strbuf_addchars(&prefix, ' ', indent); |
729 | 0 | opts.output_prefix_data = prefix.buf; |
730 | 0 | diff_setup_done(&opts); |
731 | |
|
732 | 0 | diff_tree_oid(oid1, oid2, "", &opts); |
733 | 0 | diffcore_std(&opts); |
734 | 0 | diff_flush(&opts); |
735 | |
|
736 | 0 | strbuf_release(&prefix); |
737 | 0 | } |