Skip to content

Commit b1bda75

Browse files
calvin-wan-googlegitster
authored andcommitted
parse: separate out parsing functions from config.h
The files config.{h,c} contain functions that have to do with parsing, but not config. In order to further reduce all-in-one headers, separate out functions in config.c that do not operate on config into its own file, parse.h, and update the include directives in the .c files that need only such functions accordingly. Signed-off-by: Calvin Wan <[email protected]> Signed-off-by: Jonathan Tan <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e16be13 commit b1bda75

18 files changed

+219
-205
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,7 @@ LIB_OBJS += pack-write.o
10911091
LIB_OBJS += packfile.o
10921092
LIB_OBJS += pager.o
10931093
LIB_OBJS += parallel-checkout.o
1094+
LIB_OBJS += parse.o
10941095
LIB_OBJS += parse-options-cb.o
10951096
LIB_OBJS += parse-options.o
10961097
LIB_OBJS += patch-delta.o

attr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
#include "git-compat-util.h"
10-
#include "config.h"
10+
#include "parse.h"
1111
#include "environment.h"
1212
#include "exec-cmd.h"
1313
#include "attr.h"

config.c

Lines changed: 1 addition & 179 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "date.h"
1212
#include "branch.h"
1313
#include "config.h"
14+
#include "parse.h"
1415
#include "convert.h"
1516
#include "environment.h"
1617
#include "gettext.h"
@@ -1165,129 +1166,6 @@ static int git_parse_source(struct config_source *cs, config_fn_t fn,
11651166
return error_return;
11661167
}
11671168

1168-
static uintmax_t get_unit_factor(const char *end)
1169-
{
1170-
if (!*end)
1171-
return 1;
1172-
else if (!strcasecmp(end, "k"))
1173-
return 1024;
1174-
else if (!strcasecmp(end, "m"))
1175-
return 1024 * 1024;
1176-
else if (!strcasecmp(end, "g"))
1177-
return 1024 * 1024 * 1024;
1178-
return 0;
1179-
}
1180-
1181-
static int git_parse_signed(const char *value, intmax_t *ret, intmax_t max)
1182-
{
1183-
if (value && *value) {
1184-
char *end;
1185-
intmax_t val;
1186-
intmax_t factor;
1187-
1188-
if (max < 0)
1189-
BUG("max must be a positive integer");
1190-
1191-
errno = 0;
1192-
val = strtoimax(value, &end, 0);
1193-
if (errno == ERANGE)
1194-
return 0;
1195-
if (end == value) {
1196-
errno = EINVAL;
1197-
return 0;
1198-
}
1199-
factor = get_unit_factor(end);
1200-
if (!factor) {
1201-
errno = EINVAL;
1202-
return 0;
1203-
}
1204-
if ((val < 0 && -max / factor > val) ||
1205-
(val > 0 && max / factor < val)) {
1206-
errno = ERANGE;
1207-
return 0;
1208-
}
1209-
val *= factor;
1210-
*ret = val;
1211-
return 1;
1212-
}
1213-
errno = EINVAL;
1214-
return 0;
1215-
}
1216-
1217-
static int git_parse_unsigned(const char *value, uintmax_t *ret, uintmax_t max)
1218-
{
1219-
if (value && *value) {
1220-
char *end;
1221-
uintmax_t val;
1222-
uintmax_t factor;
1223-
1224-
/* negative values would be accepted by strtoumax */
1225-
if (strchr(value, '-')) {
1226-
errno = EINVAL;
1227-
return 0;
1228-
}
1229-
errno = 0;
1230-
val = strtoumax(value, &end, 0);
1231-
if (errno == ERANGE)
1232-
return 0;
1233-
if (end == value) {
1234-
errno = EINVAL;
1235-
return 0;
1236-
}
1237-
factor = get_unit_factor(end);
1238-
if (!factor) {
1239-
errno = EINVAL;
1240-
return 0;
1241-
}
1242-
if (unsigned_mult_overflows(factor, val) ||
1243-
factor * val > max) {
1244-
errno = ERANGE;
1245-
return 0;
1246-
}
1247-
val *= factor;
1248-
*ret = val;
1249-
return 1;
1250-
}
1251-
errno = EINVAL;
1252-
return 0;
1253-
}
1254-
1255-
int git_parse_int(const char *value, int *ret)
1256-
{
1257-
intmax_t tmp;
1258-
if (!git_parse_signed(value, &tmp, maximum_signed_value_of_type(int)))
1259-
return 0;
1260-
*ret = tmp;
1261-
return 1;
1262-
}
1263-
1264-
static int git_parse_int64(const char *value, int64_t *ret)
1265-
{
1266-
intmax_t tmp;
1267-
if (!git_parse_signed(value, &tmp, maximum_signed_value_of_type(int64_t)))
1268-
return 0;
1269-
*ret = tmp;
1270-
return 1;
1271-
}
1272-
1273-
int git_parse_ulong(const char *value, unsigned long *ret)
1274-
{
1275-
uintmax_t tmp;
1276-
if (!git_parse_unsigned(value, &tmp, maximum_unsigned_value_of_type(long)))
1277-
return 0;
1278-
*ret = tmp;
1279-
return 1;
1280-
}
1281-
1282-
int git_parse_ssize_t(const char *value, ssize_t *ret)
1283-
{
1284-
intmax_t tmp;
1285-
if (!git_parse_signed(value, &tmp, maximum_signed_value_of_type(ssize_t)))
1286-
return 0;
1287-
*ret = tmp;
1288-
return 1;
1289-
}
1290-
12911169
NORETURN
12921170
static void die_bad_number(const char *name, const char *value,
12931171
const struct key_value_info *kvi)
@@ -1363,23 +1241,6 @@ ssize_t git_config_ssize_t(const char *name, const char *value,
13631241
return ret;
13641242
}
13651243

1366-
static int git_parse_maybe_bool_text(const char *value)
1367-
{
1368-
if (!value)
1369-
return 1;
1370-
if (!*value)
1371-
return 0;
1372-
if (!strcasecmp(value, "true")
1373-
|| !strcasecmp(value, "yes")
1374-
|| !strcasecmp(value, "on"))
1375-
return 1;
1376-
if (!strcasecmp(value, "false")
1377-
|| !strcasecmp(value, "no")
1378-
|| !strcasecmp(value, "off"))
1379-
return 0;
1380-
return -1;
1381-
}
1382-
13831244
static const struct fsync_component_name {
13841245
const char *name;
13851246
enum fsync_component component_bits;
@@ -1454,16 +1315,6 @@ static enum fsync_component parse_fsync_components(const char *var, const char *
14541315
return (current & ~negative) | positive;
14551316
}
14561317

1457-
int git_parse_maybe_bool(const char *value)
1458-
{
1459-
int v = git_parse_maybe_bool_text(value);
1460-
if (0 <= v)
1461-
return v;
1462-
if (git_parse_int(value, &v))
1463-
return !!v;
1464-
return -1;
1465-
}
1466-
14671318
int git_config_bool_or_int(const char *name, const char *value,
14681319
const struct key_value_info *kvi, int *is_bool)
14691320
{
@@ -2126,35 +1977,6 @@ void git_global_config(char **user_out, char **xdg_out)
21261977
*xdg_out = xdg_config;
21271978
}
21281979

2129-
/*
2130-
* Parse environment variable 'k' as a boolean (in various
2131-
* possible spellings); if missing, use the default value 'def'.
2132-
*/
2133-
int git_env_bool(const char *k, int def)
2134-
{
2135-
const char *v = getenv(k);
2136-
int val;
2137-
if (!v)
2138-
return def;
2139-
val = git_parse_maybe_bool(v);
2140-
if (val < 0)
2141-
die(_("bad boolean environment value '%s' for '%s'"),
2142-
v, k);
2143-
return val;
2144-
}
2145-
2146-
/*
2147-
* Parse environment variable 'k' as ulong with possibly a unit
2148-
* suffix; if missing, use the default value 'val'.
2149-
*/
2150-
unsigned long git_env_ulong(const char *k, unsigned long val)
2151-
{
2152-
const char *v = getenv(k);
2153-
if (v && !git_parse_ulong(v, &val))
2154-
die(_("failed to parse %s"), k);
2155-
return val;
2156-
}
2157-
21581980
int git_config_system(void)
21591981
{
21601982
return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0);

config.h

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include "hashmap.h"
55
#include "string-list.h"
66
#include "repository.h"
7-
7+
#include "parse.h"
88

99
/**
1010
* The config API gives callers a way to access Git configuration files
@@ -243,16 +243,6 @@ int config_with_options(config_fn_t fn, void *,
243243
* The following helper functions aid in parsing string values
244244
*/
245245

246-
int git_parse_ssize_t(const char *, ssize_t *);
247-
int git_parse_ulong(const char *, unsigned long *);
248-
int git_parse_int(const char *value, int *ret);
249-
250-
/**
251-
* Same as `git_config_bool`, except that it returns -1 on error rather
252-
* than dying.
253-
*/
254-
int git_parse_maybe_bool(const char *);
255-
256246
/**
257247
* Parse the string to an integer, including unit factors. Dies on error;
258248
* otherwise, returns the parsed result.
@@ -385,8 +375,6 @@ int git_config_rename_section(const char *, const char *);
385375
int git_config_rename_section_in_file(const char *, const char *, const char *);
386376
int git_config_copy_section(const char *, const char *);
387377
int git_config_copy_section_in_file(const char *, const char *, const char *);
388-
int git_env_bool(const char *, int);
389-
unsigned long git_env_ulong(const char *, unsigned long);
390378
int git_config_system(void);
391379
int config_error_nonbool(const char *);
392380
#if defined(__GNUC__)

pack-objects.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include "pack.h"
44
#include "pack-objects.h"
55
#include "packfile.h"
6-
#include "config.h"
6+
#include "parse.h"
77

88
static uint32_t locate_object_entry_hash(struct packing_data *pdata,
99
const struct object_id *oid,

pack-revindex.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include "packfile.h"
77
#include "strbuf.h"
88
#include "trace2.h"
9-
#include "config.h"
9+
#include "parse.h"
1010
#include "midx.h"
1111
#include "csum-file.h"
1212

parse-options.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#include "git-compat-util.h"
22
#include "parse-options.h"
33
#include "abspath.h"
4-
#include "config.h"
4+
#include "parse.h"
55
#include "commit.h"
66
#include "color.h"
77
#include "gettext.h"
88
#include "strbuf.h"
9+
#include "string-list.h"
910
#include "utf8.h"
1011

1112
static int disallow_abbreviated_options;

0 commit comments

Comments
 (0)