tag.con commit Allow asciidoc formatted documentation in howto/ (8d0991d)
   1#include "tag.h"
   2#include "cache.h"
   3
   4const char *tag_type = "tag";
   5
   6struct object *deref_tag(struct object *o)
   7{
   8        while (o && o->type == tag_type)
   9                o = parse_object(((struct tag *)o)->tagged->sha1);
  10        return o;
  11}
  12
  13struct tag *lookup_tag(const unsigned char *sha1)
  14{
  15        struct object *obj = lookup_object(sha1);
  16        if (!obj) {
  17                struct tag *ret = xmalloc(sizeof(struct tag));
  18                memset(ret, 0, sizeof(struct tag));
  19                created_object(sha1, &ret->object);
  20                ret->object.type = tag_type;
  21                return ret;
  22        }
  23        if (!obj->type)
  24                obj->type = tag_type;
  25        if (obj->type != tag_type) {
  26                error("Object %s is a %s, not a tree", 
  27                      sha1_to_hex(sha1), obj->type);
  28                return NULL;
  29        }
  30        return (struct tag *) obj;
  31}
  32
  33int parse_tag_buffer(struct tag *item, void *data, unsigned long size)
  34{
  35        int typelen, taglen;
  36        unsigned char object[20];
  37        const char *type_line, *tag_line, *sig_line;
  38        char type[20];
  39
  40        if (item->object.parsed)
  41                return 0;
  42        item->object.parsed = 1;
  43
  44        if (size < 64)
  45                return -1;
  46        if (memcmp("object ", data, 7) || get_sha1_hex(data + 7, object))
  47                return -1;
  48
  49        type_line = data + 48;
  50        if (memcmp("\ntype ", type_line-1, 6))
  51                return -1;
  52
  53        tag_line = strchr(type_line, '\n');
  54        if (!tag_line || memcmp("tag ", ++tag_line, 4))
  55                return -1;
  56
  57        sig_line = strchr(tag_line, '\n');
  58        if (!sig_line)
  59                return -1;
  60        sig_line++;
  61
  62        typelen = tag_line - type_line - strlen("type \n");
  63        if (typelen >= 20)
  64                return -1;
  65        memcpy(type, type_line + 5, typelen);
  66        type[typelen] = '\0';
  67        taglen = sig_line - tag_line - strlen("tag \n");
  68        item->tag = xmalloc(taglen + 1);
  69        memcpy(item->tag, tag_line + 4, taglen);
  70        item->tag[taglen] = '\0';
  71
  72        item->tagged = lookup_object_type(object, type);
  73        if (item->tagged)
  74                add_ref(&item->object, item->tagged);
  75
  76        return 0;
  77}
  78
  79int parse_tag(struct tag *item)
  80{
  81        char type[20];
  82        void *data;
  83        unsigned long size;
  84        int ret;
  85
  86        if (item->object.parsed)
  87                return 0;
  88        data = read_sha1_file(item->object.sha1, type, &size);
  89        if (!data)
  90                return error("Could not read %s",
  91                             sha1_to_hex(item->object.sha1));
  92        if (strcmp(type, tag_type)) {
  93                free(data);
  94                return error("Object %s not a tag",
  95                             sha1_to_hex(item->object.sha1));
  96        }
  97        ret = parse_tag_buffer(item, data, size);
  98        free(data);
  99        return ret;
 100}