2 #include "notes-cache.h"
3 #include "object-store.h"
7 static int notes_cache_match_validity(const char *ref
, const char *validity
)
10 struct commit
*commit
;
11 struct pretty_print_context pretty_ctx
;
12 struct strbuf msg
= STRBUF_INIT
;
15 if (read_ref(ref
, &oid
) < 0)
18 commit
= lookup_commit_reference_gently(&oid
, 1);
22 memset(&pretty_ctx
, 0, sizeof(pretty_ctx
));
23 format_commit_message(commit
, "%s", &msg
, &pretty_ctx
);
26 ret
= !strcmp(msg
.buf
, validity
);
32 void notes_cache_init(struct notes_cache
*c
, const char *name
,
35 struct strbuf ref
= STRBUF_INIT
;
36 int flags
= NOTES_INIT_WRITABLE
;
38 memset(c
, 0, sizeof(*c
));
39 c
->validity
= xstrdup(validity
);
41 strbuf_addf(&ref
, "refs/notes/%s", name
);
42 if (!notes_cache_match_validity(ref
.buf
, validity
))
43 flags
|= NOTES_INIT_EMPTY
;
44 init_notes(&c
->tree
, ref
.buf
, combine_notes_overwrite
, flags
);
48 int notes_cache_write(struct notes_cache
*c
)
50 struct object_id tree_oid
, commit_oid
;
52 if (!c
|| !c
->tree
.initialized
|| !c
->tree
.update_ref
||
58 if (write_notes_tree(&c
->tree
, &tree_oid
))
60 if (commit_tree(c
->validity
, strlen(c
->validity
), &tree_oid
, NULL
,
61 &commit_oid
, NULL
, NULL
) < 0)
63 if (update_ref("update notes cache", c
->tree
.update_ref
, &commit_oid
,
64 NULL
, 0, UPDATE_REFS_QUIET_ON_ERR
) < 0)
70 char *notes_cache_get(struct notes_cache
*c
, struct object_id
*key_oid
,
73 const struct object_id
*value_oid
;
74 enum object_type type
;
78 value_oid
= get_note(&c
->tree
, key_oid
);
81 value
= read_object_file(value_oid
, &type
, &size
);
87 int notes_cache_put(struct notes_cache
*c
, struct object_id
*key_oid
,
88 const char *data
, size_t size
)
90 struct object_id value_oid
;
92 if (write_object_file(data
, size
, "blob", &value_oid
) < 0)
94 return add_note(&c
->tree
, key_oid
, &value_oid
, NULL
);