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;
23 entries
= read_cache();
28 for (i
= 0; i
< entries
; i
++) {
30 unsigned int oldmode
, newmode
;
31 struct cache_entry
*ce
= active_cache
[i
];
34 if (!ce_path_match(ce
, revs
->prune_data
))
38 struct combine_diff_path
*dpath
;
39 int num_compare_stages
= 0;
42 path_len
= ce_namelen(ce
);
44 dpath
= xmalloc (combine_diff_path_size (5, path_len
));
45 dpath
->path
= (char *) &(dpath
->parent
[5]);
48 dpath
->len
= path_len
;
49 memcpy(dpath
->path
, ce
->name
, path_len
);
50 dpath
->path
[path_len
] = '\0';
53 memset(&(dpath
->parent
[0]), 0,
54 sizeof(struct combine_diff_parent
)*5);
57 struct cache_entry
*nce
= active_cache
[i
];
60 if (strcmp(ce
->name
, nce
->name
))
63 /* Stage #2 (ours) is the first parent,
64 * stage #3 (theirs) is the second.
66 stage
= ce_stage(nce
);
68 int mode
= ntohl(nce
->ce_mode
);
70 hashcpy(dpath
->parent
[stage
-2].sha1
, nce
->sha1
);
71 dpath
->parent
[stage
-2].mode
=
73 dpath
->parent
[stage
-2].status
=
77 /* diff against the proper unmerged stage */
78 if (stage
== diff_unmerged_stage
)
83 * Compensate for loop update
87 if (revs
->combine_merges
&& num_compare_stages
== 2) {
88 show_combined_diff(dpath
, 2,
89 revs
->dense_combined_merges
,
98 * Show the diff for the 'ce' if we found the one
99 * from the desired stage.
101 diff_unmerge(&revs
->diffopt
, ce
->name
, 0, null_sha1
);
102 if (ce_stage(ce
) != diff_unmerged_stage
)
106 if (lstat(ce
->name
, &st
) < 0) {
107 if (errno
!= ENOENT
&& errno
!= ENOTDIR
) {
111 if (silent_on_removed
)
113 diff_addremove(&revs
->diffopt
, '-', ntohl(ce
->ce_mode
),
114 ce
->sha1
, ce
->name
, NULL
);
117 changed
= ce_match_stat(ce
, &st
, 0);
118 if (!changed
&& !revs
->diffopt
.find_copies_harder
)
120 oldmode
= ntohl(ce
->ce_mode
);
122 newmode
= canon_mode(st
.st_mode
);
123 if (!trust_executable_bit
&&
124 S_ISREG(newmode
) && S_ISREG(oldmode
) &&
125 ((newmode
^ oldmode
) == 0111))
127 diff_change(&revs
->diffopt
, oldmode
, newmode
,
128 ce
->sha1
, (changed
? null_sha1
: ce
->sha1
),
132 diffcore_std(&revs
->diffopt
);
133 diff_flush(&revs
->diffopt
);
141 /* A file entry went away or appeared */
142 static void diff_index_show_file(struct rev_info
*revs
,
144 struct cache_entry
*ce
,
145 unsigned char *sha1
, unsigned int mode
)
147 diff_addremove(&revs
->diffopt
, prefix
[0], ntohl(mode
),
148 sha1
, ce
->name
, NULL
);
151 static int get_stat_data(struct cache_entry
*ce
,
152 unsigned char **sha1p
,
154 int cached
, int match_missing
)
156 unsigned char *sha1
= ce
->sha1
;
157 unsigned int mode
= ce
->ce_mode
;
160 static unsigned char no_sha1
[20];
163 if (lstat(ce
->name
, &st
) < 0) {
164 if (errno
== ENOENT
&& match_missing
) {
171 changed
= ce_match_stat(ce
, &st
, 0);
173 mode
= create_ce_mode(st
.st_mode
);
174 if (!trust_executable_bit
&& S_ISREG(st
.st_mode
))
185 static void show_new_file(struct rev_info
*revs
,
186 struct cache_entry
*new,
187 int cached
, int match_missing
)
192 /* New file in the index: it might actually be different in
195 if (get_stat_data(new, &sha1
, &mode
, cached
, match_missing
) < 0)
198 diff_index_show_file(revs
, "+", new, sha1
, mode
);
201 static int show_modified(struct rev_info
*revs
,
202 struct cache_entry
*old
,
203 struct cache_entry
*new,
205 int cached
, int match_missing
)
207 unsigned int mode
, oldmode
;
210 if (get_stat_data(new, &sha1
, &mode
, cached
, match_missing
) < 0) {
212 diff_index_show_file(revs
, "-", old
,
213 old
->sha1
, old
->ce_mode
);
217 if (revs
->combine_merges
&& !cached
&&
218 (hashcmp(sha1
, old
->sha1
) || hashcmp(old
->sha1
, new->sha1
))) {
219 struct combine_diff_path
*p
;
220 int pathlen
= ce_namelen(new);
222 p
= xmalloc(combine_diff_path_size(2, pathlen
));
223 p
->path
= (char *) &p
->parent
[2];
226 memcpy(p
->path
, new->name
, pathlen
);
227 p
->path
[pathlen
] = 0;
228 p
->mode
= ntohl(mode
);
230 memset(p
->parent
, 0, 2 * sizeof(struct combine_diff_parent
));
231 p
->parent
[0].status
= DIFF_STATUS_MODIFIED
;
232 p
->parent
[0].mode
= ntohl(new->ce_mode
);
233 hashcpy(p
->parent
[0].sha1
, new->sha1
);
234 p
->parent
[1].status
= DIFF_STATUS_MODIFIED
;
235 p
->parent
[1].mode
= ntohl(old
->ce_mode
);
236 hashcpy(p
->parent
[1].sha1
, old
->sha1
);
237 show_combined_diff(p
, 2, revs
->dense_combined_merges
, revs
);
242 oldmode
= old
->ce_mode
;
243 if (mode
== oldmode
&& !hashcmp(sha1
, old
->sha1
) &&
244 !revs
->diffopt
.find_copies_harder
)
248 oldmode
= ntohl(oldmode
);
250 diff_change(&revs
->diffopt
, oldmode
, mode
,
251 old
->sha1
, sha1
, old
->name
, NULL
);
255 static int diff_cache(struct rev_info
*revs
,
256 struct cache_entry
**ac
, int entries
,
257 const char **pathspec
,
258 int cached
, int match_missing
)
261 struct cache_entry
*ce
= *ac
;
262 int same
= (entries
> 1) && ce_same_name(ce
, ac
[1]);
264 if (!ce_path_match(ce
, pathspec
))
267 switch (ce_stage(ce
)) {
269 /* No stage 1 entry? That means it's a new file */
271 show_new_file(revs
, ce
, cached
, match_missing
);
274 /* Show difference between old and new */
275 show_modified(revs
, ac
[1], ce
, 1,
276 cached
, match_missing
);
279 /* No stage 3 (merge) entry?
280 * That means it's been deleted.
283 diff_index_show_file(revs
, "-", ce
,
284 ce
->sha1
, ce
->ce_mode
);
287 /* We come here with ce pointing at stage 1
288 * (original tree) and ac[1] pointing at stage
289 * 3 (unmerged). show-modified with
290 * report-missing set to false does not say the
291 * file is deleted but reports true if work
292 * tree does not have it, in which case we
293 * fall through to report the unmerged state.
294 * Otherwise, we show the differences between
295 * the original tree and the work tree.
298 !show_modified(revs
, ce
, ac
[1], 0,
299 cached
, match_missing
))
301 diff_unmerge(&revs
->diffopt
, ce
->name
,
302 ntohl(ce
->ce_mode
), ce
->sha1
);
305 diff_unmerge(&revs
->diffopt
, ce
->name
,
310 die("impossible cache entry stage");
315 * Ignore all the different stages for this file,
316 * we've handled the relevant cases now.
321 } while (entries
&& ce_same_name(ce
, ac
[0]));
327 * This turns all merge entries into "stage 3". That guarantees that
328 * when we read in the new tree (into "stage 1"), we won't lose sight
329 * of the fact that we had unmerged entries.
331 static void mark_merge_entries(void)
334 for (i
= 0; i
< active_nr
; i
++) {
335 struct cache_entry
*ce
= active_cache
[i
];
338 ce
->ce_flags
|= htons(CE_STAGEMASK
);
342 int run_diff_index(struct rev_info
*revs
, int cached
)
347 const char *tree_name
;
348 int match_missing
= 0;
351 * Backward compatibility wart - "diff-index -m" does
352 * not mean "do not ignore merges", but totally different.
354 if (!revs
->ignore_merges
)
357 if (read_cache() < 0) {
358 perror("read_cache");
361 mark_merge_entries();
363 ent
= revs
->pending
.objects
[0].item
;
364 tree_name
= revs
->pending
.objects
[0].name
;
365 tree
= parse_tree_indirect(ent
->sha1
);
367 return error("bad tree object %s", tree_name
);
368 if (read_tree(tree
, 1, revs
->prune_data
))
369 return error("unable to read tree object %s", tree_name
);
370 ret
= diff_cache(revs
, active_cache
, active_nr
, revs
->prune_data
,
371 cached
, match_missing
);
372 diffcore_std(&revs
->diffopt
);
373 diff_flush(&revs
->diffopt
);
377 int do_diff_cache(const unsigned char *tree_sha1
, struct diff_options
*opt
)
380 struct rev_info revs
;
382 struct cache_entry
**dst
;
383 struct cache_entry
*last
= NULL
;
386 * This is used by git-blame to run diff-cache internally;
387 * it potentially needs to repeatedly run this, so we will
388 * start by removing the higher order entries the last round
392 for (i
= 0; i
< active_nr
; i
++) {
393 struct cache_entry
*ce
= active_cache
[i
];
395 if (last
&& !strcmp(ce
->name
, last
->name
))
397 cache_tree_invalidate_path(active_cache_tree
,
401 ce
->ce_flags
&= ~htons(CE_STAGEMASK
);
405 active_nr
= dst
- active_cache
;
407 init_revisions(&revs
, NULL
);
408 revs
.prune_data
= opt
->paths
;
409 tree
= parse_tree_indirect(tree_sha1
);
411 die("bad tree object %s", sha1_to_hex(tree_sha1
));
412 if (read_tree(tree
, 1, opt
->paths
))
413 return error("unable to read tree %s", sha1_to_hex(tree_sha1
));
414 return diff_cache(&revs
, active_cache
, active_nr
, revs
.prune_data
,