From: Junio C Hamano Date: Thu, 15 Feb 2018 22:55:47 +0000 (-0800) Subject: Merge branch 'bc/hash-algo' X-Git-Tag: v2.17.0-rc0~89 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=0fd90daba84a165adda7cd4721fcd009e0bebc56;p=git Merge branch 'bc/hash-algo' More abstraction of hash function from the codepath. * bc/hash-algo: hash: update obsolete reference to SHA1_HEADER bulk-checkin: abstract SHA-1 usage csum-file: abstract uses of SHA-1 csum-file: rename sha1file to hashfile read-cache: abstract away uses of SHA-1 pack-write: switch various SHA-1 values to abstract forms pack-check: convert various uses of SHA-1 to abstract forms fast-import: switch various uses of SHA-1 to the_hash_algo sha1_file: switch uses of SHA-1 to the_hash_algo builtin/unpack-objects: switch uses of SHA-1 to the_hash_algo builtin/index-pack: improve hash function abstraction hash: create union for hash context allocation hash: move SHA-1 macros to hash.h --- 0fd90daba84a165adda7cd4721fcd009e0bebc56 diff --cc builtin/index-pack.c index 16edfebf63,0bb520c731..7e3e1a461c --- a/builtin/index-pack.c +++ b/builtin/index-pack.c @@@ -1388,56 -1389,10 +1388,56 @@@ static void fix_unresolved_deltas(struc free(sorted_by_pos); } +static const char *derive_filename(const char *pack_name, const char *suffix, + struct strbuf *buf) +{ + size_t len; + if (!strip_suffix(pack_name, ".pack", &len)) + die(_("packfile name '%s' does not end with '.pack'"), + pack_name); + strbuf_add(buf, pack_name, len); + strbuf_addch(buf, '.'); + strbuf_addstr(buf, suffix); + return buf->buf; +} + +static void write_special_file(const char *suffix, const char *msg, - const char *pack_name, const unsigned char *sha1, ++ const char *pack_name, const unsigned char *hash, + const char **report) +{ + struct strbuf name_buf = STRBUF_INIT; + const char *filename; + int fd; + int msg_len = strlen(msg); + + if (pack_name) + filename = derive_filename(pack_name, suffix, &name_buf); + else - filename = odb_pack_name(&name_buf, sha1, suffix); ++ filename = odb_pack_name(&name_buf, hash, suffix); + + fd = odb_pack_keep(filename); + if (fd < 0) { + if (errno != EEXIST) + die_errno(_("cannot write %s file '%s'"), + suffix, filename); + } else { + if (msg_len > 0) { + write_or_die(fd, msg, msg_len); + write_or_die(fd, "\n", 1); + } + if (close(fd) != 0) + die_errno(_("cannot close written %s file '%s'"), + suffix, filename); + if (report) + *report = suffix; + } + strbuf_release(&name_buf); +} + static void final(const char *final_pack_name, const char *curr_pack_name, const char *final_index_name, const char *curr_index_name, - const char *keep_name, const char *keep_msg, + const char *keep_msg, const char *promisor_msg, - unsigned char *sha1) + unsigned char *hash) { const char *report = "pack"; struct strbuf pack_name = STRBUF_INIT; @@@ -1453,12 -1409,28 +1453,12 @@@ die_errno(_("error while closing pack file")); } - if (keep_msg) { - int keep_fd, keep_msg_len = strlen(keep_msg); - - if (!keep_name) - keep_name = odb_pack_name(&keep_name_buf, hash, "keep"); - - keep_fd = odb_pack_keep(keep_name); - if (keep_fd < 0) { - if (errno != EEXIST) - die_errno(_("cannot write keep file '%s'"), - keep_name); - } else { - if (keep_msg_len > 0) { - write_or_die(keep_fd, keep_msg, keep_msg_len); - write_or_die(keep_fd, "\n", 1); - } - if (close(keep_fd) != 0) - die_errno(_("cannot close written keep file '%s'"), - keep_name); - report = "keep"; - } - } + if (keep_msg) - write_special_file("keep", keep_msg, final_pack_name, sha1, ++ write_special_file("keep", keep_msg, final_pack_name, hash, + &report); + if (promisor_msg) + write_special_file("promisor", promisor_msg, final_pack_name, - sha1, NULL); ++ hash, NULL); if (final_pack_name != curr_pack_name) { if (!final_pack_name) @@@ -1647,12 -1632,12 +1647,12 @@@ int cmd_index_pack(int argc, const cha int i, fix_thin_pack = 0, verify = 0, stat_only = 0; const char *curr_index; const char *index_name = NULL, *pack_name = NULL; - const char *keep_name = NULL, *keep_msg = NULL; - struct strbuf index_name_buf = STRBUF_INIT, - keep_name_buf = STRBUF_INIT; + const char *keep_msg = NULL; + const char *promisor_msg = NULL; + struct strbuf index_name_buf = STRBUF_INIT; struct pack_idx_entry **idx_objects; struct pack_idx_option opts; - unsigned char pack_sha1[20]; + unsigned char pack_hash[GIT_MAX_RAWSZ]; unsigned foreign_nr = 1; /* zero is a "good" value, assume bad */ int report_end_of_input = 0; @@@ -1811,8 -1790,8 +1811,8 @@@ if (!verify) final(pack_name, curr_pack, index_name, curr_index, - keep_name, keep_msg, + keep_msg, promisor_msg, - pack_sha1); + pack_hash); else close(input_fd); free(objects); diff --cc read-cache.c index 82ba2287e1,15bbb9bab4..28bf0db9d9 --- a/read-cache.c +++ b/read-cache.c @@@ -2238,9 -2234,8 +2238,9 @@@ void update_index_if_able(struct index_ static int do_write_index(struct index_state *istate, struct tempfile *tempfile, int strip_extensions) { + uint64_t start = getnanotime(); int newfd = tempfile->fd; - git_SHA_CTX c; + git_hash_ctx c; struct cache_header hdr; int i, err = 0, removed, extended, hdr_version; struct cache_entry **cache = istate->cache; diff --cc sha1_file.c index 81a4bfb801,ff55fb303d..826d7a0ae3 --- a/sha1_file.c +++ b/sha1_file.c @@@ -1443,20 -1417,20 +1441,20 @@@ void *read_object_with_reference(const } } -static void write_sha1_file_prepare(const void *buf, unsigned long len, - const char *type, unsigned char *sha1, - char *hdr, int *hdrlen) +static void write_object_file_prepare(const void *buf, unsigned long len, + const char *type, struct object_id *oid, + char *hdr, int *hdrlen) { - git_SHA_CTX c; + git_hash_ctx c; /* Generate the header */ *hdrlen = xsnprintf(hdr, *hdrlen, "%s %lu", type, len)+1; /* Sha1.. */ - git_SHA1_Init(&c); - git_SHA1_Update(&c, hdr, *hdrlen); - git_SHA1_Update(&c, buf, len); - git_SHA1_Final(oid->hash, &c); + the_hash_algo->init_fn(&c); + the_hash_algo->update_fn(&c, hdr, *hdrlen); + the_hash_algo->update_fn(&c, buf, len); - the_hash_algo->final_fn(sha1, &c); ++ the_hash_algo->final_fn(oid->hash, &c); } /* @@@ -1579,15 -1552,12 +1577,15 @@@ static int write_loose_object(const str int fd, ret; unsigned char compressed[4096]; git_zstream stream; - git_SHA_CTX c; + git_hash_ctx c; struct object_id parano_oid; static struct strbuf tmp_file = STRBUF_INIT; - const char *filename = sha1_file_name(sha1); + static struct strbuf filename = STRBUF_INIT; + + strbuf_reset(&filename); + sha1_file_name(&filename, oid->hash); - fd = create_tmpfile(&tmp_file, filename); + fd = create_tmpfile(&tmp_file, filename.buf); if (fd < 0) { if (errno == EACCES) return error("insufficient permission for adding an object to repository database %s", get_object_directory()); @@@ -1622,16 -1592,13 +1620,16 @@@ } while (ret == Z_OK); if (ret != Z_STREAM_END) - die("unable to deflate new object %s (%d)", sha1_to_hex(sha1), ret); + die("unable to deflate new object %s (%d)", oid_to_hex(oid), + ret); ret = git_deflate_end_gently(&stream); if (ret != Z_OK) - die("deflateEnd on object %s failed (%d)", sha1_to_hex(sha1), ret); + die("deflateEnd on object %s failed (%d)", oid_to_hex(oid), + ret); - git_SHA1_Final(parano_oid.hash, &c); + the_hash_algo->final_fn(parano_oid.hash, &c); - if (hashcmp(sha1, parano_oid.hash) != 0) - die("confused by unstable object source data for %s", sha1_to_hex(sha1)); + if (oidcmp(oid, ¶no_oid) != 0) + die("confused by unstable object source data for %s", + oid_to_hex(oid)); close_sha1_file(fd);