4 static int cached_only
= 0;
5 static int diff_output_format
= DIFF_FORMAT_HUMAN
;
6 static int match_nonexisting
= 0;
7 static int detect_rename
= 0;
8 static int diff_setup_opt
= 0;
9 static int diff_score_opt
= 0;
10 static const char *pickaxe
= NULL
;
11 static int pickaxe_opts
= 0;
12 static int diff_break_opt
= -1;
13 static const char *orderfile
= NULL
;
15 /* A file entry went away or appeared */
16 static void show_file(const char *prefix
, struct cache_entry
*ce
, unsigned char *sha1
, unsigned int mode
)
18 diff_addremove(prefix
[0], ntohl(mode
), sha1
, ce
->name
, NULL
);
21 static int get_stat_data(struct cache_entry
*ce
, unsigned char **sha1p
, unsigned int *modep
)
23 unsigned char *sha1
= ce
->sha1
;
24 unsigned int mode
= ce
->ce_mode
;
27 static unsigned char no_sha1
[20];
30 if (lstat(ce
->name
, &st
) < 0) {
31 if (errno
== ENOENT
&& match_nonexisting
) {
38 changed
= ce_match_stat(ce
, &st
);
40 mode
= create_ce_mode(st
.st_mode
);
50 static void show_new_file(struct cache_entry
*new)
55 /* New file in the index: it might actually be different in the working copy */
56 if (get_stat_data(new, &sha1
, &mode
) < 0)
59 show_file("+", new, sha1
, mode
);
62 static int show_modified(struct cache_entry
*old
,
63 struct cache_entry
*new,
66 unsigned int mode
, oldmode
;
69 if (get_stat_data(new, &sha1
, &mode
) < 0) {
71 show_file("-", old
, old
->sha1
, old
->ce_mode
);
75 oldmode
= old
->ce_mode
;
76 if (mode
== oldmode
&& !memcmp(sha1
, old
->sha1
, 20) &&
77 detect_rename
< DIFF_DETECT_COPY
)
81 oldmode
= ntohl(oldmode
);
83 diff_change(oldmode
, mode
,
84 old
->sha1
, sha1
, old
->name
, NULL
);
88 static int diff_cache(struct cache_entry
**ac
, int entries
)
91 struct cache_entry
*ce
= *ac
;
92 int same
= (entries
> 1) && ce_same_name(ce
, ac
[1]);
94 switch (ce_stage(ce
)) {
96 /* No stage 1 entry? That means it's a new file */
101 /* Show difference between old and new */
102 show_modified(ac
[1], ce
, 1);
105 /* No stage 3 (merge) entry? That means it's been deleted */
107 show_file("-", ce
, ce
->sha1
, ce
->ce_mode
);
110 /* We come here with ce pointing at stage 1
111 * (original tree) and ac[1] pointing at stage
112 * 3 (unmerged). show-modified with
113 * report-mising set to false does not say the
114 * file is deleted but reports true if work
115 * tree does not have it, in which case we
116 * fall through to report the unmerged state.
117 * Otherwise, we show the differences between
118 * the original tree and the work tree.
120 if (!cached_only
&& !show_modified(ce
, ac
[1], 0))
124 diff_unmerge(ce
->name
);
128 die("impossible cache entry stage");
132 * Ignore all the different stages for this file,
133 * we've handled the relevant cases now.
138 } while (entries
&& ce_same_name(ce
, ac
[0]));
144 * This turns all merge entries into "stage 3". That guarantees that
145 * when we read in the new tree (into "stage 1"), we won't lose sight
146 * of the fact that we had unmerged entries.
148 static void mark_merge_entries(void)
151 for (i
= 0; i
< active_nr
; i
++) {
152 struct cache_entry
*ce
= active_cache
[i
];
155 ce
->ce_flags
|= htons(CE_STAGEMASK
);
159 static char *diff_cache_usage
=
160 "git-diff-cache [-p] [-r] [-z] [-m] [-M] [-C] [-R] [-S<string>] [-O<orderfile>] [--cached] <tree-ish> [<path>...]";
162 int main(int argc
, const char **argv
)
164 const char *tree_name
= NULL
;
165 unsigned char sha1
[20];
166 const char **pathspec
= NULL
;
173 for (i
= 1; i
< argc
; i
++) {
174 const char *arg
= argv
[i
];
185 if (!strcmp(arg
, "-r")) {
186 /* We accept the -r flag just to look like git-diff-tree */
189 if (!strcmp(arg
, "-p")) {
190 diff_output_format
= DIFF_FORMAT_PATCH
;
193 if (!strncmp(arg
, "-B", 2)) {
194 if ((diff_break_opt
= diff_scoreopt_parse(arg
)) == -1)
195 usage(diff_cache_usage
);
198 if (!strncmp(arg
, "-M", 2)) {
199 detect_rename
= DIFF_DETECT_RENAME
;
200 if ((diff_score_opt
= diff_scoreopt_parse(arg
)) == -1)
201 usage(diff_cache_usage
);
204 if (!strncmp(arg
, "-C", 2)) {
205 detect_rename
= DIFF_DETECT_COPY
;
206 if ((diff_score_opt
= diff_scoreopt_parse(arg
)) == -1)
207 usage(diff_cache_usage
);
210 if (!strcmp(arg
, "-z")) {
211 diff_output_format
= DIFF_FORMAT_MACHINE
;
214 if (!strcmp(arg
, "-R")) {
215 diff_setup_opt
|= DIFF_SETUP_REVERSE
;
218 if (!strncmp(arg
, "-S", 2)) {
222 if (!strncmp(arg
, "-O", 2)) {
226 if (!strcmp(arg
, "--pickaxe-all")) {
227 pickaxe_opts
= DIFF_PICKAXE_ALL
;
230 if (!strcmp(arg
, "-m")) {
231 match_nonexisting
= 1;
234 if (!strcmp(arg
, "--cached")) {
238 usage(diff_cache_usage
);
241 if (!tree_name
|| get_sha1(tree_name
, sha1
))
242 usage(diff_cache_usage
);
244 /* The rest is for paths restriction. */
245 diff_setup(diff_setup_opt
);
247 mark_merge_entries();
249 tree
= read_object_with_reference(sha1
, "tree", &size
, NULL
);
251 die("bad tree object %s", tree_name
);
252 if (read_tree(tree
, size
, 1))
253 die("unable to read tree object %s", tree_name
);
255 ret
= diff_cache(active_cache
, active_nr
);
257 diffcore_std(pathspec
? : NULL
,
258 detect_rename
, diff_score_opt
,
259 pickaxe
, pickaxe_opts
,
262 diff_flush(diff_output_format
, 1);