2 * Copyright (C) 2005 Junio C Hamano
10 #include "cache-tree.h"
16 int run_diff_files(struct rev_info *revs, int silent_on_removed)
19 int diff_unmerged_stage = revs->max_count;
21 if (diff_unmerged_stage < 0)
22 diff_unmerged_stage = 2;
24 for (i = 0; i < entries; i++) {
26 unsigned int oldmode, newmode;
27 struct cache_entry *ce = active_cache[i];
30 if (!ce_path_match(ce, revs->prune_data))
34 struct combine_diff_path *dpath;
35 int num_compare_stages = 0;
38 path_len = ce_namelen(ce);
40 dpath = xmalloc (combine_diff_path_size (5, path_len));
41 dpath->path = (char *) &(dpath->parent[5]);
44 dpath->len = path_len;
45 memcpy(dpath->path, ce->name, path_len);
46 dpath->path[path_len] = '\0';
49 memset(&(dpath->parent[0]), 0,
50 sizeof(struct combine_diff_parent)*5);
53 struct cache_entry *nce = active_cache[i];
56 if (strcmp(ce->name, nce->name))
59 /* Stage #2 (ours) is the first parent,
60 * stage #3 (theirs) is the second.
62 stage = ce_stage(nce);
64 int mode = ntohl(nce->ce_mode);
66 hashcpy(dpath->parent[stage-2].sha1, nce->sha1);
67 dpath->parent[stage-2].mode =
69 dpath->parent[stage-2].status =
73 /* diff against the proper unmerged stage */
74 if (stage == diff_unmerged_stage)
79 * Compensate for loop update
83 if (revs->combine_merges && num_compare_stages == 2) {
84 show_combined_diff(dpath, 2,
85 revs->dense_combined_merges,
94 * Show the diff for the 'ce' if we found the one
95 * from the desired stage.
97 diff_unmerge(&revs->diffopt, ce->name, 0, null_sha1);
98 if (ce_stage(ce) != diff_unmerged_stage)
102 if (lstat(ce->name, &st) < 0) {
103 if (errno != ENOENT && errno != ENOTDIR) {
107 if (silent_on_removed)
109 diff_addremove(&revs->diffopt, '-', ntohl(ce->ce_mode),
110 ce->sha1, ce->name, NULL);
113 changed = ce_match_stat(ce, &st, 0);
114 if (!changed && !revs->diffopt.find_copies_harder)
116 oldmode = ntohl(ce->ce_mode);
118 newmode = canon_mode(st.st_mode);
119 if (!trust_executable_bit &&
120 S_ISREG(newmode) && S_ISREG(oldmode) &&
121 ((newmode ^ oldmode) == 0111))
123 diff_change(&revs->diffopt, oldmode, newmode,
124 ce->sha1, (changed ? null_sha1 : ce->sha1),
128 diffcore_std(&revs->diffopt);
129 diff_flush(&revs->diffopt);
137 /* A file entry went away or appeared */
138 static void diff_index_show_file(struct rev_info *revs,
140 struct cache_entry *ce,
141 unsigned char *sha1, unsigned int mode)
143 diff_addremove(&revs->diffopt, prefix[0], ntohl(mode),
144 sha1, ce->name, NULL);
147 static int get_stat_data(struct cache_entry *ce,
148 unsigned char **sha1p,
150 int cached, int match_missing)
152 unsigned char *sha1 = ce->sha1;
153 unsigned int mode = ce->ce_mode;
156 static unsigned char no_sha1[20];
159 if (lstat(ce->name, &st) < 0) {
160 if (errno == ENOENT && match_missing) {
167 changed = ce_match_stat(ce, &st, 0);
169 mode = create_ce_mode(st.st_mode);
170 if (!trust_executable_bit && S_ISREG(st.st_mode))
181 static void show_new_file(struct rev_info *revs,
182 struct cache_entry *new,
183 int cached, int match_missing)
188 /* New file in the index: it might actually be different in
191 if (get_stat_data(new, &sha1, &mode, cached, match_missing) < 0)
194 diff_index_show_file(revs, "+", new, sha1, mode);
197 static int show_modified(struct rev_info *revs,
198 struct cache_entry *old,
199 struct cache_entry *new,
201 int cached, int match_missing)
203 unsigned int mode, oldmode;
206 if (get_stat_data(new, &sha1, &mode, cached, match_missing) < 0) {
208 diff_index_show_file(revs, "-", old,
209 old->sha1, old->ce_mode);
213 if (revs->combine_merges && !cached &&
214 (hashcmp(sha1, old->sha1) || hashcmp(old->sha1, new->sha1))) {
215 struct combine_diff_path *p;
216 int pathlen = ce_namelen(new);
218 p = xmalloc(combine_diff_path_size(2, pathlen));
219 p->path = (char *) &p->parent[2];
222 memcpy(p->path, new->name, pathlen);
223 p->path[pathlen] = 0;
224 p->mode = ntohl(mode);
226 memset(p->parent, 0, 2 * sizeof(struct combine_diff_parent));
227 p->parent[0].status = DIFF_STATUS_MODIFIED;
228 p->parent[0].mode = ntohl(new->ce_mode);
229 hashcpy(p->parent[0].sha1, new->sha1);
230 p->parent[1].status = DIFF_STATUS_MODIFIED;
231 p->parent[1].mode = ntohl(old->ce_mode);
232 hashcpy(p->parent[1].sha1, old->sha1);
233 show_combined_diff(p, 2, revs->dense_combined_merges, revs);
238 oldmode = old->ce_mode;
239 if (mode == oldmode && !hashcmp(sha1, old->sha1) &&
240 !revs->diffopt.find_copies_harder)
244 oldmode = ntohl(oldmode);
246 diff_change(&revs->diffopt, oldmode, mode,
247 old->sha1, sha1, old->name, NULL);
251 static int diff_cache(struct rev_info *revs,
252 struct cache_entry **ac, int entries,
253 const char **pathspec,
254 int cached, int match_missing)
257 struct cache_entry *ce = *ac;
258 int same = (entries > 1) && ce_same_name(ce, ac[1]);
260 if (!ce_path_match(ce, pathspec))
263 switch (ce_stage(ce)) {
265 /* No stage 1 entry? That means it's a new file */
267 show_new_file(revs, ce, cached, match_missing);
270 /* Show difference between old and new */
271 show_modified(revs, ac[1], ce, 1,
272 cached, match_missing);
275 /* No stage 3 (merge) entry?
276 * That means it's been deleted.
279 diff_index_show_file(revs, "-", ce,
280 ce->sha1, ce->ce_mode);
283 /* We come here with ce pointing at stage 1
284 * (original tree) and ac[1] pointing at stage
285 * 3 (unmerged). show-modified with
286 * report-missing set to false does not say the
287 * file is deleted but reports true if work
288 * tree does not have it, in which case we
289 * fall through to report the unmerged state.
290 * Otherwise, we show the differences between
291 * the original tree and the work tree.
294 !show_modified(revs, ce, ac[1], 0,
295 cached, match_missing))
297 diff_unmerge(&revs->diffopt, ce->name,
298 ntohl(ce->ce_mode), ce->sha1);
301 diff_unmerge(&revs->diffopt, ce->name,
306 die("impossible cache entry stage");
311 * Ignore all the different stages for this file,
312 * we've handled the relevant cases now.
317 } while (entries && ce_same_name(ce, ac[0]));
323 * This turns all merge entries into "stage 3". That guarantees that
324 * when we read in the new tree (into "stage 1"), we won't lose sight
325 * of the fact that we had unmerged entries.
327 static void mark_merge_entries(void)
330 for (i = 0; i < active_nr; i++) {
331 struct cache_entry *ce = active_cache[i];
334 ce->ce_flags |= htons(CE_STAGEMASK);
338 int run_diff_index(struct rev_info *revs, int cached)
343 const char *tree_name;
344 int match_missing = 0;
347 * Backward compatibility wart - "diff-index -m" does
348 * not mean "do not ignore merges", but totally different.
350 if (!revs->ignore_merges)
353 mark_merge_entries();
355 ent = revs->pending.objects[0].item;
356 tree_name = revs->pending.objects[0].name;
357 tree = parse_tree_indirect(ent->sha1);
359 return error("bad tree object %s", tree_name);
360 if (read_tree(tree, 1, revs->prune_data))
361 return error("unable to read tree object %s", tree_name);
362 ret = diff_cache(revs, active_cache, active_nr, revs->prune_data,
363 cached, match_missing);
364 diffcore_std(&revs->diffopt);
365 diff_flush(&revs->diffopt);
369 int do_diff_cache(const unsigned char *tree_sha1, struct diff_options *opt)
372 struct rev_info revs;
374 struct cache_entry **dst;
375 struct cache_entry *last = NULL;
378 * This is used by git-blame to run diff-cache internally;
379 * it potentially needs to repeatedly run this, so we will
380 * start by removing the higher order entries the last round
384 for (i = 0; i < active_nr; i++) {
385 struct cache_entry *ce = active_cache[i];
387 if (last && !strcmp(ce->name, last->name))
389 cache_tree_invalidate_path(active_cache_tree,
393 ce->ce_flags &= ~htons(CE_STAGEMASK);
397 active_nr = dst - active_cache;
399 init_revisions(&revs, NULL);
400 revs.prune_data = opt->paths;
401 tree = parse_tree_indirect(tree_sha1);
403 die("bad tree object %s", sha1_to_hex(tree_sha1));
404 if (read_tree(tree, 1, opt->paths))
405 return error("unable to read tree %s", sha1_to_hex(tree_sha1));
406 return diff_cache(&revs, active_cache, active_nr, revs.prune_data,