blob: 14ea03178e9c44c8c8a05fb70ab038df5ad1b5f8 [file] [log] [blame]
Johan Herland49c24702013-06-12 02:13:00 +02001#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07002#include "config.h"
Johan Herland49c24702013-06-12 02:13:00 +02003#include "commit.h"
4#include "refs.h"
5#include "notes-utils.h"
Stefan Bellerc1f5eb42018-06-28 18:21:59 -07006#include "repository.h"
Johan Herlandbf9a05b2013-06-12 02:13:01 +02007
8void create_notes_commit(struct notes_tree *t, struct commit_list *parents,
Jeff King3ffefb52014-06-10 17:36:52 -04009 const char *msg, size_t msg_len,
Patryk Obara5078f342018-01-28 01:13:16 +010010 struct object_id *result_oid)
Johan Herlandbf9a05b2013-06-12 02:13:01 +020011{
brian m. carlson18b74e52017-05-06 22:10:04 +000012 struct object_id tree_oid;
Johan Herlandbf9a05b2013-06-12 02:13:01 +020013
14 assert(t->initialized);
15
Patryk Obarabbca96d2018-01-28 01:13:18 +010016 if (write_notes_tree(t, &tree_oid))
Johan Herlandbf9a05b2013-06-12 02:13:01 +020017 die("Failed to write notes tree to database");
18
19 if (!parents) {
20 /* Deduce parent commit from t->ref */
brian m. carlson18b74e52017-05-06 22:10:04 +000021 struct object_id parent_oid;
brian m. carlson34c290a2017-10-15 22:06:56 +000022 if (!read_ref(t->ref, &parent_oid)) {
Stefan Bellerc1f5eb42018-06-28 18:21:59 -070023 struct commit *parent = lookup_commit(the_repository,
24 &parent_oid);
Jeff King5e7d4d32013-10-24 04:53:19 -040025 if (parse_commit(parent))
Johan Herlandbf9a05b2013-06-12 02:13:01 +020026 die("Failed to find/parse commit %s", t->ref);
27 commit_list_insert(parent, &parents);
28 }
29 /* else: t->ref points to nothing, assume root/orphan commit */
30 }
31
Patryk Obara5078f342018-01-28 01:13:16 +010032 if (commit_tree(msg, msg_len, &tree_oid, parents, result_oid, NULL,
33 NULL))
Johan Herlandbf9a05b2013-06-12 02:13:01 +020034 die("Failed to commit notes tree to database");
35}
Johan Herland49c24702013-06-12 02:13:00 +020036
37void commit_notes(struct notes_tree *t, const char *msg)
38{
39 struct strbuf buf = STRBUF_INIT;
brian m. carlson18b74e52017-05-06 22:10:04 +000040 struct object_id commit_oid;
Johan Herland49c24702013-06-12 02:13:00 +020041
42 if (!t)
43 t = &default_notes_tree;
Mike Hommeyee76f922015-10-08 11:54:43 +090044 if (!t->initialized || !t->update_ref || !*t->update_ref)
Johan Herland49c24702013-06-12 02:13:00 +020045 die(_("Cannot commit uninitialized/unreferenced notes tree"));
46 if (!t->dirty)
47 return; /* don't have to commit an unchanged tree */
48
49 /* Prepare commit message and reflog message */
50 strbuf_addstr(&buf, msg);
René Scharfea0d49232014-12-12 20:16:38 +010051 strbuf_complete_line(&buf);
Johan Herland49c24702013-06-12 02:13:00 +020052
Patryk Obara5078f342018-01-28 01:13:16 +010053 create_notes_commit(t, NULL, buf.buf, buf.len, &commit_oid);
Johan Herland49c24702013-06-12 02:13:00 +020054 strbuf_insert(&buf, 0, "notes: ", 7); /* commit message starts at index 7 */
brian m. carlsonae077772017-10-15 22:06:51 +000055 update_ref(buf.buf, t->update_ref, &commit_oid, NULL, 0,
Michael Haggertyf4124112014-04-07 15:47:56 +020056 UPDATE_REFS_DIE_ON_ERR);
Johan Herland49c24702013-06-12 02:13:00 +020057
58 strbuf_release(&buf);
59}
60
Jacob Keller93efcad2015-08-17 14:33:31 -070061int parse_notes_merge_strategy(const char *v, enum notes_merge_strategy *s)
62{
63 if (!strcmp(v, "manual"))
64 *s = NOTES_MERGE_RESOLVE_MANUAL;
65 else if (!strcmp(v, "ours"))
66 *s = NOTES_MERGE_RESOLVE_OURS;
67 else if (!strcmp(v, "theirs"))
68 *s = NOTES_MERGE_RESOLVE_THEIRS;
69 else if (!strcmp(v, "union"))
70 *s = NOTES_MERGE_RESOLVE_UNION;
71 else if (!strcmp(v, "cat_sort_uniq"))
72 *s = NOTES_MERGE_RESOLVE_CAT_SORT_UNIQ;
73 else
74 return -1;
75
76 return 0;
77}
78
Johan Herland49c24702013-06-12 02:13:00 +020079static combine_notes_fn parse_combine_notes_fn(const char *v)
80{
81 if (!strcasecmp(v, "overwrite"))
82 return combine_notes_overwrite;
83 else if (!strcasecmp(v, "ignore"))
84 return combine_notes_ignore;
85 else if (!strcasecmp(v, "concatenate"))
86 return combine_notes_concatenate;
87 else if (!strcasecmp(v, "cat_sort_uniq"))
88 return combine_notes_cat_sort_uniq;
89 else
90 return NULL;
91}
92
93static int notes_rewrite_config(const char *k, const char *v, void *cb)
94{
95 struct notes_rewrite_cfg *c = cb;
Christian Couder59556542013-11-30 21:55:40 +010096 if (starts_with(k, "notes.rewrite.") && !strcmp(k+14, c->cmd)) {
Johan Herland49c24702013-06-12 02:13:00 +020097 c->enabled = git_config_bool(k, v);
98 return 0;
99 } else if (!c->mode_from_env && !strcmp(k, "notes.rewritemode")) {
100 if (!v)
John Keepingaa012e92014-02-16 16:06:02 +0000101 return config_error_nonbool(k);
Johan Herland49c24702013-06-12 02:13:00 +0200102 c->combine = parse_combine_notes_fn(v);
103 if (!c->combine) {
104 error(_("Bad notes.rewriteMode value: '%s'"), v);
105 return 1;
106 }
107 return 0;
108 } else if (!c->refs_from_env && !strcmp(k, "notes.rewriteref")) {
109 /* note that a refs/ prefix is implied in the
110 * underlying for_each_glob_ref */
Christian Couder59556542013-11-30 21:55:40 +0100111 if (starts_with(v, "refs/notes/"))
Johan Herland49c24702013-06-12 02:13:00 +0200112 string_list_add_refs_by_glob(c->refs, v);
113 else
114 warning(_("Refusing to rewrite notes in %s"
115 " (outside of refs/notes/)"), v);
116 return 0;
117 }
118
119 return 0;
120}
121
122
123struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd)
124{
125 struct notes_rewrite_cfg *c = xmalloc(sizeof(struct notes_rewrite_cfg));
126 const char *rewrite_mode_env = getenv(GIT_NOTES_REWRITE_MODE_ENVIRONMENT);
127 const char *rewrite_refs_env = getenv(GIT_NOTES_REWRITE_REF_ENVIRONMENT);
128 c->cmd = cmd;
129 c->enabled = 1;
130 c->combine = combine_notes_concatenate;
131 c->refs = xcalloc(1, sizeof(struct string_list));
132 c->refs->strdup_strings = 1;
133 c->refs_from_env = 0;
134 c->mode_from_env = 0;
135 if (rewrite_mode_env) {
136 c->mode_from_env = 1;
137 c->combine = parse_combine_notes_fn(rewrite_mode_env);
138 if (!c->combine)
Ævar Arnfjörð Bjarmason66f5f6d2017-05-11 21:20:12 +0000139 /*
140 * TRANSLATORS: The first %s is the name of
141 * the environment variable, the second %s is
142 * its value.
143 */
Johan Herland49c24702013-06-12 02:13:00 +0200144 error(_("Bad %s value: '%s'"), GIT_NOTES_REWRITE_MODE_ENVIRONMENT,
145 rewrite_mode_env);
146 }
147 if (rewrite_refs_env) {
148 c->refs_from_env = 1;
149 string_list_add_refs_from_colon_sep(c->refs, rewrite_refs_env);
150 }
151 git_config(notes_rewrite_config, c);
152 if (!c->enabled || !c->refs->nr) {
153 string_list_clear(c->refs, 0);
154 free(c->refs);
155 free(c);
156 return NULL;
157 }
Mike Hommeyee76f922015-10-08 11:54:43 +0900158 c->trees = load_notes_trees(c->refs, NOTES_INIT_WRITABLE);
Johan Herland49c24702013-06-12 02:13:00 +0200159 string_list_clear(c->refs, 0);
160 free(c->refs);
161 return c;
162}
163
164int copy_note_for_rewrite(struct notes_rewrite_cfg *c,
brian m. carlsonbb7e4732017-05-30 10:30:42 -0700165 const struct object_id *from_obj, const struct object_id *to_obj)
Johan Herland49c24702013-06-12 02:13:00 +0200166{
167 int ret = 0;
168 int i;
169 for (i = 0; c->trees[i]; i++)
170 ret = copy_note(c->trees[i], from_obj, to_obj, 1, c->combine) || ret;
171 return ret;
172}
173
174void finish_copy_notes_for_rewrite(struct notes_rewrite_cfg *c, const char *msg)
175{
176 int i;
177 for (i = 0; c->trees[i]; i++) {
178 commit_notes(c->trees[i], msg);
179 free_notes(c->trees[i]);
180 }
181 free(c->trees);
182 free(c);
183}