]> git.ipfire.org Git - thirdparty/git.git/blame - diff-cache.c
[PATCH] rename git-rpush and git-rpull to git-ssh-push and git-ssh-pull
[thirdparty/git.git] / diff-cache.c
CommitLineData
e74f8f6a 1#include "cache.h"
f92a4465 2#include "diff.h"
e74f8f6a
LT
3
4static int cached_only = 0;
81e50eab 5static int diff_output_format = DIFF_FORMAT_HUMAN;
160c8433 6static int match_nonexisting = 0;
5c97558c 7static int detect_rename = 0;
19feebc8 8static int diff_setup_opt = 0;
57fe64a4 9static int diff_score_opt = 0;
057c7d30 10static const char *pickaxe = NULL;
367cec1c 11static int pickaxe_opts = 0;
f345b0a0 12static int diff_break_opt = -1;
af5323e0 13static const char *orderfile = NULL;
e74f8f6a 14
e74f8f6a 15/* A file entry went away or appeared */
c9cddabe 16static void show_file(const char *prefix, struct cache_entry *ce, unsigned char *sha1, unsigned int mode)
e74f8f6a 17{
57fe64a4 18 diff_addremove(prefix[0], ntohl(mode), sha1, ce->name, NULL);
e74f8f6a
LT
19}
20
c9cddabe 21static int get_stat_data(struct cache_entry *ce, unsigned char **sha1p, unsigned int *modep)
e74f8f6a 22{
c9cddabe
LT
23 unsigned char *sha1 = ce->sha1;
24 unsigned int mode = ce->ce_mode;
e74f8f6a
LT
25
26 if (!cached_only) {
27 static unsigned char no_sha1[20];
b5af9107 28 int changed;
e74f8f6a 29 struct stat st;
160c8433
LT
30 if (lstat(ce->name, &st) < 0) {
31 if (errno == ENOENT && match_nonexisting) {
32 *sha1p = sha1;
33 *modep = mode;
34 return 0;
35 }
e74f8f6a 36 return -1;
160c8433 37 }
5d728c84 38 changed = ce_match_stat(ce, &st);
e74f8f6a 39 if (changed) {
c9cddabe 40 mode = create_ce_mode(st.st_mode);
b5af9107 41 sha1 = no_sha1;
e74f8f6a
LT
42 }
43 }
44
c9cddabe
LT
45 *sha1p = sha1;
46 *modep = mode;
47 return 0;
48}
49
b836825b 50static void show_new_file(struct cache_entry *new)
c9cddabe
LT
51{
52 unsigned char *sha1;
53 unsigned int mode;
54
55 /* New file in the index: it might actually be different in the working copy */
56 if (get_stat_data(new, &sha1, &mode) < 0)
b836825b 57 return;
c9cddabe
LT
58
59 show_file("+", new, sha1, mode);
60}
61
66026590
JH
62static int show_modified(struct cache_entry *old,
63 struct cache_entry *new,
64 int report_missing)
c9cddabe
LT
65{
66 unsigned int mode, oldmode;
67 unsigned char *sha1;
c9cddabe
LT
68
69 if (get_stat_data(new, &sha1, &mode) < 0) {
66026590
JH
70 if (report_missing)
71 show_file("-", old, old->sha1, old->ce_mode);
c9cddabe
LT
72 return -1;
73 }
74
75 oldmode = old->ce_mode;
c3e7fbcb 76 if (mode == oldmode && !memcmp(sha1, old->sha1, 20) &&
6b14d7fa 77 detect_rename < DIFF_DETECT_COPY)
e74f8f6a
LT
78 return 0;
79
c9cddabe
LT
80 mode = ntohl(mode);
81 oldmode = ntohl(oldmode);
82
57fe64a4
JH
83 diff_change(oldmode, mode,
84 old->sha1, sha1, old->name, NULL);
e74f8f6a
LT
85 return 0;
86}
87
b5af9107 88static int diff_cache(struct cache_entry **ac, int entries)
e74f8f6a 89{
b5af9107
LT
90 while (entries) {
91 struct cache_entry *ce = *ac;
dbbce55b 92 int same = (entries > 1) && ce_same_name(ce, ac[1]);
e74f8f6a 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:
57fe64a4 124 diff_unmerge(ce->name);
b0fe89ca
LT
125 break;
126
127 default:
128 die("impossible cache entry stage");
e74f8f6a 129 }
b0fe89ca
LT
130
131 /*
132 * Ignore all the different stages for this file,
133 * we've handled the relevant cases now.
134 */
135 do {
e74f8f6a
LT
136 ac++;
137 entries--;
dbbce55b 138 } while (entries && ce_same_name(ce, ac[0]));
e74f8f6a
LT
139 }
140 return 0;
141}
142
b0fe89ca
LT
143/*
144 * This turns all merge entries into "stage 3". That guarantees that
145 * when we read in the new tree (into "stage 1"), we won't lose sight
146 * of the fact that we had unmerged entries.
147 */
148static void mark_merge_entries(void)
b5af9107
LT
149{
150 int i;
151 for (i = 0; i < active_nr; i++) {
152 struct cache_entry *ce = active_cache[i];
153 if (!ce_stage(ce))
5697ecc7 154 continue;
b0fe89ca 155 ce->ce_flags |= htons(CE_STAGEMASK);
b5af9107
LT
156 }
157}
158
f92a4465 159static char *diff_cache_usage =
ce240675 160"git-diff-cache [-p] [-r] [-z] [-m] [-M] [-C] [-R] [-S<string>] [-O<orderfile>] [--cached] <tree-ish> [<path>...]";
c5bac17a 161
6b14d7fa 162int main(int argc, const char **argv)
e74f8f6a 163{
6c56c534
LT
164 const char *tree_name = NULL;
165 unsigned char sha1[20];
166 const char **pathspec = NULL;
e74f8f6a
LT
167 void *tree;
168 unsigned long size;
5c97558c 169 int ret;
6c56c534 170 int i;
e74f8f6a
LT
171
172 read_cache();
6c56c534
LT
173 for (i = 1; i < argc; i++) {
174 const char *arg = argv[i];
175
176 if (*arg != '-') {
177 if (tree_name) {
178 pathspec = argv + i;
179 break;
180 }
181 tree_name = arg;
182 continue;
183 }
184
e74f8f6a 185 if (!strcmp(arg, "-r")) {
667bb59b 186 /* We accept the -r flag just to look like git-diff-tree */
e74f8f6a
LT
187 continue;
188 }
f92a4465 189 if (!strcmp(arg, "-p")) {
81e50eab 190 diff_output_format = DIFF_FORMAT_PATCH;
f92a4465
JH
191 continue;
192 }
f345b0a0 193 if (!strncmp(arg, "-B", 2)) {
0e3994fa
JH
194 if ((diff_break_opt = diff_scoreopt_parse(arg)) == -1)
195 usage(diff_cache_usage);
f345b0a0
JH
196 continue;
197 }
57fe64a4 198 if (!strncmp(arg, "-M", 2)) {
6b14d7fa 199 detect_rename = DIFF_DETECT_RENAME;
0e3994fa
JH
200 if ((diff_score_opt = diff_scoreopt_parse(arg)) == -1)
201 usage(diff_cache_usage);
5c97558c
JH
202 continue;
203 }
427dcb4b 204 if (!strncmp(arg, "-C", 2)) {
6b14d7fa 205 detect_rename = DIFF_DETECT_COPY;
0e3994fa
JH
206 if ((diff_score_opt = diff_scoreopt_parse(arg)) == -1)
207 usage(diff_cache_usage);
427dcb4b
JH
208 continue;
209 }
e74f8f6a 210 if (!strcmp(arg, "-z")) {
81e50eab 211 diff_output_format = DIFF_FORMAT_MACHINE;
e74f8f6a
LT
212 continue;
213 }
57fe64a4 214 if (!strcmp(arg, "-R")) {
19feebc8 215 diff_setup_opt |= DIFF_SETUP_REVERSE;
57fe64a4
JH
216 continue;
217 }
e25de756 218 if (!strncmp(arg, "-S", 2)) {
52e95789
JH
219 pickaxe = arg + 2;
220 continue;
221 }
af5323e0
JH
222 if (!strncmp(arg, "-O", 2)) {
223 orderfile = arg + 2;
224 continue;
225 }
367cec1c
JH
226 if (!strcmp(arg, "--pickaxe-all")) {
227 pickaxe_opts = DIFF_PICKAXE_ALL;
228 continue;
229 }
160c8433
LT
230 if (!strcmp(arg, "-m")) {
231 match_nonexisting = 1;
232 continue;
233 }
e74f8f6a
LT
234 if (!strcmp(arg, "--cached")) {
235 cached_only = 1;
236 continue;
237 }
c5bac17a 238 usage(diff_cache_usage);
e74f8f6a
LT
239 }
240
6c56c534 241 if (!tree_name || get_sha1(tree_name, sha1))
c5bac17a 242 usage(diff_cache_usage);
e74f8f6a 243
6b14d7fa 244 /* The rest is for paths restriction. */
19feebc8 245 diff_setup(diff_setup_opt);
5c97558c 246
b0fe89ca 247 mark_merge_entries();
b5af9107 248
6c56c534 249 tree = read_object_with_reference(sha1, "tree", &size, NULL);
e74f8f6a 250 if (!tree)
6c56c534 251 die("bad tree object %s", tree_name);
b5af9107 252 if (read_tree(tree, size, 1))
6c56c534 253 die("unable to read tree object %s", tree_name);
e74f8f6a 254
5c97558c 255 ret = diff_cache(active_cache, active_nr);
f345b0a0
JH
256
257 diffcore_std(pathspec ? : NULL,
befe8639 258 detect_rename, diff_score_opt,
f345b0a0 259 pickaxe, pickaxe_opts,
af5323e0
JH
260 diff_break_opt,
261 orderfile);
b6d8f309 262 diff_flush(diff_output_format, 1);
5c97558c 263 return ret;
e74f8f6a 264}