t-ctype: avoid duplicating class names
[git.git] / notes-utils.c
blob6197a5a4556400603ed7e669d5cccb0db4089e21
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 "strbuf.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,
97 const struct config_context *ctx UNUSED,
98 void *cb)
100 struct notes_rewrite_cfg *c = cb;
101 if (starts_with(k, "notes.rewrite.") && !strcmp(k+14, c->cmd)) {
102 c->enabled = git_config_bool(k, v);
103 return 0;
104 } else if (!c->mode_from_env && !strcmp(k, "notes.rewritemode")) {
105 if (!v)
106 return config_error_nonbool(k);
107 c->combine = parse_combine_notes_fn(v);
108 if (!c->combine) {
109 error(_("Bad notes.rewriteMode value: '%s'"), v);
110 return 1;
112 return 0;
113 } else if (!c->refs_from_env && !strcmp(k, "notes.rewriteref")) {
114 if (!v)
115 return config_error_nonbool(k);
116 /* note that a refs/ prefix is implied in the
117 * underlying for_each_glob_ref */
118 if (starts_with(v, "refs/notes/"))
119 string_list_add_refs_by_glob(c->refs, v);
120 else
121 warning(_("Refusing to rewrite notes in %s"
122 " (outside of refs/notes/)"), v);
123 return 0;
126 return 0;
130 struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd)
132 struct notes_rewrite_cfg *c = xmalloc(sizeof(struct notes_rewrite_cfg));
133 const char *rewrite_mode_env = getenv(GIT_NOTES_REWRITE_MODE_ENVIRONMENT);
134 const char *rewrite_refs_env = getenv(GIT_NOTES_REWRITE_REF_ENVIRONMENT);
135 c->cmd = cmd;
136 c->enabled = 1;
137 c->combine = combine_notes_concatenate;
138 CALLOC_ARRAY(c->refs, 1);
139 c->refs->strdup_strings = 1;
140 c->refs_from_env = 0;
141 c->mode_from_env = 0;
142 if (rewrite_mode_env) {
143 c->mode_from_env = 1;
144 c->combine = parse_combine_notes_fn(rewrite_mode_env);
145 if (!c->combine)
147 * TRANSLATORS: The first %s is the name of
148 * the environment variable, the second %s is
149 * its value.
151 error(_("Bad %s value: '%s'"), GIT_NOTES_REWRITE_MODE_ENVIRONMENT,
152 rewrite_mode_env);
154 if (rewrite_refs_env) {
155 c->refs_from_env = 1;
156 string_list_add_refs_from_colon_sep(c->refs, rewrite_refs_env);
158 git_config(notes_rewrite_config, c);
159 if (!c->enabled || !c->refs->nr) {
160 string_list_clear(c->refs, 0);
161 free(c->refs);
162 free(c);
163 return NULL;
165 c->trees = load_notes_trees(c->refs, NOTES_INIT_WRITABLE);
166 string_list_clear(c->refs, 0);
167 free(c->refs);
168 return c;
171 int copy_note_for_rewrite(struct notes_rewrite_cfg *c,
172 const struct object_id *from_obj, const struct object_id *to_obj)
174 int ret = 0;
175 int i;
176 for (i = 0; c->trees[i]; i++)
177 ret = copy_note(c->trees[i], from_obj, to_obj, 1, c->combine) || ret;
178 return ret;
181 void finish_copy_notes_for_rewrite(struct repository *r,
182 struct notes_rewrite_cfg *c,
183 const char *msg)
185 int i;
186 for (i = 0; c->trees[i]; i++) {
187 commit_notes(r, c->trees[i], msg);
188 free_notes(c->trees[i]);
190 free(c->trees);
191 free(c);