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