4 static int cached_only
= 0;
5 static int generate_patch
= 0;
6 static int line_termination
= '\n';
8 /* A file entry went away or appeared */
9 static void show_file(const char *prefix
, struct cache_entry
*ce
, unsigned char *sha1
, unsigned int mode
)
12 diff_addremove(prefix
[0], ntohl(mode
), sha1
, ce
->name
, NULL
);
14 printf("%s%06o\tblob\t%s\t%s%c", prefix
, ntohl(mode
),
15 sha1_to_hex(sha1
), ce
->name
, line_termination
);
18 static int get_stat_data(struct cache_entry
*ce
, unsigned char **sha1p
, unsigned int *modep
)
20 unsigned char *sha1
= ce
->sha1
;
21 unsigned int mode
= ce
->ce_mode
;
24 static unsigned char no_sha1
[20];
27 if (stat(ce
->name
, &st
) < 0)
29 changed
= cache_match_stat(ce
, &st
);
31 mode
= create_ce_mode(st
.st_mode
);
41 static void show_new_file(struct cache_entry
*new)
46 /* New file in the index: it might actually be different in the working copy */
47 if (get_stat_data(new, &sha1
, &mode
) < 0)
50 show_file("+", new, sha1
, mode
);
53 static int show_modified(struct cache_entry
*old
, struct cache_entry
*new)
55 unsigned int mode
, oldmode
;
57 unsigned char old_sha1_hex
[60];
59 if (get_stat_data(new, &sha1
, &mode
) < 0) {
60 show_file("-", old
, old
->sha1
, old
->ce_mode
);
64 oldmode
= old
->ce_mode
;
65 if (mode
== oldmode
&& !memcmp(sha1
, old
->sha1
, 20))
69 oldmode
= ntohl(oldmode
);
72 diff_change(oldmode
, mode
,
73 old
->sha1
, sha1
, old
->name
, NULL
);
75 strcpy(old_sha1_hex
, sha1_to_hex(old
->sha1
));
76 printf("*%06o->%06o\tblob\t%s->%s\t%s%c", oldmode
, mode
,
77 old_sha1_hex
, sha1_to_hex(sha1
),
78 old
->name
, line_termination
);
83 static int diff_cache(struct cache_entry
**ac
, int entries
)
86 struct cache_entry
*ce
= *ac
;
87 int same
= (entries
> 1) && same_name(ce
, ac
[1]);
89 switch (ce_stage(ce
)) {
91 /* No stage 1 entry? That means it's a new file */
96 /* Show difference between old and new */
97 show_modified(ac
[1], ce
);
100 /* No stage 3 (merge) entry? That means it's been deleted */
102 show_file("-", ce
, ce
->sha1
, ce
->ce_mode
);
105 /* Otherwise we fall through to the "unmerged" case */
108 diff_unmerge(ce
->name
);
110 printf("U %s%c", ce
->name
, line_termination
);
114 die("impossible cache entry stage");
118 * Ignore all the different stages for this file,
119 * we've handled the relevant cases now.
124 } while (entries
&& same_name(ce
, ac
[0]));
130 * This turns all merge entries into "stage 3". That guarantees that
131 * when we read in the new tree (into "stage 1"), we won't lose sight
132 * of the fact that we had unmerged entries.
134 static void mark_merge_entries(void)
137 for (i
= 0; i
< active_nr
; i
++) {
138 struct cache_entry
*ce
= active_cache
[i
];
141 ce
->ce_flags
|= htons(CE_STAGEMASK
);
145 static char *diff_cache_usage
=
146 "diff-cache [-r] [-z] [-p] [--cached] <tree sha1>";
148 int main(int argc
, char **argv
)
150 unsigned char tree_sha1
[20];
159 if (!strcmp(arg
, "-r")) {
160 /* We accept the -r flag just to look like diff-tree */
163 if (!strcmp(arg
, "-p")) {
167 if (!strcmp(arg
, "-z")) {
168 line_termination
= '\0';
171 if (!strcmp(arg
, "--cached")) {
175 usage(diff_cache_usage
);
178 if (argc
!= 2 || get_sha1(argv
[1], tree_sha1
))
179 usage(diff_cache_usage
);
181 mark_merge_entries();
183 tree
= read_object_with_reference(tree_sha1
, "tree", &size
, 0);
185 die("bad tree object %s", argv
[1]);
186 if (read_tree(tree
, size
, 1))
187 die("unable to read tree object %s", argv
[1]);
189 return diff_cache(active_cache
, active_nr
);