1 #include "git-compat-util.h"
2 #include "notes-cache.h"
3 #include "object-store-ll.h"
5 #include "repository.h"
10 static int notes_cache_match_validity(struct repository
*r
,
15 struct commit
*commit
;
16 struct pretty_print_context pretty_ctx
;
17 struct strbuf msg
= STRBUF_INIT
;
20 if (refs_read_ref(get_main_ref_store(the_repository
), ref
, &oid
) < 0)
23 commit
= lookup_commit_reference_gently(r
, &oid
, 1);
27 memset(&pretty_ctx
, 0, sizeof(pretty_ctx
));
28 repo_format_commit_message(r
, commit
, "%s", &msg
,
32 ret
= !strcmp(msg
.buf
, validity
);
38 void notes_cache_init(struct repository
*r
, struct notes_cache
*c
,
39 const char *name
, const char *validity
)
41 struct strbuf ref
= STRBUF_INIT
;
42 int flags
= NOTES_INIT_WRITABLE
;
44 memset(c
, 0, sizeof(*c
));
45 c
->validity
= xstrdup(validity
);
47 strbuf_addf(&ref
, "refs/notes/%s", name
);
48 if (!notes_cache_match_validity(r
, ref
.buf
, validity
))
49 flags
|= NOTES_INIT_EMPTY
;
50 init_notes(&c
->tree
, ref
.buf
, combine_notes_overwrite
, flags
);
54 int notes_cache_write(struct notes_cache
*c
)
56 struct object_id tree_oid
, commit_oid
;
58 if (!c
|| !c
->tree
.initialized
|| !c
->tree
.update_ref
||
64 if (write_notes_tree(&c
->tree
, &tree_oid
))
66 if (commit_tree(c
->validity
, strlen(c
->validity
), &tree_oid
, NULL
,
67 &commit_oid
, NULL
, NULL
) < 0)
69 if (refs_update_ref(get_main_ref_store(the_repository
), "update notes cache", c
->tree
.update_ref
, &commit_oid
,
70 NULL
, 0, UPDATE_REFS_QUIET_ON_ERR
) < 0)
76 char *notes_cache_get(struct notes_cache
*c
, struct object_id
*key_oid
,
79 const struct object_id
*value_oid
;
80 enum object_type type
;
84 value_oid
= get_note(&c
->tree
, key_oid
);
87 value
= repo_read_object_file(the_repository
, value_oid
, &type
, &size
);
93 int notes_cache_put(struct notes_cache
*c
, struct object_id
*key_oid
,
94 const char *data
, size_t size
)
96 struct object_id value_oid
;
98 if (write_object_file(data
, size
, OBJ_BLOB
, &value_oid
) < 0)
100 return add_note(&c
->tree
, key_oid
, &value_oid
, NULL
);