diff: make diff_populate_filespec_options struct
The behavior of diff_populate_filespec() currently can be customized through a bitflag, but a subsequent patch requires it to support a non-boolean option. Replace the bitflag with an options struct. Signed-off-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jonathan Tan committed
Apr 7, 2020 at 15:11 UTC
1c37e86ab2834dfca311799e799568794bc474ce
5 files changed
+54
-32
diff.c
+35
-19
@@ -573,7 +573,7 @@ static int fill_mmfile(struct repository *r, mmfile_t *mf,
573
mf->size = 0;
574
return 0;
575
}
576
- else if (diff_populate_filespec(r, one, 0))
576
+ else if (diff_populate_filespec(r, one, NULL))
577
return -1;
578
579
mf->ptr = one->data;
@@ -585,9 +585,13 @@ static int fill_mmfile(struct repository *r, mmfile_t *mf,
585
static unsigned long diff_filespec_size(struct repository *r,
586
struct diff_filespec *one)
587
{
588
+ struct diff_populate_filespec_options dpf_options = {
589
+ .check_size_only = 1,
590
+ };
591
+
592
if (!DIFF_FILE_VALID(one))
593
return 0;
590
- diff_populate_filespec(r, one, CHECK_SIZE_ONLY);
594
+ diff_populate_filespec(r, one, &dpf_options);
595
return one->size;
596
}
597
@@ -3020,6 +3024,9 @@ static void show_dirstat(struct diff_options *options)
3024
struct diff_filepair *p = q->queue[i];
3025
const char *name;
3026
unsigned long copied, added, damage;
3027
+ struct diff_populate_filespec_options dpf_options = {
3028
+ .check_size_only = 1,
3029
+ };
3030
3031
name = p->two->path ? p->two->path : p->one->path;
3032
@@ -3047,19 +3054,19 @@ static void show_dirstat(struct diff_options *options)
3054
}
3055
3056
if (DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two)) {
3050
- diff_populate_filespec(options->repo, p->one, 0);
3051
- diff_populate_filespec(options->repo, p->two, 0);
3057
+ diff_populate_filespec(options->repo, p->one, NULL);
3058
+ diff_populate_filespec(options->repo, p->two, NULL);
3059
diffcore_count_changes(options->repo,
3060
p->one, p->two, NULL, NULL,
3061
&copied, &added);
3062
diff_free_filespec_data(p->one);
3063
diff_free_filespec_data(p->two);
3064
} else if (DIFF_FILE_VALID(p->one)) {
3058
- diff_populate_filespec(options->repo, p->one, CHECK_SIZE_ONLY);
3065
+ diff_populate_filespec(options->repo, p->one, &dpf_options);
3066
copied = added = 0;
3067
diff_free_filespec_data(p->one);
3068
} else if (DIFF_FILE_VALID(p->two)) {
3062
- diff_populate_filespec(options->repo, p->two, CHECK_SIZE_ONLY);
3069
+ diff_populate_filespec(options->repo, p->two, &dpf_options);
3070
copied = 0;
3071
added = p->two->size;
3072
diff_free_filespec_data(p->two);
@@ -3339,13 +3346,17 @@ static void emit_binary_diff(struct diff_options *o,
3346
int diff_filespec_is_binary(struct repository *r,
3347
struct diff_filespec *one)
3348
{
3349
+ struct diff_populate_filespec_options dpf_options = {
3350
+ .check_binary = 1,
3351
+ };
3352
+
3353
if (one->is_binary == -1) {
3354
diff_filespec_load_driver(one, r->index);
3355
if (one->driver->binary != -1)
3356
one->is_binary = one->driver->binary;
3357
else {
3358
if (!one->data && DIFF_FILE_VALID(one))
3348
- diff_populate_filespec(r, one, CHECK_BINARY);
3359
+ diff_populate_filespec(r, one, &dpf_options);
3360
if (one->is_binary == -1 && one->data)
3361
one->is_binary = buffer_is_binary(one->data,
3362
one->size);
@@ -3677,8 +3688,8 @@ static void builtin_diffstat(const char *name_a, const char *name_b,
3688
}
3689
3690
else if (complete_rewrite) {
3680
- diff_populate_filespec(o->repo, one, 0);
3681
- diff_populate_filespec(o->repo, two, 0);
3691
+ diff_populate_filespec(o->repo, one, NULL);
3692
+ diff_populate_filespec(o->repo, two, NULL);
3693
data->deleted = count_lines(one->data, one->size);
3694
data->added = count_lines(two->data, two->size);
3695
}
@@ -3914,9 +3925,10 @@ static int diff_populate_gitlink(struct diff_filespec *s, int size_only)
3925
*/
3926
int diff_populate_filespec(struct repository *r,
3927
struct diff_filespec *s,
3917
- unsigned int flags)
3928
+ const struct diff_populate_filespec_options *options)
3929
{
3919
- int size_only = flags & CHECK_SIZE_ONLY;
3930
+ int size_only = options ? options->check_size_only : 0;
3931
+ int check_binary = options ? options->check_binary : 0;
3932
int err = 0;
3933
int conv_flags = global_conv_flags_eol;
3934
/*
@@ -3986,7 +3998,7 @@ int diff_populate_filespec(struct repository *r,
3998
* opening the file and inspecting the contents, this
3999
* is probably fine.
4000
*/
3989
- if ((flags & CHECK_BINARY) &&
4001
+ if (check_binary &&
4002
s->size > big_file_threshold && s->is_binary == -1) {
4003
s->is_binary = 1;
4004
return 0;
@@ -4012,7 +4024,7 @@ int diff_populate_filespec(struct repository *r,
4024
}
4025
else {
4026
enum object_type type;
4015
- if (size_only || (flags & CHECK_BINARY)) {
4027
+ if (size_only || check_binary) {
4028
type = oid_object_info(r, &s->oid, &s->size);
4029
if (type < 0)
4030
die("unable to read %s",
@@ -4141,7 +4153,7 @@ static struct diff_tempfile *prepare_temp_file(struct repository *r,
4153
return temp;
4154
}
4155
else {
4144
- if (diff_populate_filespec(r, one, 0))
4156
+ if (diff_populate_filespec(r, one, NULL))
4157
die("cannot read data blob for %s", one->path);
4158
prep_temp_blob(r->index, name, temp,
4159
one->data, one->size,
@@ -6407,9 +6419,9 @@ static int diff_filespec_is_identical(struct repository *r,
6419
{
6420
if (S_ISGITLINK(one->mode))
6421
return 0;
6410
- if (diff_populate_filespec(r, one, 0))
6422
+ if (diff_populate_filespec(r, one, NULL))
6423
return 0;
6412
- if (diff_populate_filespec(r, two, 0))
6424
+ if (diff_populate_filespec(r, two, NULL))
6425
return 0;
6426
return !memcmp(one->data, two->data, one->size);
6427
}
@@ -6417,6 +6429,10 @@ static int diff_filespec_is_identical(struct repository *r,
6429
static int diff_filespec_check_stat_unmatch(struct repository *r,
6430
struct diff_filepair *p)
6431
{
6432
+ struct diff_populate_filespec_options dpf_options = {
6433
+ .check_size_only = 1,
6434
+ };
6435
+
6436
if (p->done_skip_stat_unmatch)
6437
return p->skip_stat_unmatch_result;
6438
@@ -6439,8 +6455,8 @@ static int diff_filespec_check_stat_unmatch(struct repository *r,
6455
!DIFF_FILE_VALID(p->two) ||
6456
(p->one->oid_valid && p->two->oid_valid) ||
6457
(p->one->mode != p->two->mode) ||
6442
- diff_populate_filespec(r, p->one, CHECK_SIZE_ONLY) ||
6443
- diff_populate_filespec(r, p->two, CHECK_SIZE_ONLY) ||
6458
+ diff_populate_filespec(r, p->one, &dpf_options) ||
6459
+ diff_populate_filespec(r, p->two, &dpf_options) ||
6460
(p->one->size != p->two->size) ||
6461
!diff_filespec_is_identical(r, p->one, p->two)) /* (2) */
6462
p->skip_stat_unmatch_result = 1;
@@ -6770,7 +6786,7 @@ size_t fill_textconv(struct repository *r,
6786
*outbuf = "";
6787
return 0;
6788
}
6773
- if (diff_populate_filespec(r, df, 0))
6789
+ if (diff_populate_filespec(r, df, NULL))
6790
die("unable to read files to diff");
6791
*outbuf = df->data;
6792
return df->size;
diffcore-break.c
+2
-2
@@ -62,8 +62,8 @@ static int should_break(struct repository *r,
62
oideq(&src->oid, &dst->oid))
63
return 0; /* they are the same */
64
65
- if (diff_populate_filespec(r, src, 0) ||
66
- diff_populate_filespec(r, dst, 0))
65
+ if (diff_populate_filespec(r, src, NULL) ||
66
+ diff_populate_filespec(r, dst, NULL))
67
return 0; /* error but caught downstream */
68
69
max_size = ((src->size > dst->size) ? src->size : dst->size);
diffcore-rename.c
+8
-5
@@ -148,6 +148,9 @@ static int estimate_similarity(struct repository *r,
148
*/
149
unsigned long max_size, delta_size, base_size, src_copied, literal_added;
150
int score;
151
+ struct diff_populate_filespec_options dpf_options = {
152
+ .check_size_only = 1
153
+ };
154
155
/* We deal only with regular files. Symlink renames are handled
156
* only when they are exact matches --- in other words, no edits
@@ -166,10 +169,10 @@ static int estimate_similarity(struct repository *r,
169
* say whether the size is valid or not!)
170
*/
171
if (!src->cnt_data &&
169
- diff_populate_filespec(r, src, CHECK_SIZE_ONLY))
172
+ diff_populate_filespec(r, src, &dpf_options))
173
return 0;
174
if (!dst->cnt_data &&
172
- diff_populate_filespec(r, dst, CHECK_SIZE_ONLY))
175
+ diff_populate_filespec(r, dst, &dpf_options))
176
return 0;
177
178
max_size = ((src->size > dst->size) ? src->size : dst->size);
@@ -187,9 +190,9 @@ static int estimate_similarity(struct repository *r,
190
if (max_size * (MAX_SCORE-minimum_score) < delta_size * MAX_SCORE)
191
return 0;
192
190
- if (!src->cnt_data && diff_populate_filespec(r, src, 0))
193
+ if (!src->cnt_data && diff_populate_filespec(r, src, NULL))
194
return 0;
192
- if (!dst->cnt_data && diff_populate_filespec(r, dst, 0))
195
+ if (!dst->cnt_data && diff_populate_filespec(r, dst, NULL))
196
return 0;
197
198
if (diffcore_count_changes(r, src, dst,
@@ -261,7 +264,7 @@ static unsigned int hash_filespec(struct repository *r,
264
struct diff_filespec *filespec)
265
{
266
if (!filespec->oid_valid) {
264
- if (diff_populate_filespec(r, filespec, 0))
267
+ if (diff_populate_filespec(r, filespec, NULL))
268
return 0;
269
hash_object_file(r->hash_algo, filespec->data, filespec->size,
270
"blob", &filespec->oid);
diffcore.h
+6
-3
@@ -65,9 +65,12 @@ void free_filespec(struct diff_filespec *);
65
void fill_filespec(struct diff_filespec *, const struct object_id *,
66
int, unsigned short);
67
68
-#define CHECK_SIZE_ONLY 1
69
-#define CHECK_BINARY 2
70
-int diff_populate_filespec(struct repository *, struct diff_filespec *, unsigned int);
68
+struct diff_populate_filespec_options {
69
+ unsigned check_size_only : 1;
70
+ unsigned check_binary : 1;
71
+};
72
+int diff_populate_filespec(struct repository *, struct diff_filespec *,
73
+ const struct diff_populate_filespec_options *);
74
void diff_free_filespec_data(struct diff_filespec *);
75
void diff_free_filespec_blob(struct diff_filespec *);
76
int diff_filespec_is_binary(struct repository *, struct diff_filespec *);
line-log.c
+3
-3
@@ -519,7 +519,7 @@ static void fill_line_ends(struct repository *r,
519
unsigned long *ends = NULL;
520
char *data = NULL;
521
522
- if (diff_populate_filespec(r, spec, 0))
522
+ if (diff_populate_filespec(r, spec, NULL))
523
die("Cannot read blob %s", oid_to_hex(&spec->oid));
524
525
ALLOC_ARRAY(ends, size);
@@ -1045,12 +1045,12 @@ static int process_diff_filepair(struct rev_info *rev,
1045
return 0;
1046
1047
assert(pair->two->oid_valid);
1048
- diff_populate_filespec(rev->diffopt.repo, pair->two, 0);
1048
+ diff_populate_filespec(rev->diffopt.repo, pair->two, NULL);
1049
file_target.ptr = pair->two->data;
1050
file_target.size = pair->two->size;
1051
1052
if (pair->one->oid_valid) {
1053
- diff_populate_filespec(rev->diffopt.repo, pair->one, 0);
1053
+ diff_populate_filespec(rev->diffopt.repo, pair->one, NULL);
1054
file_parent.ptr = pair->one->data;
1055
file_parent.size = pair->one->size;
1056
} else {