object.con commit [PATCH] Support cloning packed repo from dumb http servers. (c0a58fb)
   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
  12static int find_object(const unsigned char *sha1)
  13{
  14        int first = 0, last = nr_objs;
  15
  16        while (first < last) {
  17                int next = (first + last) / 2;
  18                struct object *obj = objs[next];
  19                int cmp;
  20
  21                cmp = memcmp(sha1, obj->sha1, 20);
  22                if (!cmp)
  23                        return next;
  24                if (cmp < 0) {
  25                        last = next;
  26                        continue;
  27                }
  28                first = next+1;
  29        }
  30        return -first-1;
  31}
  32
  33struct object *lookup_object(const unsigned char *sha1)
  34{
  35        int pos = find_object(sha1);
  36        if (pos >= 0)
  37                return objs[pos];
  38        return NULL;
  39}
  40
  41void created_object(const unsigned char *sha1, struct object *obj)
  42{
  43        int pos = find_object(sha1);
  44
  45        obj->parsed = 0;
  46        memcpy(obj->sha1, sha1, 20);
  47        obj->type = NULL;
  48        obj->refs = NULL;
  49        obj->used = 0;
  50
  51        if (pos >= 0)
  52                die("Inserting %s twice\n", sha1_to_hex(sha1));
  53        pos = -pos-1;
  54
  55        if (obj_allocs == nr_objs) {
  56                obj_allocs = alloc_nr(obj_allocs);
  57                objs = xrealloc(objs, obj_allocs * sizeof(struct object *));
  58        }
  59
  60        /* Insert it into the right place */
  61        memmove(objs + pos + 1, objs + pos, (nr_objs - pos) * 
  62                sizeof(struct object *));
  63
  64        objs[pos] = obj;
  65        nr_objs++;
  66}
  67
  68void add_ref(struct object *refer, struct object *target)
  69{
  70        struct object_list **pp = &refer->refs;
  71        struct object_list *p;
  72        
  73        while ((p = *pp) != NULL) {
  74                if (p->item == target)
  75                        return;
  76                pp = &p->next;
  77        }
  78
  79        target->used = 1;
  80        p = xmalloc(sizeof(*p));
  81        p->item = target;
  82        p->next = NULL;
  83        *pp = p;
  84}
  85
  86void mark_reachable(struct object *obj, unsigned int mask)
  87{
  88        struct object_list *p = obj->refs;
  89
  90        /* If we've been here already, don't bother */
  91        if (obj->flags & mask)
  92                return;
  93        obj->flags |= mask;
  94        while (p) {
  95                mark_reachable(p->item, mask);
  96                p = p->next;
  97        }
  98}
  99
 100struct object *lookup_object_type(const unsigned char *sha1, const char *type)
 101{
 102        if (!strcmp(type, blob_type)) {
 103                return &lookup_blob(sha1)->object;
 104        } else if (!strcmp(type, tree_type)) {
 105                return &lookup_tree(sha1)->object;
 106        } else if (!strcmp(type, commit_type)) {
 107                return &lookup_commit(sha1)->object;
 108        } else if (!strcmp(type, tag_type)) {
 109                return &lookup_tag(sha1)->object;
 110        } else {
 111                error("Unknown type %s", type);
 112                return NULL;
 113        }
 114}
 115
 116struct object *parse_object(const unsigned char *sha1)
 117{
 118        unsigned long size;
 119        char type[20];
 120        void *buffer = read_sha1_file(sha1, type, &size);
 121        if (buffer) {
 122                struct object *obj;
 123                if (check_sha1_signature(sha1, buffer, size, type) < 0)
 124                        printf("sha1 mismatch %s\n", sha1_to_hex(sha1));
 125                if (!strcmp(type, "blob")) {
 126                        struct blob *blob = lookup_blob(sha1);
 127                        parse_blob_buffer(blob, buffer, size);
 128                        obj = &blob->object;
 129                } else if (!strcmp(type, "tree")) {
 130                        struct tree *tree = lookup_tree(sha1);
 131                        parse_tree_buffer(tree, buffer, size);
 132                        obj = &tree->object;
 133                } else if (!strcmp(type, "commit")) {
 134                        struct commit *commit = lookup_commit(sha1);
 135                        parse_commit_buffer(commit, buffer, size);
 136                        if (!commit->buffer) {
 137                                commit->buffer = buffer;
 138                                buffer = NULL;
 139                        }
 140                        obj = &commit->object;
 141                } else if (!strcmp(type, "tag")) {
 142                        struct tag *tag = lookup_tag(sha1);
 143                        parse_tag_buffer(tag, buffer, size);
 144                        obj = &tag->object;
 145                } else {
 146                        obj = NULL;
 147                }
 148                free(buffer);
 149                return obj;
 150        }
 151        return NULL;
 152}