@matheusbsbs / matheus-bagliano / commits / e09ad6e1e3
Fork 0

Libify diff-index.

The second installment to libify diff brothers. The pathname arguments are checked more strictly than before because we now use the revision.c::setup_revisions() infrastructure. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Apr 22, 2006 at 02:43 UTC e09ad6e1e3308fde346b4b6287d9441363806832
4 files changed +231 -238
diff-index.c
+21 -233
@@ -1,166 +1,7 @@
1 #include "cache.h"
2 -#include "tree.h"
2 #include "diff.h"
4 -
5 -static int cached_only = 0;
6 -static int match_nonexisting = 0;
7 -static struct diff_options diff_options;
8 -
9 -/* A file entry went away or appeared */
10 -static void show_file(const char *prefix,
11 - struct cache_entry *ce,
12 - unsigned char *sha1, unsigned int mode)
13 -{
14 - diff_addremove(&diff_options, prefix[0], ntohl(mode),
15 - sha1, ce->name, NULL);
16 -}
17 -
18 -static int get_stat_data(struct cache_entry *ce,
19 - unsigned char ** sha1p, unsigned int *modep)
20 -{
21 - unsigned char *sha1 = ce->sha1;
22 - unsigned int mode = ce->ce_mode;
23 -
24 - if (!cached_only) {
25 - static unsigned char no_sha1[20];
26 - int changed;
27 - struct stat st;
28 - if (lstat(ce->name, &st) < 0) {
29 - if (errno == ENOENT && match_nonexisting) {
30 - *sha1p = sha1;
31 - *modep = mode;
32 - return 0;
33 - }
34 - return -1;
35 - }
36 - changed = ce_match_stat(ce, &st, 0);
37 - if (changed) {
38 - mode = create_ce_mode(st.st_mode);
39 - if (!trust_executable_bit && S_ISREG(st.st_mode))
40 - mode = ce->ce_mode;
41 - sha1 = no_sha1;
42 - }
43 - }
44 -
45 - *sha1p = sha1;
46 - *modep = mode;
47 - return 0;
48 -}
49 -
50 -static void show_new_file(struct cache_entry *new)
51 -{
52 - unsigned char *sha1;
53 - unsigned int mode;
54 -
55 - /* New file in the index: it might actually be different in
56 - * the working copy.
57 - */
58 - if (get_stat_data(new, &sha1, &mode) < 0)
59 - return;
60 -
61 - show_file("+", new, sha1, mode);
62 -}
63 -
64 -static int show_modified(struct cache_entry *old,
65 - struct cache_entry *new,
66 - int report_missing)
67 -{
68 - unsigned int mode, oldmode;
69 - unsigned char *sha1;
70 -
71 - if (get_stat_data(new, &sha1, &mode) < 0) {
72 - if (report_missing)
73 - show_file("-", old, old->sha1, old->ce_mode);
74 - return -1;
75 - }
76 -
77 - oldmode = old->ce_mode;
78 - if (mode == oldmode && !memcmp(sha1, old->sha1, 20) &&
79 - !diff_options.find_copies_harder)
80 - return 0;
81 -
82 - mode = ntohl(mode);
83 - oldmode = ntohl(oldmode);
84 -
85 - diff_change(&diff_options, oldmode, mode,
86 - old->sha1, sha1, old->name, NULL);
87 - return 0;
88 -}
89 -
90 -static int diff_cache(struct cache_entry **ac, int entries, const char **pathspec)
91 -{
92 - while (entries) {
93 - struct cache_entry *ce = *ac;
94 - int same = (entries > 1) && ce_same_name(ce, ac[1]);
95 -
96 - if (!ce_path_match(ce, pathspec))
97 - goto skip_entry;
98 -
99 - switch (ce_stage(ce)) {
100 - case 0:
101 - /* No stage 1 entry? That means it's a new file */
102 - if (!same) {
103 - show_new_file(ce);
104 - break;
105 - }
106 - /* Show difference between old and new */
107 - show_modified(ac[1], ce, 1);
108 - break;
109 - case 1:
110 - /* No stage 3 (merge) entry? That means it's been deleted */
111 - if (!same) {
112 - show_file("-", ce, ce->sha1, ce->ce_mode);
113 - break;
114 - }
115 - /* We come here with ce pointing at stage 1
116 - * (original tree) and ac[1] pointing at stage
117 - * 3 (unmerged). show-modified with
118 - * report-missing set to false does not say the
119 - * file is deleted but reports true if work
120 - * tree does not have it, in which case we
121 - * fall through to report the unmerged state.
122 - * Otherwise, we show the differences between
123 - * the original tree and the work tree.
124 - */
125 - if (!cached_only && !show_modified(ce, ac[1], 0))
126 - break;
127 - /* fallthru */
128 - case 3:
129 - diff_unmerge(&diff_options, ce->name);
130 - break;
131 -
132 - default:
133 - die("impossible cache entry stage");
134 - }
135 -
136 -skip_entry:
137 - /*
138 - * Ignore all the different stages for this file,
139 - * we've handled the relevant cases now.
140 - */
141 - do {
142 - ac++;
143 - entries--;
144 - } while (entries && ce_same_name(ce, ac[0]));
145 - }
146 - return 0;
147 -}
148 -
149 -/*
150 - * This turns all merge entries into "stage 3". That guarantees that
151 - * when we read in the new tree (into "stage 1"), we won't lose sight
152 - * of the fact that we had unmerged entries.
153 - */
154 -static void mark_merge_entries(void)
155 -{
156 - int i;
157 - for (i = 0; i < active_nr; i++) {
158 - struct cache_entry *ce = active_cache[i];
159 - if (!ce_stage(ce))
160 - continue;
161 - ce->ce_flags |= htons(CE_STAGEMASK);
162 - }
163 -}
3 +#include "commit.h"
4 +#include "revision.h"
5
6 static const char diff_cache_usage[] =
7 "git-diff-index [-m] [--cached] "
@@ -169,85 +10,32 @@ COMMON_DIFF_OPTIONS_HELP;
10
11 int main(int argc, const char **argv)
12 {
172 - const char *tree_name = NULL;
173 - unsigned char sha1[20];
174 - const char *prefix = setup_git_directory();
175 - const char **pathspec = NULL;
176 - struct tree *tree;
177 - int ret;
178 - int allow_options = 1;
13 + struct rev_info rev;
14 + int match_missing = 0;
15 + int cached = 0;
16 int i;
17
18 git_config(git_diff_config);
182 - diff_setup(&diff_options);
19 + init_revisions(&rev);
20 + rev.abbrev = 0;
21 +
22 + argc = setup_revisions(argc, argv, &rev, NULL);
23 for (i = 1; i < argc; i++) {
24 const char *arg = argv[i];
185 - int diff_opt_cnt;
186 -
187 - if (!allow_options || *arg != '-') {
188 - if (tree_name)
189 - break;
190 - tree_name = arg;
191 - continue;
192 - }
25
194 - if (!strcmp(arg, "--")) {
195 - allow_options = 0;
196 - continue;
197 - }
198 - if (!strcmp(arg, "-r")) {
199 - /* We accept the -r flag just to look like git-diff-tree */
200 - continue;
201 - }
202 - if (!strcmp(arg, "--cc"))
203 - /*
204 - * I _think_ "diff-index --cached HEAD" with an
205 - * unmerged index could show something else
206 - * later, but pretend --cc is the same as -p for
207 - * now. "git diff" uses --cc by default.
208 - */
209 - argv[i] = arg = "-p";
210 - diff_opt_cnt = diff_opt_parse(&diff_options, argv + i,
211 - argc - i);
212 - if (diff_opt_cnt < 0)
26 + if (!strcmp(arg, "-m"))
27 + match_missing = 1;
28 + else if (!strcmp(arg, "--cached"))
29 + cached = 1;
30 + else
31 usage(diff_cache_usage);
214 - else if (diff_opt_cnt) {
215 - i += diff_opt_cnt - 1;
216 - continue;
217 - }
218 -
219 - if (!strcmp(arg, "-m")) {
220 - match_nonexisting = 1;
221 - continue;
222 - }
223 - if (!strcmp(arg, "--cached")) {
224 - cached_only = 1;
225 - continue;
226 - }
227 - usage(diff_cache_usage);
32 }
229 -
230 - pathspec = get_pathspec(prefix, argv + i);
231 -
232 - if (diff_setup_done(&diff_options) < 0)
233 - usage(diff_cache_usage);
234 -
235 - if (!tree_name || get_sha1(tree_name, sha1))
33 + /*
34 + * Make sure there is one revision (i.e. pending object),
35 + * and there is no revision filtering parameters.
36 + */
37 + if (!rev.pending_objects || rev.pending_objects->next ||
38 + rev.max_count != -1 || rev.min_age != -1 || rev.max_age != -1)
39 usage(diff_cache_usage);
237 -
238 - read_cache();
239 -
240 - mark_merge_entries();
241 -
242 - tree = parse_tree_indirect(sha1);
243 - if (!tree)
244 - die("bad tree object %s", tree_name);
245 - if (read_tree(tree, 1, pathspec))
246 - die("unable to read tree object %s", tree_name);
247 -
248 - ret = diff_cache(active_cache, active_nr, pathspec);
249 -
250 - diffcore_std(&diff_options);
251 - diff_flush(&diff_options);
252 - return ret;
40 + return run_diff_index(&rev, cached, match_missing);
41 }
diff-lib.c
+203
@@ -131,3 +131,206 @@ int run_diff_files(struct rev_info *revs, int silent_on_removed)
131 return 0;
132 }
133
134 +/*
135 + * diff-index
136 + */
137 +
138 +/* A file entry went away or appeared */
139 +static void diff_index_show_file(struct rev_info *revs,
140 + const char *prefix,
141 + struct cache_entry *ce,
142 + unsigned char *sha1, unsigned int mode)
143 +{
144 + diff_addremove(&revs->diffopt, prefix[0], ntohl(mode),
145 + sha1, ce->name, NULL);
146 +}
147 +
148 +static int get_stat_data(struct cache_entry *ce,
149 + unsigned char **sha1p,
150 + unsigned int *modep,
151 + int cached, int match_missing)
152 +{
153 + unsigned char *sha1 = ce->sha1;
154 + unsigned int mode = ce->ce_mode;
155 +
156 + if (!cached) {
157 + static unsigned char no_sha1[20];
158 + int changed;
159 + struct stat st;
160 + if (lstat(ce->name, &st) < 0) {
161 + if (errno == ENOENT && match_missing) {
162 + *sha1p = sha1;
163 + *modep = mode;
164 + return 0;
165 + }
166 + return -1;
167 + }
168 + changed = ce_match_stat(ce, &st, 0);
169 + if (changed) {
170 + mode = create_ce_mode(st.st_mode);
171 + if (!trust_executable_bit && S_ISREG(st.st_mode))
172 + mode = ce->ce_mode;
173 + sha1 = no_sha1;
174 + }
175 + }
176 +
177 + *sha1p = sha1;
178 + *modep = mode;
179 + return 0;
180 +}
181 +
182 +static void show_new_file(struct rev_info *revs,
183 + struct cache_entry *new,
184 + int cached, int match_missing)
185 +{
186 + unsigned char *sha1;
187 + unsigned int mode;
188 +
189 + /* New file in the index: it might actually be different in
190 + * the working copy.
191 + */
192 + if (get_stat_data(new, &sha1, &mode, cached, match_missing) < 0)
193 + return;
194 +
195 + diff_index_show_file(revs, "+", new, sha1, mode);
196 +}
197 +
198 +static int show_modified(struct rev_info *revs,
199 + struct cache_entry *old,
200 + struct cache_entry *new,
201 + int report_missing,
202 + int cached, int match_missing)
203 +{
204 + unsigned int mode, oldmode;
205 + unsigned char *sha1;
206 +
207 + if (get_stat_data(new, &sha1, &mode, cached, match_missing) < 0) {
208 + if (report_missing)
209 + diff_index_show_file(revs, "-", old,
210 + old->sha1, old->ce_mode);
211 + return -1;
212 + }
213 +
214 + oldmode = old->ce_mode;
215 + if (mode == oldmode && !memcmp(sha1, old->sha1, 20) &&
216 + !revs->diffopt.find_copies_harder)
217 + return 0;
218 +
219 + mode = ntohl(mode);
220 + oldmode = ntohl(oldmode);
221 +
222 + diff_change(&revs->diffopt, oldmode, mode,
223 + old->sha1, sha1, old->name, NULL);
224 + return 0;
225 +}
226 +
227 +static int diff_cache(struct rev_info *revs,
228 + struct cache_entry **ac, int entries,
229 + const char **pathspec,
230 + int cached, int match_missing)
231 +{
232 + while (entries) {
233 + struct cache_entry *ce = *ac;
234 + int same = (entries > 1) && ce_same_name(ce, ac[1]);
235 +
236 + if (!ce_path_match(ce, pathspec))
237 + goto skip_entry;
238 +
239 + switch (ce_stage(ce)) {
240 + case 0:
241 + /* No stage 1 entry? That means it's a new file */
242 + if (!same) {
243 + show_new_file(revs, ce, cached, match_missing);
244 + break;
245 + }
246 + /* Show difference between old and new */
247 + show_modified(revs,ac[1], ce, 1,
248 + cached, match_missing);
249 + break;
250 + case 1:
251 + /* No stage 3 (merge) entry?
252 + * That means it's been deleted.
253 + */
254 + if (!same) {
255 + diff_index_show_file(revs, "-", ce,
256 + ce->sha1, ce->ce_mode);
257 + break;
258 + }
259 + /* We come here with ce pointing at stage 1
260 + * (original tree) and ac[1] pointing at stage
261 + * 3 (unmerged). show-modified with
262 + * report-missing set to false does not say the
263 + * file is deleted but reports true if work
264 + * tree does not have it, in which case we
265 + * fall through to report the unmerged state.
266 + * Otherwise, we show the differences between
267 + * the original tree and the work tree.
268 + */
269 + if (!cached &&
270 + !show_modified(revs, ce, ac[1], 0,
271 + cached, match_missing))
272 + break;
273 + /* fallthru */
274 + case 3:
275 + diff_unmerge(&revs->diffopt, ce->name);
276 + break;
277 +
278 + default:
279 + die("impossible cache entry stage");
280 + }
281 +
282 +skip_entry:
283 + /*
284 + * Ignore all the different stages for this file,
285 + * we've handled the relevant cases now.
286 + */
287 + do {
288 + ac++;
289 + entries--;
290 + } while (entries && ce_same_name(ce, ac[0]));
291 + }
292 + return 0;
293 +}
294 +
295 +/*
296 + * This turns all merge entries into "stage 3". That guarantees that
297 + * when we read in the new tree (into "stage 1"), we won't lose sight
298 + * of the fact that we had unmerged entries.
299 + */
300 +static void mark_merge_entries(void)
301 +{
302 + int i;
303 + for (i = 0; i < active_nr; i++) {
304 + struct cache_entry *ce = active_cache[i];
305 + if (!ce_stage(ce))
306 + continue;
307 + ce->ce_flags |= htons(CE_STAGEMASK);
308 + }
309 +}
310 +
311 +int run_diff_index(struct rev_info *revs, int cached, int match_missing)
312 +{
313 + int ret;
314 + struct object *ent;
315 + struct tree *tree;
316 + const char *tree_name;
317 +
318 + if (read_cache() < 0) {
319 + perror("read_cache");
320 + return -1;
321 + }
322 + mark_merge_entries();
323 +
324 + ent = revs->pending_objects->item;
325 + tree_name = revs->pending_objects->name;
326 + tree = parse_tree_indirect(ent->sha1);
327 + if (!tree)
328 + return error("bad tree object %s", tree_name);
329 + if (read_tree(tree, 1, revs->prune_data))
330 + return error("unable to read tree object %s", tree_name);
331 + ret = diff_cache(revs, active_cache, active_nr, revs->prune_data,
332 + cached, match_missing);
333 + diffcore_std(&revs->diffopt);
334 + diff_flush(&revs->diffopt);
335 + return ret;
336 +}
diff.h
+2
@@ -171,4 +171,6 @@ extern const char *diff_unique_abbrev(const unsigned char *, int);
171
172 extern int run_diff_files(struct rev_info *revs, int silent_on_removed);
173
174 +extern int run_diff_index(struct rev_info *revs, int cached, int match_missing);
175 +
176 #endif /* DIFF_H */
t/t4010-diff-pathspec.sh
+5 -5
@@ -28,7 +28,7 @@ cat >expected <<\EOF
28 EOF
29 test_expect_success \
30 'limit to path should show nothing' \
31 - 'git-diff-index --cached $tree path >current &&
31 + 'git-diff-index --cached $tree -- path >current &&
32 compare_diff_raw current expected'
33
34 cat >expected <<\EOF
@@ -36,7 +36,7 @@ cat >expected <<\EOF
36 EOF
37 test_expect_success \
38 'limit to path1 should show path1/file1' \
39 - 'git-diff-index --cached $tree path1 >current &&
39 + 'git-diff-index --cached $tree -- path1 >current &&
40 compare_diff_raw current expected'
41
42 cat >expected <<\EOF
@@ -44,7 +44,7 @@ cat >expected <<\EOF
44 EOF
45 test_expect_success \
46 'limit to path1/ should show path1/file1' \
47 - 'git-diff-index --cached $tree path1/ >current &&
47 + 'git-diff-index --cached $tree -- path1/ >current &&
48 compare_diff_raw current expected'
49
50 cat >expected <<\EOF
@@ -52,14 +52,14 @@ cat >expected <<\EOF
52 EOF
53 test_expect_success \
54 'limit to file0 should show file0' \
55 - 'git-diff-index --cached $tree file0 >current &&
55 + 'git-diff-index --cached $tree -- file0 >current &&
56 compare_diff_raw current expected'
57
58 cat >expected <<\EOF
59 EOF
60 test_expect_success \
61 'limit to file0/ should emit nothing.' \
62 - 'git-diff-index --cached $tree file0/ >current &&
62 + 'git-diff-index --cached $tree -- file0/ >current &&
63 compare_diff_raw current expected'
64
65 test_done