Merge branch 'bc/object-id'
authorJunio C Hamano <gitster@pobox.com>
Thu, 2 Aug 2018 22:30:39 +0000 (15:30 -0700)
committerJunio C Hamano <gitster@pobox.com>
Thu, 2 Aug 2018 22:30:39 +0000 (15:30 -0700)
Conversion from uchar[40] to struct object_id continues.

* bc/object-id:
pretty: switch hard-coded constants to the_hash_algo
sha1-file: convert constants to uses of the_hash_algo
log-tree: switch GIT_SHA1_HEXSZ to the_hash_algo->hexsz
diff: switch GIT_SHA1_HEXSZ to use the_hash_algo
builtin/merge-recursive: make hash independent
builtin/merge: switch to use the_hash_algo
builtin/fmt-merge-msg: make hash independent
builtin/update-index: simplify parsing of cacheinfo
builtin/update-index: convert to using the_hash_algo
refs/files-backend: use the_hash_algo for writing refs
sha1-name: use the_hash_algo when parsing object names
strbuf: allocate space with GIT_MAX_HEXSZ
commit: express tree entry constants in terms of the_hash_algo
hex: switch to using the_hash_algo
tree-walk: replace hard-coded constants with the_hash_algo
cache: update object ID functions for the_hash_algo

1  2 
builtin/fmt-merge-msg.c
builtin/merge.c
cache.h
commit.c
diff.c
log-tree.c
refs/files-backend.c
sha1-name.c
tree-walk.c
diff --combined builtin/fmt-merge-msg.c
index 1b526adb3a95bbef0eabf8d154f0f6f9da1ef214,e8c13a2c03624d779c47f89efa2eec16707d61f4..8ccb14b87071d4d7285235738bc639051124a550
@@@ -2,7 -2,6 +2,7 @@@
  #include "cache.h"
  #include "config.h"
  #include "refs.h"
 +#include "object-store.h"
  #include "commit.h"
  #include "diff.h"
  #include "revision.h"
@@@ -109,14 -108,15 +109,15 @@@ static int handle_line(char *line, stru
        struct string_list_item *item;
        int pulling_head = 0;
        struct object_id oid;
+       const unsigned hexsz = the_hash_algo->hexsz;
  
-       if (len < GIT_SHA1_HEXSZ + 3 || line[GIT_SHA1_HEXSZ] != '\t')
+       if (len < hexsz + 3 || line[hexsz] != '\t')
                return 1;
  
-       if (starts_with(line + GIT_SHA1_HEXSZ + 1, "not-for-merge"))
+       if (starts_with(line + hexsz + 1, "not-for-merge"))
                return 0;
  
-       if (line[GIT_SHA1_HEXSZ + 1] != '\t')
+       if (line[hexsz + 1] != '\t')
                return 2;
  
        i = get_oid_hex(line, &oid);
  
        if (line[len - 1] == '\n')
                line[len - 1] = 0;
-       line += GIT_SHA1_HEXSZ + 2;
+       line += hexsz + 2;
  
        /*
         * At this point, line points at the beginning of comment e.g.
@@@ -343,7 -343,7 +344,7 @@@ static void shortlog(const char *name
        const struct object_id *oid = &origin_data->oid;
        int limit = opts->shortlog_len;
  
-       branch = deref_tag(parse_object(oid), oid_to_hex(oid), GIT_SHA1_HEXSZ);
+       branch = deref_tag(parse_object(oid), oid_to_hex(oid), the_hash_algo->hexsz);
        if (!branch || branch->type != OBJ_COMMIT)
                return;
  
@@@ -546,6 -546,7 +547,7 @@@ static void find_merge_parents(struct m
                int len;
                char *p = in->buf + pos;
                char *newline = strchr(p, '\n');
+               const char *q;
                struct object_id oid;
                struct commit *parent;
                struct object *obj;
                len = newline ? newline - p : strlen(p);
                pos += len + !!newline;
  
-               if (len < GIT_SHA1_HEXSZ + 3 ||
-                   get_oid_hex(p, &oid) ||
-                   p[GIT_SHA1_HEXSZ] != '\t' ||
-                   p[GIT_SHA1_HEXSZ + 1] != '\t')
+               if (parse_oid_hex(p, &oid, &q) ||
+                   q[0] != '\t' ||
+                   q[1] != '\t')
                        continue; /* skip not-for-merge */
                /*
                 * Do not use get_merge_parent() here; we do not have
diff --combined builtin/merge.c
index d1b547d97376d74e02e8f3dae3a1afb64dd2b780,916c9f05692e869085a12536c12e9e4d7d430439..299a75425900a10b16dcba1e1f3cadba50eb791b
@@@ -247,9 -247,9 +247,9 @@@ static struct option builtin_merge_opti
  /* Cleans up metadata that is uninteresting after a succeeded merge. */
  static void drop_save(void)
  {
 -      unlink(git_path_merge_head());
 -      unlink(git_path_merge_msg());
 -      unlink(git_path_merge_mode());
 +      unlink(git_path_merge_head(the_repository));
 +      unlink(git_path_merge_msg(the_repository));
 +      unlink(git_path_merge_mode(the_repository));
  }
  
  static int save_state(struct object_id *stash)
@@@ -382,7 -382,7 +382,7 @@@ static void squash_message(struct commi
                        oid_to_hex(&commit->object.oid));
                pretty_print_commit(&ctx, commit, &out);
        }
 -      write_file_buf(git_path_squash_msg(), out.buf, out.len);
 +      write_file_buf(git_path_squash_msg(the_repository), out.buf, out.len);
        strbuf_release(&out);
  }
  
@@@ -741,7 -741,7 +741,7 @@@ static void add_strategies(const char *
  
  static void read_merge_msg(struct strbuf *msg)
  {
 -      const char *filename = git_path_merge_msg();
 +      const char *filename = git_path_merge_msg(the_repository);
        strbuf_reset(msg);
        if (strbuf_read_file(msg, filename, 0) < 0)
                die_errno(_("Could not read from '%s'"), filename);
@@@ -778,18 -778,18 +778,18 @@@ static void prepare_to_commit(struct co
        if (signoff)
                append_signoff(&msg, ignore_non_trailer(msg.buf, msg.len), 0);
        write_merge_heads(remoteheads);
 -      write_file_buf(git_path_merge_msg(), msg.buf, msg.len);
 +      write_file_buf(git_path_merge_msg(the_repository), msg.buf, msg.len);
        if (run_commit_hook(0 < option_edit, get_index_file(), "prepare-commit-msg",
 -                          git_path_merge_msg(), "merge", NULL))
 +                          git_path_merge_msg(the_repository), "merge", NULL))
                abort_commit(remoteheads, NULL);
        if (0 < option_edit) {
 -              if (launch_editor(git_path_merge_msg(), NULL, NULL))
 +              if (launch_editor(git_path_merge_msg(the_repository), NULL, NULL))
                        abort_commit(remoteheads, NULL);
        }
  
        if (verify_msg && run_commit_hook(0 < option_edit, get_index_file(),
                                          "commit-msg",
 -                                        git_path_merge_msg(), NULL))
 +                                        git_path_merge_msg(the_repository), NULL))
                abort_commit(remoteheads, NULL);
  
        read_merge_msg(&msg);
@@@ -859,7 -859,7 +859,7 @@@ static int suggest_conflicts(void
        FILE *fp;
        struct strbuf msgbuf = STRBUF_INIT;
  
 -      filename = git_path_merge_msg();
 +      filename = git_path_merge_msg(the_repository);
        fp = xfopen(filename, "a");
  
        append_conflicts_hint(&msgbuf);
@@@ -942,12 -942,12 +942,12 @@@ static void write_merge_heads(struct co
                }
                strbuf_addf(&buf, "%s\n", oid_to_hex(oid));
        }
 -      write_file_buf(git_path_merge_head(), buf.buf, buf.len);
 +      write_file_buf(git_path_merge_head(the_repository), buf.buf, buf.len);
  
        strbuf_reset(&buf);
        if (fast_forward == FF_NO)
                strbuf_addstr(&buf, "no-ff");
 -      write_file_buf(git_path_merge_mode(), buf.buf, buf.len);
 +      write_file_buf(git_path_merge_mode(the_repository), buf.buf, buf.len);
        strbuf_release(&buf);
  }
  
@@@ -955,8 -955,7 +955,8 @@@ static void write_merge_state(struct co
  {
        write_merge_heads(remoteheads);
        strbuf_addch(&merge_msg, '\n');
 -      write_file_buf(git_path_merge_msg(), merge_msg.buf, merge_msg.len);
 +      write_file_buf(git_path_merge_msg(the_repository), merge_msg.buf,
 +                     merge_msg.len);
  }
  
  static int default_edit_option(void)
@@@ -1035,11 -1034,12 +1035,12 @@@ static void handle_fetch_head(struct co
        const char *filename;
        int fd, pos, npos;
        struct strbuf fetch_head_file = STRBUF_INIT;
+       const unsigned hexsz = the_hash_algo->hexsz;
  
        if (!merge_names)
                merge_names = &fetch_head_file;
  
 -      filename = git_path_fetch_head();
 +      filename = git_path_fetch_head(the_repository);
        fd = open(filename, O_RDONLY);
        if (fd < 0)
                die_errno(_("could not open '%s' for reading"), filename);
                else
                        npos = merge_names->len;
  
-               if (npos - pos < GIT_SHA1_HEXSZ + 2 ||
+               if (npos - pos < hexsz + 2 ||
                    get_oid_hex(merge_names->buf + pos, &oid))
                        commit = NULL; /* bad */
-               else if (memcmp(merge_names->buf + pos + GIT_SHA1_HEXSZ, "\t\t", 2))
+               else if (memcmp(merge_names->buf + pos + hexsz, "\t\t", 2))
                        continue; /* not-for-merge */
                else {
-                       char saved = merge_names->buf[pos + GIT_SHA1_HEXSZ];
-                       merge_names->buf[pos + GIT_SHA1_HEXSZ] = '\0';
+                       char saved = merge_names->buf[pos + hexsz];
+                       merge_names->buf[pos + hexsz] = '\0';
                        commit = get_merge_parent(merge_names->buf + pos);
-                       merge_names->buf[pos + GIT_SHA1_HEXSZ] = saved;
+                       merge_names->buf[pos + hexsz] = saved;
                }
                if (!commit) {
                        if (ptr)
@@@ -1214,7 -1214,7 +1215,7 @@@ int cmd_merge(int argc, const char **ar
                        usage_msg_opt(_("--abort expects no arguments"),
                              builtin_merge_usage, builtin_merge_options);
  
 -              if (!file_exists(git_path_merge_head()))
 +              if (!file_exists(git_path_merge_head(the_repository)))
                        die(_("There is no merge to abort (MERGE_HEAD missing)."));
  
                /* Invoke 'git reset --merge' */
                        usage_msg_opt(_("--continue expects no arguments"),
                              builtin_merge_usage, builtin_merge_options);
  
 -              if (!file_exists(git_path_merge_head()))
 +              if (!file_exists(git_path_merge_head(the_repository)))
                        die(_("There is no merge in progress (MERGE_HEAD missing)."));
  
                /* Invoke 'git commit' */
        if (read_cache_unmerged())
                die_resolve_conflict("merge");
  
 -      if (file_exists(git_path_merge_head())) {
 +      if (file_exists(git_path_merge_head(the_repository))) {
                /*
                 * There is no unmerged entry, don't advise 'git
                 * add/rm <file>', just 'git commit'.
                else
                        die(_("You have not concluded your merge (MERGE_HEAD exists)."));
        }
 -      if (file_exists(git_path_cherry_pick_head())) {
 +      if (file_exists(git_path_cherry_pick_head(the_repository))) {
                if (advice_resolve_conflict)
                        die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).\n"
                            "Please, commit your changes before you merge."));
diff --combined cache.h
index 8b447652a7e94ad31fd28134e0f548baa37265db,f00cc810bc70c49c75b651edc9841114ebcaadc0..ebfe59d01c8ef0e41f9ec228dde124ea9b9015f2
+++ b/cache.h
@@@ -484,7 -484,7 +484,7 @@@ extern const char *get_git_dir(void)
  extern const char *get_git_common_dir(void);
  extern char *get_object_directory(void);
  extern char *get_index_file(void);
 -extern char *get_graft_file(void);
 +extern char *get_graft_file(struct repository *r);
  extern void set_git_dir(const char *path);
  extern int get_common_dir_noenv(struct strbuf *sb, const char *gitdir);
  extern int get_common_dir(struct strbuf *sb, const char *gitdir);
@@@ -972,7 -972,7 +972,7 @@@ extern const struct object_id null_oid
  
  static inline int hashcmp(const unsigned char *sha1, const unsigned char *sha2)
  {
-       return memcmp(sha1, sha2, GIT_SHA1_RAWSZ);
+       return memcmp(sha1, sha2, the_hash_algo->rawsz);
  }
  
  static inline int oidcmp(const struct object_id *oid1, const struct object_id *oid2)
@@@ -992,7 -992,7 +992,7 @@@ static inline int is_null_oid(const str
  
  static inline void hashcpy(unsigned char *sha_dst, const unsigned char *sha_src)
  {
-       memcpy(sha_dst, sha_src, GIT_SHA1_RAWSZ);
+       memcpy(sha_dst, sha_src, the_hash_algo->rawsz);
  }
  
  static inline void oidcpy(struct object_id *dst, const struct object_id *src)
@@@ -1009,7 -1009,7 +1009,7 @@@ static inline struct object_id *oiddup(
  
  static inline void hashclr(unsigned char *hash)
  {
-       memset(hash, 0, GIT_SHA1_RAWSZ);
+       memset(hash, 0, the_hash_algo->rawsz);
  }
  
  static inline void oidclr(struct object_id *oid)
@@@ -1192,6 -1192,32 +1192,6 @@@ extern char *xdg_config_home(const cha
   */
  extern char *xdg_cache_home(const char *filename);
  
 -extern void *read_object_file_extended(const struct object_id *oid,
 -                                     enum object_type *type,
 -                                     unsigned long *size, int lookup_replace);
 -static inline void *read_object_file(const struct object_id *oid, enum object_type *type, unsigned long *size)
 -{
 -      return read_object_file_extended(oid, type, size, 1);
 -}
 -
 -/* Read and unpack an object file into memory, write memory to an object file */
 -int oid_object_info(struct repository *r, const struct object_id *, unsigned long *);
 -
 -extern int hash_object_file(const void *buf, unsigned long len,
 -                          const char *type, struct object_id *oid);
 -
 -extern int write_object_file(const void *buf, unsigned long len,
 -                           const char *type, struct object_id *oid);
 -
 -extern int hash_object_file_literally(const void *buf, unsigned long len,
 -                                    const char *type, struct object_id *oid,
 -                                    unsigned flags);
 -
 -extern int pretend_object_file(void *, unsigned long, enum object_type,
 -                             struct object_id *oid);
 -
 -extern int force_object_loose(const struct object_id *oid, time_t mtime);
 -
  extern int git_open_cloexec(const char *name, int flags);
  #define git_open(name) git_open_cloexec(name, O_RDONLY)
  extern int unpack_sha1_header(git_zstream *stream, unsigned char *map, unsigned long mapsize, void *buffer, unsigned long bufsiz);
@@@ -1201,6 -1227,43 +1201,6 @@@ extern int check_object_signature(cons
  
  extern int finalize_object_file(const char *tmpfile, const char *filename);
  
 -/*
 - * Open the loose object at path, check its hash, and return the contents,
 - * type, and size. If the object is a blob, then "contents" may return NULL,
 - * to allow streaming of large blobs.
 - *
 - * Returns 0 on success, negative on error (details may be written to stderr).
 - */
 -int read_loose_object(const char *path,
 -                    const struct object_id *expected_oid,
 -                    enum object_type *type,
 -                    unsigned long *size,
 -                    void **contents);
 -
 -/*
 - * Convenience for sha1_object_info_extended() with a NULL struct
 - * object_info. OBJECT_INFO_SKIP_CACHED is automatically set; pass
 - * nonzero flags to also set other flags.
 - */
 -extern int has_sha1_file_with_flags(const unsigned char *sha1, int flags);
 -static inline int has_sha1_file(const unsigned char *sha1)
 -{
 -      return has_sha1_file_with_flags(sha1, 0);
 -}
 -
 -/* Same as the above, except for struct object_id. */
 -extern int has_object_file(const struct object_id *oid);
 -extern int has_object_file_with_flags(const struct object_id *oid, int flags);
 -
 -/*
 - * Return true iff an alternate object database has a loose object
 - * with the specified name.  This function does not respect replace
 - * references.
 - */
 -extern int has_loose_object_nonlocal(const struct object_id *oid);
 -
 -extern void assert_oid_type(const struct object_id *oid, enum object_type expect);
 -
  /* Helper to check and "touch" a file */
  extern int check_and_freshen_file(const char *fn, int freshen);
  
@@@ -1568,6 -1631,60 +1568,6 @@@ int for_each_loose_file_in_objdir_buf(s
  #define FOR_EACH_OBJECT_LOCAL_ONLY 0x1
  extern int for_each_loose_object(each_loose_object_fn, void *, unsigned flags);
  
 -struct object_info {
 -      /* Request */
 -      enum object_type *typep;
 -      unsigned long *sizep;
 -      off_t *disk_sizep;
 -      unsigned char *delta_base_sha1;
 -      struct strbuf *type_name;
 -      void **contentp;
 -
 -      /* Response */
 -      enum {
 -              OI_CACHED,
 -              OI_LOOSE,
 -              OI_PACKED,
 -              OI_DBCACHED
 -      } whence;
 -      union {
 -              /*
 -               * struct {
 -               *      ... Nothing to expose in this case
 -               * } cached;
 -               * struct {
 -               *      ... Nothing to expose in this case
 -               * } loose;
 -               */
 -              struct {
 -                      struct packed_git *pack;
 -                      off_t offset;
 -                      unsigned int is_delta;
 -              } packed;
 -      } u;
 -};
 -
 -/*
 - * Initializer for a "struct object_info" that wants no items. You may
 - * also memset() the memory to all-zeroes.
 - */
 -#define OBJECT_INFO_INIT {NULL}
 -
 -/* Invoke lookup_replace_object() on the given hash */
 -#define OBJECT_INFO_LOOKUP_REPLACE 1
 -/* Allow reading from a loose object file of unknown/bogus type */
 -#define OBJECT_INFO_ALLOW_UNKNOWN_TYPE 2
 -/* Do not check cached storage */
 -#define OBJECT_INFO_SKIP_CACHED 4
 -/* Do not retry packed storage after checking packed and loose storage */
 -#define OBJECT_INFO_QUICK 8
 -/* Do not check loose object */
 -#define OBJECT_INFO_IGNORE_LOOSE 16
 -
 -int oid_object_info_extended(struct repository *r,
 -                           const struct object_id *,
 -                           struct object_info *, unsigned flags);
 -
  /*
   * Set this to 0 to prevent sha1_object_info_extended() from fetching missing
   * blobs. This has a difference only if extensions.partialClone is set.
diff --combined commit.c
index a7c0b5f8c6ce1d1d41c7ede357116fee4a643bbc,ff05d0457006e46dc2df4aecaf30f525b7e082f2..bbca413ba75b20d80ab24dd2401c865def0d90cd
+++ b/commit.c
@@@ -2,8 -2,6 +2,8 @@@
  #include "tag.h"
  #include "commit.h"
  #include "commit-graph.h"
 +#include "repository.h"
 +#include "object-store.h"
  #include "pkt-line.h"
  #include "utf8.h"
  #include "diff.h"
@@@ -100,44 -98,41 +100,44 @@@ static timestamp_t parse_commit_date(co
        return parse_timestamp(dateptr, NULL, 10);
  }
  
 -static struct commit_graft **commit_graft;
 -static int commit_graft_alloc, commit_graft_nr;
 -
  static const unsigned char *commit_graft_sha1_access(size_t index, void *table)
  {
        struct commit_graft **commit_graft_table = table;
        return commit_graft_table[index]->oid.hash;
  }
  
 -static int commit_graft_pos(const unsigned char *sha1)
 +static int commit_graft_pos(struct repository *r, const unsigned char *sha1)
  {
 -      return sha1_pos(sha1, commit_graft, commit_graft_nr,
 +      return sha1_pos(sha1, r->parsed_objects->grafts,
 +                      r->parsed_objects->grafts_nr,
                        commit_graft_sha1_access);
  }
  
 -int register_commit_graft(struct commit_graft *graft, int ignore_dups)
 +int register_commit_graft(struct repository *r, struct commit_graft *graft,
 +                        int ignore_dups)
  {
 -      int pos = commit_graft_pos(graft->oid.hash);
 +      int pos = commit_graft_pos(r, graft->oid.hash);
  
        if (0 <= pos) {
                if (ignore_dups)
                        free(graft);
                else {
 -                      free(commit_graft[pos]);
 -                      commit_graft[pos] = graft;
 +                      free(r->parsed_objects->grafts[pos]);
 +                      r->parsed_objects->grafts[pos] = graft;
                }
                return 1;
        }
        pos = -pos - 1;
 -      ALLOC_GROW(commit_graft, commit_graft_nr + 1, commit_graft_alloc);
 -      commit_graft_nr++;
 -      if (pos < commit_graft_nr)
 -              MOVE_ARRAY(commit_graft + pos + 1, commit_graft + pos,
 -                         commit_graft_nr - pos - 1);
 -      commit_graft[pos] = graft;
 +      ALLOC_GROW(r->parsed_objects->grafts,
 +                 r->parsed_objects->grafts_nr + 1,
 +                 r->parsed_objects->grafts_alloc);
 +      r->parsed_objects->grafts_nr++;
 +      if (pos < r->parsed_objects->grafts_nr)
 +              memmove(r->parsed_objects->grafts + pos + 1,
 +                      r->parsed_objects->grafts + pos,
 +                      (r->parsed_objects->grafts_nr - pos - 1) *
 +                      sizeof(*r->parsed_objects->grafts));
 +      r->parsed_objects->grafts[pos] = graft;
        return 0;
  }
  
@@@ -179,7 -174,7 +179,7 @@@ bad_graft_data
        return NULL;
  }
  
 -static int read_graft_file(const char *graft_file)
 +static int read_graft_file(struct repository *r, const char *graft_file)
  {
        FILE *fp = fopen_or_warn(graft_file, "r");
        struct strbuf buf = STRBUF_INIT;
                struct commit_graft *graft = read_graft_line(&buf);
                if (!graft)
                        continue;
 -              if (register_commit_graft(graft, 1))
 +              if (register_commit_graft(r, graft, 1))
                        error("duplicate graft data: %s", buf.buf);
        }
        fclose(fp);
        return 0;
  }
  
 -static void prepare_commit_graft(void)
 +static void prepare_commit_graft(struct repository *r)
  {
 -      static int commit_graft_prepared;
        char *graft_file;
  
 -      if (commit_graft_prepared)
 +      if (r->parsed_objects->commit_graft_prepared)
                return;
        if (!startup_info->have_repository)
                return;
  
 -      graft_file = get_graft_file();
 -      read_graft_file(graft_file);
 +      graft_file = get_graft_file(r);
 +      read_graft_file(r, graft_file);
        /* make sure shallows are read */
 -      is_repository_shallow();
 -      commit_graft_prepared = 1;
 +      is_repository_shallow(r);
 +      r->parsed_objects->commit_graft_prepared = 1;
  }
  
 -struct commit_graft *lookup_commit_graft(const struct object_id *oid)
 +struct commit_graft *lookup_commit_graft(struct repository *r, const struct object_id *oid)
  {
        int pos;
 -      prepare_commit_graft();
 -      pos = commit_graft_pos(oid->hash);
 +      prepare_commit_graft(r);
 +      pos = commit_graft_pos(r, oid->hash);
        if (pos < 0)
                return NULL;
 -      return commit_graft[pos];
 +      return r->parsed_objects->grafts[pos];
  }
  
  int for_each_commit_graft(each_commit_graft_fn fn, void *cb_data)
  {
        int i, ret;
 -      for (i = ret = 0; i < commit_graft_nr && !ret; i++)
 -              ret = fn(commit_graft[i], cb_data);
 +      for (i = ret = 0; i < the_repository->parsed_objects->grafts_nr && !ret; i++)
 +              ret = fn(the_repository->parsed_objects->grafts[i], cb_data);
        return ret;
  }
  
  int unregister_shallow(const struct object_id *oid)
  {
 -      int pos = commit_graft_pos(oid->hash);
 +      int pos = commit_graft_pos(the_repository, oid->hash);
        if (pos < 0)
                return -1;
 -      if (pos + 1 < commit_graft_nr)
 -              MOVE_ARRAY(commit_graft + pos, commit_graft + pos + 1,
 -                         commit_graft_nr - pos - 1);
 -      commit_graft_nr--;
 +      if (pos + 1 < the_repository->parsed_objects->grafts_nr)
 +              MOVE_ARRAY(the_repository->parsed_objects->grafts + pos,
 +                         the_repository->parsed_objects->grafts + pos + 1,
 +                         the_repository->parsed_objects->grafts_nr - pos - 1);
 +      the_repository->parsed_objects->grafts_nr--;
        return 0;
  }
  
@@@ -369,8 -364,8 +369,8 @@@ int parse_commit_buffer(struct commit *
        struct object_id parent;
        struct commit_list **pptr;
        struct commit_graft *graft;
-       const int tree_entry_len = GIT_SHA1_HEXSZ + 5;
-       const int parent_entry_len = GIT_SHA1_HEXSZ + 7;
+       const int tree_entry_len = the_hash_algo->hexsz + 5;
+       const int parent_entry_len = the_hash_algo->hexsz + 7;
  
        if (item->object.parsed)
                return 0;
        bufptr += tree_entry_len + 1; /* "tree " + "hex sha1" + "\n" */
        pptr = &item->parents;
  
 -      graft = lookup_commit_graft(&item->object.oid);
 +      graft = lookup_commit_graft(the_repository, &item->object.oid);
        while (bufptr + parent_entry_len < tail && !memcmp(bufptr, "parent ", 7)) {
                struct commit *new_parent;
  
diff --combined diff.c
index dc53a19bab609eac8de52e57f437c79da7fbb1fa,485ff6c264e1d4edd2cddb47a6f5beae96d26a13..d20614605e6f50a0e693dcda9499452475000d31
--- 1/diff.c
--- 2/diff.c
+++ b/diff.c
@@@ -13,7 -13,6 +13,7 @@@
  #include "attr.h"
  #include "run-command.h"
  #include "utf8.h"
 +#include "object-store.h"
  #include "userdiff.h"
  #include "submodule-config.h"
  #include "submodule.h"
@@@ -3833,7 -3832,7 +3833,7 @@@ static const char *diff_abbrev_oid(cons
                char *hex = oid_to_hex(oid);
                if (abbrev < 0)
                        abbrev = FALLBACK_DEFAULT_ABBREV;
-               if (abbrev > GIT_SHA1_HEXSZ)
+               if (abbrev > the_hash_algo->hexsz)
                        BUG("oid abbreviation out of range: %d", abbrev);
                if (abbrev)
                        hex[abbrev] = '\0';
@@@ -4948,7 -4947,7 +4948,7 @@@ const char *diff_aligned_abbrev(const s
        const char *abbrev;
  
        /* Do we want all 40 hex characters? */
-       if (len == GIT_SHA1_HEXSZ)
+       if (len == the_hash_algo->hexsz)
                return oid_to_hex(oid);
  
        /* An abbreviated value is fine, possibly followed by an ellipsis. */
         * the automatic sizing is supposed to give abblen that ensures
         * uniqueness across all objects (statistically speaking).
         */
-       if (abblen < GIT_SHA1_HEXSZ - 3) {
+       if (abblen < the_hash_algo->hexsz - 3) {
                static char hex[GIT_MAX_HEXSZ + 1];
                if (len < abblen && abblen <= len + 2)
                        xsnprintf(hex, sizeof(hex), "%s%.*s", abbrev, len+3-abblen, "..");
diff --combined log-tree.c
index 4a3907fea02d50e1074ca1b785780a0753e77468,9655de8ad73ceb90e2de4ad55e4ec93072b4465f..f2ca0fbd53fa3da7759b5d77d97b6816dfe23430
@@@ -1,7 -1,6 +1,7 @@@
  #include "cache.h"
  #include "config.h"
  #include "diff.h"
 +#include "object-store.h"
  #include "commit.h"
  #include "tag.h"
  #include "graph.h"
@@@ -546,7 -545,7 +546,7 @@@ void show_log(struct rev_info *opt
        struct strbuf msgbuf = STRBUF_INIT;
        struct log_info *log = opt->loginfo;
        struct commit *commit = log->commit, *parent = log->parent;
-       int abbrev_commit = opt->abbrev_commit ? opt->abbrev : GIT_SHA1_HEXSZ;
+       int abbrev_commit = opt->abbrev_commit ? opt->abbrev : the_hash_algo->hexsz;
        const char *extra_headers = opt->extra_headers;
        struct pretty_print_context ctx = {0};
  
diff --combined refs/files-backend.c
index 054306d7796670d6e6049074f501568c3eadaa58,c546ffbf5d46c3cceb767ba5e43aa7da1452aac6..4e43dd46b543d3665ab5d1128ccbbd5e26b6fbb0
@@@ -1582,17 -1582,26 +1582,17 @@@ static int log_ref_write_fd(int fd, con
                            const struct object_id *new_oid,
                            const char *committer, const char *msg)
  {
 -      int msglen, written;
 -      unsigned maxlen, len;
 -      char *logrec;
 -
 -      msglen = msg ? strlen(msg) : 0;
 -      maxlen = strlen(committer) + msglen + 100;
 -      logrec = xmalloc(maxlen);
 -      len = xsnprintf(logrec, maxlen, "%s %s %s\n",
 -                      oid_to_hex(old_oid),
 -                      oid_to_hex(new_oid),
 -                      committer);
 -      if (msglen)
 -              len += copy_reflog_msg(logrec + len - 1, msg) - 1;
 -
 -      written = len <= maxlen ? write_in_full(fd, logrec, len) : -1;
 -      free(logrec);
 -      if (written < 0)
 -              return -1;
 -
 -      return 0;
 +      struct strbuf sb = STRBUF_INIT;
 +      int ret = 0;
 +
 +      strbuf_addf(&sb, "%s %s %s", oid_to_hex(old_oid), oid_to_hex(new_oid), committer);
 +      if (msg && *msg)
 +              copy_reflog_msg(&sb, msg);
 +      strbuf_addch(&sb, '\n');
 +      if (write_in_full(fd, sb.buf, sb.len) < 0)
 +              ret = -1;
 +      strbuf_release(&sb);
 +      return ret;
  }
  
  static int files_log_ref_write(struct files_ref_store *refs,
@@@ -1667,7 -1676,7 +1667,7 @@@ static int write_ref_to_lockfile(struc
                return -1;
        }
        fd = get_lock_file_fd(&lock->lk);
-       if (write_in_full(fd, oid_to_hex(oid), GIT_SHA1_HEXSZ) < 0 ||
+       if (write_in_full(fd, oid_to_hex(oid), the_hash_algo->hexsz) < 0 ||
            write_in_full(fd, &term, 1) < 0 ||
            close_ref_gently(lock) < 0) {
                strbuf_addf(err,
@@@ -3061,7 -3070,7 +3061,7 @@@ static int files_reflog_expire(struct r
                        rollback_lock_file(&reflog_lock);
                } else if (update &&
                           (write_in_full(get_lock_file_fd(&lock->lk),
-                               oid_to_hex(&cb.last_kept_oid), GIT_SHA1_HEXSZ) < 0 ||
+                               oid_to_hex(&cb.last_kept_oid), the_hash_algo->hexsz) < 0 ||
                            write_str_in_full(get_lock_file_fd(&lock->lk), "\n") < 0 ||
                            close_ref_gently(lock) < 0)) {
                        status |= error("couldn't write %s",
diff --combined sha1-name.c
index 641ca12f91bad8f5aa5739ac92a3a9498e4d42e9,ba6a5a689ff91c540f82f1576eb84a641b8309f9..e072d48ddafc5d998b9ad42ada4297a01a5fee07
@@@ -310,7 -310,7 +310,7 @@@ static int init_object_disambiguation(c
  {
        int i;
  
-       if (len < MINIMUM_ABBREV || len > GIT_SHA1_HEXSZ)
+       if (len < MINIMUM_ABBREV || len > the_hash_algo->hexsz)
                return -1;
  
        memset(ds, 0, sizeof(*ds));
@@@ -576,6 -576,8 +576,8 @@@ int find_unique_abbrev_r(char *hex, con
        struct disambiguate_state ds;
        struct min_abbrev_data mad;
        struct object_id oid_ret;
+       const unsigned hexsz = the_hash_algo->hexsz;
        if (len < 0) {
                unsigned long count = approximate_object_count();
                /*
        }
  
        oid_to_hex_r(hex, oid);
-       if (len == GIT_SHA1_HEXSZ || !len)
-               return GIT_SHA1_HEXSZ;
+       if (len == hexsz || !len)
+               return hexsz;
  
        mad.init_len = len;
        mad.cur_len = len;
@@@ -706,7 -708,7 +708,7 @@@ static int get_oid_basic(const char *st
        int refs_found = 0;
        int at, reflog_len, nth_prior = 0;
  
-       if (len == GIT_SHA1_HEXSZ && !get_oid_hex(str, oid)) {
+       if (len == the_hash_algo->hexsz && !get_oid_hex(str, oid)) {
                if (warn_ambiguous_refs && warn_on_object_refname_ambiguity) {
                        refs_found = dwim_ref(str, len, &tmp_oid, &real_ref);
                        if (refs_found > 0) {
                int detached;
  
                if (interpret_nth_prior_checkout(str, len, &buf) > 0) {
-                       detached = (buf.len == GIT_SHA1_HEXSZ && !get_oid_hex(buf.buf, oid));
+                       detached = (buf.len == the_hash_algo->hexsz && !get_oid_hex(buf.buf, oid));
                        strbuf_release(&buf);
                        if (detached)
                                return 0;
@@@ -1650,7 -1652,6 +1652,7 @@@ static int get_oid_with_context_1(cons
                        struct commit_list *list = NULL;
  
                        for_each_ref(handle_one_ref, &list);
 +                      head_ref(handle_one_ref, &list);
                        commit_list_sort_by_date(&list);
                        return get_oid_oneline(name + 2, oid, list);
                }
diff --combined tree-walk.c
index ecd6e53b8541408add0de076b01932a1ffd631e9,c1f27086a91479b4342dd4ffadc7b7d609142797..77b37f36fa1bc8fae48231d35514b88d51902488
@@@ -2,7 -2,6 +2,7 @@@
  #include "tree-walk.h"
  #include "unpack-trees.h"
  #include "dir.h"
 +#include "object-store.h"
  #include "tree.h"
  #include "pathspec.h"
  
@@@ -27,8 -26,9 +27,9 @@@ static int decode_tree_entry(struct tre
  {
        const char *path;
        unsigned int mode, len;
+       const unsigned hashsz = the_hash_algo->rawsz;
  
-       if (size < 23 || buf[size - 21]) {
+       if (size < hashsz + 3 || buf[size - (hashsz + 1)]) {
                strbuf_addstr(err, _("too-short tree object"));
                return -1;
        }