Merge branch 'bw/git-clang-format'
[gitweb.git] / shallow.c
index 11f7dde9d910093dce6a767150bb4823bff80200..df4d44ea7a34a6d6bef119d1494f1c055614cbb1 100644 (file)
--- a/shallow.c
+++ b/shallow.c
@@ -27,13 +27,13 @@ void set_alternate_shallow_file(const char *path, int override)
        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 @@ 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;
@@ -99,7 +99,7 @@ struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
                                cur_depth = 0;
                        } else {
                                commit = (struct commit *)
-                                       stack.objects[--stack.nr].item;
+                                       object_array_pop(&stack);
                                cur_depth = *(int *)commit->util;
                        }
                }
@@ -107,7 +107,7 @@ struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
                cur_depth++;
                if ((depth != INFINITE_DEPTH && cur_depth >= depth) ||
                    (is_repository_shallow() && !commit->parents &&
-                    (graft = lookup_commit_graft(commit->object.oid.hash)) != NULL &&
+                    (graft = lookup_commit_graft(&commit->object.oid)) != NULL &&
                     graft->nr_parent < 0)) {
                        commit_list_insert(commit, &result);
                        commit->object.flags |= shallow_flag;
@@ -241,7 +241,7 @@ static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
        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",
@@ -260,7 +260,7 @@ static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
 }
 
 static int write_shallow_commits_1(struct strbuf *out, int use_pack_protocol,
-                                  const struct sha1_array *extra,
+                                  const struct oid_array *extra,
                                   unsigned flags)
 {
        struct write_shallow_data data;
@@ -273,7 +273,7 @@ static int write_shallow_commits_1(struct strbuf *out, int use_pack_protocol,
        if (!extra)
                return data.count;
        for (i = 0; i < extra->nr; i++) {
-               strbuf_addstr(out, sha1_to_hex(extra->sha1[i]));
+               strbuf_addstr(out, oid_to_hex(extra->oid + i));
                strbuf_addch(out, '\n');
                data.count++;
        }
@@ -281,38 +281,36 @@ static int write_shallow_commits_1(struct strbuf *out, int use_pack_protocol,
 }
 
 int write_shallow_commits(struct strbuf *out, int use_pack_protocol,
-                         const struct sha1_array *extra)
+                         const struct oid_array *extra)
 {
        return write_shallow_commits_1(out, use_pack_protocol, extra, 0);
 }
 
-static struct tempfile temporary_shallow;
-
-const char *setup_temporary_shallow(const struct sha1_array *extra)
+const char *setup_temporary_shallow(const struct oid_array *extra)
 {
+       struct tempfile *temp;
        struct strbuf sb = STRBUF_INIT;
-       int fd;
 
        if (write_shallow_commits(&sb, 0, extra)) {
-               fd = xmks_tempfile(&temporary_shallow, git_path("shallow_XXXXXX"));
+               temp = xmks_tempfile(git_path("shallow_XXXXXX"));
 
-               if (write_in_full(fd, sb.buf, sb.len) != sb.len)
+               if (write_in_full(temp->fd, sb.buf, sb.len) < 0 ||
+                   close_tempfile_gently(temp) < 0)
                        die_errno("failed to write to %s",
-                                 get_tempfile_path(&temporary_shallow));
-               close_tempfile(&temporary_shallow);
+                                 get_tempfile_path(temp));
                strbuf_release(&sb);
-               return get_tempfile_path(&temporary_shallow);
+               return get_tempfile_path(temp);
        }
        /*
         * is_repository_shallow() sees empty string as "no shallow
         * file".
         */
-       return get_tempfile_path(&temporary_shallow);
+       return "";
 }
 
 void setup_alternate_shallow(struct lock_file *shallow_lock,
                             const char **alternate_shallow_file,
-                            const struct sha1_array *extra)
+                            const struct oid_array *extra)
 {
        struct strbuf sb = STRBUF_INIT;
        int fd;
@@ -321,7 +319,7 @@ void setup_alternate_shallow(struct lock_file *shallow_lock,
                                       LOCK_DIE_ON_ERROR);
        check_shallow_file_for_update();
        if (write_shallow_commits(&sb, 0, extra)) {
-               if (write_in_full(fd, sb.buf, sb.len) != sb.len)
+               if (write_in_full(fd, sb.buf, sb.len) < 0)
                        die_errno("failed to write to %s",
                                  get_lock_file_path(shallow_lock));
                *alternate_shallow_file = get_lock_file_path(shallow_lock);
@@ -368,7 +366,7 @@ void prune_shallow(int show_only)
                                       LOCK_DIE_ON_ERROR);
        check_shallow_file_for_update();
        if (write_shallow_commits_1(&sb, 0, NULL, SEEN_ONLY)) {
-               if (write_in_full(fd, sb.buf, sb.len) != sb.len)
+               if (write_in_full(fd, sb.buf, sb.len) < 0)
                        die_errno("failed to write to %s",
                                  get_lock_file_path(&shallow_lock));
                commit_lock_file(&shallow_lock);
@@ -385,7 +383,7 @@ struct trace_key trace_shallow = TRACE_KEY_INIT(SHALLOW);
  * Step 1, split sender shallow commits into "ours" and "theirs"
  * Step 2, clean "ours" based on .git/shallow
  */
-void prepare_shallow_info(struct shallow_info *info, struct sha1_array *sa)
+void prepare_shallow_info(struct shallow_info *info, struct oid_array *sa)
 {
        int i;
        trace_printf_key(&trace_shallow, "shallow: prepare_shallow_info\n");
@@ -396,9 +394,9 @@ void prepare_shallow_info(struct shallow_info *info, struct sha1_array *sa)
        ALLOC_ARRAY(info->ours, sa->nr);
        ALLOC_ARRAY(info->theirs, sa->nr);
        for (i = 0; i < sa->nr; i++) {
-               if (has_sha1_file(sa->sha1[i])) {
+               if (has_object_file(sa->oid + i)) {
                        struct commit_graft *graft;
-                       graft = lookup_commit_graft(sa->sha1[i]);
+                       graft = lookup_commit_graft(&sa->oid[i]);
                        if (graft && graft->nr_parent < 0)
                                continue;
                        info->ours[info->nr_ours++] = i;
@@ -417,13 +415,13 @@ void clear_shallow_info(struct shallow_info *info)
 
 void remove_nonexistent_theirs_shallow(struct shallow_info *info)
 {
-       unsigned char (*sha1)[20] = info->shallow->sha1;
+       struct object_id *oid = info->shallow->oid;
        int i, dst;
        trace_printf_key(&trace_shallow, "shallow: remove_nonexistent_theirs_shallow\n");
        for (i = dst = 0; i < info->nr_theirs; i++) {
                if (i != dst)
                        info->theirs[dst] = info->theirs[i];
-               if (has_sha1_file(sha1[info->theirs[i]]))
+               if (has_object_file(oid + info->theirs[i]))
                        dst++;
        }
        info->nr_theirs = dst;
@@ -443,7 +441,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) {
@@ -466,18 +464,22 @@ static uint32_t *paint_alloc(struct paint_info *info)
  * 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);
-       uint32_t *tmp = xmalloc(bitmap_size); /* to be freed before return */
-       uint32_t *bitmap = paint_alloc(info);
-       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;
+
        if (!c)
                return;
+
+       tmp = xmalloc(bitmap_size);
+       bitmap = paint_alloc(info);
        memset(bitmap, 0, bitmap_size);
        bitmap[id / 32] |= (1U << (id % 32));
        commit_list_insert(c, &head);
@@ -531,7 +533,7 @@ static void paint_down(struct paint_info *info, const unsigned char *sha1,
 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;
@@ -559,8 +561,8 @@ static void post_assign_shallow(struct shallow_info *info,
 void assign_shallow_commits_to_refs(struct shallow_info *info,
                                    uint32_t **used, int *ref_status)
 {
-       unsigned char (*sha1)[20] = info->shallow->sha1;
-       struct sha1_array *ref = info->ref;
+       struct object_id *oid = info->shallow->oid;
+       struct oid_array *ref = info->ref;
        unsigned int i, nr;
        int *shallow, nr_shallow = 0;
        struct paint_info pi;
@@ -599,18 +601,18 @@ void assign_shallow_commits_to_refs(struct shallow_info *info,
 
        /* Mark potential bottoms so we won't go out of bound */
        for (i = 0; i < nr_shallow; i++) {
-               struct commit *c = lookup_commit(sha1[shallow[i]]);
+               struct commit *c = lookup_commit(&oid[shallow[i]]);
                c->object.flags |= BOTTOM;
        }
 
        for (i = 0; i < ref->nr; i++)
-               paint_down(&pi, ref->sha1[i], 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(sha1[shallow[i]]);
+                       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);
@@ -641,7 +643,7 @@ static int add_ref(const char *refname, const struct object_id *oid,
 {
        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;
@@ -664,11 +666,11 @@ static void post_assign_shallow(struct shallow_info *info,
                                struct ref_bitmap *ref_bitmap,
                                int *ref_status)
 {
-       unsigned char (*sha1)[20] = info->shallow->sha1;
+       struct object_id *oid = info->shallow->oid;
        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");
@@ -679,7 +681,7 @@ static void post_assign_shallow(struct shallow_info *info,
        for (i = dst = 0; i < info->nr_theirs; i++) {
                if (i != dst)
                        info->theirs[dst] = info->theirs[i];
-               c = lookup_commit(sha1[info->theirs[i]]);
+               c = lookup_commit(&oid[info->theirs[i]]);
                bitmap = ref_bitmap_at(ref_bitmap, c);
                if (!*bitmap)
                        continue;
@@ -700,7 +702,7 @@ static void post_assign_shallow(struct shallow_info *info,
        for (i = dst = 0; i < info->nr_ours; i++) {
                if (i != dst)
                        info->ours[dst] = info->ours[i];
-               c = lookup_commit(sha1[info->ours[i]]);
+               c = lookup_commit(&oid[info->ours[i]]);
                bitmap = ref_bitmap_at(ref_bitmap, c);
                if (!*bitmap)
                        continue;
@@ -722,7 +724,7 @@ static void post_assign_shallow(struct shallow_info *info,
 int delayed_reachability_test(struct shallow_info *si, int c)
 {
        if (si->need_reachability_test[c]) {
-               struct commit *commit = lookup_commit(si->shallow->sha1[c]);
+               struct commit *commit = lookup_commit(&si->shallow->oid[c]);
 
                if (!si->commits) {
                        struct commit_array ca;