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