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