tag.con commit Git 1.7.7.2 (8d19b44)
   1#include "cache.h"
   2#include "tag.h"
   3#include "commit.h"
   4#include "tree.h"
   5#include "blob.h"
   6
   7#define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----"
   8#define PGP_MESSAGE "-----BEGIN PGP MESSAGE-----"
   9
  10const char *tag_type = "tag";
  11
  12struct object *deref_tag(struct object *o, const char *warn, int warnlen)
  13{
  14        while (o && o->type == OBJ_TAG)
  15                if (((struct tag *)o)->tagged)
  16                        o = parse_object(((struct tag *)o)->tagged->sha1);
  17                else
  18                        o = NULL;
  19        if (!o && warn) {
  20                if (!warnlen)
  21                        warnlen = strlen(warn);
  22                error("missing object referenced by '%.*s'", warnlen, warn);
  23        }
  24        return o;
  25}
  26
  27struct tag *lookup_tag(const unsigned char *sha1)
  28{
  29        struct object *obj = lookup_object(sha1);
  30        if (!obj)
  31                return create_object(sha1, OBJ_TAG, alloc_tag_node());
  32        if (!obj->type)
  33                obj->type = OBJ_TAG;
  34        if (obj->type != OBJ_TAG) {
  35                error("Object %s is a %s, not a tag",
  36                      sha1_to_hex(sha1), typename(obj->type));
  37                return NULL;
  38        }
  39        return (struct tag *) obj;
  40}
  41
  42static unsigned long parse_tag_date(const char *buf, const char *tail)
  43{
  44        const char *dateptr;
  45
  46        while (buf < tail && *buf++ != '>')
  47                /* nada */;
  48        if (buf >= tail)
  49                return 0;
  50        dateptr = buf;
  51        while (buf < tail && *buf++ != '\n')
  52                /* nada */;
  53        if (buf >= tail)
  54                return 0;
  55        /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
  56        return strtoul(dateptr, NULL, 10);
  57}
  58
  59int parse_tag_buffer(struct tag *item, const void *data, unsigned long size)
  60{
  61        unsigned char sha1[20];
  62        char type[20];
  63        const char *bufptr = data;
  64        const char *tail = bufptr + size;
  65        const char *nl;
  66
  67        if (item->object.parsed)
  68                return 0;
  69        item->object.parsed = 1;
  70
  71        if (size < 64)
  72                return -1;
  73        if (memcmp("object ", bufptr, 7) || get_sha1_hex(bufptr + 7, sha1) || bufptr[47] != '\n')
  74                return -1;
  75        bufptr += 48; /* "object " + sha1 + "\n" */
  76
  77        if (prefixcmp(bufptr, "type "))
  78                return -1;
  79        bufptr += 5;
  80        nl = memchr(bufptr, '\n', tail - bufptr);
  81        if (!nl || sizeof(type) <= (nl - bufptr))
  82                return -1;
  83        strncpy(type, bufptr, nl - bufptr);
  84        type[nl - bufptr] = '\0';
  85        bufptr = nl + 1;
  86
  87        if (!strcmp(type, blob_type)) {
  88                item->tagged = &lookup_blob(sha1)->object;
  89        } else if (!strcmp(type, tree_type)) {
  90                item->tagged = &lookup_tree(sha1)->object;
  91        } else if (!strcmp(type, commit_type)) {
  92                item->tagged = &lookup_commit(sha1)->object;
  93        } else if (!strcmp(type, tag_type)) {
  94                item->tagged = &lookup_tag(sha1)->object;
  95        } else {
  96                error("Unknown type %s", type);
  97                item->tagged = NULL;
  98        }
  99
 100        if (bufptr + 4 < tail && !prefixcmp(bufptr, "tag "))
 101                ;               /* good */
 102        else
 103                return -1;
 104        bufptr += 4;
 105        nl = memchr(bufptr, '\n', tail - bufptr);
 106        if (!nl)
 107                return -1;
 108        item->tag = xmemdupz(bufptr, nl - bufptr);
 109        bufptr = nl + 1;
 110
 111        if (bufptr + 7 < tail && !prefixcmp(bufptr, "tagger "))
 112                item->date = parse_tag_date(bufptr, tail);
 113        else
 114                item->date = 0;
 115
 116        return 0;
 117}
 118
 119int parse_tag(struct tag *item)
 120{
 121        enum object_type type;
 122        void *data;
 123        unsigned long size;
 124        int ret;
 125
 126        if (item->object.parsed)
 127                return 0;
 128        data = read_sha1_file(item->object.sha1, &type, &size);
 129        if (!data)
 130                return error("Could not read %s",
 131                             sha1_to_hex(item->object.sha1));
 132        if (type != OBJ_TAG) {
 133                free(data);
 134                return error("Object %s not a tag",
 135                             sha1_to_hex(item->object.sha1));
 136        }
 137        ret = parse_tag_buffer(item, data, size);
 138        free(data);
 139        return ret;
 140}
 141
 142size_t parse_signature(const char *buf, unsigned long size)
 143{
 144        char *eol;
 145        size_t len = 0;
 146        while (len < size && prefixcmp(buf + len, PGP_SIGNATURE) &&
 147                        prefixcmp(buf + len, PGP_MESSAGE)) {
 148                eol = memchr(buf + len, '\n', size - len);
 149                len += eol ? eol - (buf + len) + 1 : size - len;
 150        }
 151        return len;
 152}