Merge branch 'rs/use-div-round-up'
authorJunio C Hamano <gitster@pobox.com>
Wed, 12 Jul 2017 22:18:23 +0000 (15:18 -0700)
committerJunio C Hamano <gitster@pobox.com>
Wed, 12 Jul 2017 22:18:23 +0000 (15:18 -0700)
Code cleanup.

* rs/use-div-round-up:
use DIV_ROUND_UP

1  2 
builtin/gc.c
builtin/grep.c
builtin/log.c
builtin/receive-pack.c
diff.c
imap-send.c
sha1_name.c
shallow.c
diff --combined builtin/gc.c
index bd91f136fed125ab06502c22b74b4f193f60773a,0477c2451f8e47bb57d99b6ef3b8709c486d4bf8..2ba50a28735eee23887211684e60aaeda81c3e98
@@@ -11,7 -11,6 +11,7 @@@
   */
  
  #include "builtin.h"
 +#include "config.h"
  #include "tempfile.h"
  #include "lockfile.h"
  #include "parse-options.h"
@@@ -34,7 -33,7 +34,7 @@@ static int aggressive_window = 250
  static int gc_auto_threshold = 6700;
  static int gc_auto_pack_limit = 50;
  static int detach_auto = 1;
 -static unsigned long gc_log_expire_time;
 +static timestamp_t gc_log_expire_time;
  static const char *gc_log_expire = "1.day.ago";
  static const char *prune_expire = "2.weeks.ago";
  static const char *prune_worktrees_expire = "3.months.ago";
@@@ -149,7 -148,7 +149,7 @@@ static int too_many_loose_objects(void
        if (!dir)
                return 0;
  
-       auto_threshold = (gc_auto_threshold + 255) / 256;
+       auto_threshold = DIV_ROUND_UP(gc_auto_threshold, 256);
        while ((ent = readdir(dir)) != NULL) {
                if (strspn(ent->d_name, "0123456789abcdef") != 38 ||
                    ent->d_name[38] != '\0')
diff --combined builtin/grep.c
index fa351c49f4fd74b3cde1703ef98dba5f0c2c4e8f,dc001da562322e3b9b7133fcf939195040a7a8f5..0d6e669732dadd574c69924578bf85ed6e6aeaf8
@@@ -4,8 -4,6 +4,8 @@@
   * Copyright (c) 2006 Junio C Hamano
   */
  #include "cache.h"
 +#include "repository.h"
 +#include "config.h"
  #include "blob.h"
  #include "tree.h"
  #include "commit.h"
@@@ -75,14 -73,14 +75,14 @@@ static pthread_mutex_t grep_mutex
  
  static inline void grep_lock(void)
  {
 -      if (num_threads)
 -              pthread_mutex_lock(&grep_mutex);
 +      assert(num_threads);
 +      pthread_mutex_lock(&grep_mutex);
  }
  
  static inline void grep_unlock(void)
  {
 -      if (num_threads)
 -              pthread_mutex_unlock(&grep_mutex);
 +      assert(num_threads);
 +      pthread_mutex_unlock(&grep_mutex);
  }
  
  /* Signalled when a new work_item is added to todo. */
@@@ -226,8 -224,7 +226,8 @@@ static void start_threads(struct grep_o
                int err;
                struct grep_opt *o = grep_opt_dup(opt);
                o->output = strbuf_out;
 -              o->debug = 0;
 +              if (i)
 +                      o->debug = 0;
                compile_grep_patterns(o);
                err = pthread_create(&threads[i], NULL, run, o);
  
@@@ -292,22 -289,8 +292,22 @@@ static int grep_cmd_config(const char *
                if (num_threads < 0)
                        die(_("invalid number of threads specified (%d) for %s"),
                            num_threads, var);
 +#ifdef NO_PTHREADS
 +              else if (num_threads && num_threads != 1) {
 +                      /*
 +                       * TRANSLATORS: %s is the configuration
 +                       * variable for tweaking threads, currently
 +                       * grep.threads
 +                       */
 +                      warning(_("no threads support, ignoring %s"), var);
 +                      num_threads = 0;
 +              }
 +#endif
        }
  
 +      if (!strcmp(var, "submodule.recurse"))
 +              recurse_submodules = git_config_bool(var, value);
 +
        return st;
  }
  
@@@ -344,7 -327,7 +344,7 @@@ static int grep_oid(struct grep_opt *op
  
  #ifndef NO_PTHREADS
        if (num_threads) {
 -              add_work(opt, GREP_SOURCE_SHA1, pathbuf.buf, path, oid);
 +              add_work(opt, GREP_SOURCE_OID, pathbuf.buf, path, oid);
                strbuf_release(&pathbuf);
                return 0;
        } else
                struct grep_source gs;
                int hit;
  
 -              grep_source_init(&gs, GREP_SOURCE_SHA1, pathbuf.buf, path, oid);
 +              grep_source_init(&gs, GREP_SOURCE_OID, pathbuf.buf, path, oid);
                strbuf_release(&pathbuf);
                hit = grep_source(opt, &gs);
  
@@@ -512,8 -495,6 +512,8 @@@ static void compile_submodule_options(c
                break;
        case GREP_PATTERN_TYPE_UNSPECIFIED:
                break;
 +      default:
 +              die("BUG: Added a new grep pattern type without updating switch statement");
        }
  
        for (pattern = opt->pattern_list; pattern != NULL;
         * submodule process has its own thread pool.
         */
        argv_array_pushf(&submodule_options, "--threads=%d",
-                        (num_threads + 1) / 2);
+                        DIV_ROUND_UP(num_threads, 2));
  
        /* Add Pathspecs */
        argv_array_push(&submodule_options, "--");
@@@ -589,7 -570,7 +589,7 @@@ static int grep_submodule_launch(struc
         * with the object's name: 'tree-name:filename'.  In order to
         * provide uniformity of output we want to pass the name of the
         * parent project's object name to the submodule so the submodule can
 -       * prefix its output with the parent's name and not its own SHA1.
 +       * prefix its output with the parent's name and not its own OID.
         */
        if (gs->identifier && end_of_base)
                argv_array_pushf(&cp.args, "--parent-basename=%.*s",
                 * If there is a tree identifier for the submodule, add the
                 * rev after adding the submodule options but before the
                 * pathspecs.  To do this we listen for the '--' and insert the
 -               * sha1 before pushing the '--' onto the child process argv
 +               * oid before pushing the '--' onto the child process argv
                 * array.
                 */
                if (gs->identifier &&
                    !strcmp("--", submodule_options.argv[i])) {
 -                      argv_array_push(&cp.args, sha1_to_hex(gs->identifier));
 +                      argv_array_push(&cp.args, oid_to_hex(gs->identifier));
                }
  
                argv_array_push(&cp.args, submodule_options.argv[i]);
  
  /*
   * Prep grep structures for a submodule grep
 - * sha1: the sha1 of the submodule or NULL if using the working tree
 + * oid: the oid of the submodule or NULL if using the working tree
   * filename: name of the submodule including tree name of parent
   * path: location of the submodule
   */
 -static int grep_submodule(struct grep_opt *opt, const unsigned char *sha1,
 +static int grep_submodule(struct grep_opt *opt, const struct object_id *oid,
                          const char *filename, const char *path)
  {
 -      if (!is_submodule_initialized(path))
 +      if (!is_submodule_active(the_repository, path))
                return 0;
        if (!is_submodule_populated_gently(path, NULL)) {
                /*
 -               * If searching history, check for the presense of the
 +               * If searching history, check for the presence of the
                 * submodule's gitdir before skipping the submodule.
                 */
 -              if (sha1) {
 +              if (oid) {
                        const struct submodule *sub =
                                        submodule_from_path(null_sha1, path);
                        if (sub)
  
  #ifndef NO_PTHREADS
        if (num_threads) {
 -              add_work(opt, GREP_SOURCE_SUBMODULE, filename, path, sha1);
 +              add_work(opt, GREP_SOURCE_SUBMODULE, filename, path, oid);
                return 0;
        } else
  #endif
                int hit;
  
                grep_source_init(&gs, GREP_SOURCE_SUBMODULE,
 -                               filename, path, sha1);
 +                               filename, path, oid);
                hit = grep_submodule_launch(opt, &gs);
  
                grep_source_clear(&gs);
@@@ -794,7 -775,7 +794,7 @@@ static int grep_tree(struct grep_opt *o
                                         check_attr);
                        free(data);
                } else if (recurse_submodules && S_ISGITLINK(entry.mode)) {
 -                      hit |= grep_submodule(opt, entry.oid->hash, base->buf,
 +                      hit |= grep_submodule(opt, entry.oid, base->buf,
                                              base->buf + tn_len);
                }
  
@@@ -885,7 -866,7 +885,7 @@@ static int grep_directory(struct grep_o
        if (exc_std)
                setup_standard_excludes(&dir);
  
 -      fill_directory(&dir, pathspec);
 +      fill_directory(&dir, &the_index, pathspec);
        for (i = 0; i < dir.nr; i++) {
                if (!dir_path_match(dir.entries[i], pathspec, 0, NULL))
                        continue;
@@@ -1173,6 -1154,8 +1173,6 @@@ int cmd_grep(int argc, const char **arg
        if (!opt.fixed && opt.ignore_case)
                opt.regflags |= REG_ICASE;
  
 -      compile_grep_patterns(&opt);
 -
        /*
         * We have to find "--" in a separate pass, because its presence
         * influences how we will parse arguments that come before it.
                        break;
                }
  
 -              object = parse_object_or_die(oid.hash, arg);
 +              object = parse_object_or_die(&oid, arg);
                if (!seen_dashdash)
                        verify_non_filename(prefix, arg);
                add_object_array_with_path(object, arg, &list, oc.mode, oc.path);
                num_threads = GREP_NUM_THREADS_DEFAULT;
        else if (num_threads < 0)
                die(_("invalid number of threads specified (%d)"), num_threads);
 +      if (num_threads == 1)
 +              num_threads = 0;
  #else
 +      if (num_threads)
 +              warning(_("no threads support, ignoring --threads"));
        num_threads = 0;
  #endif
  
 +      if (!num_threads)
 +              /*
 +               * The compiled patterns on the main path are only
 +               * used when not using threading. Otherwise
 +               * start_threads() below calls compile_grep_patterns()
 +               * for each thread.
 +               */
 +              compile_grep_patterns(&opt);
 +
  #ifndef NO_PTHREADS
        if (num_threads) {
                if (!(opt.name_only || opt.unmatch_name_only || opt.count)
diff --combined builtin/log.c
index 8ca1de98943bdc62248ddd31708a6632557c61de,96c050b450056205ea971340e824d1e3d87e0d6a..c6362cf92ed799696128a5da6c802813db62fbcf
@@@ -5,7 -5,6 +5,7 @@@
   *             2006 Junio Hamano
   */
  #include "cache.h"
 +#include "config.h"
  #include "refs.h"
  #include "color.h"
  #include "commit.h"
@@@ -601,7 -600,7 +601,7 @@@ int cmd_show(int argc, const char **arg
                        rev.shown_one = 1;
                        if (ret)
                                break;
 -                      o = parse_object(t->tagged->oid.hash);
 +                      o = parse_object(&t->tagged->oid);
                        if (!o)
                                ret = error(_("Could not read object %s"),
                                            oid_to_hex(&t->tagged->oid));
@@@ -847,10 -846,8 +847,10 @@@ static int open_next_file(struct commi
        if (output_directory) {
                strbuf_addstr(&filename, output_directory);
                if (filename.len >=
 -                  PATH_MAX - FORMAT_PATCH_NAME_MAX - suffix_len)
 +                  PATH_MAX - FORMAT_PATCH_NAME_MAX - suffix_len) {
 +                      strbuf_release(&filename);
                        return error(_("name of output directory is too long"));
 +              }
                strbuf_complete(&filename, '/');
        }
  
        if (!quiet)
                printf("%s\n", filename.buf + outdir_offset);
  
 -      if ((rev->diffopt.file = fopen(filename.buf, "w")) == NULL)
 -              return error(_("Cannot open patch file %s"), filename.buf);
 +      if ((rev->diffopt.file = fopen(filename.buf, "w")) == NULL) {
 +              error_errno(_("Cannot open patch file %s"), filename.buf);
 +              strbuf_release(&filename);
 +              return -1;
 +      }
  
        strbuf_release(&filename);
        return 0;
@@@ -888,8 -882,8 +888,8 @@@ static void get_patch_ids(struct rev_in
        o2 = rev->pending.objects[1].item;
        flags1 = o1->flags;
        flags2 = o2->flags;
 -      c1 = lookup_commit_reference(o1->oid.hash);
 -      c2 = lookup_commit_reference(o2->oid.hash);
 +      c1 = lookup_commit_reference(&o1->oid);
 +      c2 = lookup_commit_reference(&o2->oid);
  
        if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
                die(_("Not a range."));
  static void gen_message_id(struct rev_info *info, char *base)
  {
        struct strbuf buf = STRBUF_INIT;
 -      strbuf_addf(&buf, "%s.%lu.git.%s", base,
 -                  (unsigned long) time(NULL),
 +      strbuf_addf(&buf, "%s.%"PRItime".git.%s", base,
 +                  (timestamp_t) time(NULL),
                    git_committer_info(IDENT_NO_NAME|IDENT_NO_DATE|IDENT_STRICT));
        info->message_id = strbuf_detach(&buf, NULL);
  }
@@@ -1053,9 -1047,9 +1053,9 @@@ static void make_cover_letter(struct re
  
        diff_setup_done(&opts);
  
 -      diff_tree_sha1(origin->tree->object.oid.hash,
 -                     head->tree->object.oid.hash,
 -                     "", &opts);
 +      diff_tree_oid(&origin->tree->object.oid,
 +                    &head->tree->object.oid,
 +                    "", &opts);
        diffcore_std(&opts);
        diff_flush(&opts);
  
@@@ -1273,7 -1267,7 +1273,7 @@@ static struct commit *get_base_commit(c
  
                        if (get_oid(upstream, &oid))
                                die(_("Failed to resolve '%s' as a valid ref."), upstream);
 -                      commit = lookup_commit_or_die(oid.hash, "upstream base");
 +                      commit = lookup_commit_or_die(&oid, "upstream base");
                        base_list = get_merge_bases_many(commit, total, list);
                        /* There should be one and only one merge base. */
                        if (!base_list || base_list->next)
  
                if (rev_nr % 2)
                        rev[i] = rev[2 * i];
-               rev_nr = (rev_nr + 1) / 2;
+               rev_nr = DIV_ROUND_UP(rev_nr, 2);
        }
  
        if (!in_merge_bases(base, rev[0]))
@@@ -1364,7 -1358,7 +1364,7 @@@ static void prepare_bases(struct base_t
                struct object_id *patch_id;
                if (commit->util)
                        continue;
 -              if (commit_patch_id(commit, &diffopt, oid.hash, 0))
 +              if (commit_patch_id(commit, &diffopt, &oid, 0))
                        die(_("cannot get patch id"));
                ALLOC_GROW(bases->patch_id, bases->nr_patch_id + 1, bases->alloc_patch_id);
                patch_id = bases->patch_id + bases->nr_patch_id;
@@@ -1829,7 -1823,7 +1829,7 @@@ static int add_pending_commit(const cha
  {
        struct object_id oid;
        if (get_oid(arg, &oid) == 0) {
 -              struct commit *commit = lookup_commit_reference(oid.hash);
 +              struct commit *commit = lookup_commit_reference(&oid);
                if (commit) {
                        commit->object.flags |= flags;
                        add_pending_object(revs, &commit->object, arg);
diff --combined builtin/receive-pack.c
index 71c0c768db92378b824951acd6efb7179438017b,90d58c2fedca4e86d817d9c4350d043b6249fa41..cabdc55e0933adb09d459a5a0747c718fd2c7d1e
@@@ -1,5 -1,4 +1,5 @@@
  #include "builtin.h"
 +#include "config.h"
  #include "lockfile.h"
  #include "pack.h"
  #include "refs.h"
@@@ -79,7 -78,7 +79,7 @@@ static const char *NONCE_OK = "OK"
  static const char *NONCE_SLOP = "SLOP";
  static const char *nonce_status;
  static long nonce_stamp_slop;
 -static unsigned long nonce_stamp_slop_limit;
 +static timestamp_t nonce_stamp_slop_limit;
  static struct ref_transaction *transaction;
  
  static enum {
@@@ -455,17 -454,17 +455,17 @@@ static void hmac_sha1(unsigned char *ou
        git_SHA1_Final(out, &ctx);
  }
  
 -static char *prepare_push_cert_nonce(const char *path, unsigned long stamp)
 +static char *prepare_push_cert_nonce(const char *path, timestamp_t stamp)
  {
        struct strbuf buf = STRBUF_INIT;
        unsigned char sha1[20];
  
 -      strbuf_addf(&buf, "%s:%lu", path, stamp);
 +      strbuf_addf(&buf, "%s:%"PRItime, path, stamp);
        hmac_sha1(sha1, buf.buf, buf.len, cert_nonce_seed, strlen(cert_nonce_seed));;
        strbuf_release(&buf);
  
        /* RFC 2104 5. HMAC-SHA1-80 */
 -      strbuf_addf(&buf, "%lu-%.*s", stamp, 20, sha1_to_hex(sha1));
 +      strbuf_addf(&buf, "%"PRItime"-%.*s", stamp, 20, sha1_to_hex(sha1));
        return strbuf_detach(&buf, NULL);
  }
  
@@@ -500,7 -499,7 +500,7 @@@ static char *find_header(const char *ms
  static const char *check_nonce(const char *buf, size_t len)
  {
        char *nonce = find_header(buf, len, "nonce", NULL);
 -      unsigned long stamp, ostamp;
 +      timestamp_t stamp, ostamp;
        char *bohmac, *expect = NULL;
        const char *retval = NONCE_BAD;
  
                retval = NONCE_BAD;
                goto leave;
        }
 -      stamp = strtoul(nonce, &bohmac, 10);
 +      stamp = parse_timestamp(nonce, &bohmac, 10);
        if (bohmac == nonce || bohmac[0] != '-') {
                retval = NONCE_BAD;
                goto leave;
         * would mean it was issued by another server with its clock
         * skewed in the future.
         */
 -      ostamp = strtoul(push_cert_nonce, NULL, 10);
 +      ostamp = parse_timestamp(push_cert_nonce, NULL, 10);
        nonce_stamp_slop = (long)ostamp - (long)stamp;
  
        if (nonce_stamp_slop_limit &&
@@@ -901,7 -900,7 +901,7 @@@ static int update_shallow_ref(struct co
         * not lose these new roots..
         */
        for (i = 0; i < extra.nr; i++)
 -              register_shallow(extra.oid[i].hash);
 +              register_shallow(&extra.oid[i]);
  
        si->shallow_ref[cmd->index] = 0;
        oid_array_clear(&extra);
@@@ -1103,8 -1102,8 +1103,8 @@@ static const char *update(struct comman
                struct object *old_object, *new_object;
                struct commit *old_commit, *new_commit;
  
 -              old_object = parse_object(old_oid->hash);
 -              new_object = parse_object(new_oid->hash);
 +              old_object = parse_object(old_oid);
 +              new_object = parse_object(new_oid);
  
                if (!old_object || !new_object ||
                    old_object->type != OBJ_COMMIT ||
  
        if (is_null_oid(new_oid)) {
                struct strbuf err = STRBUF_INIT;
 -              if (!parse_object(old_oid->hash)) {
 +              if (!parse_object(old_oid)) {
                        old_oid = NULL;
                        if (ref_exists(name)) {
                                rp_warning("Allowing deletion of corrupt ref.");
@@@ -1806,7 -1805,7 +1806,7 @@@ static const char *unpack_with_sideband
  static void prepare_shallow_update(struct command *commands,
                                   struct shallow_info *si)
  {
-       int i, j, k, bitmap_size = (si->ref->nr + 31) / 32;
+       int i, j, k, bitmap_size = DIV_ROUND_UP(si->ref->nr, 32);
  
        ALLOC_ARRAY(si->used_shallow, si->shallow->nr);
        assign_shallow_commits_to_refs(si, si->used_shallow, NULL);
diff --combined diff.c
index 00b4c86698e6badb5d2904cacb3ba7d92c018d76,63ff66bf6d0b611c8badeb68f5f7295b23ebd7bf..85e714f6c68d24e11228b69d2511c49811c979b4
--- 1/diff.c
--- 2/diff.c
+++ b/diff.c
@@@ -2,7 -2,6 +2,7 @@@
   * Copyright (C) 2005 Junio C Hamano
   */
  #include "cache.h"
 +#include "config.h"
  #include "tempfile.h"
  #include "quote.h"
  #include "diff.h"
@@@ -28,7 -27,7 +28,7 @@@
  #endif
  
  static int diff_detect_rename_default;
 -static int diff_indent_heuristic; /* experimental */
 +static int diff_indent_heuristic = 1;
  static int diff_rename_limit_default = 400;
  static int diff_suppress_blank_empty;
  static int diff_use_color_default = -1;
@@@ -291,6 -290,9 +291,6 @@@ int git_diff_ui_config(const char *var
                return 0;
        }
  
 -      if (git_diff_heuristic_config(var, value, cb) < 0)
 -              return -1;
 -
        if (!strcmp(var, "diff.wserrorhighlight")) {
                int val = parse_ws_error_highlight(value);
                if (val < 0)
@@@ -349,9 -351,6 +349,9 @@@ int git_diff_basic_config(const char *v
        if (starts_with(var, "submodule."))
                return parse_submodule_config_option(var, value);
  
 +      if (git_diff_heuristic_config(var, value, cb) < 0)
 +              return -1;
 +
        return git_default_config(var, value, cb);
  }
  
@@@ -1219,7 -1218,8 +1219,7 @@@ static void free_diff_words_data(struc
                        regfree(ecbdata->diff_words->word_regex);
                        free(ecbdata->diff_words->word_regex);
                }
 -              free(ecbdata->diff_words);
 -              ecbdata->diff_words = NULL;
 +              FREE_AND_NULL(ecbdata->diff_words);
        }
  }
  
@@@ -2095,7 -2095,7 +2095,7 @@@ static void show_dirstat_by_line(struc
                         * bytes per "line".
                         * This is stupid and ugly, but very cheap...
                         */
-                       damage = (damage + 63) / 64;
+                       damage = DIV_ROUND_UP(damage, 64);
                ALLOC_GROW(dir.files, dir.nr + 1, dir.alloc);
                dir.files[dir.nr].name = file->name;
                dir.files[dir.nr].changed = damage;
@@@ -2702,13 -2702,13 +2702,13 @@@ void free_filespec(struct diff_filespe
        }
  }
  
 -void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
 -                 int sha1_valid, unsigned short mode)
 +void fill_filespec(struct diff_filespec *spec, const struct object_id *oid,
 +                 int oid_valid, unsigned short mode)
  {
        if (mode) {
                spec->mode = canon_mode(mode);
 -              hashcpy(spec->oid.hash, sha1);
 -              spec->oid_valid = sha1_valid;
 +              oidcpy(&spec->oid, oid);
 +              spec->oid_valid = oid_valid;
        }
  }
  
   * the work tree has that object contents, return true, so that
   * prepare_temp_file() does not have to inflate and extract.
   */
 -static int reuse_worktree_file(const char *name, const unsigned char *sha1, int want_file)
 +static int reuse_worktree_file(const char *name, const struct object_id *oid, int want_file)
  {
        const struct cache_entry *ce;
        struct stat st;
         * objects however would tend to be slower as they need
         * to be individually opened and inflated.
         */
 -      if (!FAST_WORKING_DIRECTORY && !want_file && has_sha1_pack(sha1))
 +      if (!FAST_WORKING_DIRECTORY && !want_file && has_sha1_pack(oid->hash))
                return 0;
  
        /*
         * Similarly, if we'd have to convert the file contents anyway, that
         * makes the optimization not worthwhile.
         */
 -      if (!want_file && would_convert_to_git(name))
 +      if (!want_file && would_convert_to_git(&the_index, name))
                return 0;
  
        len = strlen(name);
         * This is not the sha1 we are looking for, or
         * unreusable because it is not a regular file.
         */
 -      if (hashcmp(sha1, ce->oid.hash) || !S_ISREG(ce->ce_mode))
 +      if (oidcmp(oid, &ce->oid) || !S_ISREG(ce->ce_mode))
                return 0;
  
        /*
@@@ -2842,7 -2842,7 +2842,7 @@@ int diff_populate_filespec(struct diff_
                return diff_populate_gitlink(s, size_only);
  
        if (!s->oid_valid ||
 -          reuse_worktree_file(s->path, s->oid.hash, 0)) {
 +          reuse_worktree_file(s->path, &s->oid, 0)) {
                struct strbuf buf = STRBUF_INIT;
                struct stat st;
                int fd;
                 * point if the path requires us to run the content
                 * conversion.
                 */
 -              if (size_only && !would_convert_to_git(s->path))
 +              if (size_only && !would_convert_to_git(&the_index, s->path))
                        return 0;
  
                /*
                /*
                 * Convert from working tree format to canonical git format
                 */
 -              if (convert_to_git(s->path, s->data, s->size, &buf, crlf_warn)) {
 +              if (convert_to_git(&the_index, s->path, s->data, s->size, &buf, crlf_warn)) {
                        size_t size = 0;
                        munmap(s->data, s->size);
                        s->should_munmap = 0;
@@@ -2951,7 -2951,8 +2951,7 @@@ void diff_free_filespec_blob(struct dif
  void diff_free_filespec_data(struct diff_filespec *s)
  {
        diff_free_filespec_blob(s);
 -      free(s->cnt_data);
 -      s->cnt_data = NULL;
 +      FREE_AND_NULL(s->cnt_data);
  }
  
  static void prep_temp_blob(const char *path, struct diff_tempfile *temp,
@@@ -3007,7 -3008,7 +3007,7 @@@ static struct diff_tempfile *prepare_te
  
        if (!S_ISGITLINK(one->mode) &&
            (!one->oid_valid ||
 -           reuse_worktree_file(name, one->oid.hash, 1))) {
 +           reuse_worktree_file(name, &one->oid, 1))) {
                struct stat st;
                if (lstat(name, &st) < 0) {
                        if (errno == ENOENT)
                        /* we can borrow from the file in the work tree */
                        temp->name = name;
                        if (!one->oid_valid)
 -                              sha1_to_hex_r(temp->hex, null_sha1);
 +                              oid_to_hex_r(temp->hex, &null_oid);
                        else
                                oid_to_hex_r(temp->hex, &one->oid);
                        /* Even though we may sometimes borrow the
                         * contents from the work tree, we always want
                         * one->mode.  mode is trustworthy even when
 -                       * !(one->sha1_valid), as long as
 +                       * !(one->oid_valid), as long as
                         * DIFF_FILE_VALID(one).
                         */
                        xsnprintf(temp->mode, sizeof(temp->mode), "%06o", one->mode);
@@@ -3238,7 -3239,7 +3238,7 @@@ static void run_diff_cmd(const char *pg
                fprintf(o->file, "* Unmerged path %s\n", name);
  }
  
 -static void diff_fill_sha1_info(struct diff_filespec *one)
 +static void diff_fill_oid_info(struct diff_filespec *one)
  {
        if (DIFF_FILE_VALID(one)) {
                if (!one->oid_valid) {
@@@ -3297,8 -3298,8 +3297,8 @@@ static void run_diff(struct diff_filepa
                return;
        }
  
 -      diff_fill_sha1_info(one);
 -      diff_fill_sha1_info(two);
 +      diff_fill_oid_info(one);
 +      diff_fill_oid_info(two);
  
        if (!pgm &&
            DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
@@@ -3343,8 -3344,8 +3343,8 @@@ static void run_diffstat(struct diff_fi
        if (o->prefix_length)
                strip_prefix(o->prefix_length, &name, &other);
  
 -      diff_fill_sha1_info(p->one);
 -      diff_fill_sha1_info(p->two);
 +      diff_fill_oid_info(p->one);
 +      diff_fill_oid_info(p->two);
  
        builtin_diffstat(name, other, p->one, p->two, diffstat, o, p);
  }
@@@ -3367,8 -3368,8 +3367,8 @@@ static void run_checkdiff(struct diff_f
        if (o->prefix_length)
                strip_prefix(o->prefix_length, &name, &other);
  
 -      diff_fill_sha1_info(p->one);
 -      diff_fill_sha1_info(p->two);
 +      diff_fill_oid_info(p->one);
 +      diff_fill_oid_info(p->two);
  
        builtin_checkdiff(name, other, attr_path, p->one, p->two, o);
  }
@@@ -4070,7 -4071,9 +4070,7 @@@ int diff_opt_parse(struct diff_options 
                DIFF_OPT_CLR(options, FUNCCONTEXT);
        else if ((argcount = parse_long_opt("output", av, &optarg))) {
                char *path = prefix_filename(prefix, optarg);
 -              options->file = fopen(path, "w");
 -              if (!options->file)
 -                      die_errno("Could not open '%s'", path);
 +              options->file = xfopen(path, "w");
                options->close_file = 1;
                if (options->use_color != GIT_COLOR_ALWAYS)
                        options->use_color = GIT_COLOR_NEVER;
@@@ -4581,7 -4584,7 +4581,7 @@@ static void patch_id_add_mode(git_SHA_C
  }
  
  /* returns 0 upon success, and writes result into sha1 */
 -static int diff_get_patch_id(struct diff_options *options, unsigned char *sha1, int diff_header_only)
 +static int diff_get_patch_id(struct diff_options *options, struct object_id *oid, int diff_header_only)
  {
        struct diff_queue_struct *q = &diff_queued_diff;
        int i;
                if (DIFF_PAIR_UNMERGED(p))
                        continue;
  
 -              diff_fill_sha1_info(p->one);
 -              diff_fill_sha1_info(p->two);
 +              diff_fill_oid_info(p->one);
 +              diff_fill_oid_info(p->two);
  
                len1 = remove_space(p->one->path, strlen(p->one->path));
                len2 = remove_space(p->two->path, strlen(p->two->path));
                if (diff_filespec_is_binary(p->one) ||
                    diff_filespec_is_binary(p->two)) {
                        git_SHA1_Update(&ctx, oid_to_hex(&p->one->oid),
 -                                      40);
 +                                      GIT_SHA1_HEXSZ);
                        git_SHA1_Update(&ctx, oid_to_hex(&p->two->oid),
 -                                      40);
 +                                      GIT_SHA1_HEXSZ);
                        continue;
                }
  
                                     p->one->path);
        }
  
 -      git_SHA1_Final(sha1, &ctx);
 +      git_SHA1_Final(oid->hash, &ctx);
        return 0;
  }
  
 -int diff_flush_patch_id(struct diff_options *options, unsigned char *sha1, int diff_header_only)
 +int diff_flush_patch_id(struct diff_options *options, struct object_id *oid, int diff_header_only)
  {
        struct diff_queue_struct *q = &diff_queued_diff;
        int i;
 -      int result = diff_get_patch_id(options, sha1, diff_header_only);
 +      int result = diff_get_patch_id(options, oid, diff_header_only);
  
        for (i = 0; i < q->nr; i++)
                diff_free_filepair(q->queue[i]);
@@@ -4804,7 -4807,9 +4804,7 @@@ void diff_flush(struct diff_options *op
                 */
                if (options->close_file)
                        fclose(options->file);
 -              options->file = fopen("/dev/null", "w");
 -              if (!options->file)
 -                      die_errno("Could not open /dev/null");
 +              options->file = xfopen("/dev/null", "w");
                options->close_file = 1;
                for (i = 0; i < q->nr; i++) {
                        struct diff_filepair *p = q->queue[i];
@@@ -5076,8 -5081,8 +5076,8 @@@ static int is_submodule_ignored(const c
  
  void diff_addremove(struct diff_options *options,
                    int addremove, unsigned mode,
 -                  const unsigned char *sha1,
 -                  int sha1_valid,
 +                  const struct object_id *oid,
 +                  int oid_valid,
                    const char *concatpath, unsigned dirty_submodule)
  {
        struct diff_filespec *one, *two;
        two = alloc_filespec(concatpath);
  
        if (addremove != '+')
 -              fill_filespec(one, sha1, sha1_valid, mode);
 +              fill_filespec(one, oid, oid_valid, mode);
        if (addremove != '-') {
 -              fill_filespec(two, sha1, sha1_valid, mode);
 +              fill_filespec(two, oid, oid_valid, mode);
                two->dirty_submodule = dirty_submodule;
        }
  
  
  void diff_change(struct diff_options *options,
                 unsigned old_mode, unsigned new_mode,
 -               const unsigned char *old_sha1,
 -               const unsigned char *new_sha1,
 -               int old_sha1_valid, int new_sha1_valid,
 +               const struct object_id *old_oid,
 +               const struct object_id *new_oid,
 +               int old_oid_valid, int new_oid_valid,
                 const char *concatpath,
                 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
  {
  
        if (DIFF_OPT_TST(options, REVERSE_DIFF)) {
                SWAP(old_mode, new_mode);
 -              SWAP(old_sha1, new_sha1);
 -              SWAP(old_sha1_valid, new_sha1_valid);
 +              SWAP(old_oid, new_oid);
 +              SWAP(old_oid_valid, new_oid_valid);
                SWAP(old_dirty_submodule, new_dirty_submodule);
        }
  
  
        one = alloc_filespec(concatpath);
        two = alloc_filespec(concatpath);
 -      fill_filespec(one, old_sha1, old_sha1_valid, old_mode);
 -      fill_filespec(two, new_sha1, new_sha1_valid, new_mode);
 +      fill_filespec(one, old_oid, old_oid_valid, old_mode);
 +      fill_filespec(two, new_oid, new_oid_valid, new_mode);
        one->dirty_submodule = old_dirty_submodule;
        two->dirty_submodule = new_dirty_submodule;
        p = diff_queue(&diff_queued_diff, one, two);
@@@ -5239,7 -5244,7 +5239,7 @@@ size_t fill_textconv(struct userdiff_dr
  
        if (driver->textconv_cache && df->oid_valid) {
                *outbuf = notes_cache_get(driver->textconv_cache,
 -                                        df->oid.hash,
 +                                        &df->oid,
                                          &size);
                if (*outbuf)
                        return size;
  
        if (driver->textconv_cache && df->oid_valid) {
                /* ignore errors, as we might be in a readonly repository */
 -              notes_cache_put(driver->textconv_cache, df->oid.hash, *outbuf,
 +              notes_cache_put(driver->textconv_cache, &df->oid, *outbuf,
                                size);
                /*
                 * we could save up changes and flush them all at the end,
        return size;
  }
  
 +int textconv_object(const char *path,
 +                  unsigned mode,
 +                  const struct object_id *oid,
 +                  int oid_valid,
 +                  char **buf,
 +                  unsigned long *buf_size)
 +{
 +      struct diff_filespec *df;
 +      struct userdiff_driver *textconv;
 +
 +      df = alloc_filespec(path);
 +      fill_filespec(df, oid, oid_valid, mode);
 +      textconv = get_textconv(df);
 +      if (!textconv) {
 +              free_filespec(df);
 +              return 0;
 +      }
 +
 +      *buf_size = fill_textconv(textconv, df, buf);
 +      free_filespec(df);
 +      return 1;
 +}
 +
  void setup_diff_pager(struct diff_options *opt)
  {
        /*
diff --combined imap-send.c
index 351e84aea172415c94d7159c47b58abf508e0576,37388f142d035f51284ed30eacac792177713697..b2d0b849bb20c88532dd2952a4a5ae94204ccad8
@@@ -23,7 -23,6 +23,7 @@@
   */
  
  #include "cache.h"
 +#include "config.h"
  #include "credential.h"
  #include "exec_cmd.h"
  #include "run-command.h"
@@@ -777,7 -776,8 +777,7 @@@ static int get_cmd_result(struct imap_s
                               offsetof(struct imap_cmd, next));
                        if (cmdp->cb.data) {
                                n = socket_write(&imap->buf.sock, cmdp->cb.data, cmdp->cb.dlen);
 -                              free(cmdp->cb.data);
 -                              cmdp->cb.data = NULL;
 +                              FREE_AND_NULL(cmdp->cb.data);
                                if (n != (int)cmdp->cb.dlen)
                                        return RESP_BAD;
                        } else if (cmdp->cb.cont) {
@@@ -861,7 -861,7 +861,7 @@@ static char hexchar(unsigned int b
        return b < 10 ? '0' + b : 'a' + (b - 10);
  }
  
- #define ENCODED_SIZE(n) (4*((n+2)/3))
+ #define ENCODED_SIZE(n) (4 * DIV_ROUND_UP((n), 3))
  static char *cram(const char *challenge_64, const char *user, const char *pass)
  {
        int i, resp_len, encoded_len, decoded_len;
diff --combined sha1_name.c
index e7f7b12cebfb4060a5d3e6567029bb308c16f61f,f7b6242914be6306f74e038c3638ef98ac24f7e9..74fcb6d788f4cb591a9f7d3c6a2692f5c6d84022
@@@ -1,5 -1,4 +1,5 @@@
  #include "cache.h"
 +#include "config.h"
  #include "tag.h"
  #include "commit.h"
  #include "tree.h"
@@@ -78,19 -77,10 +78,19 @@@ static void update_candidates(struct di
        /* otherwise, current can be discarded and candidate is still good */
  }
  
 +static int append_loose_object(const struct object_id *oid, const char *path,
 +                             void *data)
 +{
 +      oid_array_append(data, oid);
 +      return 0;
 +}
 +
 +static int match_sha(unsigned, const unsigned char *, const unsigned char *);
 +
  static void find_short_object_filename(struct disambiguate_state *ds)
  {
 +      int subdir_nr = ds->bin_pfx.hash[0];
        struct alternate_object_database *alt;
 -      char hex[GIT_MAX_HEXSZ];
        static struct alternate_object_database *fakeent;
  
        if (!fakeent) {
        }
        fakeent->next = alt_odb_list;
  
 -      xsnprintf(hex, sizeof(hex), "%.2s", ds->hex_pfx);
        for (alt = fakeent; alt && !ds->ambiguous; alt = alt->next) {
 -              struct strbuf *buf = alt_scratch_buf(alt);
 -              struct dirent *de;
 -              DIR *dir;
 -
 -              strbuf_addf(buf, "%.2s/", ds->hex_pfx);
 -              dir = opendir(buf->buf);
 -              if (!dir)
 -                      continue;
 -
 -              while (!ds->ambiguous && (de = readdir(dir)) != NULL) {
 -                      struct object_id oid;
 +              int pos;
 +
 +              if (!alt->loose_objects_subdir_seen[subdir_nr]) {
 +                      struct strbuf *buf = alt_scratch_buf(alt);
 +                      for_each_file_in_obj_subdir(subdir_nr, buf,
 +                                                  append_loose_object,
 +                                                  NULL, NULL,
 +                                                  &alt->loose_objects_cache);
 +                      alt->loose_objects_subdir_seen[subdir_nr] = 1;
 +              }
  
 -                      if (strlen(de->d_name) != GIT_SHA1_HEXSZ - 2)
 -                              continue;
 -                      if (memcmp(de->d_name, ds->hex_pfx + 2, ds->len - 2))
 -                              continue;
 -                      memcpy(hex + 2, de->d_name, GIT_SHA1_HEXSZ - 2);
 -                      if (!get_oid_hex(hex, &oid))
 -                              update_candidates(ds, &oid);
 +              pos = oid_array_lookup(&alt->loose_objects_cache, &ds->bin_pfx);
 +              if (pos < 0)
 +                      pos = -1 - pos;
 +              while (!ds->ambiguous && pos < alt->loose_objects_cache.nr) {
 +                      const struct object_id *oid;
 +                      oid = alt->loose_objects_cache.oid + pos;
 +                      if (!match_sha(ds->len, ds->bin_pfx.hash, oid->hash))
 +                              break;
 +                      update_candidates(ds, oid);
 +                      pos++;
                }
 -              closedir(dir);
        }
  }
  
@@@ -251,7 -241,7 +251,7 @@@ static int disambiguate_committish_only
                return 0;
  
        /* We need to do this the hard way... */
 -      obj = deref_tag(parse_object(oid->hash), NULL, 0);
 +      obj = deref_tag(parse_object(oid), NULL, 0);
        if (obj && obj->type == OBJ_COMMIT)
                return 1;
        return 0;
@@@ -275,7 -265,7 +275,7 @@@ static int disambiguate_treeish_only(co
                return 0;
  
        /* We need to do this the hard way... */
 -      obj = deref_tag(parse_object(oid->hash), NULL, 0);
 +      obj = deref_tag(parse_object(oid), NULL, 0);
        if (obj && (obj->type == OBJ_TREE || obj->type == OBJ_COMMIT))
                return 1;
        return 0;
@@@ -364,14 -354,14 +364,14 @@@ static int show_ambiguous_object(const 
  
        type = sha1_object_info(oid->hash, NULL);
        if (type == OBJ_COMMIT) {
 -              struct commit *commit = lookup_commit(oid->hash);
 +              struct commit *commit = lookup_commit(oid);
                if (commit) {
                        struct pretty_print_context pp = {0};
                        pp.date_mode.type = DATE_SHORT;
                        format_commit_message(commit, " %ad - %s", &desc, &pp);
                }
        } else if (type == OBJ_TAG) {
 -              struct tag *tag = lookup_tag(oid->hash);
 +              struct tag *tag = lookup_tag(oid);
                if (!parse_tag(tag) && tag->tag)
                        strbuf_addf(&desc, " %s", tag->tag);
        }
@@@ -489,10 -479,9 +489,9 @@@ int find_unique_abbrev_r(char *hex, con
                 * We now know we have on the order of 2^len objects, which
                 * expects a collision at 2^(len/2). But we also care about hex
                 * chars, not bits, and there are 4 bits per hex. So all
-                * together we need to divide by 2; but we also want to round
-                * odd numbers up, hence adding one before dividing.
+                * together we need to divide by 2 and round up.
                 */
-               len = (len + 1) / 2;
+               len = DIV_ROUND_UP(len, 2);
                /*
                 * For very small repos, we stick with our regular fallback.
                 */
@@@ -670,8 -659,8 +669,8 @@@ static int get_sha1_basic(const char *s
  
        if (reflog_len) {
                int nth, i;
 -              unsigned long at_time;
 -              unsigned long co_time;
 +              timestamp_t at_time;
 +              timestamp_t co_time;
                int co_tz, co_cnt;
  
                /* Is it asking for N-th entry, or approxidate? */
  static int get_parent(const char *name, int len,
                      unsigned char *result, int idx)
  {
 -      unsigned char sha1[20];
 -      int ret = get_sha1_1(name, len, sha1, GET_SHA1_COMMITTISH);
 +      struct object_id oid;
 +      int ret = get_sha1_1(name, len, oid.hash, GET_SHA1_COMMITTISH);
        struct commit *commit;
        struct commit_list *p;
  
        if (ret)
                return ret;
 -      commit = lookup_commit_reference(sha1);
 +      commit = lookup_commit_reference(&oid);
        if (parse_commit(commit))
                return -1;
        if (!idx) {
  static int get_nth_ancestor(const char *name, int len,
                            unsigned char *result, int generation)
  {
 -      unsigned char sha1[20];
 +      struct object_id oid;
        struct commit *commit;
        int ret;
  
 -      ret = get_sha1_1(name, len, sha1, GET_SHA1_COMMITTISH);
 +      ret = get_sha1_1(name, len, oid.hash, GET_SHA1_COMMITTISH);
        if (ret)
                return ret;
 -      commit = lookup_commit_reference(sha1);
 +      commit = lookup_commit_reference(&oid);
        if (!commit)
                return -1;
  
@@@ -786,7 -775,7 +785,7 @@@ struct object *peel_to_type(const char 
        if (name && !namelen)
                namelen = strlen(name);
        while (1) {
 -              if (!o || (!o->parsed && !parse_object(o->oid.hash)))
 +              if (!o || (!o->parsed && !parse_object(&o->oid)))
                        return NULL;
                if (expected_type == OBJ_ANY || o->type == expected_type)
                        return o;
  static int peel_onion(const char *name, int len, unsigned char *sha1,
                      unsigned lookup_flags)
  {
 -      unsigned char outer[20];
 +      struct object_id outer;
        const char *sp;
        unsigned int expected_type = 0;
        struct object *o;
        else if (expected_type == OBJ_TREE)
                lookup_flags |= GET_SHA1_TREEISH;
  
 -      if (get_sha1_1(name, sp - name - 2, outer, lookup_flags))
 +      if (get_sha1_1(name, sp - name - 2, outer.hash, lookup_flags))
                return -1;
  
 -      o = parse_object(outer);
 +      o = parse_object(&outer);
        if (!o)
                return -1;
        if (!expected_type) {
                o = deref_tag(o, name, sp - name - 2);
 -              if (!o || (!o->parsed && !parse_object(o->oid.hash)))
 +              if (!o || (!o->parsed && !parse_object(&o->oid)))
                        return -1;
                hashcpy(sha1, o->oid.hash);
                return 0;
@@@ -991,7 -980,7 +990,7 @@@ static int handle_one_ref(const char *p
                          int flag, void *cb_data)
  {
        struct commit_list **list = cb_data;
 -      struct object *object = parse_object(oid->hash);
 +      struct object *object = parse_object(oid);
        if (!object)
                return 0;
        if (object->type == OBJ_TAG) {
@@@ -1037,7 -1026,7 +1036,7 @@@ static int get_sha1_oneline(const char 
                int matches;
  
                commit = pop_most_recent_commit(&list, ONELINE_SEEN);
 -              if (!parse_object(commit->object.oid.hash))
 +              if (!parse_object(&commit->object.oid))
                        continue;
                buf = get_commit_buffer(commit, NULL);
                p = strstr(buf, "\n\n");
@@@ -1064,7 -1053,7 +1063,7 @@@ struct grab_nth_branch_switch_cbdata 
  };
  
  static int grab_nth_branch_switch(struct object_id *ooid, struct object_id *noid,
 -                                const char *email, unsigned long timestamp, int tz,
 +                                const char *email, timestamp_t timestamp, int tz,
                                  const char *message, void *cb_data)
  {
        struct grab_nth_branch_switch_cbdata *cb = cb_data;
@@@ -1146,13 -1135,13 +1145,13 @@@ int get_oid_mb(const char *name, struc
        }
        if (st)
                return st;
 -      one = lookup_commit_reference_gently(oid_tmp.hash, 0);
 +      one = lookup_commit_reference_gently(&oid_tmp, 0);
        if (!one)
                return -1;
  
        if (get_sha1_committish(dots[3] ? (dots + 3) : "HEAD", oid_tmp.hash))
                return -1;
 -      two = lookup_commit_reference_gently(oid_tmp.hash, 0);
 +      two = lookup_commit_reference_gently(&oid_tmp, 0);
        if (!two)
                return -1;
        mbs = get_merge_bases(one, two);
@@@ -1418,7 -1407,7 +1417,7 @@@ static void diagnose_invalid_sha1_path(
        if (file_exists(filename))
                die("Path '%s' exists on disk, but not in '%.*s'.",
                    filename, object_name_len, object_name);
 -      if (errno == ENOENT || errno == ENOTDIR) {
 +      if (is_missing_file_error(errno)) {
                char *fullname = xstrfmt("%s%s", prefix, filename);
  
                if (!get_tree_entry(tree_sha1, fullname,
@@@ -1483,7 -1472,7 +1482,7 @@@ static void diagnose_invalid_index_path
  
        if (file_exists(filename))
                die("Path '%s' exists on disk, but not in the index.", filename);
 -      if (errno == ENOENT || errno == ENOTDIR)
 +      if (is_missing_file_error(errno))
                die("Path '%s' does not exist (neither on disk nor in the index).",
                    filename);
  
diff --combined shallow.c
index ef7ca78993df20082c858a7d2b8c7094fcbd1590,1aee070c4da64f7f03473bc812c6c1e942944a37..54359d549075b5cfe1670ca979ff3cf5eeedc9d1
+++ b/shallow.c
@@@ -27,13 -27,13 +27,13 @@@ void set_alternate_shallow_file(const c
        alternate_shallow_file = xstrdup_or_null(path);
  }
  
 -int register_shallow(const unsigned char *sha1)
 +int register_shallow(const struct object_id *oid)
  {
        struct commit_graft *graft =
                xmalloc(sizeof(struct commit_graft));
 -      struct commit *commit = lookup_commit(sha1);
 +      struct commit *commit = lookup_commit(oid);
  
 -      hashcpy(graft->oid.hash, sha1);
 +      oidcpy(&graft->oid, oid);
        graft->nr_parent = -1;
        if (commit && commit->object.parsed)
                commit->parents = NULL;
@@@ -65,10 -65,10 +65,10 @@@ int is_repository_shallow(void
        is_shallow = 1;
  
        while (fgets(buf, sizeof(buf), fp)) {
 -              unsigned char sha1[20];
 -              if (get_sha1_hex(buf, sha1))
 +              struct object_id oid;
 +              if (get_oid_hex(buf, &oid))
                        die("bad shallow line: %s", buf);
 -              register_shallow(sha1);
 +              register_shallow(&oid);
        }
        fclose(fp);
        return is_shallow;
@@@ -241,7 -241,7 +241,7 @@@ static int write_one_shallow(const stru
        if (graft->nr_parent != -1)
                return 0;
        if (data->flags & SEEN_ONLY) {
 -              struct commit *c = lookup_commit(graft->oid.hash);
 +              struct commit *c = lookup_commit(&graft->oid);
                if (!c || !(c->object.flags & SEEN)) {
                        if (data->flags & VERBOSE)
                                printf("Removing %s from .git/shallow\n",
@@@ -443,7 -443,7 +443,7 @@@ struct paint_info 
  
  static uint32_t *paint_alloc(struct paint_info *info)
  {
-       unsigned nr = (info->nr_bits + 31) / 32;
+       unsigned nr = DIV_ROUND_UP(info->nr_bits, 32);
        unsigned size = nr * sizeof(uint32_t);
        void *p;
        if (!info->pool_count || size > info->end - info->free) {
   * UNINTERESTING or BOTTOM is hit. Set the id-th bit in ref_bitmap for
   * all walked commits.
   */
 -static void paint_down(struct paint_info *info, const unsigned char *sha1,
 +static void paint_down(struct paint_info *info, const struct object_id *oid,
                       unsigned int id)
  {
        unsigned int i, nr;
        struct commit_list *head = NULL;
-       int bitmap_nr = (info->nr_bits + 31) / 32;
+       int bitmap_nr = DIV_ROUND_UP(info->nr_bits, 32);
        size_t bitmap_size = st_mult(sizeof(uint32_t), bitmap_nr);
 -      struct commit *c = lookup_commit_reference_gently(sha1, 1);
 +      struct commit *c = lookup_commit_reference_gently(oid, 1);
        uint32_t *tmp; /* to be freed before return */
        uint32_t *bitmap;
  
  static int mark_uninteresting(const char *refname, const struct object_id *oid,
                              int flags, void *cb_data)
  {
 -      struct commit *commit = lookup_commit_reference_gently(oid->hash, 1);
 +      struct commit *commit = lookup_commit_reference_gently(oid, 1);
        if (!commit)
                return 0;
        commit->object.flags |= UNINTERESTING;
@@@ -603,18 -603,18 +603,18 @@@ void assign_shallow_commits_to_refs(str
  
        /* Mark potential bottoms so we won't go out of bound */
        for (i = 0; i < nr_shallow; i++) {
 -              struct commit *c = lookup_commit(oid[shallow[i]].hash);
 +              struct commit *c = lookup_commit(&oid[shallow[i]]);
                c->object.flags |= BOTTOM;
        }
  
        for (i = 0; i < ref->nr; i++)
 -              paint_down(&pi, ref->oid[i].hash, i);
 +              paint_down(&pi, ref->oid + i, i);
  
        if (used) {
-               int bitmap_size = ((pi.nr_bits + 31) / 32) * sizeof(uint32_t);
+               int bitmap_size = DIV_ROUND_UP(pi.nr_bits, 32) * sizeof(uint32_t);
                memset(used, 0, sizeof(*used) * info->shallow->nr);
                for (i = 0; i < nr_shallow; i++) {
 -                      const struct commit *c = lookup_commit(oid[shallow[i]].hash);
 +                      const struct commit *c = lookup_commit(&oid[shallow[i]]);
                        uint32_t **map = ref_bitmap_at(&pi.ref_bitmap, c);
                        if (*map)
                                used[shallow[i]] = xmemdupz(*map, bitmap_size);
@@@ -645,7 -645,7 +645,7 @@@ static int add_ref(const char *refname
  {
        struct commit_array *ca = cb_data;
        ALLOC_GROW(ca->commits, ca->nr + 1, ca->alloc);
 -      ca->commits[ca->nr] = lookup_commit_reference_gently(oid->hash, 1);
 +      ca->commits[ca->nr] = lookup_commit_reference_gently(oid, 1);
        if (ca->commits[ca->nr])
                ca->nr++;
        return 0;
@@@ -672,7 -672,7 +672,7 @@@ static void post_assign_shallow(struct 
        struct commit *c;
        uint32_t **bitmap;
        int dst, i, j;
-       int bitmap_nr = (info->ref->nr + 31) / 32;
+       int bitmap_nr = DIV_ROUND_UP(info->ref->nr, 32);
        struct commit_array ca;
  
        trace_printf_key(&trace_shallow, "shallow: post_assign_shallow\n");
        for (i = dst = 0; i < info->nr_theirs; i++) {
                if (i != dst)
                        info->theirs[dst] = info->theirs[i];
 -              c = lookup_commit(oid[info->theirs[i]].hash);
 +              c = lookup_commit(&oid[info->theirs[i]]);
                bitmap = ref_bitmap_at(ref_bitmap, c);
                if (!*bitmap)
                        continue;
        for (i = dst = 0; i < info->nr_ours; i++) {
                if (i != dst)
                        info->ours[dst] = info->ours[i];
 -              c = lookup_commit(oid[info->ours[i]].hash);
 +              c = lookup_commit(&oid[info->ours[i]]);
                bitmap = ref_bitmap_at(ref_bitmap, c);
                if (!*bitmap)
                        continue;
  int delayed_reachability_test(struct shallow_info *si, int c)
  {
        if (si->need_reachability_test[c]) {
 -              struct commit *commit = lookup_commit(si->shallow->oid[c].hash);
 +              struct commit *commit = lookup_commit(&si->shallow->oid[c]);
  
                if (!si->commits) {
                        struct commit_array ca;