3 static int cached_only = 0;
4 static int line_termination = '\n';
6 /* A file entry went away or appeared */
7 static void show_file(const char *prefix, struct cache_entry *ce)
9 printf("%s%o\t%s\t%s\t%s%c", prefix, ntohl(ce->ce_mode), "blob",
10 sha1_to_hex(ce->sha1), ce->name, line_termination);
13 static int show_modified(struct cache_entry *old, struct cache_entry *new)
15 unsigned int mode = ntohl(new->ce_mode), oldmode;
16 unsigned char *sha1 = new->sha1;
17 unsigned char old_sha1_hex[60];
20 static unsigned char no_sha1[20];
23 if (stat(new->name, &st) < 0) {
27 changed = cache_match_stat(new, &st);
34 oldmode = ntohl(old->ce_mode);
35 if (mode == oldmode && !memcmp(sha1, old->sha1, 20))
38 strcpy(old_sha1_hex, sha1_to_hex(old->sha1));
39 printf("*%o->%o\t%s\t%s->%s\t%s%c", oldmode, mode,
41 old_sha1_hex, sha1_to_hex(sha1),
42 old->name, line_termination);
46 static int diff_cache(struct cache_entry **ac, int entries)
49 struct cache_entry *ce = *ac;
50 int same = (entries > 1) && same_name(ce, ac[1]);
52 switch (ce_stage(ce)) {
54 /* No stage 1 entry? That means it's a new file */
59 /* Show difference between old and new */
60 show_modified(ac[1], ce);
63 /* No stage 3 (merge) entry? That means it's been deleted */
68 /* Otherwise we fall through to the "unmerged" case */
70 printf("U %s%c", ce->name, line_termination);
74 die("impossible cache entry stage");
78 * Ignore all the different stages for this file,
79 * we've handled the relevant cases now.
84 } while (entries && same_name(ce, ac[0]));
90 * This turns all merge entries into "stage 3". That guarantees that
91 * when we read in the new tree (into "stage 1"), we won't lose sight
92 * of the fact that we had unmerged entries.
94 static void mark_merge_entries(void)
97 for (i = 0; i < active_nr; i++) {
98 struct cache_entry *ce = active_cache[i];
101 ce->ce_flags |= htons(CE_STAGEMASK);
105 static char *diff_cache_usage = "diff-cache [-r] [-z] [--cached] <tree sha1>";
107 int main(int argc, char **argv)
109 unsigned char tree_sha1[20];
118 if (!strcmp(arg, "-r")) {
119 /* We accept the -r flag just to look like diff-tree */
122 if (!strcmp(arg, "-z")) {
123 line_termination = '\0';
126 if (!strcmp(arg, "--cached")) {
130 usage(diff_cache_usage);
133 if (argc != 2 || get_sha1_hex(argv[1], tree_sha1))
134 usage(diff_cache_usage);
136 mark_merge_entries();
138 tree = read_tree_with_tree_or_commit_sha1(tree_sha1, &size, 0);
140 die("bad tree object %s", argv[1]);
141 if (read_tree(tree, size, 1))
142 die("unable to read tree object %s", argv[1]);
144 return diff_cache(active_cache, active_nr);