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