Merge branch 'jc/clean-after-sanity-tests'
[gitweb.git] / shallow.c
index e8429a9a845f98f4a73299c044463ce99ebbea93..79439a818f52d5c783fbf003a7b9fa90bb0d1993 100644 (file)
--- a/shallow.c
+++ b/shallow.c
@@ -12,6 +12,7 @@
 #include "commit-slab.h"
 #include "revision.h"
 #include "list-objects.h"
+#include "commit-slab.h"
 
 static int is_shallow = -1;
 static struct stat_validity shallow_stat;
@@ -20,7 +21,7 @@ static char *alternate_shallow_file;
 void set_alternate_shallow_file(const char *path, int override)
 {
        if (is_shallow != -1)
-               die("BUG: is_repository_shallow must not be called before set_alternate_shallow_file");
+               BUG("is_repository_shallow must not be called before set_alternate_shallow_file");
        if (alternate_shallow_file && !override)
                return;
        free(alternate_shallow_file);
@@ -74,6 +75,11 @@ int is_repository_shallow(void)
        return is_shallow;
 }
 
+/*
+ * TODO: use "int" elemtype instead of "int *" when/if commit-slab
+ * supports a "valid" flag.
+ */
+define_commit_slab(commit_depth, int *);
 struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
                int shallow_flag, int not_shallow_flag)
 {
@@ -82,32 +88,36 @@ struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
        struct object_array stack = OBJECT_ARRAY_INIT;
        struct commit *commit = NULL;
        struct commit_graft *graft;
+       struct commit_depth depths;
 
+       init_commit_depth(&depths);
        while (commit || i < heads->nr || stack.nr) {
                struct commit_list *p;
                if (!commit) {
                        if (i < heads->nr) {
+                               int **depth_slot;
                                commit = (struct commit *)
                                        deref_tag(heads->objects[i++].item, NULL, 0);
                                if (!commit || commit->object.type != OBJ_COMMIT) {
                                        commit = NULL;
                                        continue;
                                }
-                               if (!commit->util)
-                                       commit->util = xmalloc(sizeof(int));
-                               *(int *)commit->util = 0;
+                               depth_slot = commit_depth_at(&depths, commit);
+                               if (!*depth_slot)
+                                       *depth_slot = xmalloc(sizeof(int));
+                               **depth_slot = 0;
                                cur_depth = 0;
                        } else {
                                commit = (struct commit *)
-                                       stack.objects[--stack.nr].item;
-                               cur_depth = *(int *)commit->util;
+                                       object_array_pop(&stack);
+                               cur_depth = **commit_depth_at(&depths, commit);
                        }
                }
                parse_commit_or_die(commit);
                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;
@@ -116,25 +126,31 @@ struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
                }
                commit->object.flags |= not_shallow_flag;
                for (p = commit->parents, commit = NULL; p; p = p->next) {
-                       if (!p->item->util) {
-                               int *pointer = xmalloc(sizeof(int));
-                               p->item->util = pointer;
-                               *pointer =  cur_depth;
+                       int **depth_slot = commit_depth_at(&depths, p->item);
+                       if (!*depth_slot) {
+                               *depth_slot = xmalloc(sizeof(int));
+                               **depth_slot = cur_depth;
                        } else {
-                               int *pointer = p->item->util;
-                               if (cur_depth >= *pointer)
+                               if (cur_depth >= **depth_slot)
                                        continue;
-                               *pointer = cur_depth;
+                               **depth_slot = cur_depth;
                        }
                        if (p->next)
                                add_object_array(&p->item->object,
                                                NULL, &stack);
                        else {
                                commit = p->item;
-                               cur_depth = *(int *)commit->util;
+                               cur_depth = **commit_depth_at(&depths, commit);
                        }
                }
        }
+       for (i = 0; i < depths.slab_count; i++) {
+               int j;
+
+               for (j = 0; j < depths.slab_size; j++)
+                       free(depths.slab[i][j]);
+       }
+       clear_commit_depth(&depths);
 
        return result;
 }
@@ -175,6 +191,9 @@ struct commit_list *get_shallow_commits_by_rev_list(int ac, const char **av,
                die("revision walk setup failed");
        traverse_commit_list(&revs, show_commit, NULL, &not_shallow_list);
 
+       if (!not_shallow_list)
+               die("no commits selected for shallow requests");
+
        /* Mark all reachable commits as NOT_SHALLOW */
        for (p = not_shallow_list; p; p = p->next)
                p->item->object.flags |= not_shallow_flag;
@@ -218,7 +237,7 @@ struct commit_list *get_shallow_commits_by_rev_list(int ac, const char **av,
 static void check_shallow_file_for_update(void)
 {
        if (is_shallow == -1)
-               die("BUG: shallow must be initialized by now");
+               BUG("shallow must be initialized by now");
 
        if (!stat_validity_check(&shallow_stat, git_path_shallow()))
                die("shallow file has changed since we read it");
@@ -286,28 +305,26 @@ int write_shallow_commits(struct strbuf *out, int use_pack_protocol,
        return write_shallow_commits_1(out, use_pack_protocol, extra, 0);
 }
 
-static struct tempfile temporary_shallow;
-
 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) < 0)
+               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,
@@ -355,7 +372,7 @@ void advertise_shallow_grafts(int fd)
  */
 void prune_shallow(int show_only)
 {
-       static struct lock_file shallow_lock;
+       struct lock_file shallow_lock = LOCK_INIT;
        struct strbuf sb = STRBUF_INIT;
        int fd;
 
@@ -398,7 +415,7 @@ void prepare_shallow_info(struct shallow_info *info, struct oid_array *sa)
        for (i = 0; i < sa->nr; i++) {
                if (has_object_file(sa->oid + i)) {
                        struct commit_graft *graft;
-                       graft = lookup_commit_graft(sa->oid[i].hash);
+                       graft = lookup_commit_graft(&sa->oid[i]);
                        if (graft && graft->nr_parent < 0)
                                continue;
                        info->ours[info->nr_ours++] = i;
@@ -448,7 +465,7 @@ static uint32_t *paint_alloc(struct paint_info *info)
        void *p;
        if (!info->pool_count || size > info->end - info->free) {
                if (size > POOL_SIZE)
-                       die("BUG: pool size too small for %d in paint_alloc()",
+                       BUG("pool size too small for %d in paint_alloc()",
                            size);
                info->pool_count++;
                REALLOC_ARRAY(info->pools, info->pool_count);