blob: 0e9ec4f68afb01f23d9b9a0142d2ffb2cd1cc79b [file] [log] [blame]
Junio C Hamanobe3cfa82005-04-26 09:25:05 -07001/*
2 * Copyright (C) 2005 Junio C Hamano
3 */
Elijah Newrenbc5c5ec2023-05-16 06:33:57 +00004#include "git-compat-util.h"
Junio C Hamano6fb737b2005-07-07 23:58:32 -07005#include "quote.h"
Junio C Hamano6973dca2006-04-21 23:57:45 -07006#include "commit.h"
Junio C Hamano86436c22005-04-25 18:22:47 -07007#include "diff.h"
Junio C Hamano427dcb42005-05-21 02:39:09 -07008#include "diffcore.h"
Elijah Newrenf394e092023-03-21 06:25:54 +00009#include "gettext.h"
Elijah Newrendf6e8742023-05-16 06:34:00 +000010#include "hash.h"
Elijah Newren41771fa2023-02-24 00:09:27 +000011#include "hex.h"
Elijah Newrendabab1d2023-04-11 00:41:49 -070012#include "object-name.h"
Elijah Newren08c46a42023-05-16 06:33:56 +000013#include "read-cache.h"
Junio C Hamano6973dca2006-04-21 23:57:45 -070014#include "revision.h"
Junio C Hamano1cfe7732007-01-30 01:11:08 -080015#include "cache-tree.h"
Linus Torvaldsd1f2d7e2008-01-19 17:27:12 -080016#include "unpack-trees.h"
Junio C Hamano948dd342008-03-30 17:29:48 -070017#include "refs.h"
Elijah Newrendf6e8742023-05-16 06:34:00 +000018#include "repository.h"
Jens Lehmannee6fc512010-01-16 18:42:24 +010019#include "submodule.h"
Elijah Newrencb2a5132023-04-22 20:17:09 +000020#include "symlinks.h"
Elijah Newren74ea5c92023-04-11 03:00:38 +000021#include "trace.h"
Nguyễn Thái Ngọc Duy429bb402014-01-24 20:40:28 +070022#include "dir.h"
Ben Peart883e2482017-09-22 12:35:40 -040023#include "fsmonitor.h"
Denton Liu177a8302020-09-20 04:22:23 -070024#include "commit-reach.h"
Junio C Hamano427dcb42005-05-21 02:39:09 -070025
Junio C Hamanob46f0b62005-05-04 01:45:24 -070026/*
Junio C Hamano6973dca2006-04-21 23:57:45 -070027 * diff-files
Junio C Hamanob46f0b62005-05-04 01:45:24 -070028 */
Junio C Hamano6973dca2006-04-21 23:57:45 -070029
Junio C Hamano948dd342008-03-30 17:29:48 -070030/*
Junio C Hamano1392a372008-05-03 17:04:42 -070031 * 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.
Junio C Hamano948dd342008-03-30 17:29:48 -070038 */
Josip Sokcevic6a044a22023-09-11 10:09:02 -070039static int check_removed(const struct cache_entry *ce, struct stat *st)
Junio C Hamano948dd342008-03-30 17:29:48 -070040{
Josip Sokcevic6a044a22023-09-11 10:09:02 -070041 if (lstat(ce->name, st) < 0) {
Junio C Hamanoc7054202017-05-30 09:23:33 +090042 if (!is_missing_file_error(errno))
Junio C Hamano948dd342008-03-30 17:29:48 -070043 return -1;
44 return 1;
45 }
Josip Sokcevic6a044a22023-09-11 10:09:02 -070046
Kjetil Barvik57199892009-02-09 21:54:06 +010047 if (has_symlink_leading_path(ce->name, ce_namelen(ce)))
Junio C Hamano948dd342008-03-30 17:29:48 -070048 return 1;
49 if (S_ISDIR(st->st_mode)) {
brian m. carlson1053fe82017-10-15 22:07:06 +000050 struct object_id sub;
Junio C Hamano1392a372008-05-03 17:04:42 -070051
52 /*
53 * If ce is already a gitlink, we can have a plain
54 * directory (i.e. the submodule is not checked out),
55 * or a checked out submodule. Either case this is not
56 * a case where something was removed from the work tree,
57 * so we will return 0.
58 *
59 * Otherwise, if the directory is not a submodule
60 * repository, that means ce which was a blob turned into
61 * a directory --- the blob was removed!
62 */
63 if (!S_ISGITLINK(ce->ce_mode) &&
brian m. carlsona98e6102017-10-15 22:07:07 +000064 resolve_gitlink_ref(ce->name, "HEAD", &sub))
Junio C Hamano948dd342008-03-30 17:29:48 -070065 return 1;
66 }
67 return 0;
68}
Johannes Schindelind516c2d2007-02-22 21:50:10 +010069
Jens Lehmannae6d5c12010-03-11 22:50:25 +010070/*
71 * Has a file changed or has a submodule new commits or a dirty work tree?
72 *
73 * Return 1 when changes are detected, 0 otherwise. If the DIRTY_SUBMODULES
74 * option is set, the caller does not only want to know if a submodule is
75 * modified at all but wants to know all the conditions that are met (new
76 * commits, untracked content and/or modified content).
77 */
78static int match_stat_with_submodule(struct diff_options *diffopt,
René Scharfeeb9ae4b2013-06-02 17:46:55 +020079 const struct cache_entry *ce,
80 struct stat *st, unsigned ce_option,
81 unsigned *dirty_submodule)
Jens Lehmannae6d5c12010-03-11 22:50:25 +010082{
Nguyễn Thái Ngọc Duy5adbb402018-09-21 17:57:26 +020083 int changed = ie_match_stat(diffopt->repo->index, ce, st, ce_option);
Jens Lehmannaee9c7d2010-08-06 00:39:25 +020084 if (S_ISGITLINK(ce->ce_mode)) {
Brandon Williams02f2f562017-10-31 11:19:05 -070085 struct diff_flags orig_flags = diffopt->flags;
Brandon Williams0d1e0e72017-10-31 11:19:11 -070086 if (!diffopt->flags.override_submodule_config)
Jens Lehmannaee9c7d2010-08-06 00:39:25 +020087 set_diffopt_flags_from_submodule_config(diffopt, ce->name);
Brandon Williams0d1e0e72017-10-31 11:19:11 -070088 if (diffopt->flags.ignore_submodules)
Jens Lehmannaee9c7d2010-08-06 00:39:25 +020089 changed = 0;
Brandon Williams0d1e0e72017-10-31 11:19:11 -070090 else if (!diffopt->flags.ignore_dirty_submodules &&
91 (!changed || diffopt->flags.dirty_submodules))
Brandon Williams3b69dae2017-10-31 11:19:08 -070092 *dirty_submodule = is_submodule_modified(ce->name,
Brandon Williams0d1e0e72017-10-31 11:19:11 -070093 diffopt->flags.ignore_untracked_in_submodules);
Jens Lehmannaee9c7d2010-08-06 00:39:25 +020094 diffopt->flags = orig_flags;
Jens Lehmannae6d5c12010-03-11 22:50:25 +010095 }
96 return changed;
97}
98
Jeff King25bd3ac2023-08-21 16:18:55 -040099void run_diff_files(struct rev_info *revs, unsigned int option)
Junio C Hamanob46f0b62005-05-04 01:45:24 -0700100{
Junio C Hamano6973dca2006-04-21 23:57:45 -0700101 int entries, i;
102 int diff_unmerged_stage = revs->max_count;
Junio C Hamanofb63d7f2007-11-09 18:22:52 -0800103 unsigned ce_option = ((option & DIFF_RACY_IS_MODIFIED)
104 ? CE_MATCH_RACY_IS_DIRTY : 0);
Nguyễn Thái Ngọc Duyca54d9b2018-01-27 19:27:56 +0700105 uint64_t start = getnanotime();
Nguyễn Thái Ngọc Duy5adbb402018-09-21 17:57:26 +0200106 struct index_state *istate = revs->diffopt.repo->index;
Junio C Hamano5c975582005-05-19 03:32:35 -0700107
Junio C Hamanoa5a818e2008-08-18 20:08:09 -0700108 diff_set_mnemonic_prefix(&revs->diffopt, "i/", "w/");
109
Alex Vandiverc9052a82020-10-20 13:40:58 +0000110 refresh_fsmonitor(istate);
111
Junio C Hamano6973dca2006-04-21 23:57:45 -0700112 if (diff_unmerged_stage < 0)
113 diff_unmerged_stage = 2;
Nguyễn Thái Ngọc Duy5adbb402018-09-21 17:57:26 +0200114 entries = istate->cache_nr;
Junio C Hamano6973dca2006-04-21 23:57:45 -0700115 for (i = 0; i < entries; i++) {
Junio C Hamano6973dca2006-04-21 23:57:45 -0700116 unsigned int oldmode, newmode;
Nguyễn Thái Ngọc Duy5adbb402018-09-21 17:57:26 +0200117 struct cache_entry *ce = istate->cache[i];
Junio C Hamano6973dca2006-04-21 23:57:45 -0700118 int changed;
Jens Lehmanne3d42c42010-01-18 21:26:18 +0100119 unsigned dirty_submodule = 0;
Brandon Williams55497b82017-05-30 10:30:48 -0700120 const struct object_id *old_oid, *new_oid;
Junio C Hamanof0c6b2a2005-05-27 15:56:38 -0700121
Junio C Hamano28b92642011-05-31 09:14:17 -0700122 if (diff_can_quit_early(&revs->diffopt))
Junio C Hamano822cac02007-03-14 11:12:51 -0700123 break;
124
Nguyễn Thái Ngọc Duy5adbb402018-09-21 17:57:26 +0200125 if (!ce_path_match(istate, ce, &revs->prune_data, NULL))
Linus Torvalds8676eb42006-02-26 15:51:24 -0800126 continue;
Linus Torvalds8676eb42006-02-26 15:51:24 -0800127
Đoàn Trần Công Danh81746272021-08-22 15:49:08 +0700128 if (revs->diffopt.prefix &&
129 strncmp(ce->name, revs->diffopt.prefix, revs->diffopt.prefix_length))
130 continue;
131
Junio C Hamano6973dca2006-04-21 23:57:45 -0700132 if (ce_stage(ce)) {
Florian Forsterb4b15502006-06-18 17:18:05 +0200133 struct combine_diff_path *dpath;
Junio C Hamano095ce952011-04-22 16:19:27 -0700134 struct diff_filepair *pair;
135 unsigned int wt_mode = 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -0700136 int num_compare_stages = 0;
Florian Forsterb4b15502006-06-18 17:18:05 +0200137 size_t path_len;
Jeff King53048102014-05-14 18:13:06 -0400138 struct stat st;
Linus Torvalds8676eb42006-02-26 15:51:24 -0800139
Florian Forsterb4b15502006-06-18 17:18:05 +0200140 path_len = ce_namelen(ce);
141
Junio C Hamano4fc970c2007-02-25 22:24:47 -0800142 dpath = xmalloc(combine_diff_path_size(5, path_len));
Florian Forsterb4b15502006-06-18 17:18:05 +0200143 dpath->path = (char *) &(dpath->parent[5]);
144
145 dpath->next = NULL;
Florian Forsterb4b15502006-06-18 17:18:05 +0200146 memcpy(dpath->path, ce->name, path_len);
147 dpath->path[path_len] = '\0';
brian m. carlson1ff57c12015-03-13 23:39:33 +0000148 oidclr(&dpath->oid);
Florian Forsterb4b15502006-06-18 17:18:05 +0200149 memset(&(dpath->parent[0]), 0,
Junio C Hamano4fc970c2007-02-25 22:24:47 -0800150 sizeof(struct combine_diff_parent)*5);
151
Josip Sokcevic6a044a22023-09-11 10:09:02 -0700152 changed = check_removed(ce, &st);
Junio C Hamanof58dbf22008-03-30 17:30:08 -0700153 if (!changed)
Junio C Hamano095ce952011-04-22 16:19:27 -0700154 wt_mode = ce_mode_from_stat(ce, st.st_mode);
Junio C Hamanof58dbf22008-03-30 17:30:08 -0700155 else {
156 if (changed < 0) {
Junio C Hamano4fc970c2007-02-25 22:24:47 -0800157 perror(ce->name);
158 continue;
159 }
Junio C Hamano095ce952011-04-22 16:19:27 -0700160 wt_mode = 0;
Junio C Hamano4fc970c2007-02-25 22:24:47 -0800161 }
Junio C Hamano095ce952011-04-22 16:19:27 -0700162 dpath->mode = wt_mode;
Junio C Hamanobe3cfa82005-04-26 09:25:05 -0700163
Junio C Hamano6973dca2006-04-21 23:57:45 -0700164 while (i < entries) {
Nguyễn Thái Ngọc Duy5adbb402018-09-21 17:57:26 +0200165 struct cache_entry *nce = istate->cache[i];
Junio C Hamano6973dca2006-04-21 23:57:45 -0700166 int stage;
Junio C Hamanobe3cfa82005-04-26 09:25:05 -0700167
Junio C Hamano6973dca2006-04-21 23:57:45 -0700168 if (strcmp(ce->name, nce->name))
169 break;
Junio C Hamanobe3cfa82005-04-26 09:25:05 -0700170
Junio C Hamano6973dca2006-04-21 23:57:45 -0700171 /* Stage #2 (ours) is the first parent,
172 * stage #3 (theirs) is the second.
173 */
174 stage = ce_stage(nce);
175 if (2 <= stage) {
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800176 int mode = nce->ce_mode;
Junio C Hamano6973dca2006-04-21 23:57:45 -0700177 num_compare_stages++;
brian m. carlson99d1a982016-09-05 20:07:52 +0000178 oidcpy(&dpath->parent[stage - 2].oid,
179 &nce->oid);
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800180 dpath->parent[stage-2].mode = ce_mode_from_stat(nce, mode);
Florian Forsterb4b15502006-06-18 17:18:05 +0200181 dpath->parent[stage-2].status =
Junio C Hamano6973dca2006-04-21 23:57:45 -0700182 DIFF_STATUS_MODIFIED;
183 }
Junio C Hamanocebff982006-03-25 23:12:17 -0800184
Junio C Hamano6973dca2006-04-21 23:57:45 -0700185 /* diff against the proper unmerged stage */
186 if (stage == diff_unmerged_stage)
187 ce = nce;
188 i++;
H. Peter Anvin1b1480f2005-11-21 14:17:12 -0800189 }
Junio C Hamano6973dca2006-04-21 23:57:45 -0700190 /*
191 * Compensate for loop update
192 */
193 i--;
Junio C Hamano0e3994f2005-06-03 01:37:54 -0700194
Junio C Hamano6973dca2006-04-21 23:57:45 -0700195 if (revs->combine_merges && num_compare_stages == 2) {
Sergey Organovd01141d2020-09-29 14:31:22 +0300196 show_combined_diff(dpath, 2, revs);
Florian Forsterb4b15502006-06-18 17:18:05 +0200197 free(dpath);
Junio C Hamano15d061b2005-05-27 15:55:55 -0700198 continue;
199 }
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000200 FREE_AND_NULL(dpath);
Junio C Hamano6973dca2006-04-21 23:57:45 -0700201
202 /*
203 * Show the diff for the 'ce' if we found the one
204 * from the desired stage.
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700205 */
Junio C Hamano095ce952011-04-22 16:19:27 -0700206 pair = diff_unmerge(&revs->diffopt, ce->name);
207 if (wt_mode)
208 pair->two->mode = wt_mode;
Junio C Hamano6973dca2006-04-21 23:57:45 -0700209 if (ce_stage(ce) != diff_unmerged_stage)
210 continue;
211 }
212
Junio C Hamano125fd982010-01-24 00:10:20 -0800213 if (ce_uptodate(ce) || ce_skip_worktree(ce))
Linus Torvaldsd1f2d7e2008-01-19 17:27:12 -0800214 continue;
Junio C Hamanof58dbf22008-03-30 17:30:08 -0700215
Alex Vandiverc9052a82020-10-20 13:40:58 +0000216 /*
217 * When CE_VALID is set (via "update-index --assume-unchanged"
218 * or via adding paths while core.ignorestat is set to true),
219 * the user has promised that the working tree file for that
220 * path will not be modified. When CE_FSMONITOR_VALID is true,
221 * the fsmonitor knows that the path hasn't been modified since
222 * we refreshed the cached stat information. In either case,
223 * we do not have to stat to see if the path has been removed
224 * or modified.
225 */
226 if (ce->ce_flags & (CE_VALID | CE_FSMONITOR_VALID)) {
Jeff King53048102014-05-14 18:13:06 -0400227 changed = 0;
228 newmode = ce->ce_mode;
229 } else {
230 struct stat st;
231
Josip Sokcevic6a044a22023-09-11 10:09:02 -0700232 changed = check_removed(ce, &st);
Jeff King53048102014-05-14 18:13:06 -0400233 if (changed) {
234 if (changed < 0) {
235 perror(ce->name);
236 continue;
237 }
238 diff_addremove(&revs->diffopt, '-', ce->ce_mode,
Brandon Williamsc26022e2017-05-30 10:30:47 -0700239 &ce->oid,
brian m. carlson99d1a982016-09-05 20:07:52 +0000240 !is_null_oid(&ce->oid),
Jeff King53048102014-05-14 18:13:06 -0400241 ce->name, 0);
Junio C Hamano6973dca2006-04-21 23:57:45 -0700242 continue;
Nguyễn Thái Ngọc Duy425a28e2016-10-24 17:42:19 +0700243 } else if (revs->diffopt.ita_invisible_in_index &&
244 ce_intent_to_add(ce)) {
Raymond E. Pascocb0dd222020-08-08 03:53:23 -0400245 newmode = ce_mode_from_stat(ce, st.st_mode);
246 diff_addremove(&revs->diffopt, '+', newmode,
brian m. carlson14228442021-04-26 01:02:56 +0000247 null_oid(), 0, ce->name, 0);
Nguyễn Thái Ngọc Duy425a28e2016-10-24 17:42:19 +0700248 continue;
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700249 }
Jeff King53048102014-05-14 18:13:06 -0400250
251 changed = match_stat_with_submodule(&revs->diffopt, ce, &st,
252 ce_option, &dirty_submodule);
253 newmode = ce_mode_from_stat(ce, st.st_mode);
Junio C Hamanobceafe72005-05-23 18:14:03 -0700254 }
Jeff King53048102014-05-14 18:13:06 -0400255
Jens Lehmann85adbf22010-03-12 22:23:52 +0100256 if (!changed && !dirty_submodule) {
Junio C Hamano8fa29602008-03-30 12:39:25 -0700257 ce_mark_uptodate(ce);
Johannes Schindelinb5a81692019-05-24 05:23:48 -0700258 mark_fsmonitor_valid(istate, ce);
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700259 if (!revs->diffopt.flags.find_copies_harder)
Junio C Hamano8fa29602008-03-30 12:39:25 -0700260 continue;
261 }
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800262 oldmode = ce->ce_mode;
Brandon Williams55497b82017-05-30 10:30:48 -0700263 old_oid = &ce->oid;
brian m. carlson14228442021-04-26 01:02:56 +0000264 new_oid = changed ? null_oid() : &ce->oid;
Junio C Hamano6973dca2006-04-21 23:57:45 -0700265 diff_change(&revs->diffopt, oldmode, newmode,
Brandon Williams94a00972017-05-30 10:30:49 -0700266 old_oid, new_oid,
Brandon Williams55497b82017-05-30 10:30:48 -0700267 !is_null_oid(old_oid),
268 !is_null_oid(new_oid),
Jens Lehmanne3d42c42010-01-18 21:26:18 +0100269 ce->name, 0, dirty_submodule);
Junio C Hamano6973dca2006-04-21 23:57:45 -0700270
Junio C Hamanobceafe72005-05-23 18:14:03 -0700271 }
Junio C Hamano6973dca2006-04-21 23:57:45 -0700272 diffcore_std(&revs->diffopt);
273 diff_flush(&revs->diffopt);
Nguyễn Thái Ngọc Duyca54d9b2018-01-27 19:27:56 +0700274 trace_performance_since(start, "diff-files");
Junio C Hamanobceafe72005-05-23 18:14:03 -0700275}
276
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -0700277/*
278 * diff-index
279 */
280
281/* A file entry went away or appeared */
282static void diff_index_show_file(struct rev_info *revs,
283 const char *prefix,
René Scharfeeb9ae4b2013-06-02 17:46:55 +0200284 const struct cache_entry *ce,
Brandon Williamsfcf2cfb2017-05-30 10:30:46 -0700285 const struct object_id *oid, int oid_valid,
Jeff Kinge5450102012-07-28 11:03:01 -0400286 unsigned int mode,
Jens Lehmanne3d42c42010-01-18 21:26:18 +0100287 unsigned dirty_submodule)
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -0700288{
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800289 diff_addremove(&revs->diffopt, prefix[0], mode,
Brandon Williamsc26022e2017-05-30 10:30:47 -0700290 oid, oid_valid, ce->name, dirty_submodule);
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -0700291}
292
Nipunn Koorapati0ec99492021-03-17 21:22:22 +0000293static int get_stat_data(const struct index_state *istate,
294 const struct cache_entry *ce,
Brandon Williams362d7652017-05-30 10:30:45 -0700295 const struct object_id **oidp,
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -0700296 unsigned int *modep,
Jens Lehmanne3d42c42010-01-18 21:26:18 +0100297 int cached, int match_missing,
Jens Lehmann4d344772010-01-23 17:37:26 +0100298 unsigned *dirty_submodule, struct diff_options *diffopt)
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -0700299{
Brandon Williams362d7652017-05-30 10:30:45 -0700300 const struct object_id *oid = &ce->oid;
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -0700301 unsigned int mode = ce->ce_mode;
302
Linus Torvalds658dd482009-05-09 14:57:30 -0700303 if (!cached && !ce_uptodate(ce)) {
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -0700304 int changed;
305 struct stat st;
Josip Sokcevic6a044a22023-09-11 10:09:02 -0700306 changed = check_removed(ce, &st);
Junio C Hamano948dd342008-03-30 17:29:48 -0700307 if (changed < 0)
308 return -1;
309 else if (changed) {
310 if (match_missing) {
Brandon Williams362d7652017-05-30 10:30:45 -0700311 *oidp = oid;
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -0700312 *modep = mode;
313 return 0;
314 }
315 return -1;
316 }
Jens Lehmannae6d5c12010-03-11 22:50:25 +0100317 changed = match_stat_with_submodule(diffopt, ce, &st,
318 0, dirty_submodule);
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -0700319 if (changed) {
Junio C Hamano185c9752007-02-16 22:43:48 -0800320 mode = ce_mode_from_stat(ce, st.st_mode);
brian m. carlson14228442021-04-26 01:02:56 +0000321 oid = null_oid();
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -0700322 }
323 }
324
Brandon Williams362d7652017-05-30 10:30:45 -0700325 *oidp = oid;
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -0700326 *modep = mode;
327 return 0;
328}
329
Kjetil Barvikff7e6aa2009-01-11 19:36:42 +0100330static void show_new_file(struct rev_info *revs,
Brandon Williamse36ede22018-02-14 10:59:38 -0800331 const struct cache_entry *new_file,
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -0700332 int cached, int match_missing)
333{
Brandon Williams362d7652017-05-30 10:30:45 -0700334 const struct object_id *oid;
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -0700335 unsigned int mode;
Jens Lehmanne3d42c42010-01-18 21:26:18 +0100336 unsigned dirty_submodule = 0;
Nipunn Koorapati0ec99492021-03-17 21:22:22 +0000337 struct index_state *istate = revs->diffopt.repo->index;
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -0700338
Derrick Stolee9eb00af2021-07-14 13:12:35 +0000339 if (new_file && S_ISSPARSEDIR(new_file->ce_mode)) {
340 diff_tree_oid(NULL, &new_file->oid, new_file->name, &revs->diffopt);
341 return;
342 }
343
Junio C Hamano948dd342008-03-30 17:29:48 -0700344 /*
345 * New file in the index: it might actually be different in
Carlos Martín Nietof7d650c2011-09-20 22:25:57 +0200346 * the working tree.
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -0700347 */
Nipunn Koorapati0ec99492021-03-17 21:22:22 +0000348 if (get_stat_data(istate, new_file, &oid, &mode, cached, match_missing,
Jens Lehmann4d344772010-01-23 17:37:26 +0100349 &dirty_submodule, &revs->diffopt) < 0)
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -0700350 return;
351
Brandon Williamse36ede22018-02-14 10:59:38 -0800352 diff_index_show_file(revs, "+", new_file, oid, !is_null_oid(oid), mode, dirty_submodule);
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -0700353}
354
Kjetil Barvikff7e6aa2009-01-11 19:36:42 +0100355static int show_modified(struct rev_info *revs,
Brandon Williamse36ede22018-02-14 10:59:38 -0800356 const struct cache_entry *old_entry,
357 const struct cache_entry *new_entry,
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -0700358 int report_missing,
359 int cached, int match_missing)
360{
361 unsigned int mode, oldmode;
Brandon Williams362d7652017-05-30 10:30:45 -0700362 const struct object_id *oid;
Jens Lehmanne3d42c42010-01-18 21:26:18 +0100363 unsigned dirty_submodule = 0;
Nipunn Koorapati0ec99492021-03-17 21:22:22 +0000364 struct index_state *istate = revs->diffopt.repo->index;
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -0700365
Derrick Stolee9eb00af2021-07-14 13:12:35 +0000366 assert(S_ISSPARSEDIR(old_entry->ce_mode) ==
367 S_ISSPARSEDIR(new_entry->ce_mode));
368
369 /*
370 * If both are sparse directory entries, then expand the
371 * modifications to the file level. If only one was a sparse
372 * directory, then they appear as an add and delete instead of
373 * a modification.
374 */
375 if (S_ISSPARSEDIR(new_entry->ce_mode)) {
376 diff_tree_oid(&old_entry->oid, &new_entry->oid, new_entry->name, &revs->diffopt);
377 return 0;
378 }
379
Nipunn Koorapati0ec99492021-03-17 21:22:22 +0000380 if (get_stat_data(istate, new_entry, &oid, &mode, cached, match_missing,
Jens Lehmann4d344772010-01-23 17:37:26 +0100381 &dirty_submodule, &revs->diffopt) < 0) {
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -0700382 if (report_missing)
Brandon Williamse36ede22018-02-14 10:59:38 -0800383 diff_index_show_file(revs, "-", old_entry,
384 &old_entry->oid, 1, old_entry->ce_mode,
brian m. carlson99d1a982016-09-05 20:07:52 +0000385 0);
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -0700386 return -1;
387 }
388
Paul Mackerrascb2b9f52006-09-04 21:38:40 +1000389 if (revs->combine_merges && !cached &&
Jeff King9001dc22018-08-28 17:22:48 -0400390 (!oideq(oid, &old_entry->oid) || !oideq(&old_entry->oid, &new_entry->oid))) {
Paul Mackerrascb2b9f52006-09-04 21:38:40 +1000391 struct combine_diff_path *p;
Brandon Williamse36ede22018-02-14 10:59:38 -0800392 int pathlen = ce_namelen(new_entry);
Paul Mackerrascb2b9f52006-09-04 21:38:40 +1000393
394 p = xmalloc(combine_diff_path_size(2, pathlen));
395 p->path = (char *) &p->parent[2];
396 p->next = NULL;
Brandon Williamse36ede22018-02-14 10:59:38 -0800397 memcpy(p->path, new_entry->name, pathlen);
Paul Mackerrascb2b9f52006-09-04 21:38:40 +1000398 p->path[pathlen] = 0;
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800399 p->mode = mode;
brian m. carlson1ff57c12015-03-13 23:39:33 +0000400 oidclr(&p->oid);
Paul Mackerrascb2b9f52006-09-04 21:38:40 +1000401 memset(p->parent, 0, 2 * sizeof(struct combine_diff_parent));
402 p->parent[0].status = DIFF_STATUS_MODIFIED;
Brandon Williamse36ede22018-02-14 10:59:38 -0800403 p->parent[0].mode = new_entry->ce_mode;
404 oidcpy(&p->parent[0].oid, &new_entry->oid);
Paul Mackerrascb2b9f52006-09-04 21:38:40 +1000405 p->parent[1].status = DIFF_STATUS_MODIFIED;
Brandon Williamse36ede22018-02-14 10:59:38 -0800406 p->parent[1].mode = old_entry->ce_mode;
407 oidcpy(&p->parent[1].oid, &old_entry->oid);
Sergey Organovd01141d2020-09-29 14:31:22 +0300408 show_combined_diff(p, 2, revs);
Paul Mackerrascb2b9f52006-09-04 21:38:40 +1000409 free(p);
410 return 0;
411 }
412
Brandon Williamse36ede22018-02-14 10:59:38 -0800413 oldmode = old_entry->ce_mode;
Jeff King4a7e27e2018-08-28 17:22:40 -0400414 if (mode == oldmode && oideq(oid, &old_entry->oid) && !dirty_submodule &&
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700415 !revs->diffopt.flags.find_copies_harder)
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -0700416 return 0;
417
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -0700418 diff_change(&revs->diffopt, oldmode, mode,
Brandon Williamse36ede22018-02-14 10:59:38 -0800419 &old_entry->oid, oid, 1, !is_null_oid(oid),
420 old_entry->name, 0, dirty_submodule);
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -0700421 return 0;
422}
423
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -0700424/*
Linus Torvaldsd1f2d7e2008-01-19 17:27:12 -0800425 * This gets a mix of an existing index and a tree, one pathname entry
426 * at a time. The index entry may be a single stage-0 one, but it could
427 * also be multiple unmerged entries (in which case idx_pos/idx_nr will
428 * give you the position and number of entries in the index).
429 */
430static void do_oneway_diff(struct unpack_trees_options *o,
René Scharfeeb9ae4b2013-06-02 17:46:55 +0200431 const struct cache_entry *idx,
432 const struct cache_entry *tree)
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -0700433{
Kjetil Barvikff7e6aa2009-01-11 19:36:42 +0100434 struct rev_info *revs = o->unpack_data;
Linus Torvaldsd1f2d7e2008-01-19 17:27:12 -0800435 int match_missing, cached;
Junio C Hamano5c21ac02006-04-22 03:58:04 -0700436
Nguyễn Thái Ngọc Duyba4e3562018-05-26 14:08:43 +0200437 /*
438 * i-t-a entries do not actually exist in the index (if we're
439 * looking at its content)
440 */
441 if (o->index_only &&
442 revs->diffopt.ita_invisible_in_index &&
Nguyễn Thái Ngọc Duy425a28e2016-10-24 17:42:19 +0700443 idx && ce_intent_to_add(idx)) {
444 idx = NULL;
445 if (!tree)
446 return; /* nothing to diff.. */
447 }
448
Nguyễn Thái Ngọc Duy540e6942009-08-11 22:43:59 +0700449 /* if the entry is not checked out, don't examine work tree */
Nguyễn Thái Ngọc Duyb4d16902009-08-20 20:46:58 +0700450 cached = o->index_only ||
451 (idx && ((idx->ce_flags & CE_VALID) || ce_skip_worktree(idx)));
Sergey Organov572fc9a2020-08-31 23:14:22 +0300452
453 match_missing = revs->match_missing;
Linus Torvaldsd1f2d7e2008-01-19 17:27:12 -0800454
455 if (cached && idx && ce_stage(idx)) {
Junio C Hamanofa7b2902011-04-22 16:05:58 -0700456 struct diff_filepair *pair;
457 pair = diff_unmerge(&revs->diffopt, idx->name);
Junio C Hamanoff00b682011-07-13 21:36:29 -0700458 if (tree)
Brandon Williamsf9704c22017-05-30 10:30:50 -0700459 fill_filespec(pair->one, &tree->oid, 1,
brian m. carlson99d1a982016-09-05 20:07:52 +0000460 tree->ce_mode);
Linus Torvaldsd1f2d7e2008-01-19 17:27:12 -0800461 return;
462 }
463
464 /*
465 * Something added to the tree?
466 */
467 if (!tree) {
Kjetil Barvikff7e6aa2009-01-11 19:36:42 +0100468 show_new_file(revs, idx, cached, match_missing);
Linus Torvaldsd1f2d7e2008-01-19 17:27:12 -0800469 return;
470 }
471
472 /*
473 * Something removed from the tree?
474 */
475 if (!idx) {
Victoria Dye56d8a272022-08-08 19:07:50 +0000476 if (S_ISSPARSEDIR(tree->ce_mode)) {
477 diff_tree_oid(&tree->oid, NULL, tree->name, &revs->diffopt);
478 return;
479 }
480
Brandon Williamsfcf2cfb2017-05-30 10:30:46 -0700481 diff_index_show_file(revs, "-", tree, &tree->oid, 1,
brian m. carlson99d1a982016-09-05 20:07:52 +0000482 tree->ce_mode, 0);
Linus Torvaldsd1f2d7e2008-01-19 17:27:12 -0800483 return;
484 }
485
486 /* Show difference between old and new */
Kjetil Barvikff7e6aa2009-01-11 19:36:42 +0100487 show_modified(revs, tree, idx, 1, cached, match_missing);
Linus Torvaldsd1f2d7e2008-01-19 17:27:12 -0800488}
489
490/*
Linus Torvaldsd1f2d7e2008-01-19 17:27:12 -0800491 * The unpack_trees() interface is designed for merging, so
492 * the different source entries are designed primarily for
493 * the source trees, with the old index being really mainly
494 * used for being replaced by the result.
495 *
496 * For diffing, the index is more important, and we only have a
497 * single tree.
498 *
Junio C Hamanoda8ba5e2009-09-17 22:12:17 -0700499 * We're supposed to advance o->pos to skip what we have already processed.
Linus Torvaldsd1f2d7e2008-01-19 17:27:12 -0800500 *
501 * This wrapper makes it all more readable, and takes care of all
502 * the fairly complex unpack_trees() semantic requirements, including
503 * the skipping, the path matching, the type conflict cases etc.
504 */
René Scharfe5828e832013-06-02 17:46:56 +0200505static int oneway_diff(const struct cache_entry * const *src,
506 struct unpack_trees_options *o)
Linus Torvaldsd1f2d7e2008-01-19 17:27:12 -0800507{
René Scharfeeb9ae4b2013-06-02 17:46:55 +0200508 const struct cache_entry *idx = src[0];
509 const struct cache_entry *tree = src[1];
Kjetil Barvikff7e6aa2009-01-11 19:36:42 +0100510 struct rev_info *revs = o->unpack_data;
Linus Torvaldsd1f2d7e2008-01-19 17:27:12 -0800511
Linus Torvaldsd1f2d7e2008-01-19 17:27:12 -0800512 /*
513 * Unpack-trees generates a DF/conflict entry if
514 * there was a directory in the index and a tree
515 * in the tree. From a diff standpoint, that's a
516 * delete of the tree and a create of the file.
517 */
518 if (tree == o->df_conflict_entry)
519 tree = NULL;
520
Nguyễn Thái Ngọc Duy5adbb402018-09-21 17:57:26 +0200521 if (ce_path_match(revs->diffopt.repo->index,
522 idx ? idx : tree,
523 &revs->prune_data, NULL)) {
Linus Torvalds34110cd2008-03-06 18:12:28 -0800524 do_oneway_diff(o, idx, tree);
Junio C Hamanob4194822011-05-31 10:06:44 -0700525 if (diff_can_quit_early(&revs->diffopt)) {
526 o->exiting_early = 1;
527 return -1;
528 }
529 }
Linus Torvaldsd1f2d7e2008-01-19 17:27:12 -0800530
Linus Torvalds34110cd2008-03-06 18:12:28 -0800531 return 0;
Linus Torvaldsd1f2d7e2008-01-19 17:27:12 -0800532}
533
Junio C Hamanobf979c02011-07-13 18:57:42 -0700534static int diff_cache(struct rev_info *revs,
brian m. carlson944cffb2017-05-06 22:10:35 +0000535 const struct object_id *tree_oid,
Junio C Hamanobf979c02011-07-13 18:57:42 -0700536 const char *tree_name,
537 int cached)
Linus Torvaldsd1f2d7e2008-01-19 17:27:12 -0800538{
Linus Torvaldsd1f2d7e2008-01-19 17:27:12 -0800539 struct tree *tree;
Linus Torvaldsd1f2d7e2008-01-19 17:27:12 -0800540 struct tree_desc t;
Junio C Hamanobf979c02011-07-13 18:57:42 -0700541 struct unpack_trees_options opts;
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -0700542
brian m. carlsona9dbc172017-05-06 22:10:37 +0000543 tree = parse_tree_indirect(tree_oid);
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -0700544 if (!tree)
Junio C Hamanobf979c02011-07-13 18:57:42 -0700545 return error("bad tree object %s",
brian m. carlson944cffb2017-05-06 22:10:35 +0000546 tree_name ? tree_name : oid_to_hex(tree_oid));
Linus Torvaldsd1f2d7e2008-01-19 17:27:12 -0800547 memset(&opts, 0, sizeof(opts));
548 opts.head_idx = 1;
549 opts.index_only = cached;
Junio C Hamanoa0919ce2009-05-22 23:14:25 -0700550 opts.diff_index_cached = (cached &&
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700551 !revs->diffopt.flags.find_copies_harder);
Linus Torvaldsd1f2d7e2008-01-19 17:27:12 -0800552 opts.merge = 1;
553 opts.fn = oneway_diff;
Kjetil Barvikff7e6aa2009-01-11 19:36:42 +0100554 opts.unpack_data = revs;
Nguyễn Thái Ngọc Duy5adbb402018-09-21 17:57:26 +0200555 opts.src_index = revs->diffopt.repo->index;
Linus Torvalds34110cd2008-03-06 18:12:28 -0800556 opts.dst_index = NULL;
Junio C Hamano2f88c192011-08-29 13:34:08 -0700557 opts.pathspec = &revs->diffopt.pathspec;
Nguyen Thai Ngoc Duy48382372012-01-15 17:03:27 +0700558 opts.pathspec->recursive = 1;
Linus Torvaldsd1f2d7e2008-01-19 17:27:12 -0800559
560 init_tree_desc(&t, tree->buffer, tree->size);
Junio C Hamanobf979c02011-07-13 18:57:42 -0700561 return unpack_trees(1, &t, &opts);
562}
563
Denton Liu177a8302020-09-20 04:22:23 -0700564void diff_get_merge_base(const struct rev_info *revs, struct object_id *mb)
565{
566 int i;
567 struct commit *mb_child[2] = {0};
568 struct commit_list *merge_bases;
569
570 for (i = 0; i < revs->pending.nr; i++) {
571 struct object *obj = revs->pending.objects[i].item;
572 if (obj->flags)
573 die(_("--merge-base does not work with ranges"));
Denton Liu177a8302020-09-20 04:22:23 -0700574 }
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
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +0200589 if (repo_get_oid(the_repository, "HEAD", &oid))
Denton Liu177a8302020-09-20 04:22:23 -0700590 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
Jeff King25bd3ac2023-08-21 16:18:55 -0400606void run_diff_index(struct rev_info *revs, unsigned int option)
Junio C Hamanobf979c02011-07-13 18:57:42 -0700607{
608 struct object_array_entry *ent;
Denton Liu4c3fe822020-09-20 04:22:22 -0700609 int cached = !!(option & DIFF_INDEX_CACHED);
Denton Liu0f5a1d42020-09-20 04:22:25 -0700610 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];
Nipunn Koorapati4f3d6d02021-03-17 21:22:21 +0000614 struct index_state *istate = revs->diffopt.repo->index;
Junio C Hamanobf979c02011-07-13 18:57:42 -0700615
Jeff King3506dc92018-07-11 10:14:06 -0400616 if (revs->pending.nr != 1)
617 BUG("run_diff_index must be passed exactly one tree");
618
Nguyễn Thái Ngọc Duyc46c4062018-08-18 16:41:22 +0200619 trace_performance_enter();
Junio C Hamanobf979c02011-07-13 18:57:42 -0700620 ent = revs->pending.objects;
Denton Liu0f5a1d42020-09-20 04:22:25 -0700621
Nipunn Koorapati4f3d6d02021-03-17 21:22:21 +0000622 refresh_fsmonitor(istate);
623
Denton Liu0f5a1d42020-09-20 04:22:25 -0700624 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))
Daniel Barkalow203a2fe2008-02-07 11:39:48 -0500633 exit(128);
Linus Torvaldsd1f2d7e2008-01-19 17:27:12 -0800634
Junio C Hamanoa5a818e2008-08-18 20:08:09 -0700635 diff_set_mnemonic_prefix(&revs->diffopt, "c/", cached ? "i/" : "w/");
Jeff King784c0da2019-02-14 00:48:03 -0500636 diffcore_fix_diff_index();
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -0700637 diffcore_std(&revs->diffopt);
638 diff_flush(&revs->diffopt);
Nguyễn Thái Ngọc Duyc46c4062018-08-18 16:41:22 +0200639 trace_performance_leave("diff-index");
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -0700640}
Junio C Hamano1cfe7732007-01-30 01:11:08 -0800641
brian m. carlson944cffb2017-05-06 22:10:35 +0000642int do_diff_cache(const struct object_id *tree_oid, struct diff_options *opt)
Junio C Hamano1cfe7732007-01-30 01:11:08 -0800643{
Junio C Hamano1cfe7732007-01-30 01:11:08 -0800644 struct rev_info revs;
Junio C Hamano1cfe7732007-01-30 01:11:08 -0800645
Nguyễn Thái Ngọc Duyffc00a42018-11-10 06:49:04 +0100646 repo_init_revisions(opt->repo, &revs, NULL);
Nguyễn Thái Ngọc Duy9a087272013-07-14 15:35:59 +0700647 copy_pathspec(&revs.prune_data, &opt->pathspec);
René Scharfed44e5262020-11-14 19:37:03 +0100648 diff_setup_done(&revs.diffopt);
Junio C Hamanobf979c02011-07-13 18:57:42 -0700649 revs.diffopt = *opt;
Johannes Schindelin204ce972008-01-20 15:19:56 +0000650
brian m. carlson944cffb2017-05-06 22:10:35 +0000651 if (diff_cache(&revs, tree_oid, NULL, 1))
Daniel Barkalow203a2fe2008-02-07 11:39:48 -0500652 exit(128);
Ævar Arnfjörð Bjarmasonf0cb6b82022-04-13 22:01:44 +0200653 release_revisions(&revs);
Johannes Schindelin204ce972008-01-20 15:19:56 +0000654 return 0;
Junio C Hamano1cfe7732007-01-30 01:11:08 -0800655}
Stephan Beyer75f3ff22009-02-10 15:30:35 +0100656
Nguyễn Thái Ngọc Duyffc00a42018-11-10 06:49:04 +0100657int index_differs_from(struct repository *r,
658 const char *def, const struct diff_flags *flags,
Nguyễn Thái Ngọc Duy018ec3c2016-10-24 17:42:21 +0700659 int ita_invisible_in_index)
Stephan Beyer75f3ff22009-02-10 15:30:35 +0100660{
661 struct rev_info rev;
Junio C Hamano32962c92010-03-08 22:58:09 -0800662 struct setup_revision_opt opt;
Ævar Arnfjörð Bjarmason54c8a7c2022-04-14 07:56:40 +0200663 unsigned has_changes;
Stephan Beyer75f3ff22009-02-10 15:30:35 +0100664
Nguyễn Thái Ngọc Duyffc00a42018-11-10 06:49:04 +0100665 repo_init_revisions(r, &rev, NULL);
Junio C Hamano32962c92010-03-08 22:58:09 -0800666 memset(&opt, 0, sizeof(opt));
667 opt.def = def;
668 setup_revisions(0, NULL, &rev, &opt);
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700669 rev.diffopt.flags.quick = 1;
670 rev.diffopt.flags.exit_with_status = 1;
Josip Sokcevic57684782023-06-14 09:48:57 -0700671 if (flags) {
Brandon Williams02f2f562017-10-31 11:19:05 -0700672 diff_flags_or(&rev.diffopt.flags, flags);
Josip Sokcevic57684782023-06-14 09:48:57 -0700673 /*
674 * Now that flags are merged, honor override_submodule_config
675 * and ignore_submodules from passed flags.
676 */
677 if (flags->override_submodule_config)
678 rev.diffopt.flags.ignore_submodules = flags->ignore_submodules;
679 }
Nguyễn Thái Ngọc Duy018ec3c2016-10-24 17:42:21 +0700680 rev.diffopt.ita_invisible_in_index = ita_invisible_in_index;
Junio C Hamano976b97e2023-08-21 16:14:14 -0400681 run_diff_index(&rev, DIFF_INDEX_CACHED);
Ævar Arnfjörð Bjarmason54c8a7c2022-04-14 07:56:40 +0200682 has_changes = rev.diffopt.flags.has_changes;
Ævar Arnfjörð Bjarmason1878b5e2022-04-13 22:01:35 +0200683 release_revisions(&rev);
Ævar Arnfjörð Bjarmason54c8a7c2022-04-14 07:56:40 +0200684 return (has_changes != 0);
Stephan Beyer75f3ff22009-02-10 15:30:35 +0100685}
Eric Sunshinecdffbdc2020-09-08 03:16:08 -0400686
Jeff King61bdc7c2022-12-13 06:13:48 -0500687static struct strbuf *idiff_prefix_cb(struct diff_options *opt UNUSED, void *data)
Eric Sunshinecdffbdc2020-09-08 03:16:08 -0400688{
689 return data;
690}
691
Eric Sunshine72a72392020-09-08 03:16:09 -0400692void show_interdiff(const struct object_id *oid1, const struct object_id *oid2,
693 int indent, struct diff_options *diffopt)
Eric Sunshinecdffbdc2020-09-08 03:16:08 -0400694{
695 struct diff_options opts;
696 struct strbuf prefix = STRBUF_INIT;
697
Eric Sunshine72a72392020-09-08 03:16:09 -0400698 memcpy(&opts, diffopt, sizeof(opts));
Eric Sunshinecdffbdc2020-09-08 03:16:08 -0400699 opts.output_format = DIFF_FORMAT_PATCH;
700 opts.output_prefix = idiff_prefix_cb;
701 strbuf_addchars(&prefix, ' ', indent);
702 opts.output_prefix_data = &prefix;
703 diff_setup_done(&opts);
704
Eric Sunshine72a72392020-09-08 03:16:09 -0400705 diff_tree_oid(oid1, oid2, "", &opts);
Eric Sunshinecdffbdc2020-09-08 03:16:08 -0400706 diffcore_std(&opts);
707 diff_flush(&opts);
708
709 strbuf_release(&prefix);
710}