Skip to content

Commit

Permalink
Merge branch 'cw/prelim-cleanup'
Browse files Browse the repository at this point in the history
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
  • Loading branch information
gitster committed Oct 10, 2023
2 parents 3df51ea + b1bda75 commit a7a2d10
Show file tree
Hide file tree
Showing 30 changed files with 313 additions and 284 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,7 @@ LIB_OBJS += hash-lookup.o
LIB_OBJS += hashmap.o
LIB_OBJS += help.o
LIB_OBJS += hex.o
LIB_OBJS += hex-ll.o
LIB_OBJS += hook.o
LIB_OBJS += ident.o
LIB_OBJS += json-writer.o
Expand Down Expand Up @@ -1089,6 +1090,7 @@ LIB_OBJS += pack-write.o
LIB_OBJS += packfile.o
LIB_OBJS += pager.o
LIB_OBJS += parallel-checkout.o
LIB_OBJS += parse.o
LIB_OBJS += parse-options-cb.o
LIB_OBJS += parse-options.o
LIB_OBJS += patch-delta.o
Expand Down
2 changes: 1 addition & 1 deletion attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

#include "git-compat-util.h"
#include "config.h"
#include "parse.h"
#include "environment.h"
#include "exec-cmd.h"
#include "attr.h"
Expand Down
2 changes: 1 addition & 1 deletion color.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "color.h"
#include "editor.h"
#include "gettext.h"
#include "hex.h"
#include "hex-ll.h"
#include "pager.h"
#include "strbuf.h"

Expand Down
173 changes: 1 addition & 172 deletions config.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "date.h"
#include "branch.h"
#include "config.h"
#include "parse.h"
#include "convert.h"
#include "environment.h"
#include "gettext.h"
Expand Down Expand Up @@ -1165,129 +1166,6 @@ static int git_parse_source(struct config_source *cs, config_fn_t fn,
return error_return;
}

static uintmax_t get_unit_factor(const char *end)
{
if (!*end)
return 1;
else if (!strcasecmp(end, "k"))
return 1024;
else if (!strcasecmp(end, "m"))
return 1024 * 1024;
else if (!strcasecmp(end, "g"))
return 1024 * 1024 * 1024;
return 0;
}

static int git_parse_signed(const char *value, intmax_t *ret, intmax_t max)
{
if (value && *value) {
char *end;
intmax_t val;
intmax_t factor;

if (max < 0)
BUG("max must be a positive integer");

errno = 0;
val = strtoimax(value, &end, 0);
if (errno == ERANGE)
return 0;
if (end == value) {
errno = EINVAL;
return 0;
}
factor = get_unit_factor(end);
if (!factor) {
errno = EINVAL;
return 0;
}
if ((val < 0 && -max / factor > val) ||
(val > 0 && max / factor < val)) {
errno = ERANGE;
return 0;
}
val *= factor;
*ret = val;
return 1;
}
errno = EINVAL;
return 0;
}

static int git_parse_unsigned(const char *value, uintmax_t *ret, uintmax_t max)
{
if (value && *value) {
char *end;
uintmax_t val;
uintmax_t factor;

/* negative values would be accepted by strtoumax */
if (strchr(value, '-')) {
errno = EINVAL;
return 0;
}
errno = 0;
val = strtoumax(value, &end, 0);
if (errno == ERANGE)
return 0;
if (end == value) {
errno = EINVAL;
return 0;
}
factor = get_unit_factor(end);
if (!factor) {
errno = EINVAL;
return 0;
}
if (unsigned_mult_overflows(factor, val) ||
factor * val > max) {
errno = ERANGE;
return 0;
}
val *= factor;
*ret = val;
return 1;
}
errno = EINVAL;
return 0;
}

int git_parse_int(const char *value, int *ret)
{
intmax_t tmp;
if (!git_parse_signed(value, &tmp, maximum_signed_value_of_type(int)))
return 0;
*ret = tmp;
return 1;
}

static int git_parse_int64(const char *value, int64_t *ret)
{
intmax_t tmp;
if (!git_parse_signed(value, &tmp, maximum_signed_value_of_type(int64_t)))
return 0;
*ret = tmp;
return 1;
}

int git_parse_ulong(const char *value, unsigned long *ret)
{
uintmax_t tmp;
if (!git_parse_unsigned(value, &tmp, maximum_unsigned_value_of_type(long)))
return 0;
*ret = tmp;
return 1;
}

int git_parse_ssize_t(const char *value, ssize_t *ret)
{
intmax_t tmp;
if (!git_parse_signed(value, &tmp, maximum_signed_value_of_type(ssize_t)))
return 0;
*ret = tmp;
return 1;
}

NORETURN
static void die_bad_number(const char *name, const char *value,
const struct key_value_info *kvi)
Expand Down Expand Up @@ -1363,23 +1241,6 @@ ssize_t git_config_ssize_t(const char *name, const char *value,
return ret;
}

static int git_parse_maybe_bool_text(const char *value)
{
if (!value)
return 1;
if (!*value)
return 0;
if (!strcasecmp(value, "true")
|| !strcasecmp(value, "yes")
|| !strcasecmp(value, "on"))
return 1;
if (!strcasecmp(value, "false")
|| !strcasecmp(value, "no")
|| !strcasecmp(value, "off"))
return 0;
return -1;
}

static const struct fsync_component_name {
const char *name;
enum fsync_component component_bits;
Expand Down Expand Up @@ -1454,16 +1315,6 @@ static enum fsync_component parse_fsync_components(const char *var, const char *
return (current & ~negative) | positive;
}

int git_parse_maybe_bool(const char *value)
{
int v = git_parse_maybe_bool_text(value);
if (0 <= v)
return v;
if (git_parse_int(value, &v))
return !!v;
return -1;
}

int git_config_bool_or_int(const char *name, const char *value,
const struct key_value_info *kvi, int *is_bool)
{
Expand Down Expand Up @@ -2131,28 +1982,6 @@ void git_global_config(char **user_out, char **xdg_out)
*xdg_out = xdg_config;
}

/*
* Parse environment variable 'k' as a boolean (in various
* possible spellings); if missing, use the default value 'def'.
*/
int git_env_bool(const char *k, int def)
{
const char *v = getenv(k);
return v ? git_config_bool(k, v) : def;
}

/*
* Parse environment variable 'k' as ulong with possibly a unit
* suffix; if missing, use the default value 'val'.
*/
unsigned long git_env_ulong(const char *k, unsigned long val)
{
const char *v = getenv(k);
if (v && !git_parse_ulong(v, &val))
die(_("failed to parse %s"), k);
return val;
}

int git_config_system(void)
{
return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0);
Expand Down
14 changes: 1 addition & 13 deletions config.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "hashmap.h"
#include "string-list.h"
#include "repository.h"

#include "parse.h"

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

int git_parse_ssize_t(const char *, ssize_t *);
int git_parse_ulong(const char *, unsigned long *);
int git_parse_int(const char *value, int *ret);

/**
* Same as `git_config_bool`, except that it returns -1 on error rather
* than dying.
*/
int git_parse_maybe_bool(const char *);

/**
* Parse the string to an integer, including unit factors. Dies on error;
* otherwise, returns the parsed result.
Expand Down Expand Up @@ -385,8 +375,6 @@ int git_config_rename_section(const char *, const char *);
int git_config_rename_section_in_file(const char *, const char *, const char *);
int git_config_copy_section(const char *, const char *);
int git_config_copy_section_in_file(const char *, const char *, const char *);
int git_env_bool(const char *, int);
unsigned long git_env_ulong(const char *, unsigned long);
int git_config_system(void);
int config_error_nonbool(const char *);
#if defined(__GNUC__)
Expand Down
5 changes: 5 additions & 0 deletions entry.c
Original file line number Diff line number Diff line change
Expand Up @@ -581,3 +581,8 @@ void unlink_entry(const struct cache_entry *ce, const char *super_prefix)
return;
schedule_dir_for_removal(ce->name, ce_namelen(ce));
}

int remove_or_warn(unsigned int mode, const char *file)
{
return S_ISGITLINK(mode) ? rmdir_or_warn(file) : unlink_or_warn(file);
}
6 changes: 6 additions & 0 deletions entry.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,10 @@ int fstat_checkout_output(int fd, const struct checkout *state, struct stat *st)
void update_ce_after_write(const struct checkout *state, struct cache_entry *ce,
struct stat *st);

/*
* Calls the correct function out of {unlink,rmdir}_or_warn based on
* the supplied file mode.
*/
int remove_or_warn(unsigned int mode, const char *path);

#endif /* ENTRY_H */
49 changes: 49 additions & 0 deletions hex-ll.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "git-compat-util.h"
#include "hex-ll.h"

const signed char hexval_table[256] = {
-1, -1, -1, -1, -1, -1, -1, -1, /* 00-07 */
-1, -1, -1, -1, -1, -1, -1, -1, /* 08-0f */
-1, -1, -1, -1, -1, -1, -1, -1, /* 10-17 */
-1, -1, -1, -1, -1, -1, -1, -1, /* 18-1f */
-1, -1, -1, -1, -1, -1, -1, -1, /* 20-27 */
-1, -1, -1, -1, -1, -1, -1, -1, /* 28-2f */
0, 1, 2, 3, 4, 5, 6, 7, /* 30-37 */
8, 9, -1, -1, -1, -1, -1, -1, /* 38-3f */
-1, 10, 11, 12, 13, 14, 15, -1, /* 40-47 */
-1, -1, -1, -1, -1, -1, -1, -1, /* 48-4f */
-1, -1, -1, -1, -1, -1, -1, -1, /* 50-57 */
-1, -1, -1, -1, -1, -1, -1, -1, /* 58-5f */
-1, 10, 11, 12, 13, 14, 15, -1, /* 60-67 */
-1, -1, -1, -1, -1, -1, -1, -1, /* 68-67 */
-1, -1, -1, -1, -1, -1, -1, -1, /* 70-77 */
-1, -1, -1, -1, -1, -1, -1, -1, /* 78-7f */
-1, -1, -1, -1, -1, -1, -1, -1, /* 80-87 */
-1, -1, -1, -1, -1, -1, -1, -1, /* 88-8f */
-1, -1, -1, -1, -1, -1, -1, -1, /* 90-97 */
-1, -1, -1, -1, -1, -1, -1, -1, /* 98-9f */
-1, -1, -1, -1, -1, -1, -1, -1, /* a0-a7 */
-1, -1, -1, -1, -1, -1, -1, -1, /* a8-af */
-1, -1, -1, -1, -1, -1, -1, -1, /* b0-b7 */
-1, -1, -1, -1, -1, -1, -1, -1, /* b8-bf */
-1, -1, -1, -1, -1, -1, -1, -1, /* c0-c7 */
-1, -1, -1, -1, -1, -1, -1, -1, /* c8-cf */
-1, -1, -1, -1, -1, -1, -1, -1, /* d0-d7 */
-1, -1, -1, -1, -1, -1, -1, -1, /* d8-df */
-1, -1, -1, -1, -1, -1, -1, -1, /* e0-e7 */
-1, -1, -1, -1, -1, -1, -1, -1, /* e8-ef */
-1, -1, -1, -1, -1, -1, -1, -1, /* f0-f7 */
-1, -1, -1, -1, -1, -1, -1, -1, /* f8-ff */
};

int hex_to_bytes(unsigned char *binary, const char *hex, size_t len)
{
for (; len; len--, hex += 2) {
unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);

if (val & ~0xff)
return -1;
*binary++ = val;
}
return 0;
}
27 changes: 27 additions & 0 deletions hex-ll.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef HEX_LL_H
#define HEX_LL_H

extern const signed char hexval_table[256];
static inline unsigned int hexval(unsigned char c)
{
return hexval_table[c];
}

/*
* Convert two consecutive hexadecimal digits into a char. Return a
* negative value on error. Don't run over the end of short strings.
*/
static inline int hex2chr(const char *s)
{
unsigned int val = hexval(s[0]);
return (val & ~0xf) ? val : (val << 4) | hexval(s[1]);
}

/*
* Read `len` pairs of hexadecimal digits from `hex` and write the
* values to `binary` as `len` bytes. Return 0 on success, or -1 if
* the input does not consist of hex digits).
*/
int hex_to_bytes(unsigned char *binary, const char *hex, size_t len);

#endif
Loading

0 comments on commit a7a2d10

Please sign in to comment.