Skip to content

Commit d829d49

Browse files
committed
Merge branch 'bc/hash-transition-part-15'
More codepaths are moving away from hardcoded hash sizes. * bc/hash-transition-part-15: rerere: convert to use the_hash_algo submodule: make zero-oid comparison hash function agnostic apply: rename new_sha1_prefix and old_sha1_prefix apply: replace hard-coded constants tag: express constant in terms of the_hash_algo transport: use parse_oid_hex instead of a constant upload-pack: express constants in terms of the_hash_algo refs/packed-backend: express constants using the_hash_algo packfile: express constants in terms of the_hash_algo pack-revindex: express constants in terms of the_hash_algo builtin/fetch-pack: remove constants with parse_oid_hex builtin/mktree: remove hard-coded constant builtin/repack: replace hard-coded constants pack-bitmap-write: use GIT_MAX_RAWSZ for allocation object_id.cocci: match only expressions of type 'struct object_id'
2 parents 7dc3e5a + 0d7c419 commit d829d49

File tree

14 files changed

+181
-156
lines changed

14 files changed

+181
-156
lines changed

apply.c

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,8 @@ struct patch {
223223
struct fragment *fragments;
224224
char *result;
225225
size_t resultsize;
226-
char old_sha1_prefix[41];
227-
char new_sha1_prefix[41];
226+
char old_oid_prefix[GIT_MAX_HEXSZ + 1];
227+
char new_oid_prefix[GIT_MAX_HEXSZ + 1];
228228
struct patch *next;
229229

230230
/* three-way fallback result */
@@ -1093,13 +1093,14 @@ static int gitdiff_index(struct apply_state *state,
10931093
*/
10941094
const char *ptr, *eol;
10951095
int len;
1096+
const unsigned hexsz = the_hash_algo->hexsz;
10961097

10971098
ptr = strchr(line, '.');
1098-
if (!ptr || ptr[1] != '.' || 40 < ptr - line)
1099+
if (!ptr || ptr[1] != '.' || hexsz < ptr - line)
10991100
return 0;
11001101
len = ptr - line;
1101-
memcpy(patch->old_sha1_prefix, line, len);
1102-
patch->old_sha1_prefix[len] = 0;
1102+
memcpy(patch->old_oid_prefix, line, len);
1103+
patch->old_oid_prefix[len] = 0;
11031104

11041105
line = ptr + 2;
11051106
ptr = strchr(line, ' ');
@@ -1109,10 +1110,10 @@ static int gitdiff_index(struct apply_state *state,
11091110
ptr = eol;
11101111
len = ptr - line;
11111112

1112-
if (40 < len)
1113+
if (hexsz < len)
11131114
return 0;
1114-
memcpy(patch->new_sha1_prefix, line, len);
1115-
patch->new_sha1_prefix[len] = 0;
1115+
memcpy(patch->new_oid_prefix, line, len);
1116+
patch->new_oid_prefix[len] = 0;
11161117
if (*ptr == ' ')
11171118
return gitdiff_oldmode(state, ptr + 1, patch);
11181119
return 0;
@@ -2206,7 +2207,7 @@ static void reverse_patches(struct patch *p)
22062207
SWAP(p->new_mode, p->old_mode);
22072208
SWAP(p->is_new, p->is_delete);
22082209
SWAP(p->lines_added, p->lines_deleted);
2209-
SWAP(p->old_sha1_prefix, p->new_sha1_prefix);
2210+
SWAP(p->old_oid_prefix, p->new_oid_prefix);
22102211

22112212
for (; frag; frag = frag->next) {
22122213
SWAP(frag->newpos, frag->oldpos);
@@ -3144,15 +3145,16 @@ static int apply_binary(struct apply_state *state,
31443145
{
31453146
const char *name = patch->old_name ? patch->old_name : patch->new_name;
31463147
struct object_id oid;
3148+
const unsigned hexsz = the_hash_algo->hexsz;
31473149

31483150
/*
31493151
* For safety, we require patch index line to contain
3150-
* full 40-byte textual SHA1 for old and new, at least for now.
3152+
* full hex textual object ID for old and new, at least for now.
31513153
*/
3152-
if (strlen(patch->old_sha1_prefix) != 40 ||
3153-
strlen(patch->new_sha1_prefix) != 40 ||
3154-
get_oid_hex(patch->old_sha1_prefix, &oid) ||
3155-
get_oid_hex(patch->new_sha1_prefix, &oid))
3154+
if (strlen(patch->old_oid_prefix) != hexsz ||
3155+
strlen(patch->new_oid_prefix) != hexsz ||
3156+
get_oid_hex(patch->old_oid_prefix, &oid) ||
3157+
get_oid_hex(patch->new_oid_prefix, &oid))
31563158
return error(_("cannot apply binary patch to '%s' "
31573159
"without full index line"), name);
31583160

@@ -3162,7 +3164,7 @@ static int apply_binary(struct apply_state *state,
31623164
* applies to.
31633165
*/
31643166
hash_object_file(img->buf, img->len, blob_type, &oid);
3165-
if (strcmp(oid_to_hex(&oid), patch->old_sha1_prefix))
3167+
if (strcmp(oid_to_hex(&oid), patch->old_oid_prefix))
31663168
return error(_("the patch applies to '%s' (%s), "
31673169
"which does not match the "
31683170
"current contents."),
@@ -3175,7 +3177,7 @@ static int apply_binary(struct apply_state *state,
31753177
"'%s' but it is not empty"), name);
31763178
}
31773179

3178-
get_oid_hex(patch->new_sha1_prefix, &oid);
3180+
get_oid_hex(patch->new_oid_prefix, &oid);
31793181
if (is_null_oid(&oid)) {
31803182
clear_image(img);
31813183
return 0; /* deletion patch */
@@ -3191,7 +3193,7 @@ static int apply_binary(struct apply_state *state,
31913193
if (!result)
31923194
return error(_("the necessary postimage %s for "
31933195
"'%s' cannot be read"),
3194-
patch->new_sha1_prefix, name);
3196+
patch->new_oid_prefix, name);
31953197
clear_image(img);
31963198
img->buf = result;
31973199
img->len = size;
@@ -3207,9 +3209,9 @@ static int apply_binary(struct apply_state *state,
32073209

32083210
/* verify that the result matches */
32093211
hash_object_file(img->buf, img->len, blob_type, &oid);
3210-
if (strcmp(oid_to_hex(&oid), patch->new_sha1_prefix))
3212+
if (strcmp(oid_to_hex(&oid), patch->new_oid_prefix))
32113213
return error(_("binary patch to '%s' creates incorrect result (expecting %s, got %s)"),
3212-
name, patch->new_sha1_prefix, oid_to_hex(&oid));
3214+
name, patch->new_oid_prefix, oid_to_hex(&oid));
32133215
}
32143216

32153217
return 0;
@@ -3568,7 +3570,7 @@ static int try_threeway(struct apply_state *state,
35683570
/* Preimage the patch was prepared for */
35693571
if (patch->is_new)
35703572
write_object_file("", 0, blob_type, &pre_oid);
3571-
else if (get_oid(patch->old_sha1_prefix, &pre_oid) ||
3573+
else if (get_oid(patch->old_oid_prefix, &pre_oid) ||
35723574
read_blob_object(&buf, &pre_oid, patch->old_mode))
35733575
return error(_("repository lacks the necessary blob to fall back on 3-way merge."));
35743576

@@ -4060,13 +4062,13 @@ static int preimage_oid_in_gitlink_patch(struct patch *p, struct object_id *oid)
40604062
starts_with(++preimage, heading) &&
40614063
/* does it record full SHA-1? */
40624064
!get_oid_hex(preimage + sizeof(heading) - 1, oid) &&
4063-
preimage[sizeof(heading) + GIT_SHA1_HEXSZ - 1] == '\n' &&
4065+
preimage[sizeof(heading) + the_hash_algo->hexsz - 1] == '\n' &&
40644066
/* does the abbreviated name on the index line agree with it? */
4065-
starts_with(preimage + sizeof(heading) - 1, p->old_sha1_prefix))
4067+
starts_with(preimage + sizeof(heading) - 1, p->old_oid_prefix))
40664068
return 0; /* it all looks fine */
40674069

40684070
/* we may have full object name on the index line */
4069-
return get_oid_hex(p->old_sha1_prefix, oid);
4071+
return get_oid_hex(p->old_oid_prefix, oid);
40704072
}
40714073

40724074
/* Build an index that contains just the files needed for a 3way merge */
@@ -4095,7 +4097,7 @@ static int build_fake_ancestor(struct apply_state *state, struct patch *list)
40954097
else
40964098
return error(_("sha1 information is lacking or "
40974099
"useless for submodule %s"), name);
4098-
} else if (!get_oid_blob(patch->old_sha1_prefix, &oid)) {
4100+
} else if (!get_oid_blob(patch->old_oid_prefix, &oid)) {
40994101
; /* ok */
41004102
} else if (!patch->lines_added && !patch->lines_deleted) {
41014103
/* mode-only change: update the current */

builtin/fetch-pack.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ static void add_sought_entry(struct ref ***sought, int *nr, int *alloc,
1616
{
1717
struct ref *ref;
1818
struct object_id oid;
19+
const char *p;
1920

20-
if (!get_oid_hex(name, &oid)) {
21-
if (name[GIT_SHA1_HEXSZ] == ' ') {
22-
/* <sha1> <ref>, find refname */
23-
name += GIT_SHA1_HEXSZ + 1;
24-
} else if (name[GIT_SHA1_HEXSZ] == '\0') {
25-
; /* <sha1>, leave sha1 as name */
21+
if (!parse_oid_hex(name, &oid, &p)) {
22+
if (*p == ' ') {
23+
/* <oid> <ref>, find refname */
24+
name = p + 1;
25+
} else if (*p == '\0') {
26+
; /* <oid>, leave oid as name */
2627
} else {
2728
/* <ref>, clear cruft from oid */
2829
oidclr(&oid);

builtin/mktree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ static void mktree_line(char *buf, size_t len, int nul_term_line, int allow_miss
9898

9999
*ntr++ = 0; /* now at the beginning of SHA1 */
100100

101-
path = ntr + 41; /* at the beginning of name */
101+
path = (char *)p + 1; /* at the beginning of name */
102102
if (!nul_term_line && path[0] == '"') {
103103
struct strbuf p_uq = STRBUF_INIT;
104104
if (unquote_c_style(&p_uq, path, NULL))

builtin/repack.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,8 @@ static void repack_promisor_objects(const struct pack_objects_args *args,
235235
while (strbuf_getline_lf(&line, out) != EOF) {
236236
char *promisor_name;
237237
int fd;
238-
if (line.len != 40)
239-
die("repack: Expecting 40 character sha1 lines only from pack-objects.");
238+
if (line.len != the_hash_algo->hexsz)
239+
die("repack: Expecting full hex object ID lines only from pack-objects.");
240240
string_list_append(names, line.buf);
241241

242242
/*
@@ -407,8 +407,8 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
407407

408408
out = xfdopen(cmd.out, "r");
409409
while (strbuf_getline_lf(&line, out) != EOF) {
410-
if (line.len != 40)
411-
die("repack: Expecting 40 character sha1 lines only from pack-objects.");
410+
if (line.len != the_hash_algo->hexsz)
411+
die("repack: Expecting full hex object ID lines only from pack-objects.");
412412
string_list_append(&names, line.buf);
413413
}
414414
fclose(out);
@@ -535,14 +535,15 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
535535
reprepare_packed_git(the_repository);
536536

537537
if (delete_redundant) {
538+
const int hexsz = the_hash_algo->hexsz;
538539
int opts = 0;
539540
string_list_sort(&names);
540541
for_each_string_list_item(item, &existing_packs) {
541542
char *sha1;
542543
size_t len = strlen(item->string);
543-
if (len < 40)
544+
if (len < hexsz)
544545
continue;
545-
sha1 = item->string + len - 40;
546+
sha1 = item->string + len - hexsz;
546547
if (!string_list_has_string(&names, sha1))
547548
remove_redundant_pack(packdir, item->string);
548549
}

0 commit comments

Comments
 (0)