Merge branch 'bc/hash-algo'
authorJunio C Hamano <gitster@pobox.com>
Thu, 15 Feb 2018 22:55:47 +0000 (14:55 -0800)
committerJunio C Hamano <gitster@pobox.com>
Thu, 15 Feb 2018 22:55:47 +0000 (14:55 -0800)
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

1  2 
builtin/index-pack.c
builtin/pack-objects.c
builtin/unpack-objects.c
cache.h
read-cache.c
sha1_file.c
index 16edfebf632a7938889d35d4d6332a0d7e814ae2,0bb520c731394e536ee107e0e212db54ddeea179..7e3e1a461c4469ea0272d9545d7173e984b1ebf4
@@@ -1388,56 -1389,10 +1388,56 @@@ static void fix_unresolved_deltas(struc
        free(sorted_by_pos);
  }
  
-                              const char *pack_name, const unsigned char *sha1,
 +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,
-               filename = odb_pack_name(&name_buf, sha1, suffix);
++                             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, 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;
                        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;
  
        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);
Simple merge
Simple merge
diff --cc cache.h
Simple merge
diff --cc read-cache.c
index 82ba2287e1bbe6121a9c1c5e9b20a9b352886f95,15bbb9bab497446519dfc69da33bd96a979a3587..28bf0db9d95777659ffe43d994f4970b46a78f56
@@@ -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 81a4bfb80137d0e76cefa65b8282806131006fd0,ff55fb303dcbc0c0a67d1bffc740896c5bc4116c..826d7a0ae377445c117a646ef26f79dc02785122
@@@ -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());
        } 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, &parano_oid) != 0)
 +              die("confused by unstable object source data for %s",
 +                  oid_to_hex(oid));
  
        close_sha1_file(fd);