Skip to content

Commit b73ecb4

Browse files
newrengitster
authored andcommitted
hex.h: move some hex-related declarations from cache.h
hex.c contains code for hex-related functions, but for some reason these functions were declared in the catch-all cache.h. Move the function declarations into a hex.h header instead. This also allows us to remove includes of cache.h from a few C files. For now, we make cache.h include hex.h, so that it is easier to review the direct changes being made by this patch. In the next patch, we will remove that, and add the necessary direct '#include "hex.h"' in the hundreds of C files that need it. Note that reviewing the header changes in this commit might be simplified via git log --no-walk -p --color-moved $COMMIT -- '*.h'` In particular, it highlights the simple movement of code in .h files rather nicely. Signed-off-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 41227cb commit b73ecb4

File tree

7 files changed

+95
-82
lines changed

7 files changed

+95
-82
lines changed

cache.h

Lines changed: 1 addition & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "string-list.h"
1414
#include "pack-revindex.h"
1515
#include "hash.h"
16+
#include "hex.h"
1617
#include "path.h"
1718
#include "oid-array.h"
1819
#include "repository.h"
@@ -1325,22 +1326,6 @@ int finalize_object_file(const char *tmpfile, const char *filename);
13251326
/* Helper to check and "touch" a file */
13261327
int check_and_freshen_file(const char *fn, int freshen);
13271328

1328-
extern const signed char hexval_table[256];
1329-
static inline unsigned int hexval(unsigned char c)
1330-
{
1331-
return hexval_table[c];
1332-
}
1333-
1334-
/*
1335-
* Convert two consecutive hexadecimal digits into a char. Return a
1336-
* negative value on error. Don't run over the end of short strings.
1337-
*/
1338-
static inline int hex2chr(const char *s)
1339-
{
1340-
unsigned int val = hexval(s[0]);
1341-
return (val & ~0xf) ? val : (val << 4) | hexval(s[1]);
1342-
}
1343-
13441329
/* Convert to/from hex/sha1 representation */
13451330
#define MINIMUM_ABBREV minimum_abbrev
13461331
#define DEFAULT_ABBREV default_abbrev
@@ -1393,68 +1378,6 @@ int repo_for_each_abbrev(struct repository *r, const char *prefix, each_abbrev_f
13931378

13941379
int set_disambiguate_hint_config(const char *var, const char *value);
13951380

1396-
/*
1397-
* Try to read a SHA1 in hexadecimal format from the 40 characters
1398-
* starting at hex. Write the 20-byte result to sha1 in binary form.
1399-
* Return 0 on success. Reading stops if a NUL is encountered in the
1400-
* input, so it is safe to pass this function an arbitrary
1401-
* null-terminated string.
1402-
*/
1403-
int get_sha1_hex(const char *hex, unsigned char *sha1);
1404-
int get_oid_hex(const char *hex, struct object_id *sha1);
1405-
1406-
/* Like get_oid_hex, but for an arbitrary hash algorithm. */
1407-
int get_oid_hex_algop(const char *hex, struct object_id *oid, const struct git_hash_algo *algop);
1408-
1409-
/*
1410-
* Read `len` pairs of hexadecimal digits from `hex` and write the
1411-
* values to `binary` as `len` bytes. Return 0 on success, or -1 if
1412-
* the input does not consist of hex digits).
1413-
*/
1414-
int hex_to_bytes(unsigned char *binary, const char *hex, size_t len);
1415-
1416-
/*
1417-
* Convert a binary hash in "unsigned char []" or an object name in
1418-
* "struct object_id *" to its hex equivalent. The `_r` variant is reentrant,
1419-
* and writes the NUL-terminated output to the buffer `out`, which must be at
1420-
* least `GIT_MAX_HEXSZ + 1` bytes, and returns a pointer to out for
1421-
* convenience.
1422-
*
1423-
* The non-`_r` variant returns a static buffer, but uses a ring of 4
1424-
* buffers, making it safe to make multiple calls for a single statement, like:
1425-
*
1426-
* printf("%s -> %s", hash_to_hex(one), hash_to_hex(two));
1427-
* printf("%s -> %s", oid_to_hex(one), oid_to_hex(two));
1428-
*/
1429-
char *hash_to_hex_algop_r(char *buffer, const unsigned char *hash, const struct git_hash_algo *);
1430-
char *oid_to_hex_r(char *out, const struct object_id *oid);
1431-
char *hash_to_hex_algop(const unsigned char *hash, const struct git_hash_algo *); /* static buffer result! */
1432-
char *hash_to_hex(const unsigned char *hash); /* same static buffer */
1433-
char *oid_to_hex(const struct object_id *oid); /* same static buffer */
1434-
1435-
/*
1436-
* Parse a 40-character hexadecimal object ID starting from hex, updating the
1437-
* pointer specified by end when parsing stops. The resulting object ID is
1438-
* stored in oid. Returns 0 on success. Parsing will stop on the first NUL or
1439-
* other invalid character. end is only updated on success; otherwise, it is
1440-
* unmodified.
1441-
*/
1442-
int parse_oid_hex(const char *hex, struct object_id *oid, const char **end);
1443-
1444-
/* Like parse_oid_hex, but for an arbitrary hash algorithm. */
1445-
int parse_oid_hex_algop(const char *hex, struct object_id *oid, const char **end,
1446-
const struct git_hash_algo *algo);
1447-
1448-
1449-
/*
1450-
* These functions work like get_oid_hex and parse_oid_hex, but they will parse
1451-
* a hex value for any algorithm. The algorithm is detected based on the length
1452-
* and the algorithm in use is returned. If this is not a hex object ID in any
1453-
* algorithm, returns GIT_HASH_UNKNOWN.
1454-
*/
1455-
int get_oid_hex_any(const char *hex, struct object_id *oid);
1456-
int parse_oid_hex_any(const char *hex, struct object_id *oid, const char **end);
1457-
14581381
/*
14591382
* This reads short-hand syntax that not only evaluates to a commit
14601383
* object name, but also can act as if the end user spelled the name

git-compat-util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,6 +1225,7 @@ extern const unsigned char tolower_trans_tbl[256];
12251225
#undef isxdigit
12261226

12271227
extern const unsigned char sane_ctype[256];
1228+
extern const signed char hexval_table[256];
12281229
#define GIT_SPACE 0x01
12291230
#define GIT_DIGIT 0x02
12301231
#define GIT_ALPHA 0x04

hex.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#include "cache.h"
1+
#include "git-compat-util.h"
2+
#include "hex.h"
23

34
const signed char hexval_table[256] = {
45
-1, -1, -1, -1, -1, -1, -1, -1, /* 00-07 */

hex.h

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#ifndef HEX_H
2+
#define HEX_H
3+
4+
#include "hash.h"
5+
6+
extern const signed char hexval_table[256];
7+
static inline unsigned int hexval(unsigned char c)
8+
{
9+
return hexval_table[c];
10+
}
11+
12+
/*
13+
* Convert two consecutive hexadecimal digits into a char. Return a
14+
* negative value on error. Don't run over the end of short strings.
15+
*/
16+
static inline int hex2chr(const char *s)
17+
{
18+
unsigned int val = hexval(s[0]);
19+
return (val & ~0xf) ? val : (val << 4) | hexval(s[1]);
20+
}
21+
22+
/*
23+
* Try to read a SHA1 in hexadecimal format from the 40 characters
24+
* starting at hex. Write the 20-byte result to sha1 in binary form.
25+
* Return 0 on success. Reading stops if a NUL is encountered in the
26+
* input, so it is safe to pass this function an arbitrary
27+
* null-terminated string.
28+
*/
29+
int get_sha1_hex(const char *hex, unsigned char *sha1);
30+
int get_oid_hex(const char *hex, struct object_id *sha1);
31+
32+
/* Like get_oid_hex, but for an arbitrary hash algorithm. */
33+
int get_oid_hex_algop(const char *hex, struct object_id *oid, const struct git_hash_algo *algop);
34+
35+
/*
36+
* Read `len` pairs of hexadecimal digits from `hex` and write the
37+
* values to `binary` as `len` bytes. Return 0 on success, or -1 if
38+
* the input does not consist of hex digits).
39+
*/
40+
int hex_to_bytes(unsigned char *binary, const char *hex, size_t len);
41+
42+
/*
43+
* Convert a binary hash in "unsigned char []" or an object name in
44+
* "struct object_id *" to its hex equivalent. The `_r` variant is reentrant,
45+
* and writes the NUL-terminated output to the buffer `out`, which must be at
46+
* least `GIT_MAX_HEXSZ + 1` bytes, and returns a pointer to out for
47+
* convenience.
48+
*
49+
* The non-`_r` variant returns a static buffer, but uses a ring of 4
50+
* buffers, making it safe to make multiple calls for a single statement, like:
51+
*
52+
* printf("%s -> %s", hash_to_hex(one), hash_to_hex(two));
53+
* printf("%s -> %s", oid_to_hex(one), oid_to_hex(two));
54+
*/
55+
char *hash_to_hex_algop_r(char *buffer, const unsigned char *hash, const struct git_hash_algo *);
56+
char *oid_to_hex_r(char *out, const struct object_id *oid);
57+
char *hash_to_hex_algop(const unsigned char *hash, const struct git_hash_algo *); /* static buffer result! */
58+
char *hash_to_hex(const unsigned char *hash); /* same static buffer */
59+
char *oid_to_hex(const struct object_id *oid); /* same static buffer */
60+
61+
/*
62+
* Parse a 40-character hexadecimal object ID starting from hex, updating the
63+
* pointer specified by end when parsing stops. The resulting object ID is
64+
* stored in oid. Returns 0 on success. Parsing will stop on the first NUL or
65+
* other invalid character. end is only updated on success; otherwise, it is
66+
* unmodified.
67+
*/
68+
int parse_oid_hex(const char *hex, struct object_id *oid, const char **end);
69+
70+
/* Like parse_oid_hex, but for an arbitrary hash algorithm. */
71+
int parse_oid_hex_algop(const char *hex, struct object_id *oid, const char **end,
72+
const struct git_hash_algo *algo);
73+
74+
75+
/*
76+
* These functions work like get_oid_hex and parse_oid_hex, but they will parse
77+
* a hex value for any algorithm. The algorithm is detected based on the length
78+
* and the algorithm in use is returned. If this is not a hex object ID in any
79+
* algorithm, returns GIT_HASH_UNKNOWN.
80+
*/
81+
int get_oid_hex_any(const char *hex, struct object_id *oid);
82+
int parse_oid_hex_any(const char *hex, struct object_id *oid, const char **end);
83+
84+
#endif

mailinfo.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
#include "cache.h"
1+
#include "git-compat-util.h"
22
#include "config.h"
3+
#include "gettext.h"
4+
#include "hex.h"
35
#include "utf8.h"
46
#include "strbuf.h"
57
#include "mailinfo.h"

oidset.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
#include "cache.h"
1+
#include "git-compat-util.h"
22
#include "oidset.h"
3+
#include "hex.h"
4+
#include "strbuf.h"
35

46
void oidset_init(struct oidset *set, size_t initial_size)
57
{

wildmatch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
** work differently than '*', and to fix the character-class code.
1010
*/
1111

12-
#include "cache.h"
12+
#include "git-compat-util.h"
1313
#include "wildmatch.h"
1414

1515
typedef unsigned char uchar;

0 commit comments

Comments
 (0)