1#include "cache.h"
23
static int cached_only = 0;
4static int line_termination = '\n';
56
/* A file entry went away or appeared */
7static void show_file(const char *prefix, struct cache_entry *ce)
8{
9printf("%s%o\t%s\t%s\t%s%c", prefix, ntohl(ce->ce_mode), "blob",
10sha1_to_hex(ce->sha1), ce->name, line_termination);
11}
1213
static int show_modified(struct cache_entry *old, struct cache_entry *new)
14{
15unsigned int mode = ntohl(new->ce_mode), oldmode;
16unsigned char *sha1 = new->sha1;
17unsigned char old_sha1_hex[60];
1819
if (!cached_only) {
20static unsigned char no_sha1[20];
21int changed;
22struct stat st;
23if (stat(new->name, &st) < 0) {
24show_file("-", old);
25return -1;
26}
27changed = cache_match_stat(new, &st);
28if (changed) {
29mode = st.st_mode;
30sha1 = no_sha1;
31}
32}
3334
oldmode = ntohl(old->ce_mode);
35if (mode == oldmode && !memcmp(sha1, old->sha1, 20))
36return 0;
3738
strcpy(old_sha1_hex, sha1_to_hex(old->sha1));
39printf("*%o->%o\t%s\t%s->%s\t%s%c", oldmode, mode,
40"blob",
41old_sha1_hex, sha1_to_hex(sha1),
42old->name, line_termination);
43return 0;
44}
4546
static int diff_cache(struct cache_entry **ac, int entries)
47{
48while (entries) {
49struct cache_entry *ce = *ac;
50int same = (entries > 1) && same_name(ce, ac[1]);
5152
switch (ce_stage(ce)) {
53case 0:
54/* No stage 1 entry? That means it's a new file */
55if (!same) {
56show_file("+", ce);
57break;
58}
59/* Show difference between old and new */
60show_modified(ac[1], ce);
61break;
62case 1:
63/* No stage 3 (merge) entry? That means it's been deleted */
64if (!same) {
65show_file("-", ce);
66break;
67}
68/* Otherwise we fall through to the "unmerged" case */
69case 3:
70printf("U %s%c", ce->name, line_termination);
71break;
7273
default:
74die("impossible cache entry stage");
75}
7677
/*
78* Ignore all the different stages for this file,
79* we've handled the relevant cases now.
80*/
81do {
82ac++;
83entries--;
84} while (entries && same_name(ce, ac[0]));
85}
86return 0;
87}
8889
/*
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.
93*/
94static void mark_merge_entries(void)
95{
96int i;
97for (i = 0; i < active_nr; i++) {
98struct cache_entry *ce = active_cache[i];
99if (!ce_stage(ce))
100continue;
101ce->ce_flags |= htons(CE_STAGEMASK);
102}
103}
104105
static char *diff_cache_usage = "diff-cache [-r] [-z] [--cached] <tree sha1>";
106107
int main(int argc, char **argv)
108{
109unsigned char tree_sha1[20];
110void *tree;
111unsigned long size;
112113
read_cache();
114while (argc > 2) {
115char *arg = argv[1];
116argv++;
117argc--;
118if (!strcmp(arg, "-r")) {
119/* We accept the -r flag just to look like diff-tree */
120continue;
121}
122if (!strcmp(arg, "-z")) {
123line_termination = '\0';
124continue;
125}
126if (!strcmp(arg, "--cached")) {
127cached_only = 1;
128continue;
129}
130usage(diff_cache_usage);
131}
132133
if (argc != 2 || get_sha1_hex(argv[1], tree_sha1))
134usage(diff_cache_usage);
135136
mark_merge_entries();
137138
tree = read_tree_with_tree_or_commit_sha1(tree_sha1, &size, 0);
139if (!tree)
140die("bad tree object %s", argv[1]);
141if (read_tree(tree, size, 1))
142die("unable to read tree object %s", argv[1]);
143144
return diff_cache(active_cache, active_nr);
145}