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