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