object.con commit Add documentation for git-pack-intersect (2a44478)
   1#include "object.h"
   2#include "blob.h"
   3#include "tree.h"
   4#include "commit.h"
   5#include "cache.h"
   6#include "tag.h"
   7
   8struct object **objs;
   9int nr_objs;
  10static int obj_allocs;
  11
  12int track_object_refs = 1;
  13
  14static int find_object(const unsigned char *sha1)
  15{
  16        int first = 0, last = nr_objs;
  17
  18        while (first < last) {
  19                int next = (first + last) / 2;
  20                struct object *obj = objs[next];
  21                int cmp;
  22
  23                cmp = memcmp(sha1, obj->sha1, 20);
  24                if (!cmp)
  25                        return next;
  26                if (cmp < 0) {
  27                        last = next;
  28                        continue;
  29                }
  30                first = next+1;
  31        }
  32        return -first-1;
  33}
  34
  35struct object *lookup_object(const unsigned char *sha1)
  36{
  37        int pos = find_object(sha1);
  38        if (pos >= 0)
  39                return objs[pos];
  40        return NULL;
  41}
  42
  43void created_object(const unsigned char *sha1, struct object *obj)
  44{
  45        int pos = find_object(sha1);
  46
  47        obj->parsed = 0;
  48        memcpy(obj->sha1, sha1, 20);
  49        obj->type = NULL;
  50        obj->refs = NULL;
  51        obj->used = 0;
  52
  53        if (pos >= 0)
  54                die("Inserting %s twice\n", sha1_to_hex(sha1));
  55        pos = -pos-1;
  56
  57        if (obj_allocs == nr_objs) {
  58                obj_allocs = alloc_nr(obj_allocs);
  59                objs = xrealloc(objs, obj_allocs * sizeof(struct object *));
  60        }
  61
  62        /* Insert it into the right place */
  63        memmove(objs + pos + 1, objs + pos, (nr_objs - pos) * 
  64                sizeof(struct object *));
  65
  66        objs[pos] = obj;
  67        nr_objs++;
  68}
  69
  70void add_ref(struct object *refer, struct object *target)
  71{
  72        struct object_list **pp, *p;
  73
  74        if (!track_object_refs)
  75                return;
  76
  77        pp = &refer->refs;
  78        while ((p = *pp) != NULL) {
  79                if (p->item == target)
  80                        return;
  81                pp = &p->next;
  82        }
  83
  84        target->used = 1;
  85        p = xmalloc(sizeof(*p));
  86        p->item = target;
  87        p->next = NULL;
  88        *pp = p;
  89}
  90
  91void mark_reachable(struct object *obj, unsigned int mask)
  92{
  93        struct object_list *p = obj->refs;
  94
  95        if (!track_object_refs)
  96                die("cannot do reachability with object refs turned off");
  97        /* If we've been here already, don't bother */
  98        if (obj->flags & mask)
  99                return;
 100        obj->flags |= mask;
 101        while (p) {
 102                mark_reachable(p->item, mask);
 103                p = p->next;
 104        }
 105}
 106
 107struct object *lookup_object_type(const unsigned char *sha1, const char *type)
 108{
 109        if (!type) {
 110                return lookup_unknown_object(sha1);
 111        } else if (!strcmp(type, blob_type)) {
 112                return &lookup_blob(sha1)->object;
 113        } else if (!strcmp(type, tree_type)) {
 114                return &lookup_tree(sha1)->object;
 115        } else if (!strcmp(type, commit_type)) {
 116                return &lookup_commit(sha1)->object;
 117        } else if (!strcmp(type, tag_type)) {
 118                return &lookup_tag(sha1)->object;
 119        } else {
 120                error("Unknown type %s", type);
 121                return NULL;
 122        }
 123}
 124
 125union any_object {
 126        struct object object;
 127        struct commit commit;
 128        struct tree tree;
 129        struct blob blob;
 130        struct tag tag;
 131};
 132
 133struct object *lookup_unknown_object(const unsigned char *sha1)
 134{
 135        struct object *obj = lookup_object(sha1);
 136        if (!obj) {
 137                union any_object *ret = xmalloc(sizeof(*ret));
 138                memset(ret, 0, sizeof(*ret));
 139                created_object(sha1, &ret->object);
 140                ret->object.type = NULL;
 141                return &ret->object;
 142        }
 143        return obj;
 144}
 145
 146struct object *parse_object(const unsigned char *sha1)
 147{
 148        unsigned long size;
 149        char type[20];
 150        void *buffer = read_sha1_file(sha1, type, &size);
 151        if (buffer) {
 152                struct object *obj;
 153                if (check_sha1_signature(sha1, buffer, size, type) < 0)
 154                        printf("sha1 mismatch %s\n", sha1_to_hex(sha1));
 155                if (!strcmp(type, "blob")) {
 156                        struct blob *blob = lookup_blob(sha1);
 157                        parse_blob_buffer(blob, buffer, size);
 158                        obj = &blob->object;
 159                } else if (!strcmp(type, "tree")) {
 160                        struct tree *tree = lookup_tree(sha1);
 161                        parse_tree_buffer(tree, buffer, size);
 162                        obj = &tree->object;
 163                } else if (!strcmp(type, "commit")) {
 164                        struct commit *commit = lookup_commit(sha1);
 165                        parse_commit_buffer(commit, buffer, size);
 166                        if (!commit->buffer) {
 167                                commit->buffer = buffer;
 168                                buffer = NULL;
 169                        }
 170                        obj = &commit->object;
 171                } else if (!strcmp(type, "tag")) {
 172                        struct tag *tag = lookup_tag(sha1);
 173                        parse_tag_buffer(tag, buffer, size);
 174                        obj = &tag->object;
 175                } else {
 176                        obj = NULL;
 177                }
 178                free(buffer);
 179                return obj;
 180        }
 181        return NULL;
 182}
 183
 184struct object_list *object_list_insert(struct object *item,
 185                                       struct object_list **list_p)
 186{
 187        struct object_list *new_list = xmalloc(sizeof(struct object_list));
 188        new_list->item = item;
 189        new_list->next = *list_p;
 190        *list_p = new_list;
 191        return new_list;
 192}
 193
 194void object_list_append(struct object *item,
 195                        struct object_list **list_p)
 196{
 197        while (*list_p) {
 198                list_p = &((*list_p)->next);
 199        }
 200        *list_p = xmalloc(sizeof(struct object_list));
 201        (*list_p)->next = NULL;
 202        (*list_p)->item = item;
 203}
 204
 205unsigned object_list_length(struct object_list *list)
 206{
 207        unsigned ret = 0;
 208        while (list) {
 209                list = list->next;
 210                ret++;
 211        }
 212        return ret;
 213}
 214
 215int object_list_contains(struct object_list *list, struct object *obj)
 216{
 217        while (list) {
 218                if (list->item == obj)
 219                        return 1;
 220                list = list->next;
 221        }
 222        return 0;
 223}