reachable.con commit Add support for "import" helper command (e65e91e)
   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
  11static void process_blob(struct blob *blob,
  12                         struct object_array *p,
  13                         struct name_path *path,
  14                         const char *name)
  15{
  16        struct object *obj = &blob->object;
  17
  18        if (!blob)
  19                die("bad blob object");
  20        if (obj->flags & SEEN)
  21                return;
  22        obj->flags |= SEEN;
  23        /* Nothing to do, really .. The blob lookup was the important part */
  24}
  25
  26static void process_gitlink(const unsigned char *sha1,
  27                            struct object_array *p,
  28                            struct name_path *path,
  29                            const char *name)
  30{
  31        /* I don't think we want to recurse into this, really. */
  32}
  33
  34static void process_tree(struct tree *tree,
  35                         struct object_array *p,
  36                         struct name_path *path,
  37                         const char *name)
  38{
  39        struct object *obj = &tree->object;
  40        struct tree_desc desc;
  41        struct name_entry entry;
  42        struct name_path me;
  43
  44        if (!tree)
  45                die("bad tree object");
  46        if (obj->flags & SEEN)
  47                return;
  48        obj->flags |= SEEN;
  49        if (parse_tree(tree) < 0)
  50                die("bad tree object %s", sha1_to_hex(obj->sha1));
  51        add_object(obj, p, path, name);
  52        me.up = path;
  53        me.elem = name;
  54        me.elem_len = strlen(name);
  55
  56        init_tree_desc(&desc, tree->buffer, tree->size);
  57
  58        while (tree_entry(&desc, &entry)) {
  59                if (S_ISDIR(entry.mode))
  60                        process_tree(lookup_tree(entry.sha1), p, &me, entry.path);
  61                else if (S_ISGITLINK(entry.mode))
  62                        process_gitlink(entry.sha1, p, &me, entry.path);
  63                else
  64                        process_blob(lookup_blob(entry.sha1), p, &me, entry.path);
  65        }
  66        free(tree->buffer);
  67        tree->buffer = NULL;
  68}
  69
  70static void process_tag(struct tag *tag, struct object_array *p, const char *name)
  71{
  72        struct object *obj = &tag->object;
  73        struct name_path me;
  74
  75        if (obj->flags & SEEN)
  76                return;
  77        obj->flags |= SEEN;
  78
  79        me.up = NULL;
  80        me.elem = "tag:/";
  81        me.elem_len = 5;
  82
  83        if (parse_tag(tag) < 0)
  84                die("bad tag object %s", sha1_to_hex(obj->sha1));
  85        if (tag->tagged)
  86                add_object(tag->tagged, p, NULL, name);
  87}
  88
  89static void walk_commit_list(struct rev_info *revs)
  90{
  91        int i;
  92        struct commit *commit;
  93        struct object_array objects = { 0, 0, NULL };
  94
  95        /* Walk all commits, process their trees */
  96        while ((commit = get_revision(revs)) != NULL)
  97                process_tree(commit->tree, &objects, NULL, "");
  98
  99        /* Then walk all the pending objects, recursively processing them too */
 100        for (i = 0; i < revs->pending.nr; i++) {
 101                struct object_array_entry *pending = revs->pending.objects + i;
 102                struct object *obj = pending->item;
 103                const char *name = pending->name;
 104                if (obj->type == OBJ_TAG) {
 105                        process_tag((struct tag *) obj, &objects, name);
 106                        continue;
 107                }
 108                if (obj->type == OBJ_TREE) {
 109                        process_tree((struct tree *)obj, &objects, NULL, name);
 110                        continue;
 111                }
 112                if (obj->type == OBJ_BLOB) {
 113                        process_blob((struct blob *)obj, &objects, NULL, name);
 114                        continue;
 115                }
 116                die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
 117        }
 118}
 119
 120static int add_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
 121                const char *email, unsigned long timestamp, int tz,
 122                const char *message, void *cb_data)
 123{
 124        struct object *object;
 125        struct rev_info *revs = (struct rev_info *)cb_data;
 126
 127        object = parse_object(osha1);
 128        if (object)
 129                add_pending_object(revs, object, "");
 130        object = parse_object(nsha1);
 131        if (object)
 132                add_pending_object(revs, object, "");
 133        return 0;
 134}
 135
 136static int add_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
 137{
 138        struct object *object = parse_object(sha1);
 139        struct rev_info *revs = (struct rev_info *)cb_data;
 140
 141        if (!object)
 142                die("bad object ref: %s:%s", path, sha1_to_hex(sha1));
 143        add_pending_object(revs, object, "");
 144
 145        return 0;
 146}
 147
 148static int add_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data)
 149{
 150        for_each_reflog_ent(path, add_one_reflog_ent, cb_data);
 151        return 0;
 152}
 153
 154static void add_one_tree(const unsigned char *sha1, struct rev_info *revs)
 155{
 156        struct tree *tree = lookup_tree(sha1);
 157        if (tree)
 158                add_pending_object(revs, &tree->object, "");
 159}
 160
 161static void add_cache_tree(struct cache_tree *it, struct rev_info *revs)
 162{
 163        int i;
 164
 165        if (it->entry_count >= 0)
 166                add_one_tree(it->sha1, revs);
 167        for (i = 0; i < it->subtree_nr; i++)
 168                add_cache_tree(it->down[i]->cache_tree, revs);
 169}
 170
 171static void add_cache_refs(struct rev_info *revs)
 172{
 173        int i;
 174
 175        read_cache();
 176        for (i = 0; i < active_nr; i++) {
 177                /*
 178                 * The index can contain blobs and GITLINKs, GITLINKs are hashes
 179                 * that don't actually point to objects in the repository, it's
 180                 * almost guaranteed that they are NOT blobs, so we don't call
 181                 * lookup_blob() on them, to avoid populating the hash table
 182                 * with invalid information
 183                 */
 184                if (S_ISGITLINK(active_cache[i]->ce_mode))
 185                        continue;
 186
 187                lookup_blob(active_cache[i]->sha1);
 188                /*
 189                 * We could add the blobs to the pending list, but quite
 190                 * frankly, we don't care. Once we've looked them up, and
 191                 * added them as objects, we've really done everything
 192                 * there is to do for a blob
 193                 */
 194        }
 195        if (active_cache_tree)
 196                add_cache_tree(active_cache_tree, revs);
 197}
 198
 199void mark_reachable_objects(struct rev_info *revs, int mark_reflog)
 200{
 201        /*
 202         * Set up revision parsing, and mark us as being interested
 203         * in all object types, not just commits.
 204         */
 205        revs->tag_objects = 1;
 206        revs->blob_objects = 1;
 207        revs->tree_objects = 1;
 208
 209        /* Add all refs from the index file */
 210        add_cache_refs(revs);
 211
 212        /* Add all external refs */
 213        for_each_ref(add_one_ref, revs);
 214
 215        /* Add all reflog info */
 216        if (mark_reflog)
 217                for_each_reflog(add_one_reflog, revs);
 218
 219        /*
 220         * Set up the revision walk - this will move all commits
 221         * from the pending list to the commit walking list.
 222         */
 223        if (prepare_revision_walk(revs))
 224                die("revision walk setup failed");
 225        walk_commit_list(revs);
 226}