notes-cache.con commit ref-filter.c: find disjoint pattern prefixes (b31e268)
   1#include "cache.h"
   2#include "notes-cache.h"
   3#include "object-store.h"
   4#include "repository.h"
   5#include "commit.h"
   6#include "refs.h"
   7
   8static int notes_cache_match_validity(struct repository *r,
   9                                      const char *ref,
  10                                      const char *validity)
  11{
  12        struct object_id oid;
  13        struct commit *commit;
  14        struct pretty_print_context pretty_ctx;
  15        struct strbuf msg = STRBUF_INIT;
  16        int ret;
  17
  18        if (read_ref(ref, &oid) < 0)
  19                return 0;
  20
  21        commit = lookup_commit_reference_gently(r, &oid, 1);
  22        if (!commit)
  23                return 0;
  24
  25        memset(&pretty_ctx, 0, sizeof(pretty_ctx));
  26        format_commit_message(commit, "%s", &msg, &pretty_ctx);
  27        strbuf_trim(&msg);
  28
  29        ret = !strcmp(msg.buf, validity);
  30        strbuf_release(&msg);
  31
  32        return ret;
  33}
  34
  35void notes_cache_init(struct repository *r, struct notes_cache *c,
  36                      const char *name, const char *validity)
  37{
  38        struct strbuf ref = STRBUF_INIT;
  39        int flags = NOTES_INIT_WRITABLE;
  40
  41        memset(c, 0, sizeof(*c));
  42        c->validity = xstrdup(validity);
  43
  44        strbuf_addf(&ref, "refs/notes/%s", name);
  45        if (!notes_cache_match_validity(r, ref.buf, validity))
  46                flags |= NOTES_INIT_EMPTY;
  47        init_notes(&c->tree, ref.buf, combine_notes_overwrite, flags);
  48        strbuf_release(&ref);
  49}
  50
  51int notes_cache_write(struct notes_cache *c)
  52{
  53        struct object_id tree_oid, commit_oid;
  54
  55        if (!c || !c->tree.initialized || !c->tree.update_ref ||
  56            !*c->tree.update_ref)
  57                return -1;
  58        if (!c->tree.dirty)
  59                return 0;
  60
  61        if (write_notes_tree(&c->tree, &tree_oid))
  62                return -1;
  63        if (commit_tree(c->validity, strlen(c->validity), &tree_oid, NULL,
  64                        &commit_oid, NULL, NULL) < 0)
  65                return -1;
  66        if (update_ref("update notes cache", c->tree.update_ref, &commit_oid,
  67                       NULL, 0, UPDATE_REFS_QUIET_ON_ERR) < 0)
  68                return -1;
  69
  70        return 0;
  71}
  72
  73char *notes_cache_get(struct notes_cache *c, struct object_id *key_oid,
  74                      size_t *outsize)
  75{
  76        const struct object_id *value_oid;
  77        enum object_type type;
  78        char *value;
  79        unsigned long size;
  80
  81        value_oid = get_note(&c->tree, key_oid);
  82        if (!value_oid)
  83                return NULL;
  84        value = read_object_file(value_oid, &type, &size);
  85
  86        *outsize = size;
  87        return value;
  88}
  89
  90int notes_cache_put(struct notes_cache *c, struct object_id *key_oid,
  91                    const char *data, size_t size)
  92{
  93        struct object_id value_oid;
  94
  95        if (write_object_file(data, size, "blob", &value_oid) < 0)
  96                return -1;
  97        return add_note(&c->tree, key_oid, &value_oid, NULL);
  98}