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