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