reachable.con commit Merge branch 'rj/http-code-cleanup' (79cc643)
   1#include "cache.h"
   2#include "refs.h"
   3#include "tag.h"
   4#include "commit.h"
   5#include "blob.h"
   6#include "diff.h"
   7#include "revision.h"
   8#include "reachable.h"
   9#include "cache-tree.h"
  10#include "progress.h"
  11#include "list-objects.h"
  12#include "packfile.h"
  13#include "worktree.h"
  14
  15struct connectivity_progress {
  16        struct progress *progress;
  17        unsigned long count;
  18};
  19
  20static void update_progress(struct connectivity_progress *cp)
  21{
  22        cp->count++;
  23        if ((cp->count & 1023) == 0)
  24                display_progress(cp->progress, cp->count);
  25}
  26
  27static int add_one_ref(const char *path, const struct object_id *oid,
  28                       int flag, void *cb_data)
  29{
  30        struct rev_info *revs = (struct rev_info *)cb_data;
  31        struct object *object;
  32
  33        if ((flag & REF_ISSYMREF) && (flag & REF_ISBROKEN)) {
  34                warning("symbolic ref is dangling: %s", path);
  35                return 0;
  36        }
  37
  38        object = parse_object_or_die(oid, path);
  39        add_pending_object(revs, object, "");
  40
  41        return 0;
  42}
  43
  44/*
  45 * The traversal will have already marked us as SEEN, so we
  46 * only need to handle any progress reporting here.
  47 */
  48static void mark_object(struct object *obj, const char *name, void *data)
  49{
  50        update_progress(data);
  51}
  52
  53static void mark_commit(struct commit *c, void *data)
  54{
  55        mark_object(&c->object, NULL, data);
  56}
  57
  58struct recent_data {
  59        struct rev_info *revs;
  60        timestamp_t timestamp;
  61};
  62
  63static void add_recent_object(const struct object_id *oid,
  64                              timestamp_t mtime,
  65                              struct recent_data *data)
  66{
  67        struct object *obj;
  68        enum object_type type;
  69
  70        if (mtime <= data->timestamp)
  71                return;
  72
  73        /*
  74         * We do not want to call parse_object here, because
  75         * inflating blobs and trees could be very expensive.
  76         * However, we do need to know the correct type for
  77         * later processing, and the revision machinery expects
  78         * commits and tags to have been parsed.
  79         */
  80        type = sha1_object_info(oid->hash, NULL);
  81        if (type < 0)
  82                die("unable to get object info for %s", oid_to_hex(oid));
  83
  84        switch (type) {
  85        case OBJ_TAG:
  86        case OBJ_COMMIT:
  87                obj = parse_object_or_die(oid, NULL);
  88                break;
  89        case OBJ_TREE:
  90                obj = (struct object *)lookup_tree(oid);
  91                break;
  92        case OBJ_BLOB:
  93                obj = (struct object *)lookup_blob(oid);
  94                break;
  95        default:
  96                die("unknown object type for %s: %s",
  97                    oid_to_hex(oid), type_name(type));
  98        }
  99
 100        if (!obj)
 101                die("unable to lookup %s", oid_to_hex(oid));
 102
 103        add_pending_object(data->revs, obj, "");
 104}
 105
 106static int add_recent_loose(const struct object_id *oid,
 107                            const char *path, void *data)
 108{
 109        struct stat st;
 110        struct object *obj = lookup_object(oid->hash);
 111
 112        if (obj && obj->flags & SEEN)
 113                return 0;
 114
 115        if (stat(path, &st) < 0) {
 116                /*
 117                 * It's OK if an object went away during our iteration; this
 118                 * could be due to a simultaneous repack. But anything else
 119                 * we should abort, since we might then fail to mark objects
 120                 * which should not be pruned.
 121                 */
 122                if (errno == ENOENT)
 123                        return 0;
 124                return error_errno("unable to stat %s", oid_to_hex(oid));
 125        }
 126
 127        add_recent_object(oid, st.st_mtime, data);
 128        return 0;
 129}
 130
 131static int add_recent_packed(const struct object_id *oid,
 132                             struct packed_git *p, uint32_t pos,
 133                             void *data)
 134{
 135        struct object *obj = lookup_object(oid->hash);
 136
 137        if (obj && obj->flags & SEEN)
 138                return 0;
 139        add_recent_object(oid, p->mtime, data);
 140        return 0;
 141}
 142
 143int add_unseen_recent_objects_to_traversal(struct rev_info *revs,
 144                                           timestamp_t timestamp)
 145{
 146        struct recent_data data;
 147        int r;
 148
 149        data.revs = revs;
 150        data.timestamp = timestamp;
 151
 152        r = for_each_loose_object(add_recent_loose, &data,
 153                                  FOR_EACH_OBJECT_LOCAL_ONLY);
 154        if (r)
 155                return r;
 156        return for_each_packed_object(add_recent_packed, &data,
 157                                      FOR_EACH_OBJECT_LOCAL_ONLY);
 158}
 159
 160void mark_reachable_objects(struct rev_info *revs, int mark_reflog,
 161                            timestamp_t mark_recent, struct progress *progress)
 162{
 163        struct connectivity_progress cp;
 164
 165        /*
 166         * Set up revision parsing, and mark us as being interested
 167         * in all object types, not just commits.
 168         */
 169        revs->tag_objects = 1;
 170        revs->blob_objects = 1;
 171        revs->tree_objects = 1;
 172
 173        /* Add all refs from the index file */
 174        add_index_objects_to_pending(revs, 0);
 175
 176        /* Add all external refs */
 177        for_each_ref(add_one_ref, revs);
 178
 179        /* detached HEAD is not included in the list above */
 180        head_ref(add_one_ref, revs);
 181        other_head_refs(add_one_ref, revs);
 182
 183        /* Add all reflog info */
 184        if (mark_reflog)
 185                add_reflogs_to_pending(revs, 0);
 186
 187        cp.progress = progress;
 188        cp.count = 0;
 189
 190        /*
 191         * Set up the revision walk - this will move all commits
 192         * from the pending list to the commit walking list.
 193         */
 194        if (prepare_revision_walk(revs))
 195                die("revision walk setup failed");
 196        traverse_commit_list(revs, mark_commit, mark_object, &cp);
 197
 198        if (mark_recent) {
 199                revs->ignore_missing_links = 1;
 200                if (add_unseen_recent_objects_to_traversal(revs, mark_recent))
 201                        die("unable to mark recent objects");
 202                if (prepare_revision_walk(revs))
 203                        die("revision walk setup failed");
 204                traverse_commit_list(revs, mark_commit, mark_object, &cp);
 205        }
 206
 207        display_progress(cp.progress, cp.count);
 208}