Skip to content

Commit 6b5ee13

Browse files
author
Junio C Hamano
committed
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>
1 parent dbc3743 commit 6b5ee13

File tree

11 files changed

+252
-366
lines changed

11 files changed

+252
-366
lines changed

cache.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,9 @@ extern char *get_graft_file(void);
143143

144144
#define ALTERNATE_DB_ENVIRONMENT "GIT_ALTERNATE_OBJECT_DIRECTORIES"
145145

146-
extern const char **get_pathspec(const char *prefix, char **pathspec);
146+
extern const char **get_pathspec(const char *prefix, const char **pathspec);
147147
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);
149149

150150
#define alloc_nr(x) (((x)+16)*3/2)
151151

@@ -158,7 +158,7 @@ extern int cache_name_pos(const char *name, int namelen);
158158
#define ADD_CACHE_SKIP_DFCHECK 4 /* Ok to skip DF conflict checks */
159159
extern int add_cache_entry(struct cache_entry *ce, int option);
160160
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);
162162
extern int ce_same_name(struct cache_entry *a, struct cache_entry *b);
163163
extern int ce_match_stat(struct cache_entry *ce, struct stat *st);
164164
extern int ce_modified(struct cache_entry *ce, struct stat *st);

diff-files.c

Lines changed: 23 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -11,95 +11,64 @@ static const char diff_files_usage[] =
1111
"[<common diff options>] [<path>...]"
1212
COMMON_DIFF_OPTIONS_HELP;
1313

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;
2515
static int silent = 0;
2616

2717
static void show_unmerge(const char *path)
2818
{
29-
diff_unmerge(path);
19+
diff_unmerge(&diff_options, path);
3020
}
3121

3222
static void show_file(int pfx, struct cache_entry *ce)
3323
{
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);
3526
}
3627

3728
static void show_modified(int oldmode, int mode,
3829
const unsigned char *old_sha1, const unsigned char *sha1,
3930
char *path)
4031
{
41-
diff_change(oldmode, mode, old_sha1, sha1, path, NULL);
32+
diff_change(&diff_options, oldmode, mode, old_sha1, sha1, path, NULL);
4233
}
4334

44-
int main(int argc, char **argv)
35+
int main(int argc, const char **argv)
4536
{
4637
static const unsigned char null_sha1[20] = { 0, };
4738
const char **pathspec;
4839
const char *prefix = setup_git_directory();
4940
int entries, i;
5041

42+
diff_setup(&diff_options);
5143
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"))
5545
silent = 1;
5646
else if (!strcmp(argv[1], "-r"))
5747
; /* no-op */
5848
else if (!strcmp(argv[1], "-s"))
5949
; /* 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)
7755
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
8862
usage(diff_files_usage);
89-
detect_rename = DIFF_DETECT_COPY;
9063
}
91-
else if (!strcmp(argv[1], "--find-copies-harder"))
92-
find_copies_harder = 1;
93-
else
94-
usage(diff_files_usage);
9564
argv++; argc--;
9665
}
9766

9867
/* Find the directory, and set up the pathspec */
9968
pathspec = get_pathspec(prefix, argv + 1);
10069
entries = read_cache();
10170

102-
if (find_copies_harder && detect_rename != DIFF_DETECT_COPY)
71+
if (diff_setup_done(&diff_options) < 0)
10372
usage(diff_files_usage);
10473

10574
/* At this point, if argc == 1, then we are doing everything.
@@ -110,8 +79,6 @@ int main(int argc, char **argv)
11079
exit(1);
11180
}
11281

113-
diff_setup(diff_setup_opt);
114-
11582
for (i = 0; i < entries; i++) {
11683
struct stat st;
11784
unsigned int oldmode;
@@ -141,18 +108,14 @@ int main(int argc, char **argv)
141108
continue;
142109
}
143110
changed = ce_match_stat(ce, &st);
144-
if (!changed && !find_copies_harder)
111+
if (!changed && !diff_options.find_copies_harder)
145112
continue;
146113
oldmode = ntohl(ce->ce_mode);
147114
show_modified(oldmode, DIFF_FILE_CANON_MODE(st.st_mode),
148115
ce->sha1, (changed ? null_sha1 : ce->sha1),
149116
ce->name);
150117
}
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);
157120
return 0;
158121
}

diff-index.c

Lines changed: 24 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,20 @@
22
#include "diff.h"
33

44
static int cached_only = 0;
5-
static int diff_output_format = DIFF_FORMAT_RAW;
6-
static int diff_line_termination = '\n';
75
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;
177

188
/* 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)
2012
{
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);
2215
}
2316

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)
2519
{
2620
unsigned char *sha1 = ce->sha1;
2721
unsigned int mode = ce->ce_mode;
@@ -77,13 +71,13 @@ static int show_modified(struct cache_entry *old,
7771

7872
oldmode = old->ce_mode;
7973
if (mode == oldmode && !memcmp(sha1, old->sha1, 20) &&
80-
!find_copies_harder)
74+
!diff_options.find_copies_harder)
8175
return 0;
8276

8377
mode = ntohl(mode);
8478
oldmode = ntohl(oldmode);
8579

86-
diff_change(oldmode, mode,
80+
diff_change(&diff_options, oldmode, mode,
8781
old->sha1, sha1, old->name, NULL);
8882
return 0;
8983
}
@@ -127,7 +121,7 @@ static int diff_cache(struct cache_entry **ac, int entries, const char **pathspe
127121
break;
128122
/* fallthru */
129123
case 3:
130-
diff_unmerge(ce->name);
124+
diff_unmerge(&diff_options, ce->name);
131125
break;
132126

133127
default:
@@ -168,7 +162,7 @@ static const char diff_cache_usage[] =
168162
"[<common diff options>] <tree-ish> [<path>...]"
169163
COMMON_DIFF_OPTIONS_HELP;
170164

171-
int main(int argc, char **argv)
165+
int main(int argc, const char **argv)
172166
{
173167
const char *tree_name = NULL;
174168
unsigned char sha1[20];
@@ -180,8 +174,10 @@ int main(int argc, char **argv)
180174
int allow_options = 1;
181175
int i;
182176

177+
diff_setup(&diff_options);
183178
for (i = 1; i < argc; i++) {
184179
const char *arg = argv[i];
180+
int diff_opt_cnt;
185181

186182
if (!allow_options || *arg != '-') {
187183
if (tree_name)
@@ -198,60 +194,15 @@ int main(int argc, char **argv)
198194
/* We accept the -r flag just to look like git-diff-tree */
199195
continue;
200196
}
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;
253203
continue;
254204
}
205+
255206
if (!strcmp(arg, "-m")) {
256207
match_nonexisting = 1;
257208
continue;
@@ -265,17 +216,14 @@ int main(int argc, char **argv)
265216

266217
pathspec = get_pathspec(prefix, argv + i);
267218

268-
if (find_copies_harder && detect_rename != DIFF_DETECT_COPY)
219+
if (diff_setup_done(&diff_options) < 0)
269220
usage(diff_cache_usage);
270221

271222
if (!tree_name || get_sha1(tree_name, sha1))
272223
usage(diff_cache_usage);
273224

274225
read_cache();
275226

276-
/* The rest is for paths restriction. */
277-
diff_setup(diff_setup_opt);
278-
279227
mark_merge_entries();
280228

281229
tree = read_object_with_reference(sha1, "tree", &size, NULL);
@@ -286,11 +234,7 @@ int main(int argc, char **argv)
286234

287235
ret = diff_cache(active_cache, active_nr, pathspec);
288236

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);
295239
return ret;
296240
}

0 commit comments

Comments
 (0)