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
)
12 diff_addremove(prefix
[0], ntohl(ce
->ce_mode
),
13 ce
->sha1
, ce
->name
, NULL
);
15 printf("%s%06o\tblob\t%s\t%s%c", prefix
, ntohl(ce
->ce_mode
),
16 sha1_to_hex(ce
->sha1
), ce
->name
,
20 static int show_modified(struct cache_entry
*old
, struct cache_entry
*new)
22 unsigned int mode
= ntohl(new->ce_mode
), oldmode
;
23 unsigned char *sha1
= new->sha1
;
24 unsigned char old_sha1_hex
[60];
27 static unsigned char no_sha1
[20];
30 if (stat(new->name
, &st
) < 0) {
34 changed
= cache_match_stat(new, &st
);
41 oldmode
= ntohl(old
->ce_mode
);
42 if (mode
== oldmode
&& !memcmp(sha1
, old
->sha1
, 20))
46 diff_change(oldmode
, mode
,
47 old
->sha1
, sha1
, old
->name
, NULL
);
49 strcpy(old_sha1_hex
, sha1_to_hex(old
->sha1
));
50 printf("*%06o->%06o\tblob\t%s->%s\t%s%c", oldmode
, mode
,
51 old_sha1_hex
, sha1_to_hex(sha1
),
52 old
->name
, line_termination
);
57 static int diff_cache(struct cache_entry
**ac
, int entries
)
60 struct cache_entry
*ce
= *ac
;
61 int same
= (entries
> 1) && same_name(ce
, ac
[1]);
63 switch (ce_stage(ce
)) {
65 /* No stage 1 entry? That means it's a new file */
70 /* Show difference between old and new */
71 show_modified(ac
[1], ce
);
74 /* No stage 3 (merge) entry? That means it's been deleted */
79 /* Otherwise we fall through to the "unmerged" case */
82 diff_unmerge(ce
->name
);
84 printf("U %s%c", ce
->name
, line_termination
);
88 die("impossible cache entry stage");
92 * Ignore all the different stages for this file,
93 * we've handled the relevant cases now.
98 } while (entries
&& same_name(ce
, ac
[0]));
104 * This turns all merge entries into "stage 3". That guarantees that
105 * when we read in the new tree (into "stage 1"), we won't lose sight
106 * of the fact that we had unmerged entries.
108 static void mark_merge_entries(void)
111 for (i
= 0; i
< active_nr
; i
++) {
112 struct cache_entry
*ce
= active_cache
[i
];
115 ce
->ce_flags
|= htons(CE_STAGEMASK
);
119 static char *diff_cache_usage
=
120 "diff-cache [-r] [-z] [-p] [--cached] <tree sha1>";
122 int main(int argc
, char **argv
)
124 unsigned char tree_sha1
[20];
133 if (!strcmp(arg
, "-r")) {
134 /* We accept the -r flag just to look like diff-tree */
137 if (!strcmp(arg
, "-p")) {
141 if (!strcmp(arg
, "-z")) {
142 line_termination
= '\0';
145 if (!strcmp(arg
, "--cached")) {
149 usage(diff_cache_usage
);
152 if (argc
!= 2 || get_sha1_hex(argv
[1], tree_sha1
))
153 usage(diff_cache_usage
);
155 mark_merge_entries();
157 tree
= read_tree_with_tree_or_commit_sha1(tree_sha1
, &size
, 0);
159 die("bad tree object %s", argv
[1]);
160 if (read_tree(tree
, size
, 1))
161 die("unable to read tree object %s", argv
[1]);
163 return diff_cache(active_cache
, active_nr
);