From 28ba1830d0533007d5cbc1110880c6599296029a Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 18 Aug 2019 20:04:02 +0000 Subject: builtin/replace: make hash size independent Instead of using GIT_SHA1_HEXSZ and hard-coded constants, switch to using the_hash_algo. Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano diff --git a/builtin/replace.c b/builtin/replace.c index 644b21c..4b00f1d 100644 --- a/builtin/replace.c +++ b/builtin/replace.c @@ -272,7 +272,7 @@ static int import_object(struct object_id *oid, enum object_type type, return error(_("unable to spawn mktree")); } - if (strbuf_read(&result, cmd.out, 41) < 0) { + if (strbuf_read(&result, cmd.out, the_hash_algo->hexsz + 1) < 0) { error_errno(_("unable to read from mktree")); close(fd); close(cmd.out); @@ -358,14 +358,15 @@ static int replace_parents(struct strbuf *buf, int argc, const char **argv) struct strbuf new_parents = STRBUF_INIT; const char *parent_start, *parent_end; int i; + const unsigned hexsz = the_hash_algo->hexsz; /* find existing parents */ parent_start = buf->buf; - parent_start += GIT_SHA1_HEXSZ + 6; /* "tree " + "hex sha1" + "\n" */ + parent_start += hexsz + 6; /* "tree " + "hex sha1" + "\n" */ parent_end = parent_start; while (starts_with(parent_end, "parent ")) - parent_end += 48; /* "parent " + "hex sha1" + "\n" */ + parent_end += hexsz + 8; /* "parent " + "hex sha1" + "\n" */ /* prepare new parents */ for (i = 0; i < argc; i++) { -- cgit v0.10.2-6-g49f6 From 36261e42ec30477434f3325f279fd91a1d9eb434 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 18 Aug 2019 20:04:03 +0000 Subject: patch-id: convert to use the_hash_algo Convert the two separate patch-id implementations to use the_hash_algo in their implementation. Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano diff --git a/builtin/patch-id.c b/builtin/patch-id.c index bd28b80..3059e52 100644 --- a/builtin/patch-id.c +++ b/builtin/patch-id.c @@ -1,15 +1,16 @@ +#include "cache.h" #include "builtin.h" #include "config.h" #include "diff.h" static void flush_current_id(int patchlen, struct object_id *id, struct object_id *result) { - char name[50]; + char name[GIT_MAX_HEXSZ + 1]; if (!patchlen) return; - memcpy(name, oid_to_hex(id), GIT_SHA1_HEXSZ + 1); + memcpy(name, oid_to_hex(id), the_hash_algo->hexsz + 1); printf("%s %s\n", oid_to_hex(result), name); } @@ -60,9 +61,9 @@ static int get_one_patchid(struct object_id *next_oid, struct object_id *result, { int patchlen = 0, found_next = 0; int before = -1, after = -1; - git_SHA_CTX ctx; + git_hash_ctx ctx; - git_SHA1_Init(&ctx); + the_hash_algo->init_fn(&ctx); oidclr(result); 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, /* Compute the sha without whitespace */ len = remove_space(line); patchlen += len; - git_SHA1_Update(&ctx, line, len); + the_hash_algo->update_fn(&ctx, line, len); } if (!found_next) diff --git a/diff.c b/diff.c index efe42b3..bf51c94 100644 --- a/diff.c +++ b/diff.c @@ -5978,7 +5978,7 @@ static void diff_summary(struct diff_options *opt, struct diff_filepair *p) } struct patch_id_t { - git_SHA_CTX *ctx; + git_hash_ctx *ctx; int patchlen; }; @@ -5995,16 +5995,16 @@ static int remove_space(char *line, int len) return dst - line; } -void flush_one_hunk(struct object_id *result, git_SHA_CTX *ctx) +void flush_one_hunk(struct object_id *result, git_hash_ctx *ctx) { unsigned char hash[GIT_MAX_RAWSZ]; unsigned short carry = 0; int i; - git_SHA1_Final(hash, ctx); - git_SHA1_Init(ctx); + the_hash_algo->final_fn(hash, ctx); + the_hash_algo->init_fn(ctx); /* 20-byte sum, with carry */ - for (i = 0; i < GIT_SHA1_RAWSZ; ++i) { + for (i = 0; i < the_hash_algo->rawsz; ++i) { carry += result->hash[i] + hash[i]; result->hash[i] = carry; carry >>= 8; @@ -6018,21 +6018,21 @@ static void patch_id_consume(void *priv, char *line, unsigned long len) new_len = remove_space(line, len); - git_SHA1_Update(data->ctx, line, new_len); + the_hash_algo->update_fn(data->ctx, line, new_len); data->patchlen += new_len; } -static void patch_id_add_string(git_SHA_CTX *ctx, const char *str) +static void patch_id_add_string(git_hash_ctx *ctx, const char *str) { - git_SHA1_Update(ctx, str, strlen(str)); + the_hash_algo->update_fn(ctx, str, strlen(str)); } -static void patch_id_add_mode(git_SHA_CTX *ctx, unsigned mode) +static void patch_id_add_mode(git_hash_ctx *ctx, unsigned mode) { /* large enough for 2^32 in octal */ char buf[12]; int len = xsnprintf(buf, sizeof(buf), "%06o", mode); - git_SHA1_Update(ctx, buf, len); + the_hash_algo->update_fn(ctx, buf, len); } /* returns 0 upon success, and writes result into oid */ @@ -6040,10 +6040,10 @@ static int diff_get_patch_id(struct diff_options *options, struct object_id *oid { struct diff_queue_struct *q = &diff_queued_diff; int i; - git_SHA_CTX ctx; + git_hash_ctx ctx; struct patch_id_t data; - git_SHA1_Init(&ctx); + the_hash_algo->init_fn(&ctx); memset(&data, 0, sizeof(struct patch_id_t)); data.ctx = &ctx; oidclr(oid); @@ -6076,27 +6076,27 @@ static int diff_get_patch_id(struct diff_options *options, struct object_id *oid len2 = remove_space(p->two->path, strlen(p->two->path)); patch_id_add_string(&ctx, "diff--git"); patch_id_add_string(&ctx, "a/"); - git_SHA1_Update(&ctx, p->one->path, len1); + the_hash_algo->update_fn(&ctx, p->one->path, len1); patch_id_add_string(&ctx, "b/"); - git_SHA1_Update(&ctx, p->two->path, len2); + the_hash_algo->update_fn(&ctx, p->two->path, len2); if (p->one->mode == 0) { patch_id_add_string(&ctx, "newfilemode"); patch_id_add_mode(&ctx, p->two->mode); patch_id_add_string(&ctx, "---/dev/null"); patch_id_add_string(&ctx, "+++b/"); - git_SHA1_Update(&ctx, p->two->path, len2); + the_hash_algo->update_fn(&ctx, p->two->path, len2); } else if (p->two->mode == 0) { patch_id_add_string(&ctx, "deletedfilemode"); patch_id_add_mode(&ctx, p->one->mode); patch_id_add_string(&ctx, "---a/"); - git_SHA1_Update(&ctx, p->one->path, len1); + the_hash_algo->update_fn(&ctx, p->one->path, len1); patch_id_add_string(&ctx, "+++/dev/null"); } else { patch_id_add_string(&ctx, "---a/"); - git_SHA1_Update(&ctx, p->one->path, len1); + the_hash_algo->update_fn(&ctx, p->one->path, len1); patch_id_add_string(&ctx, "+++b/"); - git_SHA1_Update(&ctx, p->two->path, len2); + the_hash_algo->update_fn(&ctx, p->two->path, len2); } if (diff_header_only) @@ -6108,10 +6108,10 @@ static int diff_get_patch_id(struct diff_options *options, struct object_id *oid if (diff_filespec_is_binary(options->repo, p->one) || diff_filespec_is_binary(options->repo, p->two)) { - git_SHA1_Update(&ctx, oid_to_hex(&p->one->oid), - GIT_SHA1_HEXSZ); - git_SHA1_Update(&ctx, oid_to_hex(&p->two->oid), - GIT_SHA1_HEXSZ); + the_hash_algo->update_fn(&ctx, oid_to_hex(&p->one->oid), + the_hash_algo->hexsz); + the_hash_algo->update_fn(&ctx, oid_to_hex(&p->two->oid), + the_hash_algo->hexsz); continue; } @@ -6128,7 +6128,7 @@ static int diff_get_patch_id(struct diff_options *options, struct object_id *oid } if (!stable) - git_SHA1_Final(oid->hash, &ctx); + the_hash_algo->final_fn(oid->hash, &ctx); return 0; } diff --git a/diff.h b/diff.h index c2c3056..7f8f024 100644 --- a/diff.h +++ b/diff.h @@ -438,7 +438,7 @@ int run_diff_index(struct rev_info *revs, int cached); int do_diff_cache(const struct object_id *, struct diff_options *); int diff_flush_patch_id(struct diff_options *, struct object_id *, int, int); -void flush_one_hunk(struct object_id *, git_SHA_CTX *); +void flush_one_hunk(struct object_id *result, git_hash_ctx *ctx); int diff_result_code(struct diff_options *, int); -- cgit v0.10.2-6-g49f6 From f6af19a9ad814de530bfac9b564126234434a9e3 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 18 Aug 2019 20:04:04 +0000 Subject: fetch-pack: use parse_oid_hex Instead of hard-coding constants, use parse_oid_hex to compute a pointer and use it in further parsing operations. Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano diff --git a/fetch-pack.c b/fetch-pack.c index 65be043..1f56b8a 100644 --- a/fetch-pack.c +++ b/fetch-pack.c @@ -168,16 +168,16 @@ static enum ack_type get_ack(struct packet_reader *reader, if (!strcmp(reader->line, "NAK")) return NAK; if (skip_prefix(reader->line, "ACK ", &arg)) { - if (!get_oid_hex(arg, result_oid)) { - arg += 40; - len -= arg - reader->line; + const char *p; + if (!parse_oid_hex(arg, result_oid, &p)) { + len -= p - reader->line; if (len < 1) return ACK; - if (strstr(arg, "continue")) + if (strstr(p, "continue")) return ACK_continue; - if (strstr(arg, "common")) + if (strstr(p, "common")) return ACK_common; - if (strstr(arg, "ready")) + if (strstr(p, "ready")) return ACK_ready; return ACK; } -- cgit v0.10.2-6-g49f6 From fabec2c5c33b2623fe405c40ffcdeb838e97ed26 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 18 Aug 2019 20:04:05 +0000 Subject: builtin/receive-pack: switch to use the_hash_algo The push cert code uses HMAC-SHA-1 to create a nonce. This is a secure use of SHA-1 which is not affected by its collision resistance (or lack thereof). However, it makes sense for us to use a better algorithm if one is available, one which may even be more performant. Futhermore, until we have specialized functions for computing the hex value of an arbitrary function, it simplifies the code greatly to use the same hash algorithm everywhere. Switch this code to use GIT_MAX_BLKSZ and the_hash_algo for computing the push cert nonce, and rename the hmac_sha1 function to simply "hmac". Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c index dcf3855..402edf3 100644 --- a/builtin/receive-pack.c +++ b/builtin/receive-pack.c @@ -417,24 +417,22 @@ static int copy_to_sideband(int in, int out, void *arg) return 0; } -#define HMAC_BLOCK_SIZE 64 - -static void hmac_sha1(unsigned char *out, +static void hmac(unsigned char *out, const char *key_in, size_t key_len, const char *text, size_t text_len) { - unsigned char key[HMAC_BLOCK_SIZE]; - unsigned char k_ipad[HMAC_BLOCK_SIZE]; - unsigned char k_opad[HMAC_BLOCK_SIZE]; + unsigned char key[GIT_MAX_BLKSZ]; + unsigned char k_ipad[GIT_MAX_BLKSZ]; + unsigned char k_opad[GIT_MAX_BLKSZ]; int i; - git_SHA_CTX ctx; + git_hash_ctx ctx; /* RFC 2104 2. (1) */ - memset(key, '\0', HMAC_BLOCK_SIZE); - if (HMAC_BLOCK_SIZE < key_len) { - git_SHA1_Init(&ctx); - git_SHA1_Update(&ctx, key_in, key_len); - git_SHA1_Final(key, &ctx); + memset(key, '\0', GIT_MAX_BLKSZ); + if (the_hash_algo->blksz < key_len) { + the_hash_algo->init_fn(&ctx); + the_hash_algo->update_fn(&ctx, key_in, key_len); + the_hash_algo->final_fn(key, &ctx); } else { memcpy(key, key_in, key_len); } @@ -446,29 +444,29 @@ static void hmac_sha1(unsigned char *out, } /* RFC 2104 2. (3) & (4) */ - git_SHA1_Init(&ctx); - git_SHA1_Update(&ctx, k_ipad, sizeof(k_ipad)); - git_SHA1_Update(&ctx, text, text_len); - git_SHA1_Final(out, &ctx); + the_hash_algo->init_fn(&ctx); + the_hash_algo->update_fn(&ctx, k_ipad, sizeof(k_ipad)); + the_hash_algo->update_fn(&ctx, text, text_len); + the_hash_algo->final_fn(out, &ctx); /* RFC 2104 2. (6) & (7) */ - git_SHA1_Init(&ctx); - git_SHA1_Update(&ctx, k_opad, sizeof(k_opad)); - git_SHA1_Update(&ctx, out, GIT_SHA1_RAWSZ); - git_SHA1_Final(out, &ctx); + the_hash_algo->init_fn(&ctx); + the_hash_algo->update_fn(&ctx, k_opad, sizeof(k_opad)); + the_hash_algo->update_fn(&ctx, out, the_hash_algo->rawsz); + the_hash_algo->final_fn(out, &ctx); } static char *prepare_push_cert_nonce(const char *path, timestamp_t stamp) { struct strbuf buf = STRBUF_INIT; - unsigned char sha1[GIT_SHA1_RAWSZ]; + unsigned char hash[GIT_MAX_RAWSZ]; strbuf_addf(&buf, "%s:%"PRItime, path, stamp); - hmac_sha1(sha1, buf.buf, buf.len, cert_nonce_seed, strlen(cert_nonce_seed)); + hmac(hash, buf.buf, buf.len, cert_nonce_seed, strlen(cert_nonce_seed)); strbuf_release(&buf); /* RFC 2104 5. HMAC-SHA1-80 */ - strbuf_addf(&buf, "%"PRItime"-%.*s", stamp, GIT_SHA1_HEXSZ, sha1_to_hex(sha1)); + strbuf_addf(&buf, "%"PRItime"-%.*s", stamp, (int)the_hash_algo->hexsz, sha1_to_hex(hash)); return strbuf_detach(&buf, NULL); } -- cgit v0.10.2-6-g49f6 From 319009642c5e421915ce66436aa7a3cb94c7d11c Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 18 Aug 2019 20:04:06 +0000 Subject: builtin/blame: switch uses of GIT_SHA1_HEXSZ to the_hash_algo Switch several uses of GIT_SHA1_HEXSZ to the_hash_algo. Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano diff --git a/builtin/blame.c b/builtin/blame.c index b6534d4..5a0c0c2 100644 --- a/builtin/blame.c +++ b/builtin/blame.c @@ -460,7 +460,7 @@ static void emit_other(struct blame_scoreboard *sb, struct blame_entry *ent, int for (cnt = 0; cnt < ent->num_lines; cnt++) { char ch; - int length = (opt & OUTPUT_LONG_OBJECT_NAME) ? GIT_SHA1_HEXSZ : abbrev; + int length = (opt & OUTPUT_LONG_OBJECT_NAME) ? the_hash_algo->hexsz : abbrev; if (opt & OUTPUT_COLOR_LINE) { if (cnt > 0) { @@ -885,6 +885,7 @@ int cmd_blame(int argc, const char **argv, const char *prefix) struct range_set ranges; unsigned int range_i; long anchor; + const int hexsz = the_hash_algo->hexsz; setup_default_color_by_age(); git_config(git_blame_config, &output_option); @@ -931,11 +932,11 @@ parse_done: } else if (show_progress < 0) show_progress = isatty(2); - if (0 < abbrev && abbrev < GIT_SHA1_HEXSZ) + if (0 < abbrev && abbrev < hexsz) /* one more abbrev length is needed for the boundary commit */ abbrev++; else if (!abbrev) - abbrev = GIT_SHA1_HEXSZ; + abbrev = hexsz; if (revs_file && read_ancestry(revs_file)) die_errno("reading graft file '%s' failed", revs_file); -- cgit v0.10.2-6-g49f6 From 7e0d029f187dfe3fee9de9ccce43200202fc14f6 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 18 Aug 2019 20:04:07 +0000 Subject: builtin/rev-parse: switch to use the_hash_algo Switch several hard-coded uses of the constant 40 to references to the_hash_algo. Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c index f8bbe6d..308c67e 100644 --- a/builtin/rev-parse.c +++ b/builtin/rev-parse.c @@ -593,6 +593,7 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix) const char *name = NULL; struct object_context unused; struct strbuf buf = STRBUF_INIT; + const int hexsz = the_hash_algo->hexsz; if (argc > 1 && !strcmp("--parseopt", argv[1])) return cmd_parseopt(argc - 1, argv + 1, prefix); @@ -730,8 +731,8 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix) abbrev = strtoul(arg, NULL, 10); if (abbrev < MINIMUM_ABBREV) abbrev = MINIMUM_ABBREV; - else if (40 <= abbrev) - abbrev = 40; + else if (hexsz <= abbrev) + abbrev = hexsz; continue; } if (!strcmp(arg, "--sq")) { -- cgit v0.10.2-6-g49f6 From fee49308a1a6a3270a52b31275562f3e92f27d0a Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 18 Aug 2019 20:04:08 +0000 Subject: blame: remove needless comparison with GIT_SHA1_HEXSZ When faking a working tree commit, we read in lines from MERGE_HEAD into a strbuf. Because the strbuf is NUL-terminated and get_oid_hex will fail if it unexpectedly encounters a NUL, the check for the length of the line is unnecessary. There is no optimization benefit from this case, either, since on failure we call die. Remove this check, since it is no longer needed. Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano diff --git a/blame.c b/blame.c index 36a2e7e..6596d8d 100644 --- a/blame.c +++ b/blame.c @@ -144,7 +144,7 @@ static void append_merge_parents(struct repository *r, while (!strbuf_getwholeline_fd(&line, merge_head, '\n')) { struct object_id oid; - if (line.len < GIT_SHA1_HEXSZ || get_oid_hex(line.buf, &oid)) + if (get_oid_hex(line.buf, &oid)) die("unknown line in '%s': %s", git_path_merge_head(r), line.buf); tail = append_parent(r, tail, &oid); -- cgit v0.10.2-6-g49f6 From 7962e046ff2bfdeab1dad5c969dbd2b9209efe2f Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 18 Aug 2019 20:04:09 +0000 Subject: show-index: switch hard-coded constants to the_hash_algo show-index uses a variety of hard-coded constants to enumerate the values in pack indices. Switch these hard-coded constants to use the_hash_algo or GIT_MAX_RAWSZ, as appropriate, and convert one instance of an unsigned char array to struct object_id. Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano diff --git a/builtin/show-index.c b/builtin/show-index.c index a6e6788..e95b84e 100644 --- a/builtin/show-index.c +++ b/builtin/show-index.c @@ -11,6 +11,7 @@ int cmd_show_index(int argc, const char **argv, const char *prefix) unsigned nr; unsigned int version; static unsigned int top_index[256]; + const unsigned hashsz = the_hash_algo->rawsz; if (argc != 1) usage(show_index_usage); @@ -36,9 +37,9 @@ int cmd_show_index(int argc, const char **argv, const char *prefix) } if (version == 1) { for (i = 0; i < nr; i++) { - unsigned int offset, entry[6]; + unsigned int offset, entry[(GIT_MAX_RAWSZ + 4) / sizeof(unsigned int)]; - if (fread(entry, 4 + 20, 1, stdin) != 1) + if (fread(entry, 4 + hashsz, 1, stdin) != 1) die("unable to read entry %u/%u", i, nr); offset = ntohl(entry[0]); printf("%u %s\n", offset, sha1_to_hex((void *)(entry+1))); @@ -46,13 +47,13 @@ int cmd_show_index(int argc, const char **argv, const char *prefix) } else { unsigned off64_nr = 0; struct { - unsigned char sha1[20]; + struct object_id oid; uint32_t crc; uint32_t off; } *entries; ALLOC_ARRAY(entries, nr); for (i = 0; i < nr; i++) - if (fread(entries[i].sha1, 20, 1, stdin) != 1) + if (fread(entries[i].oid.hash, hashsz, 1, stdin) != 1) die("unable to read sha1 %u/%u", i, nr); for (i = 0; i < nr; i++) 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) } printf("%" PRIuMAX " %s (%08"PRIx32")\n", (uintmax_t) offset, - sha1_to_hex(entries[i].sha1), + oid_to_hex(&entries[i].oid), ntohl(entries[i].crc)); } free(entries); -- cgit v0.10.2-6-g49f6 From 9d958cc0417ce2c8a4504ac8dd140685445ce4d7 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 18 Aug 2019 20:04:10 +0000 Subject: connected: switch GIT_SHA1_HEXSZ to the_hash_algo Switch various uses of GIT_SHA1_HEXSZ to reference the_hash_algo instead. Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano diff --git a/connected.c b/connected.c index cd9b324..7cd3bc9 100644 --- a/connected.c +++ b/connected.c @@ -28,6 +28,7 @@ int check_connected(oid_iterate_fn fn, void *cb_data, struct packed_git *new_pack = NULL; struct transport *transport; size_t base_len; + const unsigned hexsz = the_hash_algo->hexsz; if (!opt) opt = &defaults; @@ -99,7 +100,7 @@ int check_connected(oid_iterate_fn fn, void *cb_data, sigchain_push(SIGPIPE, SIG_IGN); - commit[GIT_SHA1_HEXSZ] = '\n'; + commit[hexsz] = '\n'; do { /* * If index-pack already checked that: @@ -112,8 +113,8 @@ int check_connected(oid_iterate_fn fn, void *cb_data, if (new_pack && find_pack_entry_one(oid.hash, new_pack)) continue; - memcpy(commit, oid_to_hex(&oid), GIT_SHA1_HEXSZ); - if (write_in_full(rev_list.in, commit, GIT_SHA1_HEXSZ + 1) < 0) { + memcpy(commit, oid_to_hex(&oid), hexsz); + if (write_in_full(rev_list.in, commit, hexsz + 1) < 0) { if (errno != EPIPE && errno != EINVAL) error_errno(_("failed write to rev-list")); err = -1; -- cgit v0.10.2-6-g49f6 From 703d2d4193738526d7b4116657657101e49a601e Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 18 Aug 2019 20:04:11 +0000 Subject: bundle: switch to use the_hash_algo Switch a use of the constant 40 and a use of GIT_SHA1_HEXSZ to use the_hash_algo instead. Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano diff --git a/bundle.c b/bundle.c index b5d21cd..a85ed3f 100644 --- a/bundle.c +++ b/bundle.c @@ -282,7 +282,7 @@ static int write_pack_data(int bundle_fd, struct rev_info *revs) struct object *object = revs->pending.objects[i].item; if (object->flags & UNINTERESTING) write_or_die(pack_objects.in, "^", 1); - write_or_die(pack_objects.in, oid_to_hex(&object->oid), GIT_SHA1_HEXSZ); + write_or_die(pack_objects.in, oid_to_hex(&object->oid), the_hash_algo->hexsz); write_or_die(pack_objects.in, "\n", 1); } close(pack_objects.in); @@ -414,7 +414,7 @@ static int write_bundle_refs(int bundle_fd, struct rev_info *revs) } ref_count++; - write_or_die(bundle_fd, oid_to_hex(&e->item->oid), 40); + write_or_die(bundle_fd, oid_to_hex(&e->item->oid), the_hash_algo->hexsz); write_or_die(bundle_fd, " ", 1); write_or_die(bundle_fd, display_ref, strlen(display_ref)); write_or_die(bundle_fd, "\n", 1); -- cgit v0.10.2-6-g49f6 From 976ff7e49d722d1b72effb6d547ec2b00cd69fc0 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 18 Aug 2019 20:04:12 +0000 Subject: combine-diff: replace GIT_SHA1_HEXSZ with the_hash_algo Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano diff --git a/combine-diff.c b/combine-diff.c index 3e49f3b..d5c4d83 100644 --- a/combine-diff.c +++ b/combine-diff.c @@ -930,7 +930,7 @@ static void show_combined_header(struct combine_diff_path *elem, int show_file_header) { struct diff_options *opt = &rev->diffopt; - int abbrev = opt->flags.full_index ? GIT_SHA1_HEXSZ : DEFAULT_ABBREV; + int abbrev = opt->flags.full_index ? the_hash_algo->hexsz : DEFAULT_ABBREV; const char *a_prefix = opt->a_prefix ? opt->a_prefix : "a/"; const char *b_prefix = opt->b_prefix ? opt->b_prefix : "b/"; const char *c_meta = diff_get_color_opt(opt, DIFF_METAINFO); -- cgit v0.10.2-6-g49f6 From fe9fec45f68e0d9850bf13da57b3e7a99fc33444 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 18 Aug 2019 20:04:13 +0000 Subject: config: use the_hash_algo in abbrev comparison Switch one use of a hard-coded 40 constant to use the_hash_algo. Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano diff --git a/config.c b/config.c index 3900e49..b0f79aa 100644 --- a/config.c +++ b/config.c @@ -1204,7 +1204,7 @@ static int git_default_core_config(const char *var, const char *value, void *cb) default_abbrev = -1; else { int abbrev = git_config_int(var, value); - if (abbrev < minimum_abbrev || abbrev > 40) + if (abbrev < minimum_abbrev || abbrev > the_hash_algo->hexsz) return error(_("abbrev length out of range: %d"), abbrev); default_abbrev = abbrev; } -- cgit v0.10.2-6-g49f6 From e84f3572bd9160f281629bb2a098b35ea87c10e1 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 18 Aug 2019 20:04:14 +0000 Subject: sha1-lookup: switch hard-coded constants to the_hash_algo Switch a hard-coded 18 to be a reference to the_hash_algo. Rename the sha1 parameter to be called hash instead. Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano diff --git a/sha1-lookup.c b/sha1-lookup.c index 796ab68..93d9af0 100644 --- a/sha1-lookup.c +++ b/sha1-lookup.c @@ -50,7 +50,7 @@ static uint32_t take2(const unsigned char *sha1) * The sha1 of element i (between 0 and nr - 1) should be returned * by "fn(i, table)". */ -int sha1_pos(const unsigned char *sha1, void *table, size_t nr, +int sha1_pos(const unsigned char *hash, void *table, size_t nr, sha1_access_fn fn) { size_t hi = nr; @@ -63,10 +63,10 @@ int sha1_pos(const unsigned char *sha1, void *table, size_t nr, if (nr != 1) { size_t lov, hiv, miv, ofs; - for (ofs = 0; ofs < 18; ofs += 2) { + for (ofs = 0; ofs < the_hash_algo->rawsz - 2; ofs += 2) { lov = take2(fn(0, table) + ofs); hiv = take2(fn(nr - 1, table) + ofs); - miv = take2(sha1 + ofs); + miv = take2(hash + ofs); if (miv < lov) return -1; if (hiv < miv) @@ -88,7 +88,7 @@ int sha1_pos(const unsigned char *sha1, void *table, size_t nr, do { int cmp; - cmp = hashcmp(fn(mi, table), sha1); + cmp = hashcmp(fn(mi, table), hash); if (!cmp) return mi; if (cmp > 0) -- cgit v0.10.2-6-g49f6 From 95518faac1828d077de09b4049c9c4a6602c8ae2 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 18 Aug 2019 20:04:15 +0000 Subject: bisect: switch to using the_hash_algo Instead of using GIT_SHA1_HEXSZ, use the_hash_algo so that the code is hash size independent. Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano diff --git a/bisect.c b/bisect.c index e87ac29..e81c91d 100644 --- a/bisect.c +++ b/bisect.c @@ -707,7 +707,7 @@ static int bisect_checkout(const struct object_id *bisect_rev, int no_checkout) { char bisect_rev_hex[GIT_MAX_HEXSZ + 1]; - memcpy(bisect_rev_hex, oid_to_hex(bisect_rev), GIT_SHA1_HEXSZ + 1); + memcpy(bisect_rev_hex, oid_to_hex(bisect_rev), the_hash_algo->hexsz + 1); update_ref(NULL, "BISECT_EXPECTED_REV", bisect_rev, NULL, 0, UPDATE_REFS_DIE_ON_ERR); argv_checkout[2] = bisect_rev_hex; -- cgit v0.10.2-6-g49f6 From 4439c7a360a23c39e6dea3c9aae080c3c64eb6b6 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 18 Aug 2019 20:04:16 +0000 Subject: sequencer: convert to use the_hash_algo Convert several uses of GIT_SHA1_HEXSZ constants to be references to the_hash_algo. Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano diff --git a/sequencer.c b/sequencer.c index 34ebf8e..4d25e79 100644 --- a/sequencer.c +++ b/sequencer.c @@ -3568,7 +3568,7 @@ static int do_merge(struct repository *r, goto leave_merge; } - write_message(oid_to_hex(&merge_commit->object.oid), GIT_SHA1_HEXSZ, + write_message(oid_to_hex(&merge_commit->object.oid), the_hash_algo->hexsz, git_path_merge_head(r), 0); write_message("no-ff", 5, git_path_merge_mode(r), 0); @@ -4487,7 +4487,7 @@ static const char *label_oid(struct object_id *oid, const char *label, char *p; strbuf_reset(&state->buf); - strbuf_grow(&state->buf, GIT_SHA1_HEXSZ); + strbuf_grow(&state->buf, GIT_MAX_HEXSZ); label = p = state->buf.buf; find_unique_abbrev_r(p, oid, default_abbrev); @@ -4500,7 +4500,7 @@ static const char *label_oid(struct object_id *oid, const char *label, size_t i = strlen(p) + 1; oid_to_hex_r(p, oid); - for (; i < GIT_SHA1_HEXSZ; i++) { + for (; i < the_hash_algo->hexsz; i++) { char save = p[i]; p[i] = '\0'; if (!hashmap_get_from_hash(&state->labels, -- cgit v0.10.2-6-g49f6 From 894c0f66bb21189d0677be6ecb9923b6acfbf437 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 18 Aug 2019 20:04:17 +0000 Subject: pack-write: use hash_to_hex when writing checksums Pack checksums always use the current hash algorithm in use, so switch from sha1_to_hex to hash_to_hex. Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano diff --git a/pack-write.c b/pack-write.c index 29d17a9..f0017be 100644 --- a/pack-write.c +++ b/pack-write.c @@ -349,7 +349,7 @@ void finish_tmp_packfile(struct strbuf *name_buffer, struct pack_idx_entry **written_list, uint32_t nr_written, struct pack_idx_option *pack_idx_opts, - unsigned char sha1[]) + unsigned char hash[]) { const char *idx_tmp_name; int basename_len = name_buffer->len; @@ -358,18 +358,18 @@ void finish_tmp_packfile(struct strbuf *name_buffer, die_errno("unable to make temporary pack file readable"); idx_tmp_name = write_idx_file(NULL, written_list, nr_written, - pack_idx_opts, sha1); + pack_idx_opts, hash); if (adjust_shared_perm(idx_tmp_name)) die_errno("unable to make temporary index file readable"); - strbuf_addf(name_buffer, "%s.pack", sha1_to_hex(sha1)); + strbuf_addf(name_buffer, "%s.pack", hash_to_hex(hash)); if (rename(pack_tmp_name, name_buffer->buf)) die_errno("unable to rename temporary pack file"); strbuf_setlen(name_buffer, basename_len); - strbuf_addf(name_buffer, "%s.idx", sha1_to_hex(sha1)); + strbuf_addf(name_buffer, "%s.idx", hash_to_hex(hash)); if (rename(idx_tmp_name, name_buffer->buf)) die_errno("unable to rename temporary index file"); -- cgit v0.10.2-6-g49f6 From dd336a55113a62789f039e382387d0407abaf922 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 18 Aug 2019 20:04:18 +0000 Subject: builtin/repack: write object IDs of the proper length Use the_hash_algo when calling xwrite with a hex object ID so that the proper amount of data is written. Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano diff --git a/builtin/repack.c b/builtin/repack.c index 632c0c0..5830f79 100644 --- a/builtin/repack.c +++ b/builtin/repack.c @@ -190,7 +190,7 @@ static int write_oid(const struct object_id *oid, struct packed_git *pack, die(_("could not start pack-objects to repack promisor objects")); } - xwrite(cmd->in, oid_to_hex(oid), GIT_SHA1_HEXSZ); + xwrite(cmd->in, oid_to_hex(oid), the_hash_algo->hexsz); xwrite(cmd->in, "\n", 1); return 0; } -- cgit v0.10.2-6-g49f6 From f6ca67d673c66e5c3988a18c2ec56b7fef9a23b6 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 18 Aug 2019 20:04:19 +0000 Subject: builtin/worktree: switch null_sha1 to null_oid Switch the remaining use of null_sha1 to null_oid. Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano diff --git a/builtin/worktree.c b/builtin/worktree.c index a5bb02b..0c0df3b 100644 --- a/builtin/worktree.c +++ b/builtin/worktree.c @@ -350,7 +350,7 @@ static int add_worktree(const char *path, const char *refname, */ strbuf_reset(&sb); strbuf_addf(&sb, "%s/HEAD", sb_repo.buf); - write_file(sb.buf, "%s", sha1_to_hex(null_sha1)); + write_file(sb.buf, "%s", oid_to_hex(&null_oid)); strbuf_reset(&sb); strbuf_addf(&sb, "%s/commondir", sb_repo.buf); write_file(sb.buf, "../.."); -- cgit v0.10.2-6-g49f6 From 8d4d86b0f0a875e7916d7de083a88d7f11251406 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 18 Aug 2019 20:04:20 +0000 Subject: cache: remove null_sha1 All of the existing uses of null_sha1 can be converted into uses of null_oid, so do so. Remove null_sha1 and is_null_sha1, and define is_null_oid in terms of null_oid. This also has the additional benefit of removing several uses of sha1_to_hex. Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano diff --git a/builtin/clone.c b/builtin/clone.c index f665b28..eabd028 100644 --- a/builtin/clone.c +++ b/builtin/clone.c @@ -785,7 +785,7 @@ static int checkout(int submodule_progress) if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK)) die(_("unable to write new index file")); - err |= run_hook_le(NULL, "post-checkout", sha1_to_hex(null_sha1), + err |= run_hook_le(NULL, "post-checkout", oid_to_hex(&null_oid), oid_to_hex(&oid), "1", NULL); if (!err && (option_recurse_submodules.nr > 0)) { diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c index 909e77e..8a6f2ab 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -424,7 +424,7 @@ static int module_list(int argc, const char **argv, const char *prefix) const struct cache_entry *ce = list.entries[i]; if (ce_stage(ce)) - printf("%06o %s U\t", ce->ce_mode, sha1_to_hex(null_sha1)); + printf("%06o %s U\t", ce->ce_mode, oid_to_hex(&null_oid)); else printf("%06o %s %d\t", ce->ce_mode, oid_to_hex(&ce->oid), ce_stage(ce)); diff --git a/cache.h b/cache.h index b1da1ab..79efd21 100644 --- a/cache.h +++ b/cache.h @@ -1029,7 +1029,6 @@ const char *repo_find_unique_abbrev(struct repository *r, const struct object_id int repo_find_unique_abbrev_r(struct repository *r, char *hex, const struct object_id *oid, int len); #define find_unique_abbrev_r(hex, oid, len) repo_find_unique_abbrev_r(the_repository, hex, oid, len) -extern const unsigned char null_sha1[GIT_MAX_RAWSZ]; extern const struct object_id null_oid; static inline int hashcmp(const unsigned char *sha1, const unsigned char *sha2) @@ -1064,14 +1063,9 @@ static inline int oideq(const struct object_id *oid1, const struct object_id *oi return hasheq(oid1->hash, oid2->hash); } -static inline int is_null_sha1(const unsigned char *sha1) -{ - return hasheq(sha1, null_sha1); -} - static inline int is_null_oid(const struct object_id *oid) { - return hasheq(oid->hash, null_sha1); + return oideq(oid, &null_oid); } static inline void hashcpy(unsigned char *sha_dst, const unsigned char *sha_src) diff --git a/sha1-file.c b/sha1-file.c index 487ea35..0ea28f7 100644 --- a/sha1-file.c +++ b/sha1-file.c @@ -55,7 +55,6 @@ "\x6f\xe1\x41\xf7\x74\x91\x20\xa3\x03\x72" \ "\x18\x13" -const unsigned char null_sha1[GIT_MAX_RAWSZ]; const struct object_id null_oid; static const struct object_id empty_tree_oid = { EMPTY_TREE_SHA1_BIN_LITERAL -- cgit v0.10.2-6-g49f6 From e0cb7cdb898d65bca6f1f4ff1d80f15a09b576db Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 18 Aug 2019 20:04:21 +0000 Subject: wt-status: convert struct wt_status to object_id Change struct wt_status to use struct object_id instead of an array of unsigned char. Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano diff --git a/builtin/commit.c b/builtin/commit.c index ae7aaf6..e588bc6 100644 --- a/builtin/commit.c +++ b/builtin/commit.c @@ -510,7 +510,7 @@ static int run_status(FILE *fp, const char *index_file, const char *prefix, int s->nowarn = nowarn; s->is_initial = get_oid(s->reference, &oid) ? 1 : 0; if (!s->is_initial) - hashcpy(s->sha1_commit, oid.hash); + oidcpy(&s->oid_commit, &oid); s->status_format = status_format; s->ignore_submodule_arg = ignore_submodule_arg; @@ -1406,7 +1406,7 @@ int cmd_status(int argc, const char **argv, const char *prefix) s.is_initial = get_oid(s.reference, &oid) ? 1 : 0; if (!s.is_initial) - hashcpy(s.sha1_commit, oid.hash); + oidcpy(&s.oid_commit, &oid); s.ignore_submodule_arg = ignore_submodule_arg; s.status_format = status_format; diff --git a/wt-status.c b/wt-status.c index 9f6c65a..7cf220f 100644 --- a/wt-status.c +++ b/wt-status.c @@ -2025,7 +2025,7 @@ static void wt_porcelain_v2_print_tracking(struct wt_status *s) char eol = s->null_termination ? '\0' : '\n'; fprintf(s->fp, "# branch.oid %s%c", - (s->is_initial ? "(initial)" : sha1_to_hex(s->sha1_commit)), + (s->is_initial ? "(initial)" : oid_to_hex(&s->oid_commit)), eol); if (!s->branch) diff --git a/wt-status.h b/wt-status.h index 77dad5b..71c3f25 100644 --- a/wt-status.h +++ b/wt-status.h @@ -116,7 +116,7 @@ struct wt_status { int rename_limit; enum wt_status_format status_format; struct wt_status_state state; - unsigned char sha1_commit[GIT_MAX_RAWSZ]; /* when not Initial */ + struct object_id oid_commit; /* when not Initial */ /* These are computed during processing of the individual sections */ int committable; -- cgit v0.10.2-6-g49f6 From 3a4d7aa5aeeeb6c0b3d92cbaee0d32c2c956c72b Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 18 Aug 2019 20:04:22 +0000 Subject: packfile: replace sha1_to_hex Replace a use of sha1_to_hex with hash_to_hex so that this code works with a hash algorithm other than SHA-1. Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano diff --git a/packfile.c b/packfile.c index fc43a6c..52dea88 100644 --- a/packfile.c +++ b/packfile.c @@ -19,12 +19,12 @@ #include "commit-graph.h" char *odb_pack_name(struct strbuf *buf, - const unsigned char *sha1, + const unsigned char *hash, const char *ext) { strbuf_reset(buf); strbuf_addf(buf, "%s/pack/pack-%s.%s", get_object_directory(), - sha1_to_hex(sha1), ext); + hash_to_hex(hash), ext); return buf->buf; } -- cgit v0.10.2-6-g49f6 From 69fa3370604e4410d845ff146ef86f8368e1ca79 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 18 Aug 2019 20:04:23 +0000 Subject: builtin/index-pack: replace sha1_to_hex Since sha1_to_hex is limited to SHA-1, replace it with hash_to_hex so this code works with other algorithms. Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano diff --git a/builtin/index-pack.c b/builtin/index-pack.c index 0d55f73..2abd550 100644 --- a/builtin/index-pack.c +++ b/builtin/index-pack.c @@ -1490,11 +1490,11 @@ static void final(const char *final_pack_name, const char *curr_pack_name, } if (!from_stdin) { - printf("%s\n", sha1_to_hex(hash)); + printf("%s\n", hash_to_hex(hash)); } else { struct strbuf buf = STRBUF_INIT; - strbuf_addf(&buf, "%s\t%s\n", report, sha1_to_hex(hash)); + strbuf_addf(&buf, "%s\t%s\n", report, hash_to_hex(hash)); write_or_die(1, buf.buf, buf.len); strbuf_release(&buf); -- cgit v0.10.2-6-g49f6 From fc06be3b7f065d197fd484098f517fa9d19c9d2b Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 18 Aug 2019 20:04:24 +0000 Subject: builtin/receive-pack: replace sha1_to_hex Since sha1_to_hex is limited to SHA-1, replace it with hash_to_hex. Rename several variables to indicate that they can contain any hash. Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c index 402edf3..411e0b4 100644 --- a/builtin/receive-pack.c +++ b/builtin/receive-pack.c @@ -466,7 +466,7 @@ static char *prepare_push_cert_nonce(const char *path, timestamp_t stamp) strbuf_release(&buf); /* RFC 2104 5. HMAC-SHA1-80 */ - strbuf_addf(&buf, "%"PRItime"-%.*s", stamp, (int)the_hash_algo->hexsz, sha1_to_hex(hash)); + strbuf_addf(&buf, "%"PRItime"-%.*s", stamp, (int)the_hash_algo->hexsz, hash_to_hex(hash)); return strbuf_detach(&buf, NULL); } @@ -968,7 +968,7 @@ static const char *push_to_deploy(unsigned char *sha1, if (run_command(&child)) return "Working directory has staged changes"; - read_tree[3] = sha1_to_hex(sha1); + read_tree[3] = hash_to_hex(sha1); child_process_init(&child); child.argv = read_tree; child.env = env->argv; @@ -985,13 +985,13 @@ static const char *push_to_deploy(unsigned char *sha1, static const char *push_to_checkout_hook = "push-to-checkout"; -static const char *push_to_checkout(unsigned char *sha1, +static const char *push_to_checkout(unsigned char *hash, struct argv_array *env, const char *work_tree) { argv_array_pushf(env, "GIT_WORK_TREE=%s", absolute_path(work_tree)); if (run_hook_le(env->argv, push_to_checkout_hook, - sha1_to_hex(sha1), NULL)) + hash_to_hex(hash), NULL)) return "push-to-checkout hook declined"; else return NULL; -- cgit v0.10.2-6-g49f6 From 3f34d70d4029c420d0db82fc39e77da4470eb3cc Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 18 Aug 2019 20:04:25 +0000 Subject: rerere: replace sha1_to_hex Replace the uses of sha1_to_hex in this function with hash_to_hex to allow the use of SHA-256 as well. Rename a variable since it is no longer limited to SHA-1. Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano diff --git a/rerere.c b/rerere.c index 17abb47..3e51fdf 100644 --- a/rerere.c +++ b/rerere.c @@ -52,7 +52,7 @@ static void free_rerere_id(struct string_list_item *item) static const char *rerere_id_hex(const struct rerere_id *id) { - return sha1_to_hex(id->collection->hash); + return hash_to_hex(id->collection->hash); } static void fit_variant(struct rerere_dir *rr_dir, int variant) @@ -115,7 +115,7 @@ static int is_rr_file(const char *name, const char *filename, int *variant) static void scan_rerere_dir(struct rerere_dir *rr_dir) { struct dirent *de; - DIR *dir = opendir(git_path("rr-cache/%s", sha1_to_hex(rr_dir->hash))); + DIR *dir = opendir(git_path("rr-cache/%s", hash_to_hex(rr_dir->hash))); if (!dir) return; @@ -186,9 +186,9 @@ static struct rerere_id *new_rerere_id_hex(char *hex) return id; } -static struct rerere_id *new_rerere_id(unsigned char *sha1) +static struct rerere_id *new_rerere_id(unsigned char *hash) { - return new_rerere_id_hex(sha1_to_hex(sha1)); + return new_rerere_id_hex(hash_to_hex(hash)); } /* -- cgit v0.10.2-6-g49f6 From be8e172e9f4f62c9e4cdbecb5cd667775f38d4f1 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 18 Aug 2019 20:04:26 +0000 Subject: builtin/show-index: replace sha1_to_hex In this code path, we use sha1_to_hex to display the contents of a v1 pack index. While we plan to switch to v3 indices for SHA-256, the v1 pack indices still function, so to support both algorithms, switch sha1_to_hex to hash_to_hex. Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano diff --git a/builtin/show-index.c b/builtin/show-index.c index e95b84e..0826f6a 100644 --- a/builtin/show-index.c +++ b/builtin/show-index.c @@ -42,7 +42,7 @@ int cmd_show_index(int argc, const char **argv, const char *prefix) if (fread(entry, 4 + hashsz, 1, stdin) != 1) die("unable to read entry %u/%u", i, nr); offset = ntohl(entry[0]); - printf("%u %s\n", offset, sha1_to_hex((void *)(entry+1))); + printf("%u %s\n", offset, hash_to_hex((void *)(entry+1))); } } else { unsigned off64_nr = 0; -- cgit v0.10.2-6-g49f6 From aaa95dfa051fc517dde0fa31187c744094cd17c5 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 18 Aug 2019 20:04:27 +0000 Subject: midx: switch to using the_hash_algo Instead of hard-coding the hash size, use the_hash_algo to look up the hash size at runtime. Remove the #define constant which was used to hold the hash length, since writing the expression with the_hash_algo provide enough documentary value on its own. Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano diff --git a/midx.c b/midx.c index d649644..f29afc0 100644 --- a/midx.c +++ b/midx.c @@ -19,8 +19,7 @@ #define MIDX_BYTE_NUM_PACKS 8 #define MIDX_HASH_VERSION 1 #define MIDX_HEADER_SIZE 12 -#define MIDX_HASH_LEN 20 -#define MIDX_MIN_SIZE (MIDX_HEADER_SIZE + MIDX_HASH_LEN) +#define MIDX_MIN_SIZE (MIDX_HEADER_SIZE + the_hash_algo->rawsz) #define MIDX_MAX_CHUNKS 5 #define MIDX_CHUNK_ALIGNMENT 4 @@ -93,7 +92,7 @@ struct multi_pack_index *load_multi_pack_index(const char *object_dir, int local hash_version = m->data[MIDX_BYTE_HASH_VERSION]; if (hash_version != MIDX_HASH_VERSION) die(_("hash version %u does not match"), hash_version); - m->hash_len = MIDX_HASH_LEN; + m->hash_len = the_hash_algo->rawsz; m->num_chunks = m->data[MIDX_BYTE_NUM_CHUNKS]; @@ -234,7 +233,7 @@ int prepare_midx_pack(struct repository *r, struct multi_pack_index *m, uint32_t int bsearch_midx(const struct object_id *oid, struct multi_pack_index *m, uint32_t *result) { return bsearch_hash(oid->hash, m->chunk_oid_fanout, m->chunk_oid_lookup, - MIDX_HASH_LEN, result); + the_hash_algo->rawsz, result); } struct object_id *nth_midxed_object_oid(struct object_id *oid, @@ -928,7 +927,7 @@ static int write_midx_internal(const char *object_dir, struct multi_pack_index * cur_chunk++; chunk_ids[cur_chunk] = MIDX_CHUNKID_OBJECTOFFSETS; - chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + nr_entries * MIDX_HASH_LEN; + chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + nr_entries * the_hash_algo->rawsz; cur_chunk++; chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + nr_entries * MIDX_CHUNK_OFFSET_WIDTH; @@ -976,7 +975,7 @@ static int write_midx_internal(const char *object_dir, struct multi_pack_index * break; case MIDX_CHUNKID_OIDLOOKUP: - written += write_midx_oid_lookup(f, MIDX_HASH_LEN, entries, nr_entries); + written += write_midx_oid_lookup(f, the_hash_algo->rawsz, entries, nr_entries); break; case MIDX_CHUNKID_OBJECTOFFSETS: -- cgit v0.10.2-6-g49f6