git

Форк
0
/
notes-cache.c 
103 строки · 2.5 Кб
1
#define USE_THE_REPOSITORY_VARIABLE
2

3
#include "git-compat-util.h"
4
#include "notes-cache.h"
5
#include "object-store-ll.h"
6
#include "pretty.h"
7
#include "repository.h"
8
#include "commit.h"
9
#include "refs.h"
10
#include "strbuf.h"
11

12
static int notes_cache_match_validity(struct repository *r,
13
				      const char *ref,
14
				      const char *validity)
15
{
16
	struct object_id oid;
17
	struct commit *commit;
18
	struct pretty_print_context pretty_ctx;
19
	struct strbuf msg = STRBUF_INIT;
20
	int ret;
21

22
	if (refs_read_ref(get_main_ref_store(the_repository), ref, &oid) < 0)
23
		return 0;
24

25
	commit = lookup_commit_reference_gently(r, &oid, 1);
26
	if (!commit)
27
		return 0;
28

29
	memset(&pretty_ctx, 0, sizeof(pretty_ctx));
30
	repo_format_commit_message(r, commit, "%s", &msg,
31
				   &pretty_ctx);
32
	strbuf_trim(&msg);
33

34
	ret = !strcmp(msg.buf, validity);
35
	strbuf_release(&msg);
36

37
	return ret;
38
}
39

40
void notes_cache_init(struct repository *r, struct notes_cache *c,
41
		      const char *name, const char *validity)
42
{
43
	struct strbuf ref = STRBUF_INIT;
44
	int flags = NOTES_INIT_WRITABLE;
45

46
	memset(c, 0, sizeof(*c));
47
	c->validity = xstrdup(validity);
48

49
	strbuf_addf(&ref, "refs/notes/%s", name);
50
	if (!notes_cache_match_validity(r, ref.buf, validity))
51
		flags |= NOTES_INIT_EMPTY;
52
	init_notes(&c->tree, ref.buf, combine_notes_overwrite, flags);
53
	strbuf_release(&ref);
54
}
55

56
int notes_cache_write(struct notes_cache *c)
57
{
58
	struct object_id tree_oid, commit_oid;
59

60
	if (!c || !c->tree.initialized || !c->tree.update_ref ||
61
	    !*c->tree.update_ref)
62
		return -1;
63
	if (!c->tree.dirty)
64
		return 0;
65

66
	if (write_notes_tree(&c->tree, &tree_oid))
67
		return -1;
68
	if (commit_tree(c->validity, strlen(c->validity), &tree_oid, NULL,
69
			&commit_oid, NULL, NULL) < 0)
70
		return -1;
71
	if (refs_update_ref(get_main_ref_store(the_repository), "update notes cache", c->tree.update_ref, &commit_oid,
72
			    NULL, 0, UPDATE_REFS_QUIET_ON_ERR) < 0)
73
		return -1;
74

75
	return 0;
76
}
77

78
char *notes_cache_get(struct notes_cache *c, struct object_id *key_oid,
79
		      size_t *outsize)
80
{
81
	const struct object_id *value_oid;
82
	enum object_type type;
83
	char *value;
84
	unsigned long size;
85

86
	value_oid = get_note(&c->tree, key_oid);
87
	if (!value_oid)
88
		return NULL;
89
	value = repo_read_object_file(the_repository, value_oid, &type, &size);
90

91
	*outsize = size;
92
	return value;
93
}
94

95
int notes_cache_put(struct notes_cache *c, struct object_id *key_oid,
96
		    const char *data, size_t size)
97
{
98
	struct object_id value_oid;
99

100
	if (write_object_file(data, size, OBJ_BLOB, &value_oid) < 0)
101
		return -1;
102
	return add_note(&c->tree, key_oid, &value_oid, NULL);
103
}
104

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.