commit: add repository argument to lookup_commit
[git/git.git] / notes-utils.c
CommitLineData
49c24704 1#include "cache.h"
b2141fc1 2#include "config.h"
49c24704
JH
3#include "commit.h"
4#include "refs.h"
5#include "notes-utils.h"
c1f5eb49 6#include "repository.h"
bf9a05ba
JH
7
8void create_notes_commit(struct notes_tree *t, struct commit_list *parents,
3ffefb54 9 const char *msg, size_t msg_len,
5078f344 10 struct object_id *result_oid)
bf9a05ba 11{
18b74e51 12 struct object_id tree_oid;
bf9a05ba
JH
13
14 assert(t->initialized);
15
bbca96d5 16 if (write_notes_tree(t, &tree_oid))
bf9a05ba
JH
17 die("Failed to write notes tree to database");
18
19 if (!parents) {
20 /* Deduce parent commit from t->ref */
18b74e51 21 struct object_id parent_oid;
34c290a6 22 if (!read_ref(t->ref, &parent_oid)) {
c1f5eb49
SB
23 struct commit *parent = lookup_commit(the_repository,
24 &parent_oid);
5e7d4d3e 25 if (parse_commit(parent))
bf9a05ba
JH
26 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
5078f344
PO
32 if (commit_tree(msg, msg_len, &tree_oid, parents, result_oid, NULL,
33 NULL))
bf9a05ba
JH
34 die("Failed to commit notes tree to database");
35}
49c24704
JH
36
37void commit_notes(struct notes_tree *t, const char *msg)
38{
39 struct strbuf buf = STRBUF_INIT;
18b74e51 40 struct object_id commit_oid;
49c24704
JH
41
42 if (!t)
43 t = &default_notes_tree;
ee76f92f 44 if (!t->initialized || !t->update_ref || !*t->update_ref)
49c24704
JH
45 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);
a0d4923d 51 strbuf_complete_line(&buf);
49c24704 52
5078f344 53 create_notes_commit(t, NULL, buf.buf, buf.len, &commit_oid);
49c24704 54 strbuf_insert(&buf, 0, "notes: ", 7); /* commit message starts at index 7 */
ae077771 55 update_ref(buf.buf, t->update_ref, &commit_oid, NULL, 0,
f4124112 56 UPDATE_REFS_DIE_ON_ERR);
49c24704
JH
57
58 strbuf_release(&buf);
59}
60
93efcad3
JK
61int 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
49c24704
JH
79static 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;
59556548 96 if (starts_with(k, "notes.rewrite.") && !strcmp(k+14, c->cmd)) {
49c24704
JH
97 c->enabled = git_config_bool(k, v);
98 return 0;
99 } else if (!c->mode_from_env && !strcmp(k, "notes.rewritemode")) {
100 if (!v)
aa012e90 101 return config_error_nonbool(k);
49c24704
JH
102 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 */
59556548 111 if (starts_with(v, "refs/notes/"))
49c24704
JH
112 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)
66f5f6dc
ÆAB
139 /*
140 * TRANSLATORS: The first %s is the name of
141 * the environment variable, the second %s is
142 * its value.
143 */
49c24704
JH
144 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 }
ee76f92f 158 c->trees = load_notes_trees(c->refs, NOTES_INIT_WRITABLE);
49c24704
JH
159 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,
bb7e4739 165 const struct object_id *from_obj, const struct object_id *to_obj)
49c24704
JH
166{
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}