]> git.ipfire.org Git - thirdparty/git.git/blame - notes-utils.c
Git 2.13
[thirdparty/git.git] / notes-utils.c
CommitLineData
49c24704
JH
1#include "cache.h"
2#include "commit.h"
3#include "refs.h"
4#include "notes-utils.h"
bf9a05ba
JH
5
6void create_notes_commit(struct notes_tree *t, struct commit_list *parents,
3ffefb54
JK
7 const char *msg, size_t msg_len,
8 unsigned char *result_sha1)
bf9a05ba
JH
9{
10 unsigned char tree_sha1[20];
11
12 assert(t->initialized);
13
14 if (write_notes_tree(t, tree_sha1))
15 die("Failed to write notes tree to database");
16
17 if (!parents) {
18 /* Deduce parent commit from t->ref */
19 unsigned char parent_sha1[20];
20 if (!read_ref(t->ref, parent_sha1)) {
21 struct commit *parent = lookup_commit(parent_sha1);
5e7d4d3e 22 if (parse_commit(parent))
bf9a05ba
JH
23 die("Failed to find/parse commit %s", t->ref);
24 commit_list_insert(parent, &parents);
25 }
26 /* else: t->ref points to nothing, assume root/orphan commit */
27 }
28
3ffefb54 29 if (commit_tree(msg, msg_len, tree_sha1, parents, result_sha1, NULL, NULL))
bf9a05ba
JH
30 die("Failed to commit notes tree to database");
31}
49c24704
JH
32
33void commit_notes(struct notes_tree *t, const char *msg)
34{
35 struct strbuf buf = STRBUF_INIT;
36 unsigned char commit_sha1[20];
37
38 if (!t)
39 t = &default_notes_tree;
ee76f92f 40 if (!t->initialized || !t->update_ref || !*t->update_ref)
49c24704
JH
41 die(_("Cannot commit uninitialized/unreferenced notes tree"));
42 if (!t->dirty)
43 return; /* don't have to commit an unchanged tree */
44
45 /* Prepare commit message and reflog message */
46 strbuf_addstr(&buf, msg);
a0d4923d 47 strbuf_complete_line(&buf);
49c24704 48
3ffefb54 49 create_notes_commit(t, NULL, buf.buf, buf.len, commit_sha1);
49c24704 50 strbuf_insert(&buf, 0, "notes: ", 7); /* commit message starts at index 7 */
ee76f92f 51 update_ref(buf.buf, t->update_ref, commit_sha1, NULL, 0,
f4124112 52 UPDATE_REFS_DIE_ON_ERR);
49c24704
JH
53
54 strbuf_release(&buf);
55}
56
93efcad3
JK
57int parse_notes_merge_strategy(const char *v, enum notes_merge_strategy *s)
58{
59 if (!strcmp(v, "manual"))
60 *s = NOTES_MERGE_RESOLVE_MANUAL;
61 else if (!strcmp(v, "ours"))
62 *s = NOTES_MERGE_RESOLVE_OURS;
63 else if (!strcmp(v, "theirs"))
64 *s = NOTES_MERGE_RESOLVE_THEIRS;
65 else if (!strcmp(v, "union"))
66 *s = NOTES_MERGE_RESOLVE_UNION;
67 else if (!strcmp(v, "cat_sort_uniq"))
68 *s = NOTES_MERGE_RESOLVE_CAT_SORT_UNIQ;
69 else
70 return -1;
71
72 return 0;
73}
74
49c24704
JH
75static combine_notes_fn parse_combine_notes_fn(const char *v)
76{
77 if (!strcasecmp(v, "overwrite"))
78 return combine_notes_overwrite;
79 else if (!strcasecmp(v, "ignore"))
80 return combine_notes_ignore;
81 else if (!strcasecmp(v, "concatenate"))
82 return combine_notes_concatenate;
83 else if (!strcasecmp(v, "cat_sort_uniq"))
84 return combine_notes_cat_sort_uniq;
85 else
86 return NULL;
87}
88
89static int notes_rewrite_config(const char *k, const char *v, void *cb)
90{
91 struct notes_rewrite_cfg *c = cb;
59556548 92 if (starts_with(k, "notes.rewrite.") && !strcmp(k+14, c->cmd)) {
49c24704
JH
93 c->enabled = git_config_bool(k, v);
94 return 0;
95 } else if (!c->mode_from_env && !strcmp(k, "notes.rewritemode")) {
96 if (!v)
aa012e90 97 return config_error_nonbool(k);
49c24704
JH
98 c->combine = parse_combine_notes_fn(v);
99 if (!c->combine) {
100 error(_("Bad notes.rewriteMode value: '%s'"), v);
101 return 1;
102 }
103 return 0;
104 } else if (!c->refs_from_env && !strcmp(k, "notes.rewriteref")) {
105 /* note that a refs/ prefix is implied in the
106 * underlying for_each_glob_ref */
59556548 107 if (starts_with(v, "refs/notes/"))
49c24704
JH
108 string_list_add_refs_by_glob(c->refs, v);
109 else
110 warning(_("Refusing to rewrite notes in %s"
111 " (outside of refs/notes/)"), v);
112 return 0;
113 }
114
115 return 0;
116}
117
118
119struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd)
120{
121 struct notes_rewrite_cfg *c = xmalloc(sizeof(struct notes_rewrite_cfg));
122 const char *rewrite_mode_env = getenv(GIT_NOTES_REWRITE_MODE_ENVIRONMENT);
123 const char *rewrite_refs_env = getenv(GIT_NOTES_REWRITE_REF_ENVIRONMENT);
124 c->cmd = cmd;
125 c->enabled = 1;
126 c->combine = combine_notes_concatenate;
127 c->refs = xcalloc(1, sizeof(struct string_list));
128 c->refs->strdup_strings = 1;
129 c->refs_from_env = 0;
130 c->mode_from_env = 0;
131 if (rewrite_mode_env) {
132 c->mode_from_env = 1;
133 c->combine = parse_combine_notes_fn(rewrite_mode_env);
134 if (!c->combine)
135 /* TRANSLATORS: The first %s is the name of the
136 environment variable, the second %s is its value */
137 error(_("Bad %s value: '%s'"), GIT_NOTES_REWRITE_MODE_ENVIRONMENT,
138 rewrite_mode_env);
139 }
140 if (rewrite_refs_env) {
141 c->refs_from_env = 1;
142 string_list_add_refs_from_colon_sep(c->refs, rewrite_refs_env);
143 }
144 git_config(notes_rewrite_config, c);
145 if (!c->enabled || !c->refs->nr) {
146 string_list_clear(c->refs, 0);
147 free(c->refs);
148 free(c);
149 return NULL;
150 }
ee76f92f 151 c->trees = load_notes_trees(c->refs, NOTES_INIT_WRITABLE);
49c24704
JH
152 string_list_clear(c->refs, 0);
153 free(c->refs);
154 return c;
155}
156
157int copy_note_for_rewrite(struct notes_rewrite_cfg *c,
158 const unsigned char *from_obj, const unsigned char *to_obj)
159{
160 int ret = 0;
161 int i;
162 for (i = 0; c->trees[i]; i++)
163 ret = copy_note(c->trees[i], from_obj, to_obj, 1, c->combine) || ret;
164 return ret;
165}
166
167void finish_copy_notes_for_rewrite(struct notes_rewrite_cfg *c, const char *msg)
168{
169 int i;
170 for (i = 0; c->trees[i]; i++) {
171 commit_notes(c->trees[i], msg);
172 free_notes(c->trees[i]);
173 }
174 free(c->trees);
175 free(c);
176}