@matheusbsbs / matheus-bagliano / commits / 6b5ee137e5
Fork 0

Diff clean-up.

This is a long overdue clean-up to the code for parsing and passing diff options. It also tightens some constness issues. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Sep 21, 2005 at 00:00 UTC 6b5ee137e56af8093391411389dd4b18416707ec
11 files changed +252 -366
cache.h
+3 -3
@@ -143,9 +143,9 @@ extern char *get_graft_file(void);
143
144 #define ALTERNATE_DB_ENVIRONMENT "GIT_ALTERNATE_OBJECT_DIRECTORIES"
145
146 -extern const char **get_pathspec(const char *prefix, char **pathspec);
146 +extern const char **get_pathspec(const char *prefix, const char **pathspec);
147 extern const char *setup_git_directory(void);
148 -extern char *prefix_path(const char *prefix, int len, char *path);
148 +extern const char *prefix_path(const char *prefix, int len, const char *path);
149
150 #define alloc_nr(x) (((x)+16)*3/2)
151
@@ -158,7 +158,7 @@ extern int cache_name_pos(const char *name, int namelen);
158 #define ADD_CACHE_SKIP_DFCHECK 4 /* Ok to skip DF conflict checks */
159 extern int add_cache_entry(struct cache_entry *ce, int option);
160 extern int remove_cache_entry_at(int pos);
161 -extern int remove_file_from_cache(char *path);
161 +extern int remove_file_from_cache(const char *path);
162 extern int ce_same_name(struct cache_entry *a, struct cache_entry *b);
163 extern int ce_match_stat(struct cache_entry *ce, struct stat *st);
164 extern int ce_modified(struct cache_entry *ce, struct stat *st);
diff-files.c
+23 -60
@@ -11,87 +11,56 @@ static const char diff_files_usage[] =
11 "[<common diff options>] [<path>...]"
12 COMMON_DIFF_OPTIONS_HELP;
13
14 -static int diff_output_format = DIFF_FORMAT_RAW;
15 -static int diff_line_termination = '\n';
16 -static int detect_rename = 0;
17 -static int find_copies_harder = 0;
18 -static int diff_setup_opt = 0;
19 -static int diff_score_opt = 0;
20 -static const char *pickaxe = NULL;
21 -static int pickaxe_opts = 0;
22 -static int diff_break_opt = -1;
23 -static const char *orderfile = NULL;
24 -static const char *diff_filter = NULL;
14 +static struct diff_options diff_options;
15 static int silent = 0;
16
17 static void show_unmerge(const char *path)
18 {
29 - diff_unmerge(path);
19 + diff_unmerge(&diff_options, path);
20 }
21
22 static void show_file(int pfx, struct cache_entry *ce)
23 {
34 - diff_addremove(pfx, ntohl(ce->ce_mode), ce->sha1, ce->name, NULL);
24 + diff_addremove(&diff_options, pfx, ntohl(ce->ce_mode),
25 + ce->sha1, ce->name, NULL);
26 }
27
28 static void show_modified(int oldmode, int mode,
29 const unsigned char *old_sha1, const unsigned char *sha1,
30 char *path)
31 {
41 - diff_change(oldmode, mode, old_sha1, sha1, path, NULL);
32 + diff_change(&diff_options, oldmode, mode, old_sha1, sha1, path, NULL);
33 }
34
44 -int main(int argc, char **argv)
35 +int main(int argc, const char **argv)
36 {
37 static const unsigned char null_sha1[20] = { 0, };
38 const char **pathspec;
39 const char *prefix = setup_git_directory();
40 int entries, i;
41
42 + diff_setup(&diff_options);
43 while (1 < argc && argv[1][0] == '-') {
52 - if (!strcmp(argv[1], "-p") || !strcmp(argv[1], "-u"))
53 - diff_output_format = DIFF_FORMAT_PATCH;
54 - else if (!strcmp(argv[1], "-q"))
44 + if (!strcmp(argv[1], "-q"))
45 silent = 1;
46 else if (!strcmp(argv[1], "-r"))
47 ; /* no-op */
48 else if (!strcmp(argv[1], "-s"))
49 ; /* no-op */
60 - else if (!strcmp(argv[1], "-z"))
61 - diff_line_termination = 0;
62 - else if (!strcmp(argv[1], "--name-only"))
63 - diff_output_format = DIFF_FORMAT_NAME;
64 - else if (!strcmp(argv[1], "-R"))
65 - diff_setup_opt |= DIFF_SETUP_REVERSE;
66 - else if (!strncmp(argv[1], "-S", 2))
67 - pickaxe = argv[1] + 2;
68 - else if (!strncmp(argv[1], "-O", 2))
69 - orderfile = argv[1] + 2;
70 - else if (!strncmp(argv[1], "--diff-filter=", 14))
71 - diff_filter = argv[1] + 14;
72 - else if (!strcmp(argv[1], "--pickaxe-all"))
73 - pickaxe_opts = DIFF_PICKAXE_ALL;
74 - else if (!strncmp(argv[1], "-B", 2)) {
75 - if ((diff_break_opt =
76 - diff_scoreopt_parse(argv[1])) == -1)
50 + else {
51 + int diff_opt_cnt;
52 + diff_opt_cnt = diff_opt_parse(&diff_options,
53 + argv+1, argc-1);
54 + if (diff_opt_cnt < 0)
55 usage(diff_files_usage);
78 - }
79 - else if (!strncmp(argv[1], "-M", 2)) {
80 - if ((diff_score_opt =
81 - diff_scoreopt_parse(argv[1])) == -1)
82 - usage(diff_files_usage);
83 - detect_rename = DIFF_DETECT_RENAME;
84 - }
85 - else if (!strncmp(argv[1], "-C", 2)) {
86 - if ((diff_score_opt =
87 - diff_scoreopt_parse(argv[1])) == -1)
56 + else if (diff_opt_cnt) {
57 + argv += diff_opt_cnt;
58 + argc -= diff_opt_cnt;
59 + continue;
60 + }
61 + else
62 usage(diff_files_usage);
89 - detect_rename = DIFF_DETECT_COPY;
63 }
91 - else if (!strcmp(argv[1], "--find-copies-harder"))
92 - find_copies_harder = 1;
93 - else
94 - usage(diff_files_usage);
64 argv++; argc--;
65 }
66
@@ -99,7 +68,7 @@ int main(int argc, char **argv)
68 pathspec = get_pathspec(prefix, argv + 1);
69 entries = read_cache();
70
102 - if (find_copies_harder && detect_rename != DIFF_DETECT_COPY)
71 + if (diff_setup_done(&diff_options) < 0)
72 usage(diff_files_usage);
73
74 /* At this point, if argc == 1, then we are doing everything.
@@ -110,8 +79,6 @@ int main(int argc, char **argv)
79 exit(1);
80 }
81
113 - diff_setup(diff_setup_opt);
114 -
82 for (i = 0; i < entries; i++) {
83 struct stat st;
84 unsigned int oldmode;
@@ -141,18 +108,14 @@ int main(int argc, char **argv)
108 continue;
109 }
110 changed = ce_match_stat(ce, &st);
144 - if (!changed && !find_copies_harder)
111 + if (!changed && !diff_options.find_copies_harder)
112 continue;
113 oldmode = ntohl(ce->ce_mode);
114 show_modified(oldmode, DIFF_FILE_CANON_MODE(st.st_mode),
115 ce->sha1, (changed ? null_sha1 : ce->sha1),
116 ce->name);
117 }
151 - diffcore_std(pathspec,
152 - detect_rename, diff_score_opt,
153 - pickaxe, pickaxe_opts,
154 - diff_break_opt,
155 - orderfile, diff_filter);
156 - diff_flush(diff_output_format, diff_line_termination);
118 + diffcore_std(&diff_options);
119 + diff_flush(&diff_options);
120 return 0;
121 }
diff-index.c
+24 -80
@@ -2,26 +2,20 @@
2 #include "diff.h"
3
4 static int cached_only = 0;
5 -static int diff_output_format = DIFF_FORMAT_RAW;
6 -static int diff_line_termination = '\n';
5 static int match_nonexisting = 0;
8 -static int detect_rename = 0;
9 -static int find_copies_harder = 0;
10 -static int diff_setup_opt = 0;
11 -static int diff_score_opt = 0;
12 -static const char *pickaxe = NULL;
13 -static int pickaxe_opts = 0;
14 -static int diff_break_opt = -1;
15 -static const char *orderfile = NULL;
16 -static const char *diff_filter = NULL;
6 +static struct diff_options diff_options;
7
8 /* A file entry went away or appeared */
19 -static void show_file(const char *prefix, struct cache_entry *ce, unsigned char *sha1, unsigned int mode)
9 +static void show_file(const char *prefix,
10 + struct cache_entry *ce,
11 + unsigned char *sha1, unsigned int mode)
12 {
21 - diff_addremove(prefix[0], ntohl(mode), sha1, ce->name, NULL);
13 + diff_addremove(&diff_options, prefix[0], ntohl(mode),
14 + sha1, ce->name, NULL);
15 }
16
24 -static int get_stat_data(struct cache_entry *ce, unsigned char **sha1p, unsigned int *modep)
17 +static int get_stat_data(struct cache_entry *ce,
18 + unsigned char **sha1p, unsigned int *modep)
19 {
20 unsigned char *sha1 = ce->sha1;
21 unsigned int mode = ce->ce_mode;
@@ -77,13 +71,13 @@ static int show_modified(struct cache_entry *old,
71
72 oldmode = old->ce_mode;
73 if (mode == oldmode && !memcmp(sha1, old->sha1, 20) &&
80 - !find_copies_harder)
74 + !diff_options.find_copies_harder)
75 return 0;
76
77 mode = ntohl(mode);
78 oldmode = ntohl(oldmode);
79
86 - diff_change(oldmode, mode,
80 + diff_change(&diff_options, oldmode, mode,
81 old->sha1, sha1, old->name, NULL);
82 return 0;
83 }
@@ -127,7 +121,7 @@ static int diff_cache(struct cache_entry **ac, int entries, const char **pathspe
121 break;
122 /* fallthru */
123 case 3:
130 - diff_unmerge(ce->name);
124 + diff_unmerge(&diff_options, ce->name);
125 break;
126
127 default:
@@ -168,7 +162,7 @@ static const char diff_cache_usage[] =
162 "[<common diff options>] <tree-ish> [<path>...]"
163 COMMON_DIFF_OPTIONS_HELP;
164
171 -int main(int argc, char **argv)
165 +int main(int argc, const char **argv)
166 {
167 const char *tree_name = NULL;
168 unsigned char sha1[20];
@@ -180,8 +174,10 @@ int main(int argc, char **argv)
174 int allow_options = 1;
175 int i;
176
177 + diff_setup(&diff_options);
178 for (i = 1; i < argc; i++) {
179 const char *arg = argv[i];
180 + int diff_opt_cnt;
181
182 if (!allow_options || *arg != '-') {
183 if (tree_name)
@@ -198,60 +194,15 @@ int main(int argc, char **argv)
194 /* We accept the -r flag just to look like git-diff-tree */
195 continue;
196 }
201 - /* We accept the -u flag as a synonym for "-p" */
202 - if (!strcmp(arg, "-p") || !strcmp(arg, "-u")) {
203 - diff_output_format = DIFF_FORMAT_PATCH;
204 - continue;
205 - }
206 - if (!strncmp(arg, "-B", 2)) {
207 - if ((diff_break_opt = diff_scoreopt_parse(arg)) == -1)
208 - usage(diff_cache_usage);
209 - continue;
210 - }
211 - if (!strncmp(arg, "-M", 2)) {
212 - detect_rename = DIFF_DETECT_RENAME;
213 - if ((diff_score_opt = diff_scoreopt_parse(arg)) == -1)
214 - usage(diff_cache_usage);
215 - continue;
216 - }
217 - if (!strncmp(arg, "-C", 2)) {
218 - detect_rename = DIFF_DETECT_COPY;
219 - if ((diff_score_opt = diff_scoreopt_parse(arg)) == -1)
220 - usage(diff_cache_usage);
221 - continue;
222 - }
223 - if (!strcmp(arg, "--find-copies-harder")) {
224 - find_copies_harder = 1;
225 - continue;
226 - }
227 - if (!strcmp(arg, "-z")) {
228 - diff_line_termination = 0;
229 - continue;
230 - }
231 - if (!strcmp(arg, "--name-only")) {
232 - diff_output_format = DIFF_FORMAT_NAME;
233 - continue;
234 - }
235 - if (!strcmp(arg, "-R")) {
236 - diff_setup_opt |= DIFF_SETUP_REVERSE;
237 - continue;
238 - }
239 - if (!strncmp(arg, "-S", 2)) {
240 - pickaxe = arg + 2;
241 - continue;
242 - }
243 - if (!strncmp(arg, "--diff-filter=", 14)) {
244 - diff_filter = arg + 14;
245 - continue;
246 - }
247 - if (!strncmp(arg, "-O", 2)) {
248 - orderfile = arg + 2;
249 - continue;
250 - }
251 - if (!strcmp(arg, "--pickaxe-all")) {
252 - pickaxe_opts = DIFF_PICKAXE_ALL;
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;
203 continue;
204 }
205 +
206 if (!strcmp(arg, "-m")) {
207 match_nonexisting = 1;
208 continue;
@@ -265,7 +216,7 @@ int main(int argc, char **argv)
216
217 pathspec = get_pathspec(prefix, argv + i);
218
268 - if (find_copies_harder && detect_rename != DIFF_DETECT_COPY)
219 + if (diff_setup_done(&diff_options) < 0)
220 usage(diff_cache_usage);
221
222 if (!tree_name || get_sha1(tree_name, sha1))
@@ -273,9 +224,6 @@ int main(int argc, char **argv)
224
225 read_cache();
226
276 - /* The rest is for paths restriction. */
277 - diff_setup(diff_setup_opt);
278 -
227 mark_merge_entries();
228
229 tree = read_object_with_reference(sha1, "tree", &size, NULL);
@@ -286,11 +234,7 @@ int main(int argc, char **argv)
234
235 ret = diff_cache(active_cache, active_nr, pathspec);
236
289 - diffcore_std(pathspec,
290 - detect_rename, diff_score_opt,
291 - pickaxe, pickaxe_opts,
292 - diff_break_opt,
293 - orderfile, diff_filter);
294 - diff_flush(diff_output_format, diff_line_termination);
237 + diffcore_std(&diff_options);
238 + diff_flush(&diff_options);
239 return ret;
240 }
diff-stages.c
+25 -57
@@ -5,17 +5,7 @@
5 #include "cache.h"
6 #include "diff.h"
7
8 -static int diff_output_format = DIFF_FORMAT_RAW;
9 -static int diff_line_termination = '\n';
10 -static int detect_rename = 0;
11 -static int find_copies_harder = 0;
12 -static int diff_setup_opt = 0;
13 -static int diff_score_opt = 0;
14 -static const char *pickaxe = NULL;
15 -static int pickaxe_opts = 0;
16 -static int diff_break_opt = -1;
17 -static const char *orderfile = NULL;
18 -static const char *diff_filter = NULL;
8 +static struct diff_options diff_options;
9
10 static const char diff_stages_usage[] =
11 "git-diff-stages [<common diff options>] <stage1> <stage2> [<path>...]"
@@ -47,15 +37,16 @@ static void diff_stages(int stage1, int stage2)
37 if (!one && !two)
38 continue;
39 if (!one)
50 - diff_addremove('+', ntohl(two->ce_mode),
40 + diff_addremove(&diff_options, '+', ntohl(two->ce_mode),
41 two->sha1, name, NULL);
42 else if (!two)
53 - diff_addremove('-', ntohl(one->ce_mode),
43 + diff_addremove(&diff_options, '-', ntohl(one->ce_mode),
44 one->sha1, name, NULL);
45 else if (memcmp(one->sha1, two->sha1, 20) ||
46 (one->ce_mode != two->ce_mode) ||
57 - find_copies_harder)
58 - diff_change(ntohl(one->ce_mode), ntohl(two->ce_mode),
47 + diff_options.find_copies_harder)
48 + diff_change(&diff_options,
49 + ntohl(one->ce_mode), ntohl(two->ce_mode),
50 one->sha1, two->sha1, name, NULL);
51 }
52 }
@@ -65,44 +56,25 @@ int main(int ac, const char **av)
56 int stage1, stage2;
57
58 read_cache();
59 + diff_setup(&diff_options);
60 while (1 < ac && av[1][0] == '-') {
61 const char *arg = av[1];
62 if (!strcmp(arg, "-r"))
63 ; /* as usual */
72 - else if (!strcmp(arg, "-p") || !strcmp(arg, "-u"))
73 - diff_output_format = DIFF_FORMAT_PATCH;
74 - else if (!strncmp(arg, "-B", 2)) {
75 - if ((diff_break_opt = diff_scoreopt_parse(arg)) == -1)
64 + else {
65 + int diff_opt_cnt;
66 + diff_opt_cnt = diff_opt_parse(&diff_options,
67 + av+1, ac-1);
68 + if (diff_opt_cnt < 0)
69 usage(diff_stages_usage);
77 - }
78 - else if (!strncmp(arg, "-M", 2)) {
79 - detect_rename = DIFF_DETECT_RENAME;
80 - if ((diff_score_opt = diff_scoreopt_parse(arg)) == -1)
81 - usage(diff_stages_usage);
82 - }
83 - else if (!strncmp(arg, "-C", 2)) {
84 - detect_rename = DIFF_DETECT_COPY;
85 - if ((diff_score_opt = diff_scoreopt_parse(arg)) == -1)
70 + else if (diff_opt_cnt) {
71 + av += diff_opt_cnt;
72 + ac -= diff_opt_cnt;
73 + continue;
74 + }
75 + else
76 usage(diff_stages_usage);
77 }
88 - else if (!strcmp(arg, "--find-copies-harder"))
89 - find_copies_harder = 1;
90 - else if (!strcmp(arg, "-z"))
91 - diff_line_termination = 0;
92 - else if (!strcmp(arg, "--name-only"))
93 - diff_output_format = DIFF_FORMAT_NAME;
94 - else if (!strcmp(arg, "-R"))
95 - diff_setup_opt |= DIFF_SETUP_REVERSE;
96 - else if (!strncmp(arg, "-S", 2))
97 - pickaxe = arg + 2;
98 - else if (!strncmp(arg, "-O", 2))
99 - orderfile = arg + 2;
100 - else if (!strncmp(arg, "--diff-filter=", 14))
101 - diff_filter = arg + 14;
102 - else if (!strcmp(arg, "--pickaxe-all"))
103 - pickaxe_opts = DIFF_PICKAXE_ALL;
104 - else
105 - usage(diff_stages_usage);
78 ac--; av++;
79 }
80
@@ -110,21 +82,17 @@ int main(int ac, const char **av)
82 sscanf(av[1], "%d", &stage1) != 1 ||
83 ! (0 <= stage1 && stage1 <= 3) ||
84 sscanf(av[2], "%d", &stage2) != 1 ||
113 - ! (0 <= stage2 && stage2 <= 3) ||
114 - (find_copies_harder && detect_rename != DIFF_DETECT_COPY))
85 + ! (0 <= stage2 && stage2 <= 3))
86 usage(diff_stages_usage);
87
88 av += 3; /* The rest from av[0] are for paths restriction. */
118 - diff_setup(diff_setup_opt);
89 + diff_options.paths = av;
90
120 - diff_stages(stage1, stage2);
91 + if (diff_setup_done(&diff_options) < 0)
92 + usage(diff_stages_usage);
93
122 - diffcore_std(av,
123 - detect_rename, diff_score_opt,
124 - pickaxe, pickaxe_opts,
125 - diff_break_opt,
126 - orderfile,
127 - diff_filter);
128 - diff_flush(diff_output_format, diff_line_termination);
94 + diff_stages(stage1, stage2);
95 + diffcore_std(&diff_options);
96 + diff_flush(&diff_options);
97 return 0;
98 }
diff-tree.c
+39 -92
@@ -9,17 +9,9 @@ static int ignore_merges = 1;
9 static int recursive = 0;
10 static int show_tree_entry_in_recursive = 0;
11 static int read_stdin = 0;
12 -static int diff_output_format = DIFF_FORMAT_RAW;
13 -static int diff_line_termination = '\n';
14 -static int detect_rename = 0;
15 -static int find_copies_harder = 0;
16 -static int diff_setup_opt = 0;
17 -static int diff_score_opt = 0;
18 -static const char *pickaxe = NULL;
19 -static int pickaxe_opts = 0;
20 -static int diff_break_opt = -1;
21 -static const char *orderfile = NULL;
22 -static const char *diff_filter = NULL;
12 +
13 +static struct diff_options diff_options;
14 +
15 static const char *header = NULL;
16 static const char *header_prefix = "";
17 static enum cmit_fmt commit_format = CMIT_FMT_RAW;
@@ -94,7 +86,7 @@ static void show_file(const char *prefix, void *tree, unsigned long size, const
86 return;
87 }
88
97 - diff_addremove(prefix[0], mode, sha1, base, path);
89 + diff_addremove(&diff_options, prefix[0], mode, sha1, base, path);
90 }
91
92 static int compare_tree_entry(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
@@ -118,7 +110,8 @@ static int compare_tree_entry(void *tree1, unsigned long size1, void *tree2, uns
110 show_file("+", tree2, size2, base);
111 return 1;
112 }
121 - if (!find_copies_harder && !memcmp(sha1, sha2, 20) && mode1 == mode2)
113 + if (!diff_options.find_copies_harder &&
114 + !memcmp(sha1, sha2, 20) && mode1 == mode2)
115 return 0;
116
117 /*
@@ -135,13 +128,14 @@ static int compare_tree_entry(void *tree1, unsigned long size1, void *tree2, uns
128 int retval;
129 char *newbase = malloc_base(base, path1, pathlen1);
130 if (show_tree_entry_in_recursive)
138 - diff_change(mode1, mode2, sha1, sha2, base, path1);
131 + diff_change(&diff_options, mode1, mode2,
132 + sha1, sha2, base, path1);
133 retval = diff_tree_sha1(sha1, sha2, newbase);
134 free(newbase);
135 return retval;
136 }
137
144 - diff_change(mode1, mode2, sha1, sha2, base, path1);
138 + diff_change(&diff_options, mode1, mode2, sha1, sha2, base, path1);
139 return 0;
140 }
141
@@ -263,28 +257,26 @@ static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, co
257 return retval;
258 }
259
266 -static void call_diff_setup(void)
260 +static void call_diff_setup_done(void)
261 {
268 - diff_setup(diff_setup_opt);
262 + diff_setup_done(&diff_options);
263 }
264
265 static int call_diff_flush(void)
266 {
273 - diffcore_std(NULL,
274 - detect_rename, diff_score_opt,
275 - pickaxe, pickaxe_opts,
276 - diff_break_opt,
277 - orderfile,
278 - diff_filter);
267 + diffcore_std(&diff_options);
268 if (diff_queue_is_empty()) {
280 - diff_flush(DIFF_FORMAT_NO_OUTPUT, diff_line_termination);
269 + int saved_fmt = diff_options.output_format;
270 + diff_options.output_format = DIFF_FORMAT_NO_OUTPUT;
271 + diff_flush(&diff_options);
272 + diff_options.output_format = saved_fmt;
273 return 0;
274 }
275 if (header) {
284 - printf("%s%c", header, diff_line_termination);
276 + printf("%s%c", header, diff_options.line_termination);
277 header = NULL;
278 }
287 - diff_flush(diff_output_format, diff_line_termination);
279 + diff_flush(&diff_options);
280 return 1;
281 }
282
@@ -293,7 +285,7 @@ static int diff_tree_sha1_top(const unsigned char *old,
285 {
286 int ret;
287
296 - call_diff_setup();
288 + call_diff_setup_done();
289 ret = diff_tree_sha1(old, new, base);
290 call_diff_flush();
291 return ret;
@@ -305,7 +297,7 @@ static int diff_root_tree(const unsigned char *new, const char *base)
297 void *tree;
298 unsigned long size;
299
308 - call_diff_setup();
300 + call_diff_setup_done();
301 tree = read_object_with_reference(new, "tree", &size, NULL);
302 if (!tree)
303 die("unable to read root tree (%s)", sha1_to_hex(new));
@@ -409,7 +401,7 @@ static const char diff_tree_usage[] =
401 "[<common diff options>] <tree-ish> <tree-ish>"
402 COMMON_DIFF_OPTIONS_HELP;
403
412 -int main(int argc, char **argv)
404 +int main(int argc, const char **argv)
405 {
406 int nr_sha1;
407 char line[1000];
@@ -417,7 +409,10 @@ int main(int argc, char **argv)
409 const char *prefix = setup_git_directory();
410
411 nr_sha1 = 0;
412 + diff_setup(&diff_options);
413 +
414 for (;;) {
415 + int diff_opt_cnt;
416 const char *arg;
417
418 argv++;
@@ -434,6 +429,16 @@ int main(int argc, char **argv)
429 break;
430 }
431
432 + diff_opt_cnt = diff_opt_parse(&diff_options, argv, argc);
433 + if (diff_opt_cnt < 0)
434 + usage(diff_tree_usage);
435 + else if (diff_opt_cnt) {
436 + argv += diff_opt_cnt - 1;
437 + argc -= diff_opt_cnt - 1;
438 + continue;
439 + }
440 +
441 +
442 if (!strcmp(arg, "--")) {
443 argv++;
444 argc--;
@@ -447,68 +452,10 @@ int main(int argc, char **argv)
452 recursive = show_tree_entry_in_recursive = 1;
453 continue;
454 }
450 - if (!strcmp(arg, "-R")) {
451 - diff_setup_opt |= DIFF_SETUP_REVERSE;
452 - continue;
453 - }
454 - if (!strcmp(arg, "-p") || !strcmp(arg, "-u")) {
455 - diff_output_format = DIFF_FORMAT_PATCH;
456 - recursive = 1;
457 - continue;
458 - }
459 - if (!strncmp(arg, "-S", 2)) {
460 - pickaxe = arg + 2;
461 - continue;
462 - }
463 - if (!strncmp(arg, "-O", 2)) {
464 - orderfile = arg + 2;
465 - continue;
466 - }
467 - if (!strncmp(arg, "--diff-filter=", 14)) {
468 - diff_filter = arg + 14;
469 - continue;
470 - }
471 - if (!strcmp(arg, "--pickaxe-all")) {
472 - pickaxe_opts = DIFF_PICKAXE_ALL;
473 - continue;
474 - }
475 - if (!strncmp(arg, "-M", 2)) {
476 - detect_rename = DIFF_DETECT_RENAME;
477 - if ((diff_score_opt = diff_scoreopt_parse(arg)) == -1)
478 - usage(diff_tree_usage);
479 - continue;
480 - }
481 - if (!strncmp(arg, "-C", 2)) {
482 - detect_rename = DIFF_DETECT_COPY;
483 - if ((diff_score_opt = diff_scoreopt_parse(arg)) == -1)
484 - usage(diff_tree_usage);
485 - continue;
486 - }
487 - if (!strncmp(arg, "-B", 2)) {
488 - if ((diff_break_opt = diff_scoreopt_parse(arg)) == -1)
489 - usage(diff_tree_usage);
490 - continue;
491 - }
492 - if (!strcmp(arg, "--find-copies-harder")) {
493 - find_copies_harder = 1;
494 - continue;
495 - }
496 - if (!strcmp(arg, "--name-only")) {
497 - diff_output_format = DIFF_FORMAT_NAME;
498 - continue;
499 - }
500 - if (!strcmp(arg, "-z")) {
501 - diff_line_termination = 0;
502 - continue;
503 - }
455 if (!strcmp(arg, "-m")) {
456 ignore_merges = 0;
457 continue;
458 }
508 - if (!strcmp(arg, "-s")) {
509 - diff_output_format = DIFF_FORMAT_NO_OUTPUT;
510 - continue;
511 - }
459 if (!strcmp(arg, "-v")) {
460 verbose_header = 1;
461 header_prefix = "diff-tree ";
@@ -530,8 +477,8 @@ int main(int argc, char **argv)
477 }
478 usage(diff_tree_usage);
479 }
533 - if (find_copies_harder && detect_rename != DIFF_DETECT_COPY)
534 - usage(diff_tree_usage);
480 + if (diff_options.output_format == DIFF_FORMAT_PATCH)
481 + recursive = 1;
482
483 paths = get_pathspec(prefix, argv);
484 if (paths) {
@@ -559,9 +506,9 @@ int main(int argc, char **argv)
506 if (!read_stdin)
507 return 0;
508
562 - if (detect_rename)
563 - diff_setup_opt |= (DIFF_SETUP_USE_SIZE_CACHE |
564 - DIFF_SETUP_USE_CACHE);
509 + if (diff_options.detect_rename)
510 + diff_options.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
511 + DIFF_SETUP_USE_CACHE);
512 while (fgets(line, sizeof(line), stdin))
513 diff_tree_stdin(line);
514
diff.c
+93 -44
@@ -12,7 +12,6 @@
12 static const char *diff_opts = "-pu";
13 static unsigned char null_sha1[20] = { 0, };
14
15 -static int reverse_diff;
15 static int use_size_cache;
16
17 static const char *external_diff(void)
@@ -669,11 +668,20 @@ static void run_diff(struct diff_filepair *p)
668 complete_rewrite);
669 }
670
672 -void diff_setup(int flags)
671 +void diff_setup(struct diff_options *options)
672 {
674 - if (flags & DIFF_SETUP_REVERSE)
675 - reverse_diff = 1;
676 - if (flags & DIFF_SETUP_USE_CACHE) {
673 + memset(options, 0, sizeof(*options));
674 + options->output_format = DIFF_FORMAT_RAW;
675 + options->line_termination = '\n';
676 + options->break_opt = -1;
677 +}
678 +
679 +int diff_setup_done(struct diff_options *options)
680 +{
681 + if (options->find_copies_harder &&
682 + options->detect_rename != DIFF_DETECT_COPY)
683 + return -1;
684 + if (options->setup & DIFF_SETUP_USE_CACHE) {
685 if (!active_cache)
686 /* read-cache does not die even when it fails
687 * so it is safe for us to do this here. Also
@@ -683,9 +691,55 @@ void diff_setup(int flags)
691 */
692 read_cache();
693 }
686 - if (flags & DIFF_SETUP_USE_SIZE_CACHE)
694 + if (options->setup & DIFF_SETUP_USE_SIZE_CACHE)
695 use_size_cache = 1;
688 -
696 +
697 + return 0;
698 +}
699 +
700 +int diff_opt_parse(struct diff_options *options, const char **av, int ac)
701 +{
702 + const char *arg = av[0];
703 + if (!strcmp(arg, "-p") || !strcmp(arg, "-u"))
704 + options->output_format = DIFF_FORMAT_PATCH;
705 + else if (!strcmp(arg, "-z"))
706 + options->line_termination = 0;
707 + else if (!strcmp(arg, "--name-only"))
708 + options->output_format = DIFF_FORMAT_NAME;
709 + else if (!strcmp(arg, "-R"))
710 + options->reverse_diff = 1;
711 + else if (!strncmp(arg, "-S", 2))
712 + options->pickaxe = arg + 2;
713 + else if (!strcmp(arg, "-s"))
714 + options->output_format = DIFF_FORMAT_NO_OUTPUT;
715 + else if (!strncmp(arg, "-O", 2))
716 + options->orderfile = arg + 2;
717 + else if (!strncmp(arg, "--diff-filter=", 14))
718 + options->filter = arg + 14;
719 + else if (!strcmp(arg, "--pickaxe-all"))
720 + options->pickaxe_opts = DIFF_PICKAXE_ALL;
721 + else if (!strncmp(arg, "-B", 2)) {
722 + if ((options->break_opt =
723 + diff_scoreopt_parse(arg)) == -1)
724 + return -1;
725 + }
726 + else if (!strncmp(arg, "-M", 2)) {
727 + if ((options->rename_score =
728 + diff_scoreopt_parse(arg)) == -1)
729 + return -1;
730 + options->detect_rename = DIFF_DETECT_RENAME;
731 + }
732 + else if (!strncmp(arg, "-C", 2)) {
733 + if ((options->rename_score =
734 + diff_scoreopt_parse(arg)) == -1)
735 + return -1;
736 + options->detect_rename = DIFF_DETECT_COPY;
737 + }
738 + else if (!strcmp(arg, "--find-copies-harder"))
739 + options->find_copies_harder = 1;
740 + else
741 + return 0;
742 + return 1;
743 }
744
745 static int parse_num(const char **cp_p)
@@ -987,23 +1041,25 @@ static void diff_resolve_rename_copy(void)
1041 diff_debug_queue("resolve-rename-copy done", q);
1042 }
1043
990 -void diff_flush(int diff_output_style, int line_termination)
1044 +void diff_flush(struct diff_options *options)
1045 {
1046 struct diff_queue_struct *q = &diff_queued_diff;
1047 int i;
1048 int inter_name_termination = '\t';
1049 + int diff_output_format = options->output_format;
1050 + int line_termination = options->line_termination;
1051
1052 if (!line_termination)
1053 inter_name_termination = 0;
1054
1055 for (i = 0; i < q->nr; i++) {
1056 struct diff_filepair *p = q->queue[i];
1001 - if ((diff_output_style == DIFF_FORMAT_NO_OUTPUT) ||
1057 + if ((diff_output_format == DIFF_FORMAT_NO_OUTPUT) ||
1058 (p->status == DIFF_STATUS_UNKNOWN))
1059 continue;
1060 if (p->status == 0)
1061 die("internal error in diff-resolve-rename-copy");
1006 - switch (diff_output_style) {
1062 + switch (diff_output_format) {
1063 case DIFF_FORMAT_PATCH:
1064 diff_flush_patch(p);
1065 break;
@@ -1078,45 +1134,36 @@ static void diffcore_apply_filter(const char *filter)
1134 *q = outq;
1135 }
1136
1081 -void diffcore_std(const char **paths,
1082 - int detect_rename, int rename_score,
1083 - const char *pickaxe, int pickaxe_opts,
1084 - int break_opt,
1085 - const char *orderfile,
1086 - const char *filter)
1137 +void diffcore_std(struct diff_options *options)
1138 {
1088 - if (paths && paths[0])
1089 - diffcore_pathspec(paths);
1090 - if (break_opt != -1)
1091 - diffcore_break(break_opt);
1092 - if (detect_rename)
1093 - diffcore_rename(detect_rename, rename_score);
1094 - if (break_opt != -1)
1139 + if (options->paths && options->paths[0])
1140 + diffcore_pathspec(options->paths);
1141 + if (options->break_opt != -1)
1142 + diffcore_break(options->break_opt);
1143 + if (options->detect_rename)
1144 + diffcore_rename(options->detect_rename, options->rename_score);
1145 + if (options->break_opt != -1)
1146 diffcore_merge_broken();
1096 - if (pickaxe)
1097 - diffcore_pickaxe(pickaxe, pickaxe_opts);
1098 - if (orderfile)
1099 - diffcore_order(orderfile);
1147 + if (options->pickaxe)
1148 + diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
1149 + if (options->orderfile)
1150 + diffcore_order(options->orderfile);
1151 diff_resolve_rename_copy();
1101 - diffcore_apply_filter(filter);
1152 + diffcore_apply_filter(options->filter);
1153 }
1154
1155
1105 -void diffcore_std_no_resolve(const char **paths,
1106 - const char *pickaxe, int pickaxe_opts,
1107 - const char *orderfile,
1108 - const char *filter)
1156 +void diffcore_std_no_resolve(struct diff_options *options)
1157 {
1110 - if (paths && paths[0])
1111 - diffcore_pathspec(paths);
1112 - if (pickaxe)
1113 - diffcore_pickaxe(pickaxe, pickaxe_opts);
1114 - if (orderfile)
1115 - diffcore_order(orderfile);
1116 - diffcore_apply_filter(filter);
1158 + if (options->pickaxe)
1159 + diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
1160 + if (options->orderfile)
1161 + diffcore_order(options->orderfile);
1162 + diffcore_apply_filter(options->filter);
1163 }
1164
1119 -void diff_addremove(int addremove, unsigned mode,
1165 +void diff_addremove(struct diff_options *options,
1166 + int addremove, unsigned mode,
1167 const unsigned char *sha1,
1168 const char *base, const char *path)
1169 {
@@ -1135,7 +1182,7 @@ void diff_addremove(int addremove, unsigned mode,
1182 * Before the final output happens, they are pruned after
1183 * merged into rename/copy pairs as appropriate.
1184 */
1138 - if (reverse_diff)
1185 + if (options->reverse_diff)
1186 addremove = (addremove == '+' ? '-' :
1187 addremove == '-' ? '+' : addremove);
1188
@@ -1152,7 +1199,8 @@ void diff_addremove(int addremove, unsigned mode,
1199 diff_queue(&diff_queued_diff, one, two);
1200 }
1201
1155 -void diff_change(unsigned old_mode, unsigned new_mode,
1202 +void diff_change(struct diff_options *options,
1203 + unsigned old_mode, unsigned new_mode,
1204 const unsigned char *old_sha1,
1205 const unsigned char *new_sha1,
1206 const char *base, const char *path)
@@ -1160,7 +1208,7 @@ void diff_change(unsigned old_mode, unsigned new_mode,
1208 char concatpath[PATH_MAX];
1209 struct diff_filespec *one, *two;
1210
1163 - if (reverse_diff) {
1211 + if (options->reverse_diff) {
1212 unsigned tmp;
1213 const unsigned char *tmp_c;
1214 tmp = old_mode; old_mode = new_mode; new_mode = tmp;
@@ -1176,7 +1224,8 @@ void diff_change(unsigned old_mode, unsigned new_mode,
1224 diff_queue(&diff_queued_diff, one, two);
1225 }
1226
1179 -void diff_unmerge(const char *path)
1227 +void diff_unmerge(struct diff_options *options,
1228 + const char *path)
1229 {
1230 struct diff_filespec *one, *two;
1231 one = alloc_filespec(path);
diff.h
+31 -16
@@ -8,18 +8,37 @@
8 (S_ISREG(mode) ? (S_IFREG | ce_permissions(mode)) : \
9 S_ISLNK(mode) ? S_IFLNK : S_IFDIR)
10
11 -extern void diff_addremove(int addremove,
11 +struct diff_options {
12 + const char **paths;
13 + const char *filter;
14 + const char *orderfile;
15 + const char *pickaxe;
16 + int break_opt;
17 + int detect_rename;
18 + int find_copies_harder;
19 + int line_termination;
20 + int output_format;
21 + int pickaxe_opts;
22 + int rename_score;
23 + int reverse_diff;
24 + int setup;
25 +};
26 +
27 +extern void diff_addremove(struct diff_options *,
28 + int addremove,
29 unsigned mode,
30 const unsigned char *sha1,
31 const char *base,
32 const char *path);
33
17 -extern void diff_change(unsigned mode1, unsigned mode2,
18 - const unsigned char *sha1,
19 - const unsigned char *sha2,
20 - const char *base, const char *path);
34 +extern void diff_change(struct diff_options *,
35 + unsigned mode1, unsigned mode2,
36 + const unsigned char *sha1,
37 + const unsigned char *sha2,
38 + const char *base, const char *path);
39
22 -extern void diff_unmerge(const char *path);
40 +extern void diff_unmerge(struct diff_options *,
41 + const char *path);
42
43 extern int diff_scoreopt_parse(const char *opt);
44
@@ -27,22 +46,18 @@ extern int diff_scoreopt_parse(const char *opt);
46 #define DIFF_SETUP_USE_CACHE 2
47 #define DIFF_SETUP_USE_SIZE_CACHE 4
48
30 -extern void diff_setup(int flags);
49 +extern void diff_setup(struct diff_options *);
50 +extern int diff_opt_parse(struct diff_options *, const char **, int);
51 +extern int diff_setup_done(struct diff_options *);
52
53 #define DIFF_DETECT_RENAME 1
54 #define DIFF_DETECT_COPY 2
55
56 #define DIFF_PICKAXE_ALL 1
57
37 -extern void diffcore_std(const char **paths,
38 - int detect_rename, int rename_score,
39 - const char *pickaxe, int pickaxe_opts,
40 - int break_opt,
41 - const char *orderfile, const char *filter);
58 +extern void diffcore_std(struct diff_options *);
59
43 -extern void diffcore_std_no_resolve(const char **paths,
44 - const char *pickaxe, int pickaxe_opts,
45 - const char *orderfile, const char *filter);
60 +extern void diffcore_std_no_resolve(struct diff_options *);
61
62 #define COMMON_DIFF_OPTIONS_HELP \
63 "\ncommon diff options:\n" \
@@ -71,7 +86,7 @@ extern int diff_queue_is_empty(void);
86 #define DIFF_FORMAT_NO_OUTPUT 3
87 #define DIFF_FORMAT_NAME 4
88
74 -extern void diff_flush(int output_style, int line_terminator);
89 +extern void diff_flush(struct diff_options*);
90
91 /* diff-raw status letters */
92 #define DIFF_STATUS_ADDED 'A'
ls-files.c
+3 -3
@@ -31,7 +31,7 @@ static const char *tag_other = "";
31 static const char *tag_killed = "";
32 static const char *tag_modified = "";
33
34 -static char *exclude_per_dir = NULL;
34 +static const char *exclude_per_dir = NULL;
35
36 /* We maintain three exclude pattern lists:
37 * EXC_CMDL lists patterns explicitly given on the command line.
@@ -532,7 +532,7 @@ static const char ls_files_usage[] =
532 "[ --ignored ] [--exclude=<pattern>] [--exclude-from=<file>] "
533 "[ --exclude-per-directory=<filename> ]";
534
535 -int main(int argc, char **argv)
535 +int main(int argc, const char **argv)
536 {
537 int i;
538 int exc_given = 0;
@@ -542,7 +542,7 @@ int main(int argc, char **argv)
542 prefix_offset = strlen(prefix);
543
544 for (i = 1; i < argc; i++) {
545 - char *arg = argv[i];
545 + const char *arg = argv[i];
546
547 if (!strcmp(arg, "-z")) {
548 line_terminator = 0;
read-cache.c
+1 -1
@@ -232,7 +232,7 @@ int remove_cache_entry_at(int pos)
232 return 1;
233 }
234
235 -int remove_file_from_cache(char *path)
235 +int remove_file_from_cache(const char *path)
236 {
237 int pos = cache_name_pos(path, strlen(path));
238 if (pos < 0)
setup.c
+5 -5
@@ -1,8 +1,8 @@
1 #include "cache.h"
2
3 -char *prefix_path(const char *prefix, int len, char *path)
3 +const char *prefix_path(const char *prefix, int len, const char *path)
4 {
5 - char *orig = path;
5 + const char *orig = path;
6 for (;;) {
7 char c;
8 if (*path != '.')
@@ -47,10 +47,10 @@ char *prefix_path(const char *prefix, int len, char *path)
47 return path;
48 }
49
50 -const char **get_pathspec(const char *prefix, char **pathspec)
50 +const char **get_pathspec(const char *prefix, const char **pathspec)
51 {
52 - char *entry = *pathspec;
53 - char **p;
52 + const char *entry = *pathspec;
53 + const char **p;
54 int prefixlen;
55
56 if (!prefix && !entry)
update-index.c
+5 -5
@@ -31,7 +31,7 @@ static inline long IS_ERR(const void *ptr)
31 return (unsigned long)ptr > (unsigned long)-1000L;
32 }
33
34 -static int add_file_to_cache(char *path)
34 +static int add_file_to_cache(const char *path)
35 {
36 int size, namelen, option, status;
37 struct cache_entry *ce;
@@ -221,7 +221,7 @@ static int verify_dotfile(const char *rest)
221 return 1;
222 }
223
224 -static int verify_path(char *path)
224 +static int verify_path(const char *path)
225 {
226 char c;
227
@@ -247,7 +247,7 @@ inside:
247 }
248 }
249
250 -static int add_cacheinfo(char *arg1, char *arg2, char *arg3)
250 +static int add_cacheinfo(const char *arg1, const char *arg2, const char *arg3)
251 {
252 int size, len, option;
253 unsigned int mode;
@@ -277,7 +277,7 @@ static int add_cacheinfo(char *arg1, char *arg2, char *arg3)
277
278 static struct cache_file cache_file;
279
280 -int main(int argc, char **argv)
280 +int main(int argc, const char **argv)
281 {
282 int i, newfd, entries, has_errors = 0;
283 int allow_options = 1;
@@ -292,7 +292,7 @@ int main(int argc, char **argv)
292 die("cache corrupted");
293
294 for (i = 1 ; i < argc; i++) {
295 - char *path = argv[i];
295 + const char *path = argv[i];
296
297 if (allow_options && *path == '-') {
298 if (!strcmp(path, "--")) {