4 static int cached_only
= 0;
5 static int generate_patch
= 0;
6 static int match_nonexisting
= 0;
7 static int line_termination
= '\n';
9 /* A file entry went away or appeared */
10 static void show_file(const char *prefix
, struct cache_entry
*ce
, unsigned char *sha1
, unsigned int mode
)
13 diff_addremove(prefix
[0], ntohl(mode
), sha1
, ce
->name
, NULL
);
15 printf("%s%06o\tblob\t%s\t%s%c", prefix
, ntohl(mode
),
16 sha1_to_hex(sha1
), ce
->name
, line_termination
);
19 static int get_stat_data(struct cache_entry
*ce
, unsigned char **sha1p
, unsigned int *modep
)
21 unsigned char *sha1
= ce
->sha1
;
22 unsigned int mode
= ce
->ce_mode
;
25 static unsigned char no_sha1
[20];
28 if (lstat(ce
->name
, &st
) < 0) {
29 if (errno
== ENOENT
&& match_nonexisting
) {
36 changed
= cache_match_stat(ce
, &st
);
38 mode
= create_ce_mode(st
.st_mode
);
48 static void show_new_file(struct cache_entry
*new)
53 /* New file in the index: it might actually be different in the working copy */
54 if (get_stat_data(new, &sha1
, &mode
) < 0)
57 show_file("+", new, sha1
, mode
);
60 static int show_modified(struct cache_entry
*old
, struct cache_entry
*new)
62 unsigned int mode
, oldmode
;
64 unsigned char old_sha1_hex
[60];
66 if (get_stat_data(new, &sha1
, &mode
) < 0) {
67 show_file("-", old
, old
->sha1
, old
->ce_mode
);
71 oldmode
= old
->ce_mode
;
72 if (mode
== oldmode
&& !memcmp(sha1
, old
->sha1
, 20))
76 oldmode
= ntohl(oldmode
);
79 diff_change(oldmode
, mode
,
80 old
->sha1
, sha1
, old
->name
, NULL
);
82 strcpy(old_sha1_hex
, sha1_to_hex(old
->sha1
));
83 printf("*%06o->%06o\tblob\t%s->%s\t%s%c", oldmode
, mode
,
84 old_sha1_hex
, sha1_to_hex(sha1
),
85 old
->name
, line_termination
);
90 static int diff_cache(struct cache_entry
**ac
, int entries
)
93 struct cache_entry
*ce
= *ac
;
94 int same
= (entries
> 1) && same_name(ce
, ac
[1]);
96 switch (ce_stage(ce
)) {
98 /* No stage 1 entry? That means it's a new file */
103 /* Show difference between old and new */
104 show_modified(ac
[1], ce
);
107 /* No stage 3 (merge) entry? That means it's been deleted */
109 show_file("-", ce
, ce
->sha1
, ce
->ce_mode
);
112 /* Otherwise we fall through to the "unmerged" case */
115 diff_unmerge(ce
->name
);
117 printf("U %s%c", ce
->name
, line_termination
);
121 die("impossible cache entry stage");
125 * Ignore all the different stages for this file,
126 * we've handled the relevant cases now.
131 } while (entries
&& same_name(ce
, ac
[0]));
137 * This turns all merge entries into "stage 3". That guarantees that
138 * when we read in the new tree (into "stage 1"), we won't lose sight
139 * of the fact that we had unmerged entries.
141 static void mark_merge_entries(void)
144 for (i
= 0; i
< active_nr
; i
++) {
145 struct cache_entry
*ce
= active_cache
[i
];
148 ce
->ce_flags
|= htons(CE_STAGEMASK
);
152 static char *diff_cache_usage
=
153 "diff-cache [-r] [-z] [-p] [-i] [--cached] <tree sha1>";
155 int main(int argc
, char **argv
)
157 unsigned char tree_sha1
[20];
166 if (!strcmp(arg
, "-r")) {
167 /* We accept the -r flag just to look like diff-tree */
170 if (!strcmp(arg
, "-p")) {
174 if (!strcmp(arg
, "-z")) {
175 line_termination
= '\0';
178 if (!strcmp(arg
, "-m")) {
179 match_nonexisting
= 1;
182 if (!strcmp(arg
, "--cached")) {
186 usage(diff_cache_usage
);
189 if (argc
!= 2 || get_sha1(argv
[1], tree_sha1
))
190 usage(diff_cache_usage
);
192 mark_merge_entries();
194 tree
= read_object_with_reference(tree_sha1
, "tree", &size
, 0);
196 die("bad tree object %s", argv
[1]);
197 if (read_tree(tree
, size
, 1))
198 die("unable to read tree object %s", argv
[1]);
200 return diff_cache(active_cache
, active_nr
);