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