blob: bca71274beea4d03f1b0af9b0a509f4fa991b669 [file] [log] [blame]
Patrick Steinhardte7da9382024-06-14 08:50:23 +02001#define USE_THE_REPOSITORY_VARIABLE
2
Elijah Newren98750582023-03-21 06:26:04 +00003#include "git-compat-util.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07004#include "config.h"
Johan Herland49c24702013-06-12 02:13:00 +02005#include "commit.h"
Elijah Newren32a8f512023-03-21 06:26:03 +00006#include "environment.h"
Elijah Newrenf394e092023-03-21 06:25:54 +00007#include "gettext.h"
Johan Herland49c24702013-06-12 02:13:00 +02008#include "refs.h"
9#include "notes-utils.h"
Elijah Newrend4a4f922023-04-22 20:17:26 +000010#include "strbuf.h"
Johan Herlandbf9a05b2013-06-12 02:13:01 +020011
Nguyễn Thái Ngọc Duy1d18d752019-01-12 09:13:23 +070012void create_notes_commit(struct repository *r,
13 struct notes_tree *t,
14 struct commit_list *parents,
Jeff King3ffefb52014-06-10 17:36:52 -040015 const char *msg, size_t msg_len,
Patryk Obara5078f342018-01-28 01:13:16 +010016 struct object_id *result_oid)
Johan Herlandbf9a05b2013-06-12 02:13:01 +020017{
brian m. carlson18b74e52017-05-06 22:10:04 +000018 struct object_id tree_oid;
Johan Herlandbf9a05b2013-06-12 02:13:01 +020019
20 assert(t->initialized);
21
Patryk Obarabbca96d2018-01-28 01:13:18 +010022 if (write_notes_tree(t, &tree_oid))
Johan Herlandbf9a05b2013-06-12 02:13:01 +020023 die("Failed to write notes tree to database");
24
25 if (!parents) {
26 /* Deduce parent commit from t->ref */
brian m. carlson18b74e52017-05-06 22:10:04 +000027 struct object_id parent_oid;
Patrick Steinhardt2e5c4752024-05-07 09:11:53 +020028 if (!refs_read_ref(get_main_ref_store(the_repository), t->ref, &parent_oid)) {
Nguyễn Thái Ngọc Duy1d18d752019-01-12 09:13:23 +070029 struct commit *parent = lookup_commit(r, &parent_oid);
Ævar Arnfjörð Bjarmason4a93b892023-03-28 15:58:58 +020030 if (repo_parse_commit(r, parent))
Johan Herlandbf9a05b2013-06-12 02:13:01 +020031 die("Failed to find/parse commit %s", t->ref);
32 commit_list_insert(parent, &parents);
33 }
34 /* else: t->ref points to nothing, assume root/orphan commit */
35 }
36
Patryk Obara5078f342018-01-28 01:13:16 +010037 if (commit_tree(msg, msg_len, &tree_oid, parents, result_oid, NULL,
38 NULL))
Johan Herlandbf9a05b2013-06-12 02:13:01 +020039 die("Failed to commit notes tree to database");
40}
Johan Herland49c24702013-06-12 02:13:00 +020041
Nguyễn Thái Ngọc Duy1d18d752019-01-12 09:13:23 +070042void commit_notes(struct repository *r, struct notes_tree *t, const char *msg)
Johan Herland49c24702013-06-12 02:13:00 +020043{
44 struct strbuf buf = STRBUF_INIT;
brian m. carlson18b74e52017-05-06 22:10:04 +000045 struct object_id commit_oid;
Johan Herland49c24702013-06-12 02:13:00 +020046
47 if (!t)
48 t = &default_notes_tree;
Mike Hommeyee76f922015-10-08 11:54:43 +090049 if (!t->initialized || !t->update_ref || !*t->update_ref)
Johan Herland49c24702013-06-12 02:13:00 +020050 die(_("Cannot commit uninitialized/unreferenced notes tree"));
51 if (!t->dirty)
52 return; /* don't have to commit an unchanged tree */
53
54 /* Prepare commit message and reflog message */
55 strbuf_addstr(&buf, msg);
René Scharfea0d49232014-12-12 20:16:38 +010056 strbuf_complete_line(&buf);
Johan Herland49c24702013-06-12 02:13:00 +020057
Nguyễn Thái Ngọc Duy1d18d752019-01-12 09:13:23 +070058 create_notes_commit(r, t, NULL, buf.buf, buf.len, &commit_oid);
René Scharfea91cc7f2020-02-09 14:44:23 +010059 strbuf_insertstr(&buf, 0, "notes: ");
Patrick Steinhardt2e5c4752024-05-07 09:11:53 +020060 refs_update_ref(get_main_ref_store(the_repository), buf.buf,
61 t->update_ref, &commit_oid, NULL, 0,
62 UPDATE_REFS_DIE_ON_ERR);
Johan Herland49c24702013-06-12 02:13:00 +020063
64 strbuf_release(&buf);
65}
66
Jacob Keller93efcad2015-08-17 14:33:31 -070067int parse_notes_merge_strategy(const char *v, enum notes_merge_strategy *s)
68{
69 if (!strcmp(v, "manual"))
70 *s = NOTES_MERGE_RESOLVE_MANUAL;
71 else if (!strcmp(v, "ours"))
72 *s = NOTES_MERGE_RESOLVE_OURS;
73 else if (!strcmp(v, "theirs"))
74 *s = NOTES_MERGE_RESOLVE_THEIRS;
75 else if (!strcmp(v, "union"))
76 *s = NOTES_MERGE_RESOLVE_UNION;
77 else if (!strcmp(v, "cat_sort_uniq"))
78 *s = NOTES_MERGE_RESOLVE_CAT_SORT_UNIQ;
79 else
80 return -1;
81
82 return 0;
83}
84
Johan Herland49c24702013-06-12 02:13:00 +020085static combine_notes_fn parse_combine_notes_fn(const char *v)
86{
87 if (!strcasecmp(v, "overwrite"))
88 return combine_notes_overwrite;
89 else if (!strcasecmp(v, "ignore"))
90 return combine_notes_ignore;
91 else if (!strcasecmp(v, "concatenate"))
92 return combine_notes_concatenate;
93 else if (!strcasecmp(v, "cat_sort_uniq"))
94 return combine_notes_cat_sort_uniq;
95 else
96 return NULL;
97}
98
Glen Chooa4e7e312023-06-28 19:26:22 +000099static int notes_rewrite_config(const char *k, const char *v,
100 const struct config_context *ctx UNUSED,
101 void *cb)
Johan Herland49c24702013-06-12 02:13:00 +0200102{
103 struct notes_rewrite_cfg *c = cb;
Christian Couder59556542013-11-30 21:55:40 +0100104 if (starts_with(k, "notes.rewrite.") && !strcmp(k+14, c->cmd)) {
Johan Herland49c24702013-06-12 02:13:00 +0200105 c->enabled = git_config_bool(k, v);
106 return 0;
107 } else if (!c->mode_from_env && !strcmp(k, "notes.rewritemode")) {
108 if (!v)
John Keepingaa012e92014-02-16 16:06:02 +0000109 return config_error_nonbool(k);
Johan Herland49c24702013-06-12 02:13:00 +0200110 c->combine = parse_combine_notes_fn(v);
111 if (!c->combine) {
112 error(_("Bad notes.rewriteMode value: '%s'"), v);
113 return 1;
114 }
115 return 0;
116 } else if (!c->refs_from_env && !strcmp(k, "notes.rewriteref")) {
Jeff Kingba176db2023-12-07 02:11:14 -0500117 if (!v)
118 return config_error_nonbool(k);
Johan Herland49c24702013-06-12 02:13:00 +0200119 /* note that a refs/ prefix is implied in the
120 * underlying for_each_glob_ref */
Christian Couder59556542013-11-30 21:55:40 +0100121 if (starts_with(v, "refs/notes/"))
Johan Herland49c24702013-06-12 02:13:00 +0200122 string_list_add_refs_by_glob(c->refs, v);
123 else
124 warning(_("Refusing to rewrite notes in %s"
125 " (outside of refs/notes/)"), v);
126 return 0;
127 }
128
129 return 0;
130}
131
132
133struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd)
134{
135 struct notes_rewrite_cfg *c = xmalloc(sizeof(struct notes_rewrite_cfg));
136 const char *rewrite_mode_env = getenv(GIT_NOTES_REWRITE_MODE_ENVIRONMENT);
137 const char *rewrite_refs_env = getenv(GIT_NOTES_REWRITE_REF_ENVIRONMENT);
138 c->cmd = cmd;
139 c->enabled = 1;
140 c->combine = combine_notes_concatenate;
René Scharfeca56dad2021-03-13 17:17:22 +0100141 CALLOC_ARRAY(c->refs, 1);
Johan Herland49c24702013-06-12 02:13:00 +0200142 c->refs->strdup_strings = 1;
143 c->refs_from_env = 0;
144 c->mode_from_env = 0;
145 if (rewrite_mode_env) {
146 c->mode_from_env = 1;
147 c->combine = parse_combine_notes_fn(rewrite_mode_env);
148 if (!c->combine)
Ævar Arnfjörð Bjarmason66f5f6d2017-05-11 21:20:12 +0000149 /*
150 * TRANSLATORS: The first %s is the name of
151 * the environment variable, the second %s is
152 * its value.
153 */
Johan Herland49c24702013-06-12 02:13:00 +0200154 error(_("Bad %s value: '%s'"), GIT_NOTES_REWRITE_MODE_ENVIRONMENT,
155 rewrite_mode_env);
156 }
157 if (rewrite_refs_env) {
158 c->refs_from_env = 1;
159 string_list_add_refs_from_colon_sep(c->refs, rewrite_refs_env);
160 }
161 git_config(notes_rewrite_config, c);
162 if (!c->enabled || !c->refs->nr) {
163 string_list_clear(c->refs, 0);
164 free(c->refs);
165 free(c);
166 return NULL;
167 }
Mike Hommeyee76f922015-10-08 11:54:43 +0900168 c->trees = load_notes_trees(c->refs, NOTES_INIT_WRITABLE);
Johan Herland49c24702013-06-12 02:13:00 +0200169 string_list_clear(c->refs, 0);
170 free(c->refs);
171 return c;
172}
173
174int copy_note_for_rewrite(struct notes_rewrite_cfg *c,
brian m. carlsonbb7e4732017-05-30 10:30:42 -0700175 const struct object_id *from_obj, const struct object_id *to_obj)
Johan Herland49c24702013-06-12 02:13:00 +0200176{
177 int ret = 0;
178 int i;
179 for (i = 0; c->trees[i]; i++)
180 ret = copy_note(c->trees[i], from_obj, to_obj, 1, c->combine) || ret;
181 return ret;
182}
183
Nguyễn Thái Ngọc Duy1d18d752019-01-12 09:13:23 +0700184void finish_copy_notes_for_rewrite(struct repository *r,
185 struct notes_rewrite_cfg *c,
186 const char *msg)
Johan Herland49c24702013-06-12 02:13:00 +0200187{
188 int i;
189 for (i = 0; c->trees[i]; i++) {
Nguyễn Thái Ngọc Duy1d18d752019-01-12 09:13:23 +0700190 commit_notes(r, c->trees[i], msg);
Johan Herland49c24702013-06-12 02:13:00 +0200191 free_notes(c->trees[i]);
192 }
193 free(c->trees);
194 free(c);
195}