Skip to content

Commit 676278f

Browse files
committed
Merge branch 'bc/object-id-part17'
Preparation for SHA-256 upgrade continues. * bc/object-id-part17: (26 commits) midx: switch to using the_hash_algo builtin/show-index: replace sha1_to_hex rerere: replace sha1_to_hex builtin/receive-pack: replace sha1_to_hex builtin/index-pack: replace sha1_to_hex packfile: replace sha1_to_hex wt-status: convert struct wt_status to object_id cache: remove null_sha1 builtin/worktree: switch null_sha1 to null_oid builtin/repack: write object IDs of the proper length pack-write: use hash_to_hex when writing checksums sequencer: convert to use the_hash_algo bisect: switch to using the_hash_algo sha1-lookup: switch hard-coded constants to the_hash_algo config: use the_hash_algo in abbrev comparison combine-diff: replace GIT_SHA1_HEXSZ with the_hash_algo bundle: switch to use the_hash_algo connected: switch GIT_SHA1_HEXSZ to the_hash_algo show-index: switch hard-coded constants to the_hash_algo blame: remove needless comparison with GIT_SHA1_HEXSZ ...
2 parents aafb754 + aaa95df commit 676278f

31 files changed

+121
-125
lines changed

bisect.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ static int bisect_checkout(const struct object_id *bisect_rev, int no_checkout)
707707
{
708708
char bisect_rev_hex[GIT_MAX_HEXSZ + 1];
709709

710-
memcpy(bisect_rev_hex, oid_to_hex(bisect_rev), GIT_SHA1_HEXSZ + 1);
710+
memcpy(bisect_rev_hex, oid_to_hex(bisect_rev), the_hash_algo->hexsz + 1);
711711
update_ref(NULL, "BISECT_EXPECTED_REV", bisect_rev, NULL, 0, UPDATE_REFS_DIE_ON_ERR);
712712

713713
argv_checkout[2] = bisect_rev_hex;

blame.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ static void append_merge_parents(struct repository *r,
144144

145145
while (!strbuf_getwholeline_fd(&line, merge_head, '\n')) {
146146
struct object_id oid;
147-
if (line.len < GIT_SHA1_HEXSZ || get_oid_hex(line.buf, &oid))
147+
if (get_oid_hex(line.buf, &oid))
148148
die("unknown line in '%s': %s",
149149
git_path_merge_head(r), line.buf);
150150
tail = append_parent(r, tail, &oid);

builtin/blame.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ static void emit_other(struct blame_scoreboard *sb, struct blame_entry *ent, int
460460

461461
for (cnt = 0; cnt < ent->num_lines; cnt++) {
462462
char ch;
463-
int length = (opt & OUTPUT_LONG_OBJECT_NAME) ? GIT_SHA1_HEXSZ : abbrev;
463+
int length = (opt & OUTPUT_LONG_OBJECT_NAME) ? the_hash_algo->hexsz : abbrev;
464464

465465
if (opt & OUTPUT_COLOR_LINE) {
466466
if (cnt > 0) {
@@ -885,6 +885,7 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
885885
struct range_set ranges;
886886
unsigned int range_i;
887887
long anchor;
888+
const int hexsz = the_hash_algo->hexsz;
888889

889890
setup_default_color_by_age();
890891
git_config(git_blame_config, &output_option);
@@ -931,11 +932,11 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
931932
} else if (show_progress < 0)
932933
show_progress = isatty(2);
933934

934-
if (0 < abbrev && abbrev < GIT_SHA1_HEXSZ)
935+
if (0 < abbrev && abbrev < hexsz)
935936
/* one more abbrev length is needed for the boundary commit */
936937
abbrev++;
937938
else if (!abbrev)
938-
abbrev = GIT_SHA1_HEXSZ;
939+
abbrev = hexsz;
939940

940941
if (revs_file && read_ancestry(revs_file))
941942
die_errno("reading graft file '%s' failed", revs_file);

builtin/clone.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@ static int checkout(int submodule_progress)
785785
if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
786786
die(_("unable to write new index file"));
787787

788-
err |= run_hook_le(NULL, "post-checkout", sha1_to_hex(null_sha1),
788+
err |= run_hook_le(NULL, "post-checkout", oid_to_hex(&null_oid),
789789
oid_to_hex(&oid), "1", NULL);
790790

791791
if (!err && (option_recurse_submodules.nr > 0)) {

builtin/commit.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ static int run_status(FILE *fp, const char *index_file, const char *prefix, int
510510
s->nowarn = nowarn;
511511
s->is_initial = get_oid(s->reference, &oid) ? 1 : 0;
512512
if (!s->is_initial)
513-
hashcpy(s->sha1_commit, oid.hash);
513+
oidcpy(&s->oid_commit, &oid);
514514
s->status_format = status_format;
515515
s->ignore_submodule_arg = ignore_submodule_arg;
516516

@@ -1406,7 +1406,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
14061406

14071407
s.is_initial = get_oid(s.reference, &oid) ? 1 : 0;
14081408
if (!s.is_initial)
1409-
hashcpy(s.sha1_commit, oid.hash);
1409+
oidcpy(&s.oid_commit, &oid);
14101410

14111411
s.ignore_submodule_arg = ignore_submodule_arg;
14121412
s.status_format = status_format;

builtin/index-pack.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1490,11 +1490,11 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
14901490
}
14911491

14921492
if (!from_stdin) {
1493-
printf("%s\n", sha1_to_hex(hash));
1493+
printf("%s\n", hash_to_hex(hash));
14941494
} else {
14951495
struct strbuf buf = STRBUF_INIT;
14961496

1497-
strbuf_addf(&buf, "%s\t%s\n", report, sha1_to_hex(hash));
1497+
strbuf_addf(&buf, "%s\t%s\n", report, hash_to_hex(hash));
14981498
write_or_die(1, buf.buf, buf.len);
14991499
strbuf_release(&buf);
15001500

builtin/patch-id.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1+
#include "cache.h"
12
#include "builtin.h"
23
#include "config.h"
34
#include "diff.h"
45

56
static void flush_current_id(int patchlen, struct object_id *id, struct object_id *result)
67
{
7-
char name[50];
8+
char name[GIT_MAX_HEXSZ + 1];
89

910
if (!patchlen)
1011
return;
1112

12-
memcpy(name, oid_to_hex(id), GIT_SHA1_HEXSZ + 1);
13+
memcpy(name, oid_to_hex(id), the_hash_algo->hexsz + 1);
1314
printf("%s %s\n", oid_to_hex(result), name);
1415
}
1516

@@ -60,9 +61,9 @@ static int get_one_patchid(struct object_id *next_oid, struct object_id *result,
6061
{
6162
int patchlen = 0, found_next = 0;
6263
int before = -1, after = -1;
63-
git_SHA_CTX ctx;
64+
git_hash_ctx ctx;
6465

65-
git_SHA1_Init(&ctx);
66+
the_hash_algo->init_fn(&ctx);
6667
oidclr(result);
6768

6869
while (strbuf_getwholeline(line_buf, stdin, '\n') != EOF) {
@@ -122,7 +123,7 @@ static int get_one_patchid(struct object_id *next_oid, struct object_id *result,
122123
/* Compute the sha without whitespace */
123124
len = remove_space(line);
124125
patchlen += len;
125-
git_SHA1_Update(&ctx, line, len);
126+
the_hash_algo->update_fn(&ctx, line, len);
126127
}
127128

128129
if (!found_next)

builtin/receive-pack.c

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -417,24 +417,22 @@ static int copy_to_sideband(int in, int out, void *arg)
417417
return 0;
418418
}
419419

420-
#define HMAC_BLOCK_SIZE 64
421-
422-
static void hmac_sha1(unsigned char *out,
420+
static void hmac(unsigned char *out,
423421
const char *key_in, size_t key_len,
424422
const char *text, size_t text_len)
425423
{
426-
unsigned char key[HMAC_BLOCK_SIZE];
427-
unsigned char k_ipad[HMAC_BLOCK_SIZE];
428-
unsigned char k_opad[HMAC_BLOCK_SIZE];
424+
unsigned char key[GIT_MAX_BLKSZ];
425+
unsigned char k_ipad[GIT_MAX_BLKSZ];
426+
unsigned char k_opad[GIT_MAX_BLKSZ];
429427
int i;
430-
git_SHA_CTX ctx;
428+
git_hash_ctx ctx;
431429

432430
/* RFC 2104 2. (1) */
433-
memset(key, '\0', HMAC_BLOCK_SIZE);
434-
if (HMAC_BLOCK_SIZE < key_len) {
435-
git_SHA1_Init(&ctx);
436-
git_SHA1_Update(&ctx, key_in, key_len);
437-
git_SHA1_Final(key, &ctx);
431+
memset(key, '\0', GIT_MAX_BLKSZ);
432+
if (the_hash_algo->blksz < key_len) {
433+
the_hash_algo->init_fn(&ctx);
434+
the_hash_algo->update_fn(&ctx, key_in, key_len);
435+
the_hash_algo->final_fn(key, &ctx);
438436
} else {
439437
memcpy(key, key_in, key_len);
440438
}
@@ -446,29 +444,29 @@ static void hmac_sha1(unsigned char *out,
446444
}
447445

448446
/* RFC 2104 2. (3) & (4) */
449-
git_SHA1_Init(&ctx);
450-
git_SHA1_Update(&ctx, k_ipad, sizeof(k_ipad));
451-
git_SHA1_Update(&ctx, text, text_len);
452-
git_SHA1_Final(out, &ctx);
447+
the_hash_algo->init_fn(&ctx);
448+
the_hash_algo->update_fn(&ctx, k_ipad, sizeof(k_ipad));
449+
the_hash_algo->update_fn(&ctx, text, text_len);
450+
the_hash_algo->final_fn(out, &ctx);
453451

454452
/* RFC 2104 2. (6) & (7) */
455-
git_SHA1_Init(&ctx);
456-
git_SHA1_Update(&ctx, k_opad, sizeof(k_opad));
457-
git_SHA1_Update(&ctx, out, GIT_SHA1_RAWSZ);
458-
git_SHA1_Final(out, &ctx);
453+
the_hash_algo->init_fn(&ctx);
454+
the_hash_algo->update_fn(&ctx, k_opad, sizeof(k_opad));
455+
the_hash_algo->update_fn(&ctx, out, the_hash_algo->rawsz);
456+
the_hash_algo->final_fn(out, &ctx);
459457
}
460458

461459
static char *prepare_push_cert_nonce(const char *path, timestamp_t stamp)
462460
{
463461
struct strbuf buf = STRBUF_INIT;
464-
unsigned char sha1[GIT_SHA1_RAWSZ];
462+
unsigned char hash[GIT_MAX_RAWSZ];
465463

466464
strbuf_addf(&buf, "%s:%"PRItime, path, stamp);
467-
hmac_sha1(sha1, buf.buf, buf.len, cert_nonce_seed, strlen(cert_nonce_seed));
465+
hmac(hash, buf.buf, buf.len, cert_nonce_seed, strlen(cert_nonce_seed));
468466
strbuf_release(&buf);
469467

470468
/* RFC 2104 5. HMAC-SHA1-80 */
471-
strbuf_addf(&buf, "%"PRItime"-%.*s", stamp, GIT_SHA1_HEXSZ, sha1_to_hex(sha1));
469+
strbuf_addf(&buf, "%"PRItime"-%.*s", stamp, (int)the_hash_algo->hexsz, hash_to_hex(hash));
472470
return strbuf_detach(&buf, NULL);
473471
}
474472

@@ -970,7 +968,7 @@ static const char *push_to_deploy(unsigned char *sha1,
970968
if (run_command(&child))
971969
return "Working directory has staged changes";
972970

973-
read_tree[3] = sha1_to_hex(sha1);
971+
read_tree[3] = hash_to_hex(sha1);
974972
child_process_init(&child);
975973
child.argv = read_tree;
976974
child.env = env->argv;
@@ -987,13 +985,13 @@ static const char *push_to_deploy(unsigned char *sha1,
987985

988986
static const char *push_to_checkout_hook = "push-to-checkout";
989987

990-
static const char *push_to_checkout(unsigned char *sha1,
988+
static const char *push_to_checkout(unsigned char *hash,
991989
struct argv_array *env,
992990
const char *work_tree)
993991
{
994992
argv_array_pushf(env, "GIT_WORK_TREE=%s", absolute_path(work_tree));
995993
if (run_hook_le(env->argv, push_to_checkout_hook,
996-
sha1_to_hex(sha1), NULL))
994+
hash_to_hex(hash), NULL))
997995
return "push-to-checkout hook declined";
998996
else
999997
return NULL;

builtin/repack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ static int write_oid(const struct object_id *oid, struct packed_git *pack,
191191
die(_("could not start pack-objects to repack promisor objects"));
192192
}
193193

194-
xwrite(cmd->in, oid_to_hex(oid), GIT_SHA1_HEXSZ);
194+
xwrite(cmd->in, oid_to_hex(oid), the_hash_algo->hexsz);
195195
xwrite(cmd->in, "\n", 1);
196196
return 0;
197197
}

builtin/replace.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ static int import_object(struct object_id *oid, enum object_type type,
272272
return error(_("unable to spawn mktree"));
273273
}
274274

275-
if (strbuf_read(&result, cmd.out, 41) < 0) {
275+
if (strbuf_read(&result, cmd.out, the_hash_algo->hexsz + 1) < 0) {
276276
error_errno(_("unable to read from mktree"));
277277
close(fd);
278278
close(cmd.out);
@@ -358,14 +358,15 @@ static int replace_parents(struct strbuf *buf, int argc, const char **argv)
358358
struct strbuf new_parents = STRBUF_INIT;
359359
const char *parent_start, *parent_end;
360360
int i;
361+
const unsigned hexsz = the_hash_algo->hexsz;
361362

362363
/* find existing parents */
363364
parent_start = buf->buf;
364-
parent_start += GIT_SHA1_HEXSZ + 6; /* "tree " + "hex sha1" + "\n" */
365+
parent_start += hexsz + 6; /* "tree " + "hex sha1" + "\n" */
365366
parent_end = parent_start;
366367

367368
while (starts_with(parent_end, "parent "))
368-
parent_end += 48; /* "parent " + "hex sha1" + "\n" */
369+
parent_end += hexsz + 8; /* "parent " + "hex sha1" + "\n" */
369370

370371
/* prepare new parents */
371372
for (i = 0; i < argc; i++) {

builtin/rev-parse.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,7 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
593593
const char *name = NULL;
594594
struct object_context unused;
595595
struct strbuf buf = STRBUF_INIT;
596+
const int hexsz = the_hash_algo->hexsz;
596597

597598
if (argc > 1 && !strcmp("--parseopt", argv[1]))
598599
return cmd_parseopt(argc - 1, argv + 1, prefix);
@@ -730,8 +731,8 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
730731
abbrev = strtoul(arg, NULL, 10);
731732
if (abbrev < MINIMUM_ABBREV)
732733
abbrev = MINIMUM_ABBREV;
733-
else if (40 <= abbrev)
734-
abbrev = 40;
734+
else if (hexsz <= abbrev)
735+
abbrev = hexsz;
735736
continue;
736737
}
737738
if (!strcmp(arg, "--sq")) {

builtin/show-index.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ int cmd_show_index(int argc, const char **argv, const char *prefix)
1111
unsigned nr;
1212
unsigned int version;
1313
static unsigned int top_index[256];
14+
const unsigned hashsz = the_hash_algo->rawsz;
1415

1516
if (argc != 1)
1617
usage(show_index_usage);
@@ -36,23 +37,23 @@ int cmd_show_index(int argc, const char **argv, const char *prefix)
3637
}
3738
if (version == 1) {
3839
for (i = 0; i < nr; i++) {
39-
unsigned int offset, entry[6];
40+
unsigned int offset, entry[(GIT_MAX_RAWSZ + 4) / sizeof(unsigned int)];
4041

41-
if (fread(entry, 4 + 20, 1, stdin) != 1)
42+
if (fread(entry, 4 + hashsz, 1, stdin) != 1)
4243
die("unable to read entry %u/%u", i, nr);
4344
offset = ntohl(entry[0]);
44-
printf("%u %s\n", offset, sha1_to_hex((void *)(entry+1)));
45+
printf("%u %s\n", offset, hash_to_hex((void *)(entry+1)));
4546
}
4647
} else {
4748
unsigned off64_nr = 0;
4849
struct {
49-
unsigned char sha1[20];
50+
struct object_id oid;
5051
uint32_t crc;
5152
uint32_t off;
5253
} *entries;
5354
ALLOC_ARRAY(entries, nr);
5455
for (i = 0; i < nr; i++)
55-
if (fread(entries[i].sha1, 20, 1, stdin) != 1)
56+
if (fread(entries[i].oid.hash, hashsz, 1, stdin) != 1)
5657
die("unable to read sha1 %u/%u", i, nr);
5758
for (i = 0; i < nr; i++)
5859
if (fread(&entries[i].crc, 4, 1, stdin) != 1)
@@ -77,7 +78,7 @@ int cmd_show_index(int argc, const char **argv, const char *prefix)
7778
}
7879
printf("%" PRIuMAX " %s (%08"PRIx32")\n",
7980
(uintmax_t) offset,
80-
sha1_to_hex(entries[i].sha1),
81+
oid_to_hex(&entries[i].oid),
8182
ntohl(entries[i].crc));
8283
}
8384
free(entries);

builtin/submodule--helper.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ static int module_list(int argc, const char **argv, const char *prefix)
424424
const struct cache_entry *ce = list.entries[i];
425425

426426
if (ce_stage(ce))
427-
printf("%06o %s U\t", ce->ce_mode, sha1_to_hex(null_sha1));
427+
printf("%06o %s U\t", ce->ce_mode, oid_to_hex(&null_oid));
428428
else
429429
printf("%06o %s %d\t", ce->ce_mode,
430430
oid_to_hex(&ce->oid), ce_stage(ce));

builtin/worktree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ static int add_worktree(const char *path, const char *refname,
350350
*/
351351
strbuf_reset(&sb);
352352
strbuf_addf(&sb, "%s/HEAD", sb_repo.buf);
353-
write_file(sb.buf, "%s", sha1_to_hex(null_sha1));
353+
write_file(sb.buf, "%s", oid_to_hex(&null_oid));
354354
strbuf_reset(&sb);
355355
strbuf_addf(&sb, "%s/commondir", sb_repo.buf);
356356
write_file(sb.buf, "../..");

bundle.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ static int write_pack_data(int bundle_fd, struct rev_info *revs)
282282
struct object *object = revs->pending.objects[i].item;
283283
if (object->flags & UNINTERESTING)
284284
write_or_die(pack_objects.in, "^", 1);
285-
write_or_die(pack_objects.in, oid_to_hex(&object->oid), GIT_SHA1_HEXSZ);
285+
write_or_die(pack_objects.in, oid_to_hex(&object->oid), the_hash_algo->hexsz);
286286
write_or_die(pack_objects.in, "\n", 1);
287287
}
288288
close(pack_objects.in);
@@ -414,7 +414,7 @@ static int write_bundle_refs(int bundle_fd, struct rev_info *revs)
414414
}
415415

416416
ref_count++;
417-
write_or_die(bundle_fd, oid_to_hex(&e->item->oid), 40);
417+
write_or_die(bundle_fd, oid_to_hex(&e->item->oid), the_hash_algo->hexsz);
418418
write_or_die(bundle_fd, " ", 1);
419419
write_or_die(bundle_fd, display_ref, strlen(display_ref));
420420
write_or_die(bundle_fd, "\n", 1);

cache.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,6 @@ const char *repo_find_unique_abbrev(struct repository *r, const struct object_id
10671067
int repo_find_unique_abbrev_r(struct repository *r, char *hex, const struct object_id *oid, int len);
10681068
#define find_unique_abbrev_r(hex, oid, len) repo_find_unique_abbrev_r(the_repository, hex, oid, len)
10691069

1070-
extern const unsigned char null_sha1[GIT_MAX_RAWSZ];
10711070
extern const struct object_id null_oid;
10721071

10731072
static inline int hashcmp(const unsigned char *sha1, const unsigned char *sha2)
@@ -1102,14 +1101,9 @@ static inline int oideq(const struct object_id *oid1, const struct object_id *oi
11021101
return hasheq(oid1->hash, oid2->hash);
11031102
}
11041103

1105-
static inline int is_null_sha1(const unsigned char *sha1)
1106-
{
1107-
return hasheq(sha1, null_sha1);
1108-
}
1109-
11101104
static inline int is_null_oid(const struct object_id *oid)
11111105
{
1112-
return hasheq(oid->hash, null_sha1);
1106+
return oideq(oid, &null_oid);
11131107
}
11141108

11151109
static inline void hashcpy(unsigned char *sha_dst, const unsigned char *sha_src)

0 commit comments

Comments
 (0)