SubmittingPatches: use of older maintenance tracks is an exception
[git.git] / notes-utils.c
blob4a793eb347f74bf031b69d9315d2c678793e4935
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"
9 #include "strbuf.h"
11 void create_notes_commit(struct repository *r,
12 struct notes_tree *t,
13 struct commit_list *parents,
14 const char *msg, size_t msg_len,
15 struct object_id *result_oid)
17 struct object_id tree_oid;
19 assert(t->initialized);
21 if (write_notes_tree(t, &tree_oid))
22 die("Failed to write notes tree to database");
24 if (!parents) {
25 /* Deduce parent commit from t->ref */
26 struct object_id parent_oid;
27 if (!read_ref(t->ref, &parent_oid)) {
28 struct commit *parent = lookup_commit(r, &parent_oid);
29 if (repo_parse_commit(r, parent))
30 die("Failed to find/parse commit %s", t->ref);
31 commit_list_insert(parent, &parents);
33 /* else: t->ref points to nothing, assume root/orphan commit */
36 if (commit_tree(msg, msg_len, &tree_oid, parents, result_oid, NULL,
37 NULL))
38 die("Failed to commit notes tree to database");
41 void commit_notes(struct repository *r, struct notes_tree *t, const char *msg)
43 struct strbuf buf = STRBUF_INIT;
44 struct object_id commit_oid;
46 if (!t)
47 t = &default_notes_tree;
48 if (!t->initialized || !t->update_ref || !*t->update_ref)
49 die(_("Cannot commit uninitialized/unreferenced notes tree"));
50 if (!t->dirty)
51 return; /* don't have to commit an unchanged tree */
53 /* Prepare commit message and reflog message */
54 strbuf_addstr(&buf, msg);
55 strbuf_complete_line(&buf);
57 create_notes_commit(r, t, NULL, buf.buf, buf.len, &commit_oid);
58 strbuf_insertstr(&buf, 0, "notes: ");
59 update_ref(buf.buf, 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, void *cb)
99 struct notes_rewrite_cfg *c = cb;
100 if (starts_with(k, "notes.rewrite.") && !strcmp(k+14, c->cmd)) {
101 c->enabled = git_config_bool(k, v);
102 return 0;
103 } else if (!c->mode_from_env && !strcmp(k, "notes.rewritemode")) {
104 if (!v)
105 return config_error_nonbool(k);
106 c->combine = parse_combine_notes_fn(v);
107 if (!c->combine) {
108 error(_("Bad notes.rewriteMode value: '%s'"), v);
109 return 1;
111 return 0;
112 } else if (!c->refs_from_env && !strcmp(k, "notes.rewriteref")) {
113 /* note that a refs/ prefix is implied in the
114 * underlying for_each_glob_ref */
115 if (starts_with(v, "refs/notes/"))
116 string_list_add_refs_by_glob(c->refs, v);
117 else
118 warning(_("Refusing to rewrite notes in %s"
119 " (outside of refs/notes/)"), v);
120 return 0;
123 return 0;
127 struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd)
129 struct notes_rewrite_cfg *c = xmalloc(sizeof(struct notes_rewrite_cfg));
130 const char *rewrite_mode_env = getenv(GIT_NOTES_REWRITE_MODE_ENVIRONMENT);
131 const char *rewrite_refs_env = getenv(GIT_NOTES_REWRITE_REF_ENVIRONMENT);
132 c->cmd = cmd;
133 c->enabled = 1;
134 c->combine = combine_notes_concatenate;
135 CALLOC_ARRAY(c->refs, 1);
136 c->refs->strdup_strings = 1;
137 c->refs_from_env = 0;
138 c->mode_from_env = 0;
139 if (rewrite_mode_env) {
140 c->mode_from_env = 1;
141 c->combine = parse_combine_notes_fn(rewrite_mode_env);
142 if (!c->combine)
144 * TRANSLATORS: The first %s is the name of
145 * the environment variable, the second %s is
146 * its value.
148 error(_("Bad %s value: '%s'"), GIT_NOTES_REWRITE_MODE_ENVIRONMENT,
149 rewrite_mode_env);
151 if (rewrite_refs_env) {
152 c->refs_from_env = 1;
153 string_list_add_refs_from_colon_sep(c->refs, rewrite_refs_env);
155 git_config(notes_rewrite_config, c);
156 if (!c->enabled || !c->refs->nr) {
157 string_list_clear(c->refs, 0);
158 free(c->refs);
159 free(c);
160 return NULL;
162 c->trees = load_notes_trees(c->refs, NOTES_INIT_WRITABLE);
163 string_list_clear(c->refs, 0);
164 free(c->refs);
165 return c;
168 int copy_note_for_rewrite(struct notes_rewrite_cfg *c,
169 const struct object_id *from_obj, const struct object_id *to_obj)
171 int ret = 0;
172 int i;
173 for (i = 0; c->trees[i]; i++)
174 ret = copy_note(c->trees[i], from_obj, to_obj, 1, c->combine) || ret;
175 return ret;
178 void finish_copy_notes_for_rewrite(struct repository *r,
179 struct notes_rewrite_cfg *c,
180 const char *msg)
182 int i;
183 for (i = 0; c->trees[i]; i++) {
184 commit_notes(r, c->trees[i], msg);
185 free_notes(c->trees[i]);
187 free(c->trees);
188 free(c);