1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "notes-cache.h"
5 #include "object-file.h"
6 #include "object-store.h"
8 #include "repository.h"
13 static int notes_cache_match_validity(struct repository
*r
,
18 struct commit
*commit
;
19 struct pretty_print_context pretty_ctx
;
20 struct strbuf msg
= STRBUF_INIT
;
23 if (refs_read_ref(get_main_ref_store(the_repository
), ref
, &oid
) < 0)
26 commit
= lookup_commit_reference_gently(r
, &oid
, 1);
30 memset(&pretty_ctx
, 0, sizeof(pretty_ctx
));
31 repo_format_commit_message(r
, commit
, "%s", &msg
,
35 ret
= !strcmp(msg
.buf
, validity
);
41 void notes_cache_init(struct repository
*r
, struct notes_cache
*c
,
42 const char *name
, const char *validity
)
44 struct strbuf ref
= STRBUF_INIT
;
45 int flags
= NOTES_INIT_WRITABLE
;
47 memset(c
, 0, sizeof(*c
));
48 c
->validity
= xstrdup(validity
);
50 strbuf_addf(&ref
, "refs/notes/%s", name
);
51 if (!notes_cache_match_validity(r
, ref
.buf
, validity
))
52 flags
|= NOTES_INIT_EMPTY
;
53 init_notes(&c
->tree
, ref
.buf
, combine_notes_overwrite
, flags
);
57 int notes_cache_write(struct notes_cache
*c
)
59 struct object_id tree_oid
, commit_oid
;
61 if (!c
|| !c
->tree
.initialized
|| !c
->tree
.update_ref
||
67 if (write_notes_tree(&c
->tree
, &tree_oid
))
69 if (commit_tree(c
->validity
, strlen(c
->validity
), &tree_oid
, NULL
,
70 &commit_oid
, NULL
, NULL
) < 0)
72 if (refs_update_ref(get_main_ref_store(the_repository
), "update notes cache", c
->tree
.update_ref
, &commit_oid
,
73 NULL
, 0, UPDATE_REFS_QUIET_ON_ERR
) < 0)
79 char *notes_cache_get(struct notes_cache
*c
, struct object_id
*key_oid
,
82 const struct object_id
*value_oid
;
83 enum object_type type
;
87 value_oid
= get_note(&c
->tree
, key_oid
);
90 value
= repo_read_object_file(the_repository
, value_oid
, &type
, &size
);
96 int notes_cache_put(struct notes_cache
*c
, struct object_id
*key_oid
,
97 const char *data
, size_t size
)
99 struct object_id value_oid
;
101 if (write_object_file(data
, size
, OBJ_BLOB
, &value_oid
) < 0)
103 return add_note(&c
->tree
, key_oid
, &value_oid
, NULL
);