refs.c: make prune_ref use a transaction to delete the ref
[git] / notes-cache.c
1 #include "cache.h"
2 #include "notes-cache.h"
3 #include "commit.h"
4 #include "refs.h"
5
6 static int notes_cache_match_validity(const char *ref, const char *validity)
7 {
8         unsigned char sha1[20];
9         struct commit *commit;
10         struct pretty_print_context pretty_ctx;
11         struct strbuf msg = STRBUF_INIT;
12         int ret;
13
14         if (read_ref(ref, sha1) < 0)
15                 return 0;
16
17         commit = lookup_commit_reference_gently(sha1, 1);
18         if (!commit)
19                 return 0;
20
21         memset(&pretty_ctx, 0, sizeof(pretty_ctx));
22         format_commit_message(commit, "%s", &msg, &pretty_ctx);
23         strbuf_trim(&msg);
24
25         ret = !strcmp(msg.buf, validity);
26         strbuf_release(&msg);
27
28         return ret;
29 }
30
31 void notes_cache_init(struct notes_cache *c, const char *name,
32                      const char *validity)
33 {
34         struct strbuf ref = STRBUF_INIT;
35         int flags = 0;
36
37         memset(c, 0, sizeof(*c));
38         c->validity = xstrdup(validity);
39
40         strbuf_addf(&ref, "refs/notes/%s", name);
41         if (!notes_cache_match_validity(ref.buf, validity))
42                 flags = NOTES_INIT_EMPTY;
43         init_notes(&c->tree, ref.buf, combine_notes_overwrite, flags);
44         strbuf_release(&ref);
45 }
46
47 int notes_cache_write(struct notes_cache *c)
48 {
49         unsigned char tree_sha1[20];
50         unsigned char commit_sha1[20];
51         struct strbuf msg = STRBUF_INIT;
52
53         if (!c || !c->tree.initialized || !c->tree.ref || !*c->tree.ref)
54                 return -1;
55         if (!c->tree.dirty)
56                 return 0;
57
58         if (write_notes_tree(&c->tree, tree_sha1))
59                 return -1;
60         strbuf_attach(&msg, c->validity,
61                       strlen(c->validity), strlen(c->validity) + 1);
62         if (commit_tree(&msg, tree_sha1, NULL, commit_sha1, NULL, NULL) < 0)
63                 return -1;
64         if (update_ref("update notes cache", c->tree.ref, commit_sha1, NULL,
65                        0, UPDATE_REFS_QUIET_ON_ERR) < 0)
66                 return -1;
67
68         return 0;
69 }
70
71 char *notes_cache_get(struct notes_cache *c, unsigned char key_sha1[20],
72                       size_t *outsize)
73 {
74         const unsigned char *value_sha1;
75         enum object_type type;
76         char *value;
77         unsigned long size;
78
79         value_sha1 = get_note(&c->tree, key_sha1);
80         if (!value_sha1)
81                 return NULL;
82         value = read_sha1_file(value_sha1, &type, &size);
83
84         *outsize = size;
85         return value;
86 }
87
88 int notes_cache_put(struct notes_cache *c, unsigned char key_sha1[20],
89                     const char *data, size_t size)
90 {
91         unsigned char value_sha1[20];
92
93         if (write_sha1_file(data, size, "blob", value_sha1) < 0)
94                 return -1;
95         return add_note(&c->tree, key_sha1, value_sha1, NULL);
96 }