blob: d87e7ca91cdecb158ca93659fb58968e92b18b31 [file] [log] [blame]
Jeff Kinga941d5e2010-04-01 20:07:40 -04001#include "cache.h"
2#include "notes-cache.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -07003#include "object-store.h"
Stefan Beller21e1ee82018-06-28 18:21:57 -07004#include "repository.h"
Jeff Kinga941d5e2010-04-01 20:07:40 -04005#include "commit.h"
6#include "refs.h"
7
8static int notes_cache_match_validity(const char *ref, const char *validity)
9{
brian m. carlson569aa372017-05-06 22:09:58 +000010 struct object_id oid;
Jeff Kinga941d5e2010-04-01 20:07:40 -040011 struct commit *commit;
12 struct pretty_print_context pretty_ctx;
13 struct strbuf msg = STRBUF_INIT;
14 int ret;
15
brian m. carlson34c290a2017-10-15 22:06:56 +000016 if (read_ref(ref, &oid) < 0)
Jeff Kinga941d5e2010-04-01 20:07:40 -040017 return 0;
18
Stefan Beller21e1ee82018-06-28 18:21:57 -070019 commit = lookup_commit_reference_gently(the_repository, &oid, 1);
Jeff Kinga941d5e2010-04-01 20:07:40 -040020 if (!commit)
21 return 0;
22
23 memset(&pretty_ctx, 0, sizeof(pretty_ctx));
24 format_commit_message(commit, "%s", &msg, &pretty_ctx);
25 strbuf_trim(&msg);
26
27 ret = !strcmp(msg.buf, validity);
28 strbuf_release(&msg);
29
30 return ret;
31}
32
33void notes_cache_init(struct notes_cache *c, const char *name,
34 const char *validity)
35{
36 struct strbuf ref = STRBUF_INIT;
Mike Hommeyee76f922015-10-08 11:54:43 +090037 int flags = NOTES_INIT_WRITABLE;
Jeff Kinga941d5e2010-04-01 20:07:40 -040038
39 memset(c, 0, sizeof(*c));
40 c->validity = xstrdup(validity);
41
42 strbuf_addf(&ref, "refs/notes/%s", name);
43 if (!notes_cache_match_validity(ref.buf, validity))
Mike Hommeyee76f922015-10-08 11:54:43 +090044 flags |= NOTES_INIT_EMPTY;
Jeff Kinga941d5e2010-04-01 20:07:40 -040045 init_notes(&c->tree, ref.buf, combine_notes_overwrite, flags);
46 strbuf_release(&ref);
47}
48
49int notes_cache_write(struct notes_cache *c)
50{
brian m. carlson569aa372017-05-06 22:09:58 +000051 struct object_id tree_oid, commit_oid;
Jeff Kinga941d5e2010-04-01 20:07:40 -040052
Mike Hommeyee76f922015-10-08 11:54:43 +090053 if (!c || !c->tree.initialized || !c->tree.update_ref ||
54 !*c->tree.update_ref)
Jeff Kinga941d5e2010-04-01 20:07:40 -040055 return -1;
56 if (!c->tree.dirty)
57 return 0;
58
Patryk Obarabbca96d2018-01-28 01:13:18 +010059 if (write_notes_tree(&c->tree, &tree_oid))
Jeff Kinga941d5e2010-04-01 20:07:40 -040060 return -1;
Patryk Obara5078f342018-01-28 01:13:16 +010061 if (commit_tree(c->validity, strlen(c->validity), &tree_oid, NULL,
62 &commit_oid, NULL, NULL) < 0)
Jeff Kinga941d5e2010-04-01 20:07:40 -040063 return -1;
brian m. carlsonae077772017-10-15 22:06:51 +000064 if (update_ref("update notes cache", c->tree.update_ref, &commit_oid,
Mike Hommeyee76f922015-10-08 11:54:43 +090065 NULL, 0, UPDATE_REFS_QUIET_ON_ERR) < 0)
Jeff Kinga941d5e2010-04-01 20:07:40 -040066 return -1;
67
68 return 0;
69}
70
brian m. carlson569aa372017-05-06 22:09:58 +000071char *notes_cache_get(struct notes_cache *c, struct object_id *key_oid,
Jeff Kinga941d5e2010-04-01 20:07:40 -040072 size_t *outsize)
73{
brian m. carlson9ef72232017-05-30 10:30:40 -070074 const struct object_id *value_oid;
Jeff Kinga941d5e2010-04-01 20:07:40 -040075 enum object_type type;
76 char *value;
77 unsigned long size;
78
brian m. carlson5ee8a952017-05-30 10:30:43 -070079 value_oid = get_note(&c->tree, key_oid);
brian m. carlson9ef72232017-05-30 10:30:40 -070080 if (!value_oid)
Jeff Kinga941d5e2010-04-01 20:07:40 -040081 return NULL;
brian m. carlsonb4f5aca2018-03-12 02:27:53 +000082 value = read_object_file(value_oid, &type, &size);
Jeff Kinga941d5e2010-04-01 20:07:40 -040083
84 *outsize = size;
85 return value;
86}
87
brian m. carlson569aa372017-05-06 22:09:58 +000088int notes_cache_put(struct notes_cache *c, struct object_id *key_oid,
Jeff Kinga941d5e2010-04-01 20:07:40 -040089 const char *data, size_t size)
90{
brian m. carlson569aa372017-05-06 22:09:58 +000091 struct object_id value_oid;
Jeff Kinga941d5e2010-04-01 20:07:40 -040092
Patryk Obaraa09c9852018-01-28 01:13:19 +010093 if (write_object_file(data, size, "blob", &value_oid) < 0)
Jeff Kinga941d5e2010-04-01 20:07:40 -040094 return -1;
brian m. carlson5ee8a952017-05-30 10:30:43 -070095 return add_note(&c->tree, key_oid, &value_oid, NULL);
Jeff Kinga941d5e2010-04-01 20:07:40 -040096}