treewide: reduce includes of cache.h in other headers
[git.git] / notes-utils.c
blobcb88171b7bb9e6def420a63e91487d1671992d1b
1 #include "git-compat-util.h"
2 #include "config.h"
3 #include "commit.h"
4 #include "environment.h"
5 #include "gettext.h"
6 #include "refs.h"
7 #include "notes-utils.h"
8 #include "repository.h"
10 void create_notes_commit(struct repository *r,
11 struct notes_tree *t,
12 struct commit_list *parents,
13 const char *msg, size_t msg_len,
14 struct object_id *result_oid)
16 struct object_id tree_oid;
18 assert(t->initialized);
20 if (write_notes_tree(t, &tree_oid))
21 die("Failed to write notes tree to database");
23 if (!parents) {
24 /* Deduce parent commit from t->ref */
25 struct object_id parent_oid;
26 if (!read_ref(t->ref, &parent_oid)) {
27 struct commit *parent = lookup_commit(r, &parent_oid);
28 if (repo_parse_commit(r, parent))
29 die("Failed to find/parse commit %s", t->ref);
30 commit_list_insert(parent, &parents);
32 /* else: t->ref points to nothing, assume root/orphan commit */
35 if (commit_tree(msg, msg_len, &tree_oid, parents, result_oid, NULL,
36 NULL))
37 die("Failed to commit notes tree to database");
40 void commit_notes(struct repository *r, struct notes_tree *t, const char *msg)
42 struct strbuf buf = STRBUF_INIT;
43 struct object_id commit_oid;
45 if (!t)
46 t = &default_notes_tree;
47 if (!t->initialized || !t->update_ref || !*t->update_ref)
48 die(_("Cannot commit uninitialized/unreferenced notes tree"));
49 if (!t->dirty)
50 return; /* don't have to commit an unchanged tree */
52 /* Prepare commit message and reflog message */
53 strbuf_addstr(&buf, msg);
54 strbuf_complete_line(&buf);
56 create_notes_commit(r, t, NULL, buf.buf, buf.len, &commit_oid);
57 strbuf_insertstr(&buf, 0, "notes: ");
58 update_ref(buf.buf, t->update_ref, &commit_oid, NULL, 0,
59 UPDATE_REFS_DIE_ON_ERR);
61 strbuf_release(&buf);
64 int parse_notes_merge_strategy(const char *v, enum notes_merge_strategy *s)
66 if (!strcmp(v, "manual"))
67 *s = NOTES_MERGE_RESOLVE_MANUAL;
68 else if (!strcmp(v, "ours"))
69 *s = NOTES_MERGE_RESOLVE_OURS;
70 else if (!strcmp(v, "theirs"))
71 *s = NOTES_MERGE_RESOLVE_THEIRS;
72 else if (!strcmp(v, "union"))
73 *s = NOTES_MERGE_RESOLVE_UNION;
74 else if (!strcmp(v, "cat_sort_uniq"))
75 *s = NOTES_MERGE_RESOLVE_CAT_SORT_UNIQ;
76 else
77 return -1;
79 return 0;
82 static combine_notes_fn parse_combine_notes_fn(const char *v)
84 if (!strcasecmp(v, "overwrite"))
85 return combine_notes_overwrite;
86 else if (!strcasecmp(v, "ignore"))
87 return combine_notes_ignore;
88 else if (!strcasecmp(v, "concatenate"))
89 return combine_notes_concatenate;
90 else if (!strcasecmp(v, "cat_sort_uniq"))
91 return combine_notes_cat_sort_uniq;
92 else
93 return NULL;
96 static int notes_rewrite_config(const char *k, const char *v, void *cb)
98 struct notes_rewrite_cfg *c = cb;
99 if (starts_with(k, "notes.rewrite.") && !strcmp(k+14, c->cmd)) {
100 c->enabled = git_config_bool(k, v);
101 return 0;
102 } else if (!c->mode_from_env && !strcmp(k, "notes.rewritemode")) {
103 if (!v)
104 return config_error_nonbool(k);
105 c->combine = parse_combine_notes_fn(v);
106 if (!c->combine) {
107 error(_("Bad notes.rewriteMode value: '%s'"), v);
108 return 1;
110 return 0;
111 } else if (!c->refs_from_env && !strcmp(k, "notes.rewriteref")) {
112 /* note that a refs/ prefix is implied in the
113 * underlying for_each_glob_ref */
114 if (starts_with(v, "refs/notes/"))
115 string_list_add_refs_by_glob(c->refs, v);
116 else
117 warning(_("Refusing to rewrite notes in %s"
118 " (outside of refs/notes/)"), v);
119 return 0;
122 return 0;
126 struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd)
128 struct notes_rewrite_cfg *c = xmalloc(sizeof(struct notes_rewrite_cfg));
129 const char *rewrite_mode_env = getenv(GIT_NOTES_REWRITE_MODE_ENVIRONMENT);
130 const char *rewrite_refs_env = getenv(GIT_NOTES_REWRITE_REF_ENVIRONMENT);
131 c->cmd = cmd;
132 c->enabled = 1;
133 c->combine = combine_notes_concatenate;
134 CALLOC_ARRAY(c->refs, 1);
135 c->refs->strdup_strings = 1;
136 c->refs_from_env = 0;
137 c->mode_from_env = 0;
138 if (rewrite_mode_env) {
139 c->mode_from_env = 1;
140 c->combine = parse_combine_notes_fn(rewrite_mode_env);
141 if (!c->combine)
143 * TRANSLATORS: The first %s is the name of
144 * the environment variable, the second %s is
145 * its value.
147 error(_("Bad %s value: '%s'"), GIT_NOTES_REWRITE_MODE_ENVIRONMENT,
148 rewrite_mode_env);
150 if (rewrite_refs_env) {
151 c->refs_from_env = 1;
152 string_list_add_refs_from_colon_sep(c->refs, rewrite_refs_env);
154 git_config(notes_rewrite_config, c);
155 if (!c->enabled || !c->refs->nr) {
156 string_list_clear(c->refs, 0);
157 free(c->refs);
158 free(c);
159 return NULL;
161 c->trees = load_notes_trees(c->refs, NOTES_INIT_WRITABLE);
162 string_list_clear(c->refs, 0);
163 free(c->refs);
164 return c;
167 int copy_note_for_rewrite(struct notes_rewrite_cfg *c,
168 const struct object_id *from_obj, const struct object_id *to_obj)
170 int ret = 0;
171 int i;
172 for (i = 0; c->trees[i]; i++)
173 ret = copy_note(c->trees[i], from_obj, to_obj, 1, c->combine) || ret;
174 return ret;
177 void finish_copy_notes_for_rewrite(struct repository *r,
178 struct notes_rewrite_cfg *c,
179 const char *msg)
181 int i;
182 for (i = 0; c->trees[i]; i++) {
183 commit_notes(r, c->trees[i], msg);
184 free_notes(c->trees[i]);
186 free(c->trees);
187 free(c);