blob: 97c031c26ec7c7c2fdade8478fa039d470820bad [file] [log] [blame]
Elijah Newren98750582023-03-21 06:26:04 +00001#include "git-compat-util.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07002#include "config.h"
Johan Herland49c24702013-06-12 02:13:00 +02003#include "commit.h"
Elijah Newren32a8f512023-03-21 06:26:03 +00004#include "environment.h"
Elijah Newrenf394e092023-03-21 06:25:54 +00005#include "gettext.h"
Johan Herland49c24702013-06-12 02:13:00 +02006#include "refs.h"
7#include "notes-utils.h"
Stefan Bellerc1f5eb42018-06-28 18:21:59 -07008#include "repository.h"
Elijah Newrend4a4f922023-04-22 20:17:26 +00009#include "strbuf.h"
Johan Herlandbf9a05b2013-06-12 02:13:01 +020010
Nguyễn Thái Ngọc Duy1d18d752019-01-12 09:13:23 +070011void create_notes_commit(struct repository *r,
12 struct notes_tree *t,
13 struct commit_list *parents,
Jeff King3ffefb52014-06-10 17:36:52 -040014 const char *msg, size_t msg_len,
Patryk Obara5078f342018-01-28 01:13:16 +010015 struct object_id *result_oid)
Johan Herlandbf9a05b2013-06-12 02:13:01 +020016{
brian m. carlson18b74e52017-05-06 22:10:04 +000017 struct object_id tree_oid;
Johan Herlandbf9a05b2013-06-12 02:13:01 +020018
19 assert(t->initialized);
20
Patryk Obarabbca96d2018-01-28 01:13:18 +010021 if (write_notes_tree(t, &tree_oid))
Johan Herlandbf9a05b2013-06-12 02:13:01 +020022 die("Failed to write notes tree to database");
23
24 if (!parents) {
25 /* Deduce parent commit from t->ref */
brian m. carlson18b74e52017-05-06 22:10:04 +000026 struct object_id parent_oid;
brian m. carlson34c290a2017-10-15 22:06:56 +000027 if (!read_ref(t->ref, &parent_oid)) {
Nguyễn Thái Ngọc Duy1d18d752019-01-12 09:13:23 +070028 struct commit *parent = lookup_commit(r, &parent_oid);
Ævar Arnfjörð Bjarmason4a93b892023-03-28 15:58:58 +020029 if (repo_parse_commit(r, parent))
Johan Herlandbf9a05b2013-06-12 02:13:01 +020030 die("Failed to find/parse commit %s", t->ref);
31 commit_list_insert(parent, &parents);
32 }
33 /* else: t->ref points to nothing, assume root/orphan commit */
34 }
35
Patryk Obara5078f342018-01-28 01:13:16 +010036 if (commit_tree(msg, msg_len, &tree_oid, parents, result_oid, NULL,
37 NULL))
Johan Herlandbf9a05b2013-06-12 02:13:01 +020038 die("Failed to commit notes tree to database");
39}
Johan Herland49c24702013-06-12 02:13:00 +020040
Nguyễn Thái Ngọc Duy1d18d752019-01-12 09:13:23 +070041void commit_notes(struct repository *r, struct notes_tree *t, const char *msg)
Johan Herland49c24702013-06-12 02:13:00 +020042{
43 struct strbuf buf = STRBUF_INIT;
brian m. carlson18b74e52017-05-06 22:10:04 +000044 struct object_id commit_oid;
Johan Herland49c24702013-06-12 02:13:00 +020045
46 if (!t)
47 t = &default_notes_tree;
Mike Hommeyee76f922015-10-08 11:54:43 +090048 if (!t->initialized || !t->update_ref || !*t->update_ref)
Johan Herland49c24702013-06-12 02:13:00 +020049 die(_("Cannot commit uninitialized/unreferenced notes tree"));
50 if (!t->dirty)
51 return; /* don't have to commit an unchanged tree */
52
53 /* Prepare commit message and reflog message */
54 strbuf_addstr(&buf, msg);
René Scharfea0d49232014-12-12 20:16:38 +010055 strbuf_complete_line(&buf);
Johan Herland49c24702013-06-12 02:13:00 +020056
Nguyễn Thái Ngọc Duy1d18d752019-01-12 09:13:23 +070057 create_notes_commit(r, t, NULL, buf.buf, buf.len, &commit_oid);
René Scharfea91cc7f2020-02-09 14:44:23 +010058 strbuf_insertstr(&buf, 0, "notes: ");
brian m. carlsonae077772017-10-15 22:06:51 +000059 update_ref(buf.buf, t->update_ref, &commit_oid, NULL, 0,
Michael Haggertyf4124112014-04-07 15:47:56 +020060 UPDATE_REFS_DIE_ON_ERR);
Johan Herland49c24702013-06-12 02:13:00 +020061
62 strbuf_release(&buf);
63}
64
Jacob Keller93efcad2015-08-17 14:33:31 -070065int parse_notes_merge_strategy(const char *v, enum notes_merge_strategy *s)
66{
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;
79
80 return 0;
81}
82
Johan Herland49c24702013-06-12 02:13:00 +020083static combine_notes_fn parse_combine_notes_fn(const char *v)
84{
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;
95}
96
Glen Chooa4e7e312023-06-28 19:26:22 +000097static int notes_rewrite_config(const char *k, const char *v,
98 const struct config_context *ctx UNUSED,
99 void *cb)
Johan Herland49c24702013-06-12 02:13:00 +0200100{
101 struct notes_rewrite_cfg *c = cb;
Christian Couder59556542013-11-30 21:55:40 +0100102 if (starts_with(k, "notes.rewrite.") && !strcmp(k+14, c->cmd)) {
Johan Herland49c24702013-06-12 02:13:00 +0200103 c->enabled = git_config_bool(k, v);
104 return 0;
105 } else if (!c->mode_from_env && !strcmp(k, "notes.rewritemode")) {
106 if (!v)
John Keepingaa012e92014-02-16 16:06:02 +0000107 return config_error_nonbool(k);
Johan Herland49c24702013-06-12 02:13:00 +0200108 c->combine = parse_combine_notes_fn(v);
109 if (!c->combine) {
110 error(_("Bad notes.rewriteMode value: '%s'"), v);
111 return 1;
112 }
113 return 0;
114 } else if (!c->refs_from_env && !strcmp(k, "notes.rewriteref")) {
115 /* note that a refs/ prefix is implied in the
116 * underlying for_each_glob_ref */
Christian Couder59556542013-11-30 21:55:40 +0100117 if (starts_with(v, "refs/notes/"))
Johan Herland49c24702013-06-12 02:13:00 +0200118 string_list_add_refs_by_glob(c->refs, v);
119 else
120 warning(_("Refusing to rewrite notes in %s"
121 " (outside of refs/notes/)"), v);
122 return 0;
123 }
124
125 return 0;
126}
127
128
129struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd)
130{
131 struct notes_rewrite_cfg *c = xmalloc(sizeof(struct notes_rewrite_cfg));
132 const char *rewrite_mode_env = getenv(GIT_NOTES_REWRITE_MODE_ENVIRONMENT);
133 const char *rewrite_refs_env = getenv(GIT_NOTES_REWRITE_REF_ENVIRONMENT);
134 c->cmd = cmd;
135 c->enabled = 1;
136 c->combine = combine_notes_concatenate;
René Scharfeca56dad2021-03-13 17:17:22 +0100137 CALLOC_ARRAY(c->refs, 1);
Johan Herland49c24702013-06-12 02:13:00 +0200138 c->refs->strdup_strings = 1;
139 c->refs_from_env = 0;
140 c->mode_from_env = 0;
141 if (rewrite_mode_env) {
142 c->mode_from_env = 1;
143 c->combine = parse_combine_notes_fn(rewrite_mode_env);
144 if (!c->combine)
Ævar Arnfjörð Bjarmason66f5f6d2017-05-11 21:20:12 +0000145 /*
146 * TRANSLATORS: The first %s is the name of
147 * the environment variable, the second %s is
148 * its value.
149 */
Johan Herland49c24702013-06-12 02:13:00 +0200150 error(_("Bad %s value: '%s'"), GIT_NOTES_REWRITE_MODE_ENVIRONMENT,
151 rewrite_mode_env);
152 }
153 if (rewrite_refs_env) {
154 c->refs_from_env = 1;
155 string_list_add_refs_from_colon_sep(c->refs, rewrite_refs_env);
156 }
157 git_config(notes_rewrite_config, c);
158 if (!c->enabled || !c->refs->nr) {
159 string_list_clear(c->refs, 0);
160 free(c->refs);
161 free(c);
162 return NULL;
163 }
Mike Hommeyee76f922015-10-08 11:54:43 +0900164 c->trees = load_notes_trees(c->refs, NOTES_INIT_WRITABLE);
Johan Herland49c24702013-06-12 02:13:00 +0200165 string_list_clear(c->refs, 0);
166 free(c->refs);
167 return c;
168}
169
170int copy_note_for_rewrite(struct notes_rewrite_cfg *c,
brian m. carlsonbb7e4732017-05-30 10:30:42 -0700171 const struct object_id *from_obj, const struct object_id *to_obj)
Johan Herland49c24702013-06-12 02:13:00 +0200172{
173 int ret = 0;
174 int i;
175 for (i = 0; c->trees[i]; i++)
176 ret = copy_note(c->trees[i], from_obj, to_obj, 1, c->combine) || ret;
177 return ret;
178}
179
Nguyễn Thái Ngọc Duy1d18d752019-01-12 09:13:23 +0700180void finish_copy_notes_for_rewrite(struct repository *r,
181 struct notes_rewrite_cfg *c,
182 const char *msg)
Johan Herland49c24702013-06-12 02:13:00 +0200183{
184 int i;
185 for (i = 0; c->trees[i]; i++) {
Nguyễn Thái Ngọc Duy1d18d752019-01-12 09:13:23 +0700186 commit_notes(r, c->trees[i], msg);
Johan Herland49c24702013-06-12 02:13:00 +0200187 free_notes(c->trees[i]);
188 }
189 free(c->trees);
190 free(c);
191}