Diff clean-up.
[git/git.git] / diff-index.c
CommitLineData
e74f8f6a 1#include "cache.h"
f92a4465 2#include "diff.h"
e74f8f6a
LT
3
4static int cached_only = 0;
160c8433 5static int match_nonexisting = 0;
6b5ee137 6static struct diff_options diff_options;
e74f8f6a 7
e74f8f6a 8/* A file entry went away or appeared */
6b5ee137
JH
9static void show_file(const char *prefix,
10 struct cache_entry *ce,
11 unsigned char *sha1, unsigned int mode)
e74f8f6a 12{
6b5ee137
JH
13 diff_addremove(&diff_options, prefix[0], ntohl(mode),
14 sha1, ce->name, NULL);
e74f8f6a
LT
15}
16
6b5ee137
JH
17static int get_stat_data(struct cache_entry *ce,
18 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;
160c8433
LT
27 if (lstat(ce->name, &st) < 0) {
28 if (errno == ENOENT && match_nonexisting) {
29 *sha1p = sha1;
30 *modep = mode;
31 return 0;
32 }
e74f8f6a 33 return -1;
160c8433 34 }
5d728c84 35 changed = ce_match_stat(ce, &st);
e74f8f6a 36 if (changed) {
c9cddabe 37 mode = create_ce_mode(st.st_mode);
b5af9107 38 sha1 = no_sha1;
e74f8f6a
LT
39 }
40 }
41
c9cddabe
LT
42 *sha1p = sha1;
43 *modep = mode;
44 return 0;
45}
46
b836825b 47static void show_new_file(struct cache_entry *new)
c9cddabe
LT
48{
49 unsigned char *sha1;
50 unsigned int mode;
51
52 /* New file in the index: it might actually be different in the working copy */
53 if (get_stat_data(new, &sha1, &mode) < 0)
b836825b 54 return;
c9cddabe
LT
55
56 show_file("+", new, sha1, mode);
57}
58
66026590
JH
59static int show_modified(struct cache_entry *old,
60 struct cache_entry *new,
61 int report_missing)
c9cddabe
LT
62{
63 unsigned int mode, oldmode;
64 unsigned char *sha1;
c9cddabe
LT
65
66 if (get_stat_data(new, &sha1, &mode) < 0) {
66026590
JH
67 if (report_missing)
68 show_file("-", old, old->sha1, old->ce_mode);
c9cddabe
LT
69 return -1;
70 }
71
72 oldmode = old->ce_mode;
c3e7fbcb 73 if (mode == oldmode && !memcmp(sha1, old->sha1, 20) &&
6b5ee137 74 !diff_options.find_copies_harder)
e74f8f6a
LT
75 return 0;
76
c9cddabe
LT
77 mode = ntohl(mode);
78 oldmode = ntohl(oldmode);
79
6b5ee137 80 diff_change(&diff_options, oldmode, mode,
57fe64a4 81 old->sha1, sha1, old->name, NULL);
e74f8f6a
LT
82 return 0;
83}
84
fdee7d07 85static int diff_cache(struct cache_entry **ac, int entries, const char **pathspec)
e74f8f6a 86{
b5af9107
LT
87 while (entries) {
88 struct cache_entry *ce = *ac;
dbbce55b 89 int same = (entries > 1) && ce_same_name(ce, ac[1]);
e74f8f6a 90
fdee7d07
LT
91 if (!ce_path_match(ce, pathspec))
92 goto skip_entry;
93
b0fe89ca
LT
94 switch (ce_stage(ce)) {
95 case 0:
96 /* No stage 1 entry? That means it's a new file */
97 if (!same) {
c9cddabe 98 show_new_file(ce);
b0fe89ca
LT
99 break;
100 }
101 /* Show difference between old and new */
66026590 102 show_modified(ac[1], ce, 1);
b0fe89ca
LT
103 break;
104 case 1:
105 /* No stage 3 (merge) entry? That means it's been deleted */
106 if (!same) {
c9cddabe 107 show_file("-", ce, ce->sha1, ce->ce_mode);
b0fe89ca
LT
108 break;
109 }
66026590
JH
110 /* We come here with ce pointing at stage 1
111 * (original tree) and ac[1] pointing at stage
112 * 3 (unmerged). show-modified with
113 * report-mising set to false does not say the
114 * file is deleted but reports true if work
115 * tree does not have it, in which case we
116 * fall through to report the unmerged state.
117 * Otherwise, we show the differences between
118 * the original tree and the work tree.
119 */
120 if (!cached_only && !show_modified(ce, ac[1], 0))
121 break;
122 /* fallthru */
b0fe89ca 123 case 3:
6b5ee137 124 diff_unmerge(&diff_options, ce->name);
b0fe89ca
LT
125 break;
126
127 default:
128 die("impossible cache entry stage");
e74f8f6a 129 }
b0fe89ca 130
fdee7d07 131skip_entry:
b0fe89ca
LT
132 /*
133 * Ignore all the different stages for this file,
134 * we've handled the relevant cases now.
135 */
136 do {
e74f8f6a
LT
137 ac++;
138 entries--;
dbbce55b 139 } while (entries && ce_same_name(ce, ac[0]));
e74f8f6a
LT
140 }
141 return 0;
142}
143
b0fe89ca
LT
144/*
145 * This turns all merge entries into "stage 3". That guarantees that
146 * when we read in the new tree (into "stage 1"), we won't lose sight
147 * of the fact that we had unmerged entries.
148 */
149static void mark_merge_entries(void)
b5af9107
LT
150{
151 int i;
152 for (i = 0; i < active_nr; i++) {
153 struct cache_entry *ce = active_cache[i];
154 if (!ce_stage(ce))
5697ecc7 155 continue;
b0fe89ca 156 ce->ce_flags |= htons(CE_STAGEMASK);
b5af9107
LT
157 }
158}
159
4d1f1190 160static const char diff_cache_usage[] =
215a7ad1 161"git-diff-index [-m] [--cached] "
dda2d79a
JH
162"[<common diff options>] <tree-ish> [<path>...]"
163COMMON_DIFF_OPTIONS_HELP;
c5bac17a 164
6b5ee137 165int main(int argc, const char **argv)
e74f8f6a 166{
6c56c534
LT
167 const char *tree_name = NULL;
168 unsigned char sha1[20];
d288a700 169 const char *prefix = setup_git_directory();
6c56c534 170 const char **pathspec = NULL;
e74f8f6a
LT
171 void *tree;
172 unsigned long size;
5c97558c 173 int ret;
e0f0e891 174 int allow_options = 1;
6c56c534 175 int i;
e74f8f6a 176
6b5ee137 177 diff_setup(&diff_options);
6c56c534
LT
178 for (i = 1; i < argc; i++) {
179 const char *arg = argv[i];
6b5ee137 180 int diff_opt_cnt;
6c56c534 181
e0f0e891 182 if (!allow_options || *arg != '-') {
d288a700 183 if (tree_name)
6c56c534 184 break;
6c56c534
LT
185 tree_name = arg;
186 continue;
187 }
188
e0f0e891
JF
189 if (!strcmp(arg, "--")) {
190 allow_options = 0;
191 continue;
192 }
e74f8f6a 193 if (!strcmp(arg, "-r")) {
667bb59b 194 /* We accept the -r flag just to look like git-diff-tree */
e74f8f6a
LT
195 continue;
196 }
6b5ee137
JH
197 diff_opt_cnt = diff_opt_parse(&diff_options, argv + i,
198 argc - i);
199 if (diff_opt_cnt < 0)
200 usage(diff_cache_usage);
201 else if (diff_opt_cnt) {
202 i += diff_opt_cnt - 1;
367cec1c
JH
203 continue;
204 }
6b5ee137 205
160c8433
LT
206 if (!strcmp(arg, "-m")) {
207 match_nonexisting = 1;
208 continue;
209 }
e74f8f6a
LT
210 if (!strcmp(arg, "--cached")) {
211 cached_only = 1;
212 continue;
213 }
c5bac17a 214 usage(diff_cache_usage);
e74f8f6a
LT
215 }
216
d288a700
LT
217 pathspec = get_pathspec(prefix, argv + i);
218
6b5ee137 219 if (diff_setup_done(&diff_options) < 0)
4727f640
JH
220 usage(diff_cache_usage);
221
6c56c534 222 if (!tree_name || get_sha1(tree_name, sha1))
c5bac17a 223 usage(diff_cache_usage);
e74f8f6a 224
d288a700
LT
225 read_cache();
226
b0fe89ca 227 mark_merge_entries();
b5af9107 228
6c56c534 229 tree = read_object_with_reference(sha1, "tree", &size, NULL);
e74f8f6a 230 if (!tree)
6c56c534 231 die("bad tree object %s", tree_name);
a74ba54b 232 if (read_tree(tree, size, 1, pathspec))
6c56c534 233 die("unable to read tree object %s", tree_name);
e74f8f6a 234
fdee7d07 235 ret = diff_cache(active_cache, active_nr, pathspec);
f345b0a0 236
6b5ee137
JH
237 diffcore_std(&diff_options);
238 diff_flush(&diff_options);
5c97558c 239 return ret;
e74f8f6a 240}