tag.con commit commit: add repository argument to get_cached_commit_buffer (3ce85f7)
   1#include "cache.h"
   2#include "tag.h"
   3#include "object-store.h"
   4#include "commit.h"
   5#include "tree.h"
   6#include "blob.h"
   7#include "alloc.h"
   8#include "gpg-interface.h"
   9
  10const char *tag_type = "tag";
  11
  12static int run_gpg_verify(const char *buf, unsigned long size, unsigned flags)
  13{
  14        struct signature_check sigc;
  15        size_t payload_size;
  16        int ret;
  17
  18        memset(&sigc, 0, sizeof(sigc));
  19
  20        payload_size = parse_signature(buf, size);
  21
  22        if (size == payload_size) {
  23                if (flags & GPG_VERIFY_VERBOSE)
  24                        write_in_full(1, buf, payload_size);
  25                return error("no signature found");
  26        }
  27
  28        ret = check_signature(buf, payload_size, buf + payload_size,
  29                                size - payload_size, &sigc);
  30
  31        if (!(flags & GPG_VERIFY_OMIT_STATUS))
  32                print_signature_buffer(&sigc, flags);
  33
  34        signature_check_clear(&sigc);
  35        return ret;
  36}
  37
  38int gpg_verify_tag(const struct object_id *oid, const char *name_to_report,
  39                unsigned flags)
  40{
  41        enum object_type type;
  42        char *buf;
  43        unsigned long size;
  44        int ret;
  45
  46        type = oid_object_info(the_repository, oid, NULL);
  47        if (type != OBJ_TAG)
  48                return error("%s: cannot verify a non-tag object of type %s.",
  49                                name_to_report ?
  50                                name_to_report :
  51                                find_unique_abbrev(oid, DEFAULT_ABBREV),
  52                                type_name(type));
  53
  54        buf = read_object_file(oid, &type, &size);
  55        if (!buf)
  56                return error("%s: unable to read file.",
  57                                name_to_report ?
  58                                name_to_report :
  59                                find_unique_abbrev(oid, DEFAULT_ABBREV));
  60
  61        ret = run_gpg_verify(buf, size, flags);
  62
  63        free(buf);
  64        return ret;
  65}
  66
  67struct object *deref_tag(struct object *o, const char *warn, int warnlen)
  68{
  69        while (o && o->type == OBJ_TAG)
  70                if (((struct tag *)o)->tagged)
  71                        o = parse_object(the_repository,
  72                                         &((struct tag *)o)->tagged->oid);
  73                else
  74                        o = NULL;
  75        if (!o && warn) {
  76                if (!warnlen)
  77                        warnlen = strlen(warn);
  78                error("missing object referenced by '%.*s'", warnlen, warn);
  79        }
  80        return o;
  81}
  82
  83struct object *deref_tag_noverify(struct object *o)
  84{
  85        while (o && o->type == OBJ_TAG) {
  86                o = parse_object(the_repository, &o->oid);
  87                if (o && o->type == OBJ_TAG && ((struct tag *)o)->tagged)
  88                        o = ((struct tag *)o)->tagged;
  89                else
  90                        o = NULL;
  91        }
  92        return o;
  93}
  94
  95struct tag *lookup_tag(const struct object_id *oid)
  96{
  97        struct object *obj = lookup_object(the_repository, oid->hash);
  98        if (!obj)
  99                return create_object(the_repository, oid->hash,
 100                                     alloc_tag_node(the_repository));
 101        return object_as_type(the_repository, obj, OBJ_TAG, 0);
 102}
 103
 104static timestamp_t parse_tag_date(const char *buf, const char *tail)
 105{
 106        const char *dateptr;
 107
 108        while (buf < tail && *buf++ != '>')
 109                /* nada */;
 110        if (buf >= tail)
 111                return 0;
 112        dateptr = buf;
 113        while (buf < tail && *buf++ != '\n')
 114                /* nada */;
 115        if (buf >= tail)
 116                return 0;
 117        /* dateptr < buf && buf[-1] == '\n', so parsing will stop at buf-1 */
 118        return parse_timestamp(dateptr, NULL, 10);
 119}
 120
 121void release_tag_memory(struct tag *t)
 122{
 123        free(t->tag);
 124        t->tagged = NULL;
 125        t->object.parsed = 0;
 126        t->date = 0;
 127}
 128
 129int parse_tag_buffer(struct tag *item, const void *data, unsigned long size)
 130{
 131        struct object_id oid;
 132        char type[20];
 133        const char *bufptr = data;
 134        const char *tail = bufptr + size;
 135        const char *nl;
 136
 137        if (item->object.parsed)
 138                return 0;
 139        item->object.parsed = 1;
 140
 141        if (size < GIT_SHA1_HEXSZ + 24)
 142                return -1;
 143        if (memcmp("object ", bufptr, 7) || parse_oid_hex(bufptr + 7, &oid, &bufptr) || *bufptr++ != '\n')
 144                return -1;
 145
 146        if (!starts_with(bufptr, "type "))
 147                return -1;
 148        bufptr += 5;
 149        nl = memchr(bufptr, '\n', tail - bufptr);
 150        if (!nl || sizeof(type) <= (nl - bufptr))
 151                return -1;
 152        memcpy(type, bufptr, nl - bufptr);
 153        type[nl - bufptr] = '\0';
 154        bufptr = nl + 1;
 155
 156        if (!strcmp(type, blob_type)) {
 157                item->tagged = (struct object *)lookup_blob(the_repository, &oid);
 158        } else if (!strcmp(type, tree_type)) {
 159                item->tagged = (struct object *)lookup_tree(the_repository, &oid);
 160        } else if (!strcmp(type, commit_type)) {
 161                item->tagged = (struct object *)lookup_commit(the_repository, &oid);
 162        } else if (!strcmp(type, tag_type)) {
 163                item->tagged = (struct object *)lookup_tag(&oid);
 164        } else {
 165                error("Unknown type %s", type);
 166                item->tagged = NULL;
 167        }
 168
 169        if (bufptr + 4 < tail && starts_with(bufptr, "tag "))
 170                ;               /* good */
 171        else
 172                return -1;
 173        bufptr += 4;
 174        nl = memchr(bufptr, '\n', tail - bufptr);
 175        if (!nl)
 176                return -1;
 177        item->tag = xmemdupz(bufptr, nl - bufptr);
 178        bufptr = nl + 1;
 179
 180        if (bufptr + 7 < tail && starts_with(bufptr, "tagger "))
 181                item->date = parse_tag_date(bufptr, tail);
 182        else
 183                item->date = 0;
 184
 185        return 0;
 186}
 187
 188int parse_tag(struct tag *item)
 189{
 190        enum object_type type;
 191        void *data;
 192        unsigned long size;
 193        int ret;
 194
 195        if (item->object.parsed)
 196                return 0;
 197        data = read_object_file(&item->object.oid, &type, &size);
 198        if (!data)
 199                return error("Could not read %s",
 200                             oid_to_hex(&item->object.oid));
 201        if (type != OBJ_TAG) {
 202                free(data);
 203                return error("Object %s not a tag",
 204                             oid_to_hex(&item->object.oid));
 205        }
 206        ret = parse_tag_buffer(item, data, size);
 207        free(data);
 208        return ret;
 209}