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