[PATCH] git and symlinks as tracked content
[git/git.git] / diff-cache.c
CommitLineData
e74f8f6a 1#include "cache.h"
f92a4465 2#include "diff.h"
e74f8f6a
LT
3
4static int cached_only = 0;
f92a4465 5static int generate_patch = 0;
e74f8f6a
LT
6static int line_termination = '\n';
7
e74f8f6a 8/* A file entry went away or appeared */
c9cddabe 9static void show_file(const char *prefix, struct cache_entry *ce, unsigned char *sha1, unsigned int mode)
e74f8f6a 10{
f92a4465 11 if (generate_patch)
c9cddabe 12 diff_addremove(prefix[0], ntohl(mode), sha1, ce->name, NULL);
f92a4465 13 else
c9cddabe
LT
14 printf("%s%06o\tblob\t%s\t%s%c", prefix, ntohl(mode),
15 sha1_to_hex(sha1), ce->name, line_termination);
e74f8f6a
LT
16}
17
c9cddabe 18static int get_stat_data(struct cache_entry *ce, unsigned char **sha1p, unsigned int *modep)
e74f8f6a 19{
c9cddabe
LT
20 unsigned char *sha1 = ce->sha1;
21 unsigned int mode = ce->ce_mode;
e74f8f6a
LT
22
23 if (!cached_only) {
24 static unsigned char no_sha1[20];
b5af9107 25 int changed;
e74f8f6a 26 struct stat st;
8ae0a8c5 27 if (lstat(ce->name, &st) < 0)
e74f8f6a 28 return -1;
c9cddabe 29 changed = cache_match_stat(ce, &st);
e74f8f6a 30 if (changed) {
c9cddabe 31 mode = create_ce_mode(st.st_mode);
b5af9107 32 sha1 = no_sha1;
e74f8f6a
LT
33 }
34 }
35
c9cddabe
LT
36 *sha1p = sha1;
37 *modep = mode;
38 return 0;
39}
40
b836825b 41static void show_new_file(struct cache_entry *new)
c9cddabe
LT
42{
43 unsigned char *sha1;
44 unsigned int mode;
45
46 /* New file in the index: it might actually be different in the working copy */
47 if (get_stat_data(new, &sha1, &mode) < 0)
b836825b 48 return;
c9cddabe
LT
49
50 show_file("+", new, sha1, mode);
51}
52
53static int show_modified(struct cache_entry *old, struct cache_entry *new)
54{
55 unsigned int mode, oldmode;
56 unsigned char *sha1;
57 unsigned char old_sha1_hex[60];
58
59 if (get_stat_data(new, &sha1, &mode) < 0) {
60 show_file("-", old, old->sha1, old->ce_mode);
61 return -1;
62 }
63
64 oldmode = old->ce_mode;
b5af9107 65 if (mode == oldmode && !memcmp(sha1, old->sha1, 20))
e74f8f6a
LT
66 return 0;
67
c9cddabe
LT
68 mode = ntohl(mode);
69 oldmode = ntohl(oldmode);
70
f92a4465
JH
71 if (generate_patch)
72 diff_change(oldmode, mode,
73 old->sha1, sha1, old->name, NULL);
74 else {
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);
79 }
e74f8f6a
LT
80 return 0;
81}
82
b5af9107 83static int diff_cache(struct cache_entry **ac, int entries)
e74f8f6a 84{
b5af9107
LT
85 while (entries) {
86 struct cache_entry *ce = *ac;
b0fe89ca 87 int same = (entries > 1) && same_name(ce, ac[1]);
e74f8f6a 88
b0fe89ca
LT
89 switch (ce_stage(ce)) {
90 case 0:
91 /* No stage 1 entry? That means it's a new file */
92 if (!same) {
c9cddabe 93 show_new_file(ce);
b0fe89ca
LT
94 break;
95 }
96 /* Show difference between old and new */
97 show_modified(ac[1], ce);
98 break;
99 case 1:
100 /* No stage 3 (merge) entry? That means it's been deleted */
101 if (!same) {
c9cddabe 102 show_file("-", ce, ce->sha1, ce->ce_mode);
b0fe89ca
LT
103 break;
104 }
105 /* Otherwise we fall through to the "unmerged" case */
106 case 3:
f92a4465
JH
107 if (generate_patch)
108 diff_unmerge(ce->name);
109 else
110 printf("U %s%c", ce->name, line_termination);
b0fe89ca
LT
111 break;
112
113 default:
114 die("impossible cache entry stage");
e74f8f6a 115 }
b0fe89ca
LT
116
117 /*
118 * Ignore all the different stages for this file,
119 * we've handled the relevant cases now.
120 */
121 do {
e74f8f6a
LT
122 ac++;
123 entries--;
b0fe89ca 124 } while (entries && same_name(ce, ac[0]));
e74f8f6a
LT
125 }
126 return 0;
127}
128
b0fe89ca
LT
129/*
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.
133 */
134static void mark_merge_entries(void)
b5af9107
LT
135{
136 int i;
137 for (i = 0; i < active_nr; i++) {
138 struct cache_entry *ce = active_cache[i];
139 if (!ce_stage(ce))
5697ecc7 140 continue;
b0fe89ca 141 ce->ce_flags |= htons(CE_STAGEMASK);
b5af9107
LT
142 }
143}
144
f92a4465
JH
145static char *diff_cache_usage =
146"diff-cache [-r] [-z] [-p] [--cached] <tree sha1>";
c5bac17a 147
e74f8f6a
LT
148int main(int argc, char **argv)
149{
150 unsigned char tree_sha1[20];
151 void *tree;
152 unsigned long size;
e74f8f6a
LT
153
154 read_cache();
155 while (argc > 2) {
156 char *arg = argv[1];
157 argv++;
158 argc--;
159 if (!strcmp(arg, "-r")) {
b5af9107 160 /* We accept the -r flag just to look like diff-tree */
e74f8f6a
LT
161 continue;
162 }
f92a4465
JH
163 if (!strcmp(arg, "-p")) {
164 generate_patch = 1;
165 continue;
166 }
e74f8f6a
LT
167 if (!strcmp(arg, "-z")) {
168 line_termination = '\0';
169 continue;
170 }
171 if (!strcmp(arg, "--cached")) {
172 cached_only = 1;
173 continue;
174 }
c5bac17a 175 usage(diff_cache_usage);
e74f8f6a
LT
176 }
177
3c249c95 178 if (argc != 2 || get_sha1(argv[1], tree_sha1))
c5bac17a 179 usage(diff_cache_usage);
e74f8f6a 180
b0fe89ca 181 mark_merge_entries();
b5af9107 182
40469ee9 183 tree = read_object_with_reference(tree_sha1, "tree", &size, 0);
e74f8f6a
LT
184 if (!tree)
185 die("bad tree object %s", argv[1]);
b5af9107
LT
186 if (read_tree(tree, size, 1))
187 die("unable to read tree object %s", argv[1]);
e74f8f6a 188
b5af9107 189 return diff_cache(active_cache, active_nr);
e74f8f6a 190}