tree-diff: rework diff_tree interface to be sha1 based
[gitweb.git] / shallow.c
index fb6069ba0c2c97d4f47c7c8df2aa29f14b43babc..bbc98b55c07969474b0afb01d9c4da70455f1903 100644 (file)
--- a/shallow.c
+++ b/shallow.c
@@ -13,10 +13,12 @@ static int is_shallow = -1;
 static struct stat shallow_stat;
 static char *alternate_shallow_file;
 
-void set_alternate_shallow_file(const char *path)
+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");
+       if (alternate_shallow_file && !override)
+               return;
        free(alternate_shallow_file);
        alternate_shallow_file = path ? xstrdup(path) : NULL;
 }
@@ -75,6 +77,7 @@ struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
        struct commit_list *result = NULL;
        struct object_array stack = OBJECT_ARRAY_INIT;
        struct commit *commit = NULL;
+       struct commit_graft *graft;
 
        while (commit || i < heads->nr || stack.nr) {
                struct commit_list *p;
@@ -96,10 +99,12 @@ struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
                                cur_depth = *(int *)commit->util;
                        }
                }
-               if (parse_commit(commit))
-                       die("invalid commit");
+               parse_commit_or_die(commit);
                cur_depth++;
-               if (cur_depth >= depth) {
+               if ((depth != INFINITE_DEPTH && cur_depth >= depth) ||
+                   (is_repository_shallow() && !commit->parents &&
+                    (graft = lookup_commit_graft(commit->object.sha1)) != NULL &&
+                    graft->nr_parent < 0)) {
                        commit_list_insert(commit, &result);
                        commit->object.flags |= shallow_flag;
                        commit = NULL;
@@ -149,10 +154,14 @@ void check_shallow_file_for_update(void)
                die("shallow file was changed during fetch");
 }
 
+#define SEEN_ONLY 1
+#define VERBOSE   2
+
 struct write_shallow_data {
        struct strbuf *out;
        int use_pack_protocol;
        int count;
+       unsigned flags;
 };
 
 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
@@ -161,6 +170,15 @@ static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
        const char *hex = sha1_to_hex(graft->sha1);
        if (graft->nr_parent != -1)
                return 0;
+       if (data->flags & SEEN_ONLY) {
+               struct commit *c = lookup_commit(graft->sha1);
+               if (!c || !(c->object.flags & SEEN)) {
+                       if (data->flags & VERBOSE)
+                               printf("Removing %s from .git/shallow\n",
+                                      sha1_to_hex(c->object.sha1));
+                       return 0;
+               }
+       }
        data->count++;
        if (data->use_pack_protocol)
                packet_buf_write(data->out, "shallow %s", hex);
@@ -171,14 +189,16 @@ static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
        return 0;
 }
 
-int write_shallow_commits(struct strbuf *out, int use_pack_protocol,
-                         const struct sha1_array *extra)
+static int write_shallow_commits_1(struct strbuf *out, int use_pack_protocol,
+                                  const struct sha1_array *extra,
+                                  unsigned flags)
 {
        struct write_shallow_data data;
        int i;
        data.out = out;
        data.use_pack_protocol = use_pack_protocol;
        data.count = 0;
+       data.flags = flags;
        for_each_commit_graft(write_one_shallow, &data);
        if (!extra)
                return data.count;
@@ -190,6 +210,12 @@ int write_shallow_commits(struct strbuf *out, int use_pack_protocol,
        return data.count;
 }
 
+int write_shallow_commits(struct strbuf *out, int use_pack_protocol,
+                         const struct sha1_array *extra)
+{
+       return write_shallow_commits_1(out, use_pack_protocol, extra, 0);
+}
+
 char *setup_temporary_shallow(const struct sha1_array *extra)
 {
        struct strbuf sb = STRBUF_INIT;
@@ -252,6 +278,36 @@ void advertise_shallow_grafts(int fd)
        for_each_commit_graft(advertise_shallow_grafts_cb, &fd);
 }
 
+/*
+ * mark_reachable_objects() should have been run prior to this and all
+ * reachable commits marked as "SEEN".
+ */
+void prune_shallow(int show_only)
+{
+       static struct lock_file shallow_lock;
+       struct strbuf sb = STRBUF_INIT;
+       int fd;
+
+       if (show_only) {
+               write_shallow_commits_1(&sb, 0, NULL, SEEN_ONLY | VERBOSE);
+               strbuf_release(&sb);
+               return;
+       }
+       check_shallow_file_for_update();
+       fd = hold_lock_file_for_update(&shallow_lock, git_path("shallow"),
+                                      LOCK_DIE_ON_ERROR);
+       if (write_shallow_commits_1(&sb, 0, NULL, SEEN_ONLY)) {
+               if (write_in_full(fd, sb.buf, sb.len) != sb.len)
+                       die_errno("failed to write to %s",
+                                 shallow_lock.filename);
+               commit_lock_file(&shallow_lock);
+       } else {
+               unlink(git_path("shallow"));
+               rollback_lock_file(&shallow_lock);
+       }
+       strbuf_release(&sb);
+}
+
 #define TRACE_KEY "GIT_TRACE_SHALLOW"
 
 /*
@@ -302,22 +358,6 @@ void remove_nonexistent_theirs_shallow(struct shallow_info *info)
        info->nr_theirs = dst;
 }
 
-/* Step 5, remove non-existent ones in "ours" in the pack */
-void remove_nonexistent_ours_in_pack(struct shallow_info *info,
-                                    struct packed_git *p)
-{
-       unsigned char (*sha1)[20] = info->shallow->sha1;
-       int i, dst;
-       trace_printf_key(TRACE_KEY, "shallow: remove_nonexistent_ours_in_pack\n");
-       for (i = dst = 0; i < info->nr_ours; i++) {
-               if (i != dst)
-                       info->ours[dst] = info->ours[i];
-               if (find_pack_entry_one(sha1[info->ours[i]], p))
-                       dst++;
-       }
-       info->nr_ours = dst;
-}
-
 define_commit_slab(ref_bitmap, uint32_t *);
 
 struct paint_info {
@@ -611,3 +651,26 @@ static void post_assign_shallow(struct shallow_info *info,
 
        free(ca.commits);
 }
+
+/* (Delayed) step 7, reachability test at commit level */
+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]);
+
+               if (!si->commits) {
+                       struct commit_array ca;
+                       memset(&ca, 0, sizeof(ca));
+                       head_ref(add_ref, &ca);
+                       for_each_ref(add_ref, &ca);
+                       si->commits = ca.commits;
+                       si->nr_commits = ca.nr;
+               }
+
+               si->reachable[c] = in_merge_bases_many(commit,
+                                                      si->nr_commits,
+                                                      si->commits);
+               si->need_reachability_test[c] = 0;
+       }
+       return si->reachable[c];
+}