2 #include "notes-cache.h"
6 static int notes_cache_match_validity(const char *ref, const char *validity)
10 struct pretty_print_context pretty_ctx;
11 struct strbuf msg = STRBUF_INIT;
14 if (read_ref(ref, oid.hash) < 0)
17 commit = lookup_commit_reference_gently(&oid, 1);
21 memset(&pretty_ctx, 0, sizeof(pretty_ctx));
22 format_commit_message(commit, "%s", &msg, &pretty_ctx);
25 ret = !strcmp(msg.buf, validity);
31 void notes_cache_init(struct notes_cache *c, const char *name,
34 struct strbuf ref = STRBUF_INIT;
35 int flags = NOTES_INIT_WRITABLE;
37 memset(c, 0, sizeof(*c));
38 c->validity = xstrdup(validity);
40 strbuf_addf(&ref, "refs/notes/%s", name);
41 if (!notes_cache_match_validity(ref.buf, validity))
42 flags |= NOTES_INIT_EMPTY;
43 init_notes(&c->tree, ref.buf, combine_notes_overwrite, flags);
47 int notes_cache_write(struct notes_cache *c)
49 struct object_id tree_oid, commit_oid;
51 if (!c || !c->tree.initialized || !c->tree.update_ref ||
57 if (write_notes_tree(&c->tree, tree_oid.hash))
59 if (commit_tree(c->validity, strlen(c->validity), tree_oid.hash, NULL,
60 commit_oid.hash, NULL, NULL) < 0)
62 if (update_ref("update notes cache", c->tree.update_ref, commit_oid.hash,
63 NULL, 0, UPDATE_REFS_QUIET_ON_ERR) < 0)
69 char *notes_cache_get(struct notes_cache *c, struct object_id *key_oid,
72 const struct object_id *value_oid;
73 enum object_type type;
77 value_oid = get_note(&c->tree, key_oid);
80 value = read_sha1_file(value_oid->hash, &type, &size);
86 int notes_cache_put(struct notes_cache *c, struct object_id *key_oid,
87 const char *data, size_t size)
89 struct object_id value_oid;
91 if (write_sha1_file(data, size, "blob", value_oid.hash) < 0)
93 return add_note(&c->tree, key_oid, &value_oid, NULL);