Skip to content

Commit a7a2d10

Browse files
committed
Merge branch 'cw/prelim-cleanup'
Shuffle some bits across headers and sources to prepare for libification effort. * cw/prelim-cleanup: parse: separate out parsing functions from config.h config: correct bad boolean env value error message wrapper: reduce scope of remove_or_warn() hex-ll: separate out non-hash-algo functions
2 parents 3df51ea + b1bda75 commit a7a2d10

30 files changed

+313
-284
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,6 +1039,7 @@ LIB_OBJS += hash-lookup.o
10391039
LIB_OBJS += hashmap.o
10401040
LIB_OBJS += help.o
10411041
LIB_OBJS += hex.o
1042+
LIB_OBJS += hex-ll.o
10421043
LIB_OBJS += hook.o
10431044
LIB_OBJS += ident.o
10441045
LIB_OBJS += json-writer.o
@@ -1089,6 +1090,7 @@ LIB_OBJS += pack-write.o
10891090
LIB_OBJS += packfile.o
10901091
LIB_OBJS += pager.o
10911092
LIB_OBJS += parallel-checkout.o
1093+
LIB_OBJS += parse.o
10921094
LIB_OBJS += parse-options-cb.o
10931095
LIB_OBJS += parse-options.o
10941096
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"

color.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include "color.h"
44
#include "editor.h"
55
#include "gettext.h"
6-
#include "hex.h"
6+
#include "hex-ll.h"
77
#include "pager.h"
88
#include "strbuf.h"
99

config.c

Lines changed: 1 addition & 172 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
{
@@ -2131,28 +1982,6 @@ void git_global_config(char **user_out, char **xdg_out)
21311982
*xdg_out = xdg_config;
21321983
}
21331984

2134-
/*
2135-
* Parse environment variable 'k' as a boolean (in various
2136-
* possible spellings); if missing, use the default value 'def'.
2137-
*/
2138-
int git_env_bool(const char *k, int def)
2139-
{
2140-
const char *v = getenv(k);
2141-
return v ? git_config_bool(k, v) : def;
2142-
}
2143-
2144-
/*
2145-
* Parse environment variable 'k' as ulong with possibly a unit
2146-
* suffix; if missing, use the default value 'val'.
2147-
*/
2148-
unsigned long git_env_ulong(const char *k, unsigned long val)
2149-
{
2150-
const char *v = getenv(k);
2151-
if (v && !git_parse_ulong(v, &val))
2152-
die(_("failed to parse %s"), k);
2153-
return val;
2154-
}
2155-
21561985
int git_config_system(void)
21571986
{
21581987
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__)

entry.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,3 +581,8 @@ void unlink_entry(const struct cache_entry *ce, const char *super_prefix)
581581
return;
582582
schedule_dir_for_removal(ce->name, ce_namelen(ce));
583583
}
584+
585+
int remove_or_warn(unsigned int mode, const char *file)
586+
{
587+
return S_ISGITLINK(mode) ? rmdir_or_warn(file) : unlink_or_warn(file);
588+
}

entry.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,10 @@ int fstat_checkout_output(int fd, const struct checkout *state, struct stat *st)
6262
void update_ce_after_write(const struct checkout *state, struct cache_entry *ce,
6363
struct stat *st);
6464

65+
/*
66+
* Calls the correct function out of {unlink,rmdir}_or_warn based on
67+
* the supplied file mode.
68+
*/
69+
int remove_or_warn(unsigned int mode, const char *path);
70+
6571
#endif /* ENTRY_H */

hex-ll.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include "git-compat-util.h"
2+
#include "hex-ll.h"
3+
4+
const signed char hexval_table[256] = {
5+
-1, -1, -1, -1, -1, -1, -1, -1, /* 00-07 */
6+
-1, -1, -1, -1, -1, -1, -1, -1, /* 08-0f */
7+
-1, -1, -1, -1, -1, -1, -1, -1, /* 10-17 */
8+
-1, -1, -1, -1, -1, -1, -1, -1, /* 18-1f */
9+
-1, -1, -1, -1, -1, -1, -1, -1, /* 20-27 */
10+
-1, -1, -1, -1, -1, -1, -1, -1, /* 28-2f */
11+
0, 1, 2, 3, 4, 5, 6, 7, /* 30-37 */
12+
8, 9, -1, -1, -1, -1, -1, -1, /* 38-3f */
13+
-1, 10, 11, 12, 13, 14, 15, -1, /* 40-47 */
14+
-1, -1, -1, -1, -1, -1, -1, -1, /* 48-4f */
15+
-1, -1, -1, -1, -1, -1, -1, -1, /* 50-57 */
16+
-1, -1, -1, -1, -1, -1, -1, -1, /* 58-5f */
17+
-1, 10, 11, 12, 13, 14, 15, -1, /* 60-67 */
18+
-1, -1, -1, -1, -1, -1, -1, -1, /* 68-67 */
19+
-1, -1, -1, -1, -1, -1, -1, -1, /* 70-77 */
20+
-1, -1, -1, -1, -1, -1, -1, -1, /* 78-7f */
21+
-1, -1, -1, -1, -1, -1, -1, -1, /* 80-87 */
22+
-1, -1, -1, -1, -1, -1, -1, -1, /* 88-8f */
23+
-1, -1, -1, -1, -1, -1, -1, -1, /* 90-97 */
24+
-1, -1, -1, -1, -1, -1, -1, -1, /* 98-9f */
25+
-1, -1, -1, -1, -1, -1, -1, -1, /* a0-a7 */
26+
-1, -1, -1, -1, -1, -1, -1, -1, /* a8-af */
27+
-1, -1, -1, -1, -1, -1, -1, -1, /* b0-b7 */
28+
-1, -1, -1, -1, -1, -1, -1, -1, /* b8-bf */
29+
-1, -1, -1, -1, -1, -1, -1, -1, /* c0-c7 */
30+
-1, -1, -1, -1, -1, -1, -1, -1, /* c8-cf */
31+
-1, -1, -1, -1, -1, -1, -1, -1, /* d0-d7 */
32+
-1, -1, -1, -1, -1, -1, -1, -1, /* d8-df */
33+
-1, -1, -1, -1, -1, -1, -1, -1, /* e0-e7 */
34+
-1, -1, -1, -1, -1, -1, -1, -1, /* e8-ef */
35+
-1, -1, -1, -1, -1, -1, -1, -1, /* f0-f7 */
36+
-1, -1, -1, -1, -1, -1, -1, -1, /* f8-ff */
37+
};
38+
39+
int hex_to_bytes(unsigned char *binary, const char *hex, size_t len)
40+
{
41+
for (; len; len--, hex += 2) {
42+
unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
43+
44+
if (val & ~0xff)
45+
return -1;
46+
*binary++ = val;
47+
}
48+
return 0;
49+
}

hex-ll.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#ifndef HEX_LL_H
2+
#define HEX_LL_H
3+
4+
extern const signed char hexval_table[256];
5+
static inline unsigned int hexval(unsigned char c)
6+
{
7+
return hexval_table[c];
8+
}
9+
10+
/*
11+
* Convert two consecutive hexadecimal digits into a char. Return a
12+
* negative value on error. Don't run over the end of short strings.
13+
*/
14+
static inline int hex2chr(const char *s)
15+
{
16+
unsigned int val = hexval(s[0]);
17+
return (val & ~0xf) ? val : (val << 4) | hexval(s[1]);
18+
}
19+
20+
/*
21+
* Read `len` pairs of hexadecimal digits from `hex` and write the
22+
* values to `binary` as `len` bytes. Return 0 on success, or -1 if
23+
* the input does not consist of hex digits).
24+
*/
25+
int hex_to_bytes(unsigned char *binary, const char *hex, size_t len);
26+
27+
#endif

0 commit comments

Comments
 (0)