blob: 9765deb41ab02dba909ec9027701bf2fdc53f310 [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,
9 unsigned char *result_sha1)
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
brian m. carlson18b74e52017-05-06 22:10:04 +000015 if (write_notes_tree(t, tree_oid.hash))
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;
21 if (!read_ref(t->ref, parent_oid.hash)) {
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
brian m. carlson18b74e52017-05-06 22:10:04 +000030 if (commit_tree(msg, msg_len, tree_oid.hash, parents, result_sha1, NULL, NULL))
Johan Herlandbf9a05b2013-06-12 02:13:01 +020031 die("Failed to commit notes tree to database");
32}
Johan Herland49c24702013-06-12 02:13:00 +020033
34void commit_notes(struct notes_tree *t, const char *msg)
35{
36 struct strbuf buf = STRBUF_INIT;
brian m. carlson18b74e52017-05-06 22:10:04 +000037 struct object_id commit_oid;
Johan Herland49c24702013-06-12 02:13:00 +020038
39 if (!t)
40 t = &default_notes_tree;
Mike Hommeyee76f922015-10-08 11:54:43 +090041 if (!t->initialized || !t->update_ref || !*t->update_ref)
Johan Herland49c24702013-06-12 02:13:00 +020042 die(_("Cannot commit uninitialized/unreferenced notes tree"));
43 if (!t->dirty)
44 return; /* don't have to commit an unchanged tree */
45
46 /* Prepare commit message and reflog message */
47 strbuf_addstr(&buf, msg);
René Scharfea0d49232014-12-12 20:16:38 +010048 strbuf_complete_line(&buf);
Johan Herland49c24702013-06-12 02:13:00 +020049
brian m. carlson18b74e52017-05-06 22:10:04 +000050 create_notes_commit(t, NULL, buf.buf, buf.len, commit_oid.hash);
Johan Herland49c24702013-06-12 02:13:00 +020051 strbuf_insert(&buf, 0, "notes: ", 7); /* commit message starts at index 7 */
brian m. carlson18b74e52017-05-06 22:10:04 +000052 update_ref(buf.buf, t->update_ref, commit_oid.hash, NULL, 0,
Michael Haggertyf4124112014-04-07 15:47:56 +020053 UPDATE_REFS_DIE_ON_ERR);
Johan Herland49c24702013-06-12 02:13:00 +020054
55 strbuf_release(&buf);
56}
57
Jacob Keller93efcad2015-08-17 14:33:31 -070058int parse_notes_merge_strategy(const char *v, enum notes_merge_strategy *s)
59{
60 if (!strcmp(v, "manual"))
61 *s = NOTES_MERGE_RESOLVE_MANUAL;
62 else if (!strcmp(v, "ours"))
63 *s = NOTES_MERGE_RESOLVE_OURS;
64 else if (!strcmp(v, "theirs"))
65 *s = NOTES_MERGE_RESOLVE_THEIRS;
66 else if (!strcmp(v, "union"))
67 *s = NOTES_MERGE_RESOLVE_UNION;
68 else if (!strcmp(v, "cat_sort_uniq"))
69 *s = NOTES_MERGE_RESOLVE_CAT_SORT_UNIQ;
70 else
71 return -1;
72
73 return 0;
74}
75
Johan Herland49c24702013-06-12 02:13:00 +020076static combine_notes_fn parse_combine_notes_fn(const char *v)
77{
78 if (!strcasecmp(v, "overwrite"))
79 return combine_notes_overwrite;
80 else if (!strcasecmp(v, "ignore"))
81 return combine_notes_ignore;
82 else if (!strcasecmp(v, "concatenate"))
83 return combine_notes_concatenate;
84 else if (!strcasecmp(v, "cat_sort_uniq"))
85 return combine_notes_cat_sort_uniq;
86 else
87 return NULL;
88}
89
90static int notes_rewrite_config(const char *k, const char *v, void *cb)
91{
92 struct notes_rewrite_cfg *c = cb;
Christian Couder59556542013-11-30 21:55:40 +010093 if (starts_with(k, "notes.rewrite.") && !strcmp(k+14, c->cmd)) {
Johan Herland49c24702013-06-12 02:13:00 +020094 c->enabled = git_config_bool(k, v);
95 return 0;
96 } else if (!c->mode_from_env && !strcmp(k, "notes.rewritemode")) {
97 if (!v)
John Keepingaa012e92014-02-16 16:06:02 +000098 return config_error_nonbool(k);
Johan Herland49c24702013-06-12 02:13:00 +020099 c->combine = parse_combine_notes_fn(v);
100 if (!c->combine) {
101 error(_("Bad notes.rewriteMode value: '%s'"), v);
102 return 1;
103 }
104 return 0;
105 } else if (!c->refs_from_env && !strcmp(k, "notes.rewriteref")) {
106 /* note that a refs/ prefix is implied in the
107 * underlying for_each_glob_ref */
Christian Couder59556542013-11-30 21:55:40 +0100108 if (starts_with(v, "refs/notes/"))
Johan Herland49c24702013-06-12 02:13:00 +0200109 string_list_add_refs_by_glob(c->refs, v);
110 else
111 warning(_("Refusing to rewrite notes in %s"
112 " (outside of refs/notes/)"), v);
113 return 0;
114 }
115
116 return 0;
117}
118
119
120struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd)
121{
122 struct notes_rewrite_cfg *c = xmalloc(sizeof(struct notes_rewrite_cfg));
123 const char *rewrite_mode_env = getenv(GIT_NOTES_REWRITE_MODE_ENVIRONMENT);
124 const char *rewrite_refs_env = getenv(GIT_NOTES_REWRITE_REF_ENVIRONMENT);
125 c->cmd = cmd;
126 c->enabled = 1;
127 c->combine = combine_notes_concatenate;
128 c->refs = xcalloc(1, sizeof(struct string_list));
129 c->refs->strdup_strings = 1;
130 c->refs_from_env = 0;
131 c->mode_from_env = 0;
132 if (rewrite_mode_env) {
133 c->mode_from_env = 1;
134 c->combine = parse_combine_notes_fn(rewrite_mode_env);
135 if (!c->combine)
Ævar Arnfjörð Bjarmason66f5f6d2017-05-11 21:20:12 +0000136 /*
137 * TRANSLATORS: The first %s is the name of
138 * the environment variable, the second %s is
139 * its value.
140 */
Johan Herland49c24702013-06-12 02:13:00 +0200141 error(_("Bad %s value: '%s'"), GIT_NOTES_REWRITE_MODE_ENVIRONMENT,
142 rewrite_mode_env);
143 }
144 if (rewrite_refs_env) {
145 c->refs_from_env = 1;
146 string_list_add_refs_from_colon_sep(c->refs, rewrite_refs_env);
147 }
148 git_config(notes_rewrite_config, c);
149 if (!c->enabled || !c->refs->nr) {
150 string_list_clear(c->refs, 0);
151 free(c->refs);
152 free(c);
153 return NULL;
154 }
Mike Hommeyee76f922015-10-08 11:54:43 +0900155 c->trees = load_notes_trees(c->refs, NOTES_INIT_WRITABLE);
Johan Herland49c24702013-06-12 02:13:00 +0200156 string_list_clear(c->refs, 0);
157 free(c->refs);
158 return c;
159}
160
161int copy_note_for_rewrite(struct notes_rewrite_cfg *c,
brian m. carlsonbb7e4732017-05-30 10:30:42 -0700162 const struct object_id *from_obj, const struct object_id *to_obj)
Johan Herland49c24702013-06-12 02:13:00 +0200163{
164 int ret = 0;
165 int i;
166 for (i = 0; c->trees[i]; i++)
167 ret = copy_note(c->trees[i], from_obj, to_obj, 1, c->combine) || ret;
168 return ret;
169}
170
171void finish_copy_notes_for_rewrite(struct notes_rewrite_cfg *c, const char *msg)
172{
173 int i;
174 for (i = 0; c->trees[i]; i++) {
175 commit_notes(c->trees[i], msg);
176 free_notes(c->trees[i]);
177 }
178 free(c->trees);
179 free(c);
180}