blob: 02407fe2a7327947dda6bf755405d2778ae450ae [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"
Johan Herlandbf9a05b2013-06-12 02:13:01 +02006
7void create_notes_commit(struct notes_tree *t, struct commit_list *parents,
Jeff King3ffefb52014-06-10 17:36:52 -04008 const char *msg, size_t msg_len,
Patryk Obara5078f342018-01-28 01:13:16 +01009 struct object_id *result_oid)
Johan Herlandbf9a05b2013-06-12 02:13:01 +020010{
brian m. carlson18b74e52017-05-06 22:10:04 +000011 struct object_id tree_oid;
Johan Herlandbf9a05b2013-06-12 02:13:01 +020012
13 assert(t->initialized);
14
Patryk Obarabbca96d2018-01-28 01:13:18 +010015 if (write_notes_tree(t, &tree_oid))
Johan Herlandbf9a05b2013-06-12 02:13:01 +020016 die("Failed to write notes tree to database");
17
18 if (!parents) {
19 /* Deduce parent commit from t->ref */
brian m. carlson18b74e52017-05-06 22:10:04 +000020 struct object_id parent_oid;
brian m. carlson34c290a2017-10-15 22:06:56 +000021 if (!read_ref(t->ref, &parent_oid)) {
brian m. carlsonbc832662017-05-06 22:10:10 +000022 struct commit *parent = lookup_commit(&parent_oid);
Jeff King5e7d4d32013-10-24 04:53:19 -040023 if (parse_commit(parent))
Johan Herlandbf9a05b2013-06-12 02:13:01 +020024 die("Failed to find/parse commit %s", t->ref);
25 commit_list_insert(parent, &parents);
26 }
27 /* else: t->ref points to nothing, assume root/orphan commit */
28 }
29
Patryk Obara5078f342018-01-28 01:13:16 +010030 if (commit_tree(msg, msg_len, &tree_oid, parents, result_oid, NULL,
31 NULL))
Johan Herlandbf9a05b2013-06-12 02:13:01 +020032 die("Failed to commit notes tree to database");
33}
Johan Herland49c24702013-06-12 02:13:00 +020034
35void commit_notes(struct notes_tree *t, const char *msg)
36{
37 struct strbuf buf = STRBUF_INIT;
brian m. carlson18b74e52017-05-06 22:10:04 +000038 struct object_id commit_oid;
Johan Herland49c24702013-06-12 02:13:00 +020039
40 if (!t)
41 t = &default_notes_tree;
Mike Hommeyee76f922015-10-08 11:54:43 +090042 if (!t->initialized || !t->update_ref || !*t->update_ref)
Johan Herland49c24702013-06-12 02:13:00 +020043 die(_("Cannot commit uninitialized/unreferenced notes tree"));
44 if (!t->dirty)
45 return; /* don't have to commit an unchanged tree */
46
47 /* Prepare commit message and reflog message */
48 strbuf_addstr(&buf, msg);
René Scharfea0d49232014-12-12 20:16:38 +010049 strbuf_complete_line(&buf);
Johan Herland49c24702013-06-12 02:13:00 +020050
Patryk Obara5078f342018-01-28 01:13:16 +010051 create_notes_commit(t, NULL, buf.buf, buf.len, &commit_oid);
Johan Herland49c24702013-06-12 02:13:00 +020052 strbuf_insert(&buf, 0, "notes: ", 7); /* commit message starts at index 7 */
brian m. carlsonae077772017-10-15 22:06:51 +000053 update_ref(buf.buf, t->update_ref, &commit_oid, NULL, 0,
Michael Haggertyf4124112014-04-07 15:47:56 +020054 UPDATE_REFS_DIE_ON_ERR);
Johan Herland49c24702013-06-12 02:13:00 +020055
56 strbuf_release(&buf);
57}
58
Jacob Keller93efcad2015-08-17 14:33:31 -070059int parse_notes_merge_strategy(const char *v, enum notes_merge_strategy *s)
60{
61 if (!strcmp(v, "manual"))
62 *s = NOTES_MERGE_RESOLVE_MANUAL;
63 else if (!strcmp(v, "ours"))
64 *s = NOTES_MERGE_RESOLVE_OURS;
65 else if (!strcmp(v, "theirs"))
66 *s = NOTES_MERGE_RESOLVE_THEIRS;
67 else if (!strcmp(v, "union"))
68 *s = NOTES_MERGE_RESOLVE_UNION;
69 else if (!strcmp(v, "cat_sort_uniq"))
70 *s = NOTES_MERGE_RESOLVE_CAT_SORT_UNIQ;
71 else
72 return -1;
73
74 return 0;
75}
76
Johan Herland49c24702013-06-12 02:13:00 +020077static combine_notes_fn parse_combine_notes_fn(const char *v)
78{
79 if (!strcasecmp(v, "overwrite"))
80 return combine_notes_overwrite;
81 else if (!strcasecmp(v, "ignore"))
82 return combine_notes_ignore;
83 else if (!strcasecmp(v, "concatenate"))
84 return combine_notes_concatenate;
85 else if (!strcasecmp(v, "cat_sort_uniq"))
86 return combine_notes_cat_sort_uniq;
87 else
88 return NULL;
89}
90
91static int notes_rewrite_config(const char *k, const char *v, void *cb)
92{
93 struct notes_rewrite_cfg *c = cb;
Christian Couder59556542013-11-30 21:55:40 +010094 if (starts_with(k, "notes.rewrite.") && !strcmp(k+14, c->cmd)) {
Johan Herland49c24702013-06-12 02:13:00 +020095 c->enabled = git_config_bool(k, v);
96 return 0;
97 } else if (!c->mode_from_env && !strcmp(k, "notes.rewritemode")) {
98 if (!v)
John Keepingaa012e92014-02-16 16:06:02 +000099 return config_error_nonbool(k);
Johan Herland49c24702013-06-12 02:13:00 +0200100 c->combine = parse_combine_notes_fn(v);
101 if (!c->combine) {
102 error(_("Bad notes.rewriteMode value: '%s'"), v);
103 return 1;
104 }
105 return 0;
106 } else if (!c->refs_from_env && !strcmp(k, "notes.rewriteref")) {
107 /* note that a refs/ prefix is implied in the
108 * underlying for_each_glob_ref */
Christian Couder59556542013-11-30 21:55:40 +0100109 if (starts_with(v, "refs/notes/"))
Johan Herland49c24702013-06-12 02:13:00 +0200110 string_list_add_refs_by_glob(c->refs, v);
111 else
112 warning(_("Refusing to rewrite notes in %s"
113 " (outside of refs/notes/)"), v);
114 return 0;
115 }
116
117 return 0;
118}
119
120
121struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd)
122{
123 struct notes_rewrite_cfg *c = xmalloc(sizeof(struct notes_rewrite_cfg));
124 const char *rewrite_mode_env = getenv(GIT_NOTES_REWRITE_MODE_ENVIRONMENT);
125 const char *rewrite_refs_env = getenv(GIT_NOTES_REWRITE_REF_ENVIRONMENT);
126 c->cmd = cmd;
127 c->enabled = 1;
128 c->combine = combine_notes_concatenate;
129 c->refs = xcalloc(1, sizeof(struct string_list));
130 c->refs->strdup_strings = 1;
131 c->refs_from_env = 0;
132 c->mode_from_env = 0;
133 if (rewrite_mode_env) {
134 c->mode_from_env = 1;
135 c->combine = parse_combine_notes_fn(rewrite_mode_env);
136 if (!c->combine)
Ævar Arnfjörð Bjarmason66f5f6d2017-05-11 21:20:12 +0000137 /*
138 * TRANSLATORS: The first %s is the name of
139 * the environment variable, the second %s is
140 * its value.
141 */
Johan Herland49c24702013-06-12 02:13:00 +0200142 error(_("Bad %s value: '%s'"), GIT_NOTES_REWRITE_MODE_ENVIRONMENT,
143 rewrite_mode_env);
144 }
145 if (rewrite_refs_env) {
146 c->refs_from_env = 1;
147 string_list_add_refs_from_colon_sep(c->refs, rewrite_refs_env);
148 }
149 git_config(notes_rewrite_config, c);
150 if (!c->enabled || !c->refs->nr) {
151 string_list_clear(c->refs, 0);
152 free(c->refs);
153 free(c);
154 return NULL;
155 }
Mike Hommeyee76f922015-10-08 11:54:43 +0900156 c->trees = load_notes_trees(c->refs, NOTES_INIT_WRITABLE);
Johan Herland49c24702013-06-12 02:13:00 +0200157 string_list_clear(c->refs, 0);
158 free(c->refs);
159 return c;
160}
161
162int copy_note_for_rewrite(struct notes_rewrite_cfg *c,
brian m. carlsonbb7e4732017-05-30 10:30:42 -0700163 const struct object_id *from_obj, const struct object_id *to_obj)
Johan Herland49c24702013-06-12 02:13:00 +0200164{
165 int ret = 0;
166 int i;
167 for (i = 0; c->trees[i]; i++)
168 ret = copy_note(c->trees[i], from_obj, to_obj, 1, c->combine) || ret;
169 return ret;
170}
171
172void finish_copy_notes_for_rewrite(struct notes_rewrite_cfg *c, const char *msg)
173{
174 int i;
175 for (i = 0; c->trees[i]; i++) {
176 commit_notes(c->trees[i], msg);
177 free_notes(c->trees[i]);
178 }
179 free(c->trees);
180 free(c);
181}