Sync with 'master'
[alt-git.git] / notes-utils.c
blobe33aa86c4b9f736da765f79072a48a28c841d70b
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 (!refs_read_ref(get_main_ref_store(the_repository), 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 refs_update_ref(get_main_ref_store(the_repository), buf.buf,
59 t->update_ref, &commit_oid, NULL, 0,
60 UPDATE_REFS_DIE_ON_ERR);
62 strbuf_release(&buf);
65 int parse_notes_merge_strategy(const char *v, enum notes_merge_strategy *s)
67 if (!strcmp(v, "manual"))
68 *s = NOTES_MERGE_RESOLVE_MANUAL;
69 else if (!strcmp(v, "ours"))
70 *s = NOTES_MERGE_RESOLVE_OURS;
71 else if (!strcmp(v, "theirs"))
72 *s = NOTES_MERGE_RESOLVE_THEIRS;
73 else if (!strcmp(v, "union"))
74 *s = NOTES_MERGE_RESOLVE_UNION;
75 else if (!strcmp(v, "cat_sort_uniq"))
76 *s = NOTES_MERGE_RESOLVE_CAT_SORT_UNIQ;
77 else
78 return -1;
80 return 0;
83 static combine_notes_fn parse_combine_notes_fn(const char *v)
85 if (!strcasecmp(v, "overwrite"))
86 return combine_notes_overwrite;
87 else if (!strcasecmp(v, "ignore"))
88 return combine_notes_ignore;
89 else if (!strcasecmp(v, "concatenate"))
90 return combine_notes_concatenate;
91 else if (!strcasecmp(v, "cat_sort_uniq"))
92 return combine_notes_cat_sort_uniq;
93 else
94 return NULL;
97 static int notes_rewrite_config(const char *k, const char *v,
98 const struct config_context *ctx UNUSED,
99 void *cb)
101 struct notes_rewrite_cfg *c = cb;
102 if (starts_with(k, "notes.rewrite.") && !strcmp(k+14, c->cmd)) {
103 c->enabled = git_config_bool(k, v);
104 return 0;
105 } else if (!c->mode_from_env && !strcmp(k, "notes.rewritemode")) {
106 if (!v)
107 return config_error_nonbool(k);
108 c->combine = parse_combine_notes_fn(v);
109 if (!c->combine) {
110 error(_("Bad notes.rewriteMode value: '%s'"), v);
111 return 1;
113 return 0;
114 } else if (!c->refs_from_env && !strcmp(k, "notes.rewriteref")) {
115 if (!v)
116 return config_error_nonbool(k);
117 /* note that a refs/ prefix is implied in the
118 * underlying for_each_glob_ref */
119 if (starts_with(v, "refs/notes/"))
120 string_list_add_refs_by_glob(c->refs, v);
121 else
122 warning(_("Refusing to rewrite notes in %s"
123 " (outside of refs/notes/)"), v);
124 return 0;
127 return 0;
131 struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd)
133 struct notes_rewrite_cfg *c = xmalloc(sizeof(struct notes_rewrite_cfg));
134 const char *rewrite_mode_env = getenv(GIT_NOTES_REWRITE_MODE_ENVIRONMENT);
135 const char *rewrite_refs_env = getenv(GIT_NOTES_REWRITE_REF_ENVIRONMENT);
136 c->cmd = cmd;
137 c->enabled = 1;
138 c->combine = combine_notes_concatenate;
139 CALLOC_ARRAY(c->refs, 1);
140 c->refs->strdup_strings = 1;
141 c->refs_from_env = 0;
142 c->mode_from_env = 0;
143 if (rewrite_mode_env) {
144 c->mode_from_env = 1;
145 c->combine = parse_combine_notes_fn(rewrite_mode_env);
146 if (!c->combine)
148 * TRANSLATORS: The first %s is the name of
149 * the environment variable, the second %s is
150 * its value.
152 error(_("Bad %s value: '%s'"), GIT_NOTES_REWRITE_MODE_ENVIRONMENT,
153 rewrite_mode_env);
155 if (rewrite_refs_env) {
156 c->refs_from_env = 1;
157 string_list_add_refs_from_colon_sep(c->refs, rewrite_refs_env);
159 git_config(notes_rewrite_config, c);
160 if (!c->enabled || !c->refs->nr) {
161 string_list_clear(c->refs, 0);
162 free(c->refs);
163 free(c);
164 return NULL;
166 c->trees = load_notes_trees(c->refs, NOTES_INIT_WRITABLE);
167 string_list_clear(c->refs, 0);
168 free(c->refs);
169 return c;
172 int copy_note_for_rewrite(struct notes_rewrite_cfg *c,
173 const struct object_id *from_obj, const struct object_id *to_obj)
175 int ret = 0;
176 int i;
177 for (i = 0; c->trees[i]; i++)
178 ret = copy_note(c->trees[i], from_obj, to_obj, 1, c->combine) || ret;
179 return ret;
182 void finish_copy_notes_for_rewrite(struct repository *r,
183 struct notes_rewrite_cfg *c,
184 const char *msg)
186 int i;
187 for (i = 0; c->trees[i]; i++) {
188 commit_notes(r, c->trees[i], msg);
189 free_notes(c->trees[i]);
191 free(c->trees);
192 free(c);