2 #include "notes-cache.h"
3 #include "object-store.h"
4 #include "repository.h"
8 static int notes_cache_match_validity(const char *ref
, const char *validity
)
11 struct commit
*commit
;
12 struct pretty_print_context pretty_ctx
;
13 struct strbuf msg
= STRBUF_INIT
;
16 if (read_ref(ref
, &oid
) < 0)
19 commit
= lookup_commit_reference_gently(the_repository
, &oid
, 1);
23 memset(&pretty_ctx
, 0, sizeof(pretty_ctx
));
24 format_commit_message(commit
, "%s", &msg
, &pretty_ctx
);
27 ret
= !strcmp(msg
.buf
, validity
);
33 void notes_cache_init(struct notes_cache
*c
, const char *name
,
36 struct strbuf ref
= STRBUF_INIT
;
37 int flags
= NOTES_INIT_WRITABLE
;
39 memset(c
, 0, sizeof(*c
));
40 c
->validity
= xstrdup(validity
);
42 strbuf_addf(&ref
, "refs/notes/%s", name
);
43 if (!notes_cache_match_validity(ref
.buf
, validity
))
44 flags
|= NOTES_INIT_EMPTY
;
45 init_notes(&c
->tree
, ref
.buf
, combine_notes_overwrite
, flags
);
49 int notes_cache_write(struct notes_cache
*c
)
51 struct object_id tree_oid
, commit_oid
;
53 if (!c
|| !c
->tree
.initialized
|| !c
->tree
.update_ref
||
59 if (write_notes_tree(&c
->tree
, &tree_oid
))
61 if (commit_tree(c
->validity
, strlen(c
->validity
), &tree_oid
, NULL
,
62 &commit_oid
, NULL
, NULL
) < 0)
64 if (update_ref("update notes cache", c
->tree
.update_ref
, &commit_oid
,
65 NULL
, 0, UPDATE_REFS_QUIET_ON_ERR
) < 0)
71 char *notes_cache_get(struct notes_cache
*c
, struct object_id
*key_oid
,
74 const struct object_id
*value_oid
;
75 enum object_type type
;
79 value_oid
= get_note(&c
->tree
, key_oid
);
82 value
= read_object_file(value_oid
, &type
, &size
);
88 int notes_cache_put(struct notes_cache
*c
, struct object_id
*key_oid
,
89 const char *data
, size_t size
)
91 struct object_id value_oid
;
93 if (write_object_file(data
, size
, "blob", &value_oid
) < 0)
95 return add_note(&c
->tree
, key_oid
, &value_oid
, NULL
);