]> git.ipfire.org Git - thirdparty/git.git/blame - notes-cache.c
Merge branch 'rj/add-i-leak-fix'
[thirdparty/git.git] / notes-cache.c
CommitLineData
4f6728d5 1#include "git-compat-util.h"
a941d5e3 2#include "notes-cache.h"
a034e910 3#include "object-store-ll.h"
d4a4f929 4#include "pretty.h"
21e1ee8f 5#include "repository.h"
a941d5e3
JK
6#include "commit.h"
7#include "refs.h"
a034e910 8#include "strbuf.h"
a941d5e3 9
bd7ad45b
NTND
10static int notes_cache_match_validity(struct repository *r,
11 const char *ref,
12 const char *validity)
a941d5e3 13{
569aa376 14 struct object_id oid;
a941d5e3
JK
15 struct commit *commit;
16 struct pretty_print_context pretty_ctx;
17 struct strbuf msg = STRBUF_INIT;
18 int ret;
19
34c290a6 20 if (read_ref(ref, &oid) < 0)
a941d5e3
JK
21 return 0;
22
bd7ad45b 23 commit = lookup_commit_reference_gently(r, &oid, 1);
a941d5e3
JK
24 if (!commit)
25 return 0;
26
27 memset(&pretty_ctx, 0, sizeof(pretty_ctx));
4a93b899 28 repo_format_commit_message(r, commit, "%s", &msg,
bab82164 29 &pretty_ctx);
a941d5e3
JK
30 strbuf_trim(&msg);
31
32 ret = !strcmp(msg.buf, validity);
33 strbuf_release(&msg);
34
35 return ret;
36}
37
bd7ad45b
NTND
38void notes_cache_init(struct repository *r, struct notes_cache *c,
39 const char *name, const char *validity)
a941d5e3
JK
40{
41 struct strbuf ref = STRBUF_INIT;
ee76f92f 42 int flags = NOTES_INIT_WRITABLE;
a941d5e3
JK
43
44 memset(c, 0, sizeof(*c));
45 c->validity = xstrdup(validity);
46
47 strbuf_addf(&ref, "refs/notes/%s", name);
bd7ad45b 48 if (!notes_cache_match_validity(r, ref.buf, validity))
ee76f92f 49 flags |= NOTES_INIT_EMPTY;
a941d5e3
JK
50 init_notes(&c->tree, ref.buf, combine_notes_overwrite, flags);
51 strbuf_release(&ref);
52}
53
54int notes_cache_write(struct notes_cache *c)
55{
569aa376 56 struct object_id tree_oid, commit_oid;
a941d5e3 57
ee76f92f
MH
58 if (!c || !c->tree.initialized || !c->tree.update_ref ||
59 !*c->tree.update_ref)
a941d5e3
JK
60 return -1;
61 if (!c->tree.dirty)
62 return 0;
63
bbca96d5 64 if (write_notes_tree(&c->tree, &tree_oid))
a941d5e3 65 return -1;
5078f344
PO
66 if (commit_tree(c->validity, strlen(c->validity), &tree_oid, NULL,
67 &commit_oid, NULL, NULL) < 0)
a941d5e3 68 return -1;
ae077771 69 if (update_ref("update notes cache", c->tree.update_ref, &commit_oid,
ee76f92f 70 NULL, 0, UPDATE_REFS_QUIET_ON_ERR) < 0)
a941d5e3
JK
71 return -1;
72
73 return 0;
74}
75
569aa376 76char *notes_cache_get(struct notes_cache *c, struct object_id *key_oid,
a941d5e3
JK
77 size_t *outsize)
78{
9ef72230 79 const struct object_id *value_oid;
a941d5e3
JK
80 enum object_type type;
81 char *value;
82 unsigned long size;
83
5ee8a954 84 value_oid = get_note(&c->tree, key_oid);
9ef72230 85 if (!value_oid)
a941d5e3 86 return NULL;
bc726bd0 87 value = repo_read_object_file(the_repository, value_oid, &type, &size);
a941d5e3
JK
88
89 *outsize = size;
90 return value;
91}
92
569aa376 93int notes_cache_put(struct notes_cache *c, struct object_id *key_oid,
a941d5e3
JK
94 const char *data, size_t size)
95{
569aa376 96 struct object_id value_oid;
a941d5e3 97
c80d226a 98 if (write_object_file(data, size, OBJ_BLOB, &value_oid) < 0)
a941d5e3 99 return -1;
5ee8a954 100 return add_note(&c->tree, key_oid, &value_oid, NULL);
a941d5e3 101}