Skip to content

Commit e12b51e

Browse files
committed
Merge branch 'cb/parse-magnitude'
Move machinery to parse human-readable scaled numbers like 1k, 4M, and 2G as an option parameter's value from pack-objects to parse-options API, to make it available to other codepaths. * cb/parse-magnitude: parse-options: move unsigned long option parsing out of pack-objects.c test-parse-options: update to handle negative ints
2 parents ba12cb2 + 2a514ed commit e12b51e

File tree

6 files changed

+76
-27
lines changed

6 files changed

+76
-27
lines changed

Documentation/technical/api-parse-options.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@ There are some macros to easily define options:
168168
Introduce an option with integer argument.
169169
The integer is put into `int_var`.
170170

171+
`OPT_MAGNITUDE(short, long, &unsigned_long_var, description)`::
172+
Introduce an option with a size argument. The argument must be a
173+
non-negative integer and may include a suffix of 'k', 'm' or 'g' to
174+
scale the provided value by 1024, 1024^2 or 1024^3 respectively.
175+
The scaled value is put into `unsigned_long_var`.
176+
171177
`OPT_DATE(short, long, &int_var, description)`::
172178
Introduce an option with date argument, see `approxidate()`.
173179
The timestamp is put into `int_var`.

builtin/pack-objects.c

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2588,23 +2588,6 @@ static int option_parse_unpack_unreachable(const struct option *opt,
25882588
return 0;
25892589
}
25902590

2591-
static int option_parse_ulong(const struct option *opt,
2592-
const char *arg, int unset)
2593-
{
2594-
if (unset)
2595-
die(_("option %s does not accept negative form"),
2596-
opt->long_name);
2597-
2598-
if (!git_parse_ulong(arg, opt->value))
2599-
die(_("unable to parse value '%s' for option %s"),
2600-
arg, opt->long_name);
2601-
return 0;
2602-
}
2603-
2604-
#define OPT_ULONG(s, l, v, h) \
2605-
{ OPTION_CALLBACK, (s), (l), (v), "n", (h), \
2606-
PARSE_OPT_NONEG, option_parse_ulong }
2607-
26082591
int cmd_pack_objects(int argc, const char **argv, const char *prefix)
26092592
{
26102593
int use_internal_rev_list = 0;
@@ -2627,16 +2610,16 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
26272610
{ OPTION_CALLBACK, 0, "index-version", NULL, N_("version[,offset]"),
26282611
N_("write the pack index file in the specified idx format version"),
26292612
0, option_parse_index_version },
2630-
OPT_ULONG(0, "max-pack-size", &pack_size_limit,
2631-
N_("maximum size of each output pack file")),
2613+
OPT_MAGNITUDE(0, "max-pack-size", &pack_size_limit,
2614+
N_("maximum size of each output pack file")),
26322615
OPT_BOOL(0, "local", &local,
26332616
N_("ignore borrowed objects from alternate object store")),
26342617
OPT_BOOL(0, "incremental", &incremental,
26352618
N_("ignore packed objects")),
26362619
OPT_INTEGER(0, "window", &window,
26372620
N_("limit pack window by objects")),
2638-
OPT_ULONG(0, "window-memory", &window_memory_limit,
2639-
N_("limit pack window by memory in addition to object limit")),
2621+
OPT_MAGNITUDE(0, "window-memory", &window_memory_limit,
2622+
N_("limit pack window by memory in addition to object limit")),
26402623
OPT_INTEGER(0, "depth", &depth,
26412624
N_("maximum length of delta chain allowed in the resulting pack")),
26422625
OPT_BOOL(0, "reuse-delta", &reuse_delta,

parse-options.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,23 @@ static int get_value(struct parse_opt_ctx_t *p,
180180
return opterror(opt, "expects a numerical value", flags);
181181
return 0;
182182

183+
case OPTION_MAGNITUDE:
184+
if (unset) {
185+
*(unsigned long *)opt->value = 0;
186+
return 0;
187+
}
188+
if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
189+
*(unsigned long *)opt->value = opt->defval;
190+
return 0;
191+
}
192+
if (get_arg(p, opt, flags, &arg))
193+
return -1;
194+
if (!git_parse_ulong(arg, opt->value))
195+
return opterror(opt,
196+
"expects a non-negative integer value with an optional k/m/g suffix",
197+
flags);
198+
return 0;
199+
183200
default:
184201
die("should not happen, someone must be hit on the forehead");
185202
}

parse-options.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ enum parse_opt_type {
1616
/* options with arguments (usually) */
1717
OPTION_STRING,
1818
OPTION_INTEGER,
19+
OPTION_MAGNITUDE,
1920
OPTION_CALLBACK,
2021
OPTION_LOWLEVEL_CALLBACK,
2122
OPTION_FILENAME
@@ -129,6 +130,8 @@ struct option {
129130
#define OPT_CMDMODE(s, l, v, h, i) { OPTION_CMDMODE, (s), (l), (v), NULL, \
130131
(h), PARSE_OPT_NOARG|PARSE_OPT_NONEG, NULL, (i) }
131132
#define OPT_INTEGER(s, l, v, h) { OPTION_INTEGER, (s), (l), (v), N_("n"), (h) }
133+
#define OPT_MAGNITUDE(s, l, v, h) { OPTION_MAGNITUDE, (s), (l), (v), \
134+
N_("n"), (h), PARSE_OPT_NONEG }
132135
#define OPT_STRING(s, l, v, a, h) { OPTION_STRING, (s), (l), (v), (a), (h) }
133136
#define OPT_STRING_LIST(s, l, v, a, h) \
134137
{ OPTION_CALLBACK, (s), (l), (v), (a), \

t/t0040-parse-options.sh

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ usage: test-parse-options <options>
1919
2020
-i, --integer <n> get a integer
2121
-j <n> get a integer, too
22+
-m, --magnitude <n> get a magnitude
2223
--set23 set integer to 23
2324
-t <time> get timestamp of <time>
2425
-L, --length <str> get length of <str>
@@ -58,6 +59,7 @@ mv expect expect.err
5859
cat >expect.template <<EOF
5960
boolean: 0
6061
integer: 0
62+
magnitude: 0
6163
timestamp: 0
6264
string: (not set)
6365
abbrev: 7
@@ -132,9 +134,32 @@ test_expect_success 'OPT_BOOL() no negation #2' 'check_unknown_i18n --no-no-fear
132134

133135
test_expect_success 'OPT_BOOL() positivation' 'check boolean: 0 -D --doubt'
134136

137+
test_expect_success 'OPT_INT() negative' 'check integer: -2345 -i -2345'
138+
139+
test_expect_success 'OPT_MAGNITUDE() simple' '
140+
check magnitude: 2345678 -m 2345678
141+
'
142+
143+
test_expect_success 'OPT_MAGNITUDE() kilo' '
144+
check magnitude: 239616 -m 234k
145+
'
146+
147+
test_expect_success 'OPT_MAGNITUDE() mega' '
148+
check magnitude: 104857600 -m 100m
149+
'
150+
151+
test_expect_success 'OPT_MAGNITUDE() giga' '
152+
check magnitude: 1073741824 -m 1g
153+
'
154+
155+
test_expect_success 'OPT_MAGNITUDE() 3giga' '
156+
check magnitude: 3221225472 -m 3g
157+
'
158+
135159
cat > expect << EOF
136160
boolean: 2
137161
integer: 1729
162+
magnitude: 16384
138163
timestamp: 0
139164
string: 123
140165
abbrev: 7
@@ -145,15 +170,16 @@ file: prefix/my.file
145170
EOF
146171

147172
test_expect_success 'short options' '
148-
test-parse-options -s123 -b -i 1729 -b -vv -n -F my.file \
149-
> output 2> output.err &&
173+
test-parse-options -s123 -b -i 1729 -m 16k -b -vv -n -F my.file \
174+
>output 2>output.err &&
150175
test_cmp expect output &&
151176
test_must_be_empty output.err
152177
'
153178

154179
cat > expect << EOF
155180
boolean: 2
156181
integer: 1729
182+
magnitude: 16384
157183
timestamp: 0
158184
string: 321
159185
abbrev: 10
@@ -164,9 +190,10 @@ file: prefix/fi.le
164190
EOF
165191

166192
test_expect_success 'long options' '
167-
test-parse-options --boolean --integer 1729 --boolean --string2=321 \
168-
--verbose --verbose --no-dry-run --abbrev=10 --file fi.le\
169-
--obsolete > output 2> output.err &&
193+
test-parse-options --boolean --integer 1729 --magnitude 16k \
194+
--boolean --string2=321 --verbose --verbose --no-dry-run \
195+
--abbrev=10 --file fi.le --obsolete \
196+
>output 2>output.err &&
170197
test_must_be_empty output.err &&
171198
test_cmp expect output
172199
'
@@ -180,6 +207,7 @@ test_expect_success 'missing required value' '
180207
cat > expect << EOF
181208
boolean: 1
182209
integer: 13
210+
magnitude: 0
183211
timestamp: 0
184212
string: 123
185213
abbrev: 7
@@ -202,6 +230,7 @@ test_expect_success 'intermingled arguments' '
202230
cat > expect << EOF
203231
boolean: 0
204232
integer: 2
233+
magnitude: 0
205234
timestamp: 0
206235
string: (not set)
207236
abbrev: 7
@@ -230,6 +259,7 @@ test_expect_success 'ambiguously abbreviated option' '
230259
cat > expect << EOF
231260
boolean: 0
232261
integer: 0
262+
magnitude: 0
233263
timestamp: 0
234264
string: 123
235265
abbrev: 7
@@ -268,6 +298,7 @@ test_expect_success 'detect possible typos' '
268298
cat > expect <<EOF
269299
boolean: 0
270300
integer: 0
301+
magnitude: 0
271302
timestamp: 0
272303
string: (not set)
273304
abbrev: 7
@@ -287,6 +318,7 @@ test_expect_success 'keep some options as arguments' '
287318
cat > expect <<EOF
288319
boolean: 0
289320
integer: 0
321+
magnitude: 0
290322
timestamp: 1
291323
string: (not set)
292324
abbrev: 7
@@ -308,6 +340,7 @@ cat > expect <<EOF
308340
Callback: "four", 0
309341
boolean: 5
310342
integer: 4
343+
magnitude: 0
311344
timestamp: 0
312345
string: (not set)
313346
abbrev: 7
@@ -336,6 +369,7 @@ test_expect_success 'OPT_CALLBACK() and callback errors work' '
336369
cat > expect <<EOF
337370
boolean: 1
338371
integer: 23
372+
magnitude: 0
339373
timestamp: 0
340374
string: (not set)
341375
abbrev: 7
@@ -360,6 +394,7 @@ test_expect_success 'OPT_NEGBIT() and OPT_SET_INT() work' '
360394
cat > expect <<EOF
361395
boolean: 6
362396
integer: 0
397+
magnitude: 0
363398
timestamp: 0
364399
string: (not set)
365400
abbrev: 7
@@ -390,6 +425,7 @@ test_expect_success 'OPT_COUNTUP() with PARSE_OPT_NODASH works' '
390425
cat > expect <<EOF
391426
boolean: 0
392427
integer: 12345
428+
magnitude: 0
393429
timestamp: 0
394430
string: (not set)
395431
abbrev: 7
@@ -408,6 +444,7 @@ test_expect_success 'OPT_NUMBER_CALLBACK() works' '
408444
cat >expect <<EOF
409445
boolean: 0
410446
integer: 0
447+
magnitude: 0
411448
timestamp: 0
412449
string: (not set)
413450
abbrev: 7

test-parse-options.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
static int boolean = 0;
66
static int integer = 0;
7+
static unsigned long magnitude = 0;
78
static unsigned long timestamp;
89
static int abbrev = 7;
910
static int verbose = 0, dry_run = 0, quiet = 0;
@@ -48,6 +49,7 @@ int main(int argc, char **argv)
4849
OPT_GROUP(""),
4950
OPT_INTEGER('i', "integer", &integer, "get a integer"),
5051
OPT_INTEGER('j', NULL, &integer, "get a integer, too"),
52+
OPT_MAGNITUDE('m', "magnitude", &magnitude, "get a magnitude"),
5153
OPT_SET_INT(0, "set23", &integer, "set integer to 23", 23),
5254
OPT_DATE('t', NULL, &timestamp, "get timestamp of <time>"),
5355
OPT_CALLBACK('L', "length", &integer, "str",
@@ -82,7 +84,8 @@ int main(int argc, char **argv)
8284
argc = parse_options(argc, (const char **)argv, prefix, options, usage, 0);
8385

8486
printf("boolean: %d\n", boolean);
85-
printf("integer: %u\n", integer);
87+
printf("integer: %d\n", integer);
88+
printf("magnitude: %lu\n", magnitude);
8689
printf("timestamp: %lu\n", timestamp);
8790
printf("string: %s\n", string ? string : "(not set)");
8891
printf("abbrev: %d\n", abbrev);

0 commit comments

Comments
 (0)