Skip to content

Commit 08e5fb1

Browse files
committed
hex: retire get_sha1_hex()
The naming convention around get_sha1_hex() and its friends is awkward these days, after "struct object_id" was introduced. There are three public functions around this area: * get_sha1_hex() - use the implied the_hash_algo, fill uchar * * get_oid_hex() - use the implied the_hash_algo, fill oid * * get_oid_hex_algop() - use the passed algop, fill oid * Between the latter two, the "_algop" suffix signals whether the the_hash_algo is used as the implied algorithm or the caller should pass an algorithm explicitly. That is very much understandable and is a good convention. Between the former two, however, the "SHA1" vs "OID" in the names differentiate in what type of variable the result is stored. We could argue that it makes sense to use "SHA1" to mean "flat byte buffer" to honor the historical practice in the days before "struct object_id" was invented, but the natural fourth friend of the above group would take an algop and fill a flat byte buffer, and it would be strange to name it get_sha1_hex_algop(). Do we use the passed in algo, or are we limited to SHA-1 ;-)? In fact, such a function exists, albeit as a private helper function used by the implementation of these functions, and is named a lot more sensibly: get_hash_hex_algop(). Correct the misnomer of get_sha1_hex() and use "hash", instead of "sha1", as "flat byte buffer that stores binary (as opposed to hexadecimal) representation of the hash". The four (2x2) friends now become: * get_hash_hex() - use the implied the_hash_algo, fill uchar * * get_oid_hex() - use the implied the_hash_algo, fill oid * * get_hash_hex_algop() - use the passed algop, fill uchar * * get_oid_hex_algop() - use the passed algop, fill oid * As there are only two remaining calls to get_sha1_hex() in the codebase right now, the blast radious of this change is fairly small. Signed-off-by: Junio C Hamano <[email protected]>
1 parent fb7d80e commit 08e5fb1

File tree

4 files changed

+9
-7
lines changed

4 files changed

+9
-7
lines changed

hex.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ static int get_hash_hex_algop(const char *hex, unsigned char *hash,
6363
return 0;
6464
}
6565

66-
int get_sha1_hex(const char *hex, unsigned char *sha1)
66+
int get_hash_hex(const char *hex, unsigned char *sha1)
6767
{
6868
return get_hash_hex_algop(hex, sha1, the_hash_algo);
6969
}

hex.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@ static inline int hex2chr(const char *s)
2020
}
2121

2222
/*
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.
23+
* Try to read a hash (specified by the_hash_algo) in hexadecimal
24+
* format from the 40 (or whatever length the hash algorithm uses)
25+
* characters starting at hex. Write the 20-byte (or the length of
26+
* the hash) result to hash in binary form.
2527
* Return 0 on success. Reading stops if a NUL is encountered in the
2628
* input, so it is safe to pass this function an arbitrary
2729
* null-terminated string.
2830
*/
29-
int get_sha1_hex(const char *hex, unsigned char *sha1);
30-
int get_oid_hex(const char *hex, struct object_id *sha1);
31+
int get_hash_hex(const char *hex, unsigned char *hash);
32+
int get_oid_hex(const char *hex, struct object_id *oid);
3133

3234
/* Like get_oid_hex, but for an arbitrary hash algorithm. */
3335
int get_oid_hex_algop(const char *hex, struct object_id *oid, const struct git_hash_algo *algop);

packfile.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ struct packed_git *add_packed_git(const char *path, size_t path_len, int local)
753753
p->pack_local = local;
754754
p->mtime = st.st_mtime;
755755
if (path_len < the_hash_algo->hexsz ||
756-
get_sha1_hex(path + path_len - the_hash_algo->hexsz, p->hash))
756+
get_hash_hex(path + path_len - the_hash_algo->hexsz, p->hash))
757757
hashclr(p->hash);
758758
return p;
759759
}

rerere.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ static void read_rr(struct repository *r, struct string_list *rr)
204204
const unsigned hexsz = the_hash_algo->hexsz;
205205

206206
/* There has to be the hash, tab, path and then NUL */
207-
if (buf.len < hexsz + 2 || get_sha1_hex(buf.buf, hash))
207+
if (buf.len < hexsz + 2 || get_hash_hex(buf.buf, hash))
208208
die(_("corrupt MERGE_RR"));
209209

210210
if (buf.buf[hexsz] != '.') {

0 commit comments

Comments
 (0)