tag.con commit Do linear-time/space rename logic for exact renames (9027f53)
   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                o = parse_object(((struct tag *)o)->tagged->sha1);
  13        if (!o && warn) {
  14                if (!warnlen)
  15                        warnlen = strlen(warn);
  16                error("missing object referenced by '%.*s'", warnlen, warn);
  17        }
  18        return o;
  19}
  20
  21struct tag *lookup_tag(const unsigned char *sha1)
  22{
  23        struct object *obj = lookup_object(sha1);
  24        if (!obj)
  25                return create_object(sha1, OBJ_TAG, alloc_tag_node());
  26        if (!obj->type)
  27                obj->type = OBJ_TAG;
  28        if (obj->type != OBJ_TAG) {
  29                error("Object %s is a %s, not a tag",
  30                      sha1_to_hex(sha1), typename(obj->type));
  31                return NULL;
  32        }
  33        return (struct tag *) obj;
  34}
  35
  36int parse_tag_buffer(struct tag *item, void *data, unsigned long size)
  37{
  38        int typelen, taglen;
  39        unsigned char sha1[20];
  40        const char *type_line, *tag_line, *sig_line;
  41        char type[20];
  42
  43        if (item->object.parsed)
  44                return 0;
  45        item->object.parsed = 1;
  46
  47        if (size < 64)
  48                return -1;
  49        if (memcmp("object ", data, 7) || get_sha1_hex((char *) data + 7, sha1))
  50                return -1;
  51
  52        type_line = (char *) data + 48;
  53        if (memcmp("\ntype ", type_line-1, 6))
  54                return -1;
  55
  56        tag_line = strchr(type_line, '\n');
  57        if (!tag_line || memcmp("tag ", ++tag_line, 4))
  58                return -1;
  59
  60        sig_line = strchr(tag_line, '\n');
  61        if (!sig_line)
  62                return -1;
  63        sig_line++;
  64
  65        typelen = tag_line - type_line - strlen("type \n");
  66        if (typelen >= 20)
  67                return -1;
  68        memcpy(type, type_line + 5, typelen);
  69        type[typelen] = '\0';
  70        taglen = sig_line - tag_line - strlen("tag \n");
  71        item->tag = xmemdupz(tag_line + 4, taglen);
  72
  73        if (!strcmp(type, blob_type)) {
  74                item->tagged = &lookup_blob(sha1)->object;
  75        } else if (!strcmp(type, tree_type)) {
  76                item->tagged = &lookup_tree(sha1)->object;
  77        } else if (!strcmp(type, commit_type)) {
  78                item->tagged = &lookup_commit(sha1)->object;
  79        } else if (!strcmp(type, tag_type)) {
  80                item->tagged = &lookup_tag(sha1)->object;
  81        } else {
  82                error("Unknown type %s", type);
  83                item->tagged = NULL;
  84        }
  85
  86        if (item->tagged && track_object_refs) {
  87                struct object_refs *refs = alloc_object_refs(1);
  88                refs->ref[0] = item->tagged;
  89                set_object_refs(&item->object, refs);
  90        }
  91
  92        return 0;
  93}
  94
  95int parse_tag(struct tag *item)
  96{
  97        enum object_type type;
  98        void *data;
  99        unsigned long size;
 100        int ret;
 101
 102        if (item->object.parsed)
 103                return 0;
 104        data = read_sha1_file(item->object.sha1, &type, &size);
 105        if (!data)
 106                return error("Could not read %s",
 107                             sha1_to_hex(item->object.sha1));
 108        if (type != OBJ_TAG) {
 109                free(data);
 110                return error("Object %s not a tag",
 111                             sha1_to_hex(item->object.sha1));
 112        }
 113        ret = parse_tag_buffer(item, data, size);
 114        free(data);
 115        return ret;
 116}