Skip to content

Commit 37aac3e

Browse files
committed
Merge branch 'bc/object-id'
Conversion from uchar[40] to struct object_id continues. * bc/object-id: pretty: switch hard-coded constants to the_hash_algo sha1-file: convert constants to uses of the_hash_algo log-tree: switch GIT_SHA1_HEXSZ to the_hash_algo->hexsz diff: switch GIT_SHA1_HEXSZ to use the_hash_algo builtin/merge-recursive: make hash independent builtin/merge: switch to use the_hash_algo builtin/fmt-merge-msg: make hash independent builtin/update-index: simplify parsing of cacheinfo builtin/update-index: convert to using the_hash_algo refs/files-backend: use the_hash_algo for writing refs sha1-name: use the_hash_algo when parsing object names strbuf: allocate space with GIT_MAX_HEXSZ commit: express tree entry constants in terms of the_hash_algo hex: switch to using the_hash_algo tree-walk: replace hard-coded constants with the_hash_algo cache: update object ID functions for the_hash_algo
2 parents bba1a55 + 580f098 commit 37aac3e

File tree

15 files changed

+56
-49
lines changed

15 files changed

+56
-49
lines changed

builtin/fmt-merge-msg.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,15 @@ static int handle_line(char *line, struct merge_parents *merge_parents)
109109
struct string_list_item *item;
110110
int pulling_head = 0;
111111
struct object_id oid;
112+
const unsigned hexsz = the_hash_algo->hexsz;
112113

113-
if (len < GIT_SHA1_HEXSZ + 3 || line[GIT_SHA1_HEXSZ] != '\t')
114+
if (len < hexsz + 3 || line[hexsz] != '\t')
114115
return 1;
115116

116-
if (starts_with(line + GIT_SHA1_HEXSZ + 1, "not-for-merge"))
117+
if (starts_with(line + hexsz + 1, "not-for-merge"))
117118
return 0;
118119

119-
if (line[GIT_SHA1_HEXSZ + 1] != '\t')
120+
if (line[hexsz + 1] != '\t')
120121
return 2;
121122

122123
i = get_oid_hex(line, &oid);
@@ -131,7 +132,7 @@ static int handle_line(char *line, struct merge_parents *merge_parents)
131132

132133
if (line[len - 1] == '\n')
133134
line[len - 1] = 0;
134-
line += GIT_SHA1_HEXSZ + 2;
135+
line += hexsz + 2;
135136

136137
/*
137138
* At this point, line points at the beginning of comment e.g.
@@ -343,7 +344,7 @@ static void shortlog(const char *name,
343344
const struct object_id *oid = &origin_data->oid;
344345
int limit = opts->shortlog_len;
345346

346-
branch = deref_tag(parse_object(oid), oid_to_hex(oid), GIT_SHA1_HEXSZ);
347+
branch = deref_tag(parse_object(oid), oid_to_hex(oid), the_hash_algo->hexsz);
347348
if (!branch || branch->type != OBJ_COMMIT)
348349
return;
349350

@@ -546,17 +547,17 @@ static void find_merge_parents(struct merge_parents *result,
546547
int len;
547548
char *p = in->buf + pos;
548549
char *newline = strchr(p, '\n');
550+
const char *q;
549551
struct object_id oid;
550552
struct commit *parent;
551553
struct object *obj;
552554

553555
len = newline ? newline - p : strlen(p);
554556
pos += len + !!newline;
555557

556-
if (len < GIT_SHA1_HEXSZ + 3 ||
557-
get_oid_hex(p, &oid) ||
558-
p[GIT_SHA1_HEXSZ] != '\t' ||
559-
p[GIT_SHA1_HEXSZ + 1] != '\t')
558+
if (parse_oid_hex(p, &oid, &q) ||
559+
q[0] != '\t' ||
560+
q[1] != '\t')
560561
continue; /* skip not-for-merge */
561562
/*
562563
* Do not use get_merge_parent() here; we do not have

builtin/merge-recursive.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ static const char builtin_merge_recursive_usage[] =
99

1010
static const char *better_branch_name(const char *branch)
1111
{
12-
static char githead_env[8 + GIT_SHA1_HEXSZ + 1];
12+
static char githead_env[8 + GIT_MAX_HEXSZ + 1];
1313
char *name;
1414

15-
if (strlen(branch) != GIT_SHA1_HEXSZ)
15+
if (strlen(branch) != the_hash_algo->hexsz)
1616
return branch;
1717
xsnprintf(githead_env, sizeof(githead_env), "GITHEAD_%s", branch);
1818
name = getenv(githead_env);

builtin/merge.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,7 @@ static void handle_fetch_head(struct commit_list **remotes, struct strbuf *merge
10351035
const char *filename;
10361036
int fd, pos, npos;
10371037
struct strbuf fetch_head_file = STRBUF_INIT;
1038+
const unsigned hexsz = the_hash_algo->hexsz;
10381039

10391040
if (!merge_names)
10401041
merge_names = &fetch_head_file;
@@ -1060,16 +1061,16 @@ static void handle_fetch_head(struct commit_list **remotes, struct strbuf *merge
10601061
else
10611062
npos = merge_names->len;
10621063

1063-
if (npos - pos < GIT_SHA1_HEXSZ + 2 ||
1064+
if (npos - pos < hexsz + 2 ||
10641065
get_oid_hex(merge_names->buf + pos, &oid))
10651066
commit = NULL; /* bad */
1066-
else if (memcmp(merge_names->buf + pos + GIT_SHA1_HEXSZ, "\t\t", 2))
1067+
else if (memcmp(merge_names->buf + pos + hexsz, "\t\t", 2))
10671068
continue; /* not-for-merge */
10681069
else {
1069-
char saved = merge_names->buf[pos + GIT_SHA1_HEXSZ];
1070-
merge_names->buf[pos + GIT_SHA1_HEXSZ] = '\0';
1070+
char saved = merge_names->buf[pos + hexsz];
1071+
merge_names->buf[pos + hexsz] = '\0';
10711072
commit = get_merge_parent(merge_names->buf + pos);
1072-
merge_names->buf[pos + GIT_SHA1_HEXSZ] = saved;
1073+
merge_names->buf[pos + hexsz] = saved;
10731074
}
10741075
if (!commit) {
10751076
if (ptr)

builtin/update-index.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,7 @@ static void update_one(const char *path)
492492

493493
static void read_index_info(int nul_term_line)
494494
{
495+
const int hexsz = the_hash_algo->hexsz;
495496
struct strbuf buf = STRBUF_INIT;
496497
struct strbuf uq = STRBUF_INIT;
497498
strbuf_getline_fn getline_fn;
@@ -529,7 +530,7 @@ static void read_index_info(int nul_term_line)
529530
mode = ul;
530531

531532
tab = strchr(ptr, '\t');
532-
if (!tab || tab - ptr < GIT_SHA1_HEXSZ + 1)
533+
if (!tab || tab - ptr < hexsz + 1)
533534
goto bad_line;
534535

535536
if (tab[-2] == ' ' && '0' <= tab[-1] && tab[-1] <= '3') {
@@ -542,8 +543,8 @@ static void read_index_info(int nul_term_line)
542543
ptr = tab + 1; /* point at the head of path */
543544
}
544545

545-
if (get_oid_hex(tab - GIT_SHA1_HEXSZ, &oid) ||
546-
tab[-(GIT_SHA1_HEXSZ + 1)] != ' ')
546+
if (get_oid_hex(tab - hexsz, &oid) ||
547+
tab[-(hexsz + 1)] != ' ')
547548
goto bad_line;
548549

549550
path_name = ptr;
@@ -571,7 +572,7 @@ static void read_index_info(int nul_term_line)
571572
* ptr[-1] points at tab,
572573
* ptr[-41] is at the beginning of sha1
573574
*/
574-
ptr[-(GIT_SHA1_HEXSZ + 2)] = ptr[-1] = 0;
575+
ptr[-(hexsz + 2)] = ptr[-1] = 0;
575576
if (add_cacheinfo(mode, &oid, path_name, stage))
576577
die("git update-index: unable to update %s",
577578
path_name);
@@ -826,6 +827,7 @@ static int parse_new_style_cacheinfo(const char *arg,
826827
{
827828
unsigned long ul;
828829
char *endp;
830+
const char *p;
829831

830832
if (!arg)
831833
return -1;
@@ -836,9 +838,9 @@ static int parse_new_style_cacheinfo(const char *arg,
836838
return -1; /* not a new-style cacheinfo */
837839
*mode = ul;
838840
endp++;
839-
if (get_oid_hex(endp, oid) || endp[GIT_SHA1_HEXSZ] != ',')
841+
if (parse_oid_hex(endp, oid, &p) || *p != ',')
840842
return -1;
841-
*path = endp + GIT_SHA1_HEXSZ + 1;
843+
*path = p + 1;
842844
return 0;
843845
}
844846

cache.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,7 @@ extern const struct object_id null_oid;
972972

973973
static inline int hashcmp(const unsigned char *sha1, const unsigned char *sha2)
974974
{
975-
return memcmp(sha1, sha2, GIT_SHA1_RAWSZ);
975+
return memcmp(sha1, sha2, the_hash_algo->rawsz);
976976
}
977977

978978
static inline int oidcmp(const struct object_id *oid1, const struct object_id *oid2)
@@ -992,7 +992,7 @@ static inline int is_null_oid(const struct object_id *oid)
992992

993993
static inline void hashcpy(unsigned char *sha_dst, const unsigned char *sha_src)
994994
{
995-
memcpy(sha_dst, sha_src, GIT_SHA1_RAWSZ);
995+
memcpy(sha_dst, sha_src, the_hash_algo->rawsz);
996996
}
997997

998998
static inline void oidcpy(struct object_id *dst, const struct object_id *src)
@@ -1009,7 +1009,7 @@ static inline struct object_id *oiddup(const struct object_id *src)
10091009

10101010
static inline void hashclr(unsigned char *hash)
10111011
{
1012-
memset(hash, 0, GIT_SHA1_RAWSZ);
1012+
memset(hash, 0, the_hash_algo->rawsz);
10131013
}
10141014

10151015
static inline void oidclr(struct object_id *oid)

commit.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,8 @@ int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long s
369369
struct object_id parent;
370370
struct commit_list **pptr;
371371
struct commit_graft *graft;
372-
const int tree_entry_len = GIT_SHA1_HEXSZ + 5;
373-
const int parent_entry_len = GIT_SHA1_HEXSZ + 7;
372+
const int tree_entry_len = the_hash_algo->hexsz + 5;
373+
const int parent_entry_len = the_hash_algo->hexsz + 7;
374374

375375
if (item->object.parsed)
376376
return 0;

diff.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3833,7 +3833,7 @@ static const char *diff_abbrev_oid(const struct object_id *oid, int abbrev)
38333833
char *hex = oid_to_hex(oid);
38343834
if (abbrev < 0)
38353835
abbrev = FALLBACK_DEFAULT_ABBREV;
3836-
if (abbrev > GIT_SHA1_HEXSZ)
3836+
if (abbrev > the_hash_algo->hexsz)
38373837
BUG("oid abbreviation out of range: %d", abbrev);
38383838
if (abbrev)
38393839
hex[abbrev] = '\0';
@@ -4948,7 +4948,7 @@ const char *diff_aligned_abbrev(const struct object_id *oid, int len)
49484948
const char *abbrev;
49494949

49504950
/* Do we want all 40 hex characters? */
4951-
if (len == GIT_SHA1_HEXSZ)
4951+
if (len == the_hash_algo->hexsz)
49524952
return oid_to_hex(oid);
49534953

49544954
/* An abbreviated value is fine, possibly followed by an ellipsis. */
@@ -4978,7 +4978,7 @@ const char *diff_aligned_abbrev(const struct object_id *oid, int len)
49784978
* the automatic sizing is supposed to give abblen that ensures
49794979
* uniqueness across all objects (statistically speaking).
49804980
*/
4981-
if (abblen < GIT_SHA1_HEXSZ - 3) {
4981+
if (abblen < the_hash_algo->hexsz - 3) {
49824982
static char hex[GIT_MAX_HEXSZ + 1];
49834983
if (len < abblen && abblen <= len + 2)
49844984
xsnprintf(hex, sizeof(hex), "%s%.*s", abbrev, len+3-abblen, "..");

hex.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ int hex_to_bytes(unsigned char *binary, const char *hex, size_t len)
5050
int get_sha1_hex(const char *hex, unsigned char *sha1)
5151
{
5252
int i;
53-
for (i = 0; i < GIT_SHA1_RAWSZ; i++) {
53+
for (i = 0; i < the_hash_algo->rawsz; i++) {
5454
int val = hex2chr(hex);
5555
if (val < 0)
5656
return -1;
@@ -69,7 +69,7 @@ int parse_oid_hex(const char *hex, struct object_id *oid, const char **end)
6969
{
7070
int ret = get_oid_hex(hex, oid);
7171
if (!ret)
72-
*end = hex + GIT_SHA1_HEXSZ;
72+
*end = hex + the_hash_algo->hexsz;
7373
return ret;
7474
}
7575

@@ -79,7 +79,7 @@ char *sha1_to_hex_r(char *buffer, const unsigned char *sha1)
7979
char *buf = buffer;
8080
int i;
8181

82-
for (i = 0; i < GIT_SHA1_RAWSZ; i++) {
82+
for (i = 0; i < the_hash_algo->rawsz; i++) {
8383
unsigned int val = *sha1++;
8484
*buf++ = hex[val >> 4];
8585
*buf++ = hex[val & 0xf];

log-tree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ void show_log(struct rev_info *opt)
546546
struct strbuf msgbuf = STRBUF_INIT;
547547
struct log_info *log = opt->loginfo;
548548
struct commit *commit = log->commit, *parent = log->parent;
549-
int abbrev_commit = opt->abbrev_commit ? opt->abbrev : GIT_SHA1_HEXSZ;
549+
int abbrev_commit = opt->abbrev_commit ? opt->abbrev : the_hash_algo->hexsz;
550550
const char *extra_headers = opt->extra_headers;
551551
struct pretty_print_context ctx = {0};
552552

pretty.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,15 +1575,15 @@ static void pp_header(struct pretty_print_context *pp,
15751575
}
15761576

15771577
if (starts_with(line, "parent ")) {
1578-
if (linelen != 48)
1578+
if (linelen != the_hash_algo->hexsz + 8)
15791579
die("bad parent line in commit");
15801580
continue;
15811581
}
15821582

15831583
if (!parents_shown) {
15841584
unsigned num = commit_list_count(commit->parents);
15851585
/* with enough slop */
1586-
strbuf_grow(sb, num * 50 + 20);
1586+
strbuf_grow(sb, num * (GIT_MAX_HEXSZ + 10) + 20);
15871587
add_merge_info(pp, sb, commit);
15881588
parents_shown = 1;
15891589
}

0 commit comments

Comments
 (0)