mmapped_ref_iterator: add iterator over a packed-refs file
[git.git] / notes-utils.c
blob9765deb41ab02dba909ec9027701bf2fdc53f310
1 #include "cache.h"
2 #include "config.h"
3 #include "commit.h"
4 #include "refs.h"
5 #include "notes-utils.h"
7 void create_notes_commit(struct notes_tree *t, struct commit_list *parents,
8 const char *msg, size_t msg_len,
9 unsigned char *result_sha1)
11 struct object_id tree_oid;
13 assert(t->initialized);
15 if (write_notes_tree(t, tree_oid.hash))
16 die("Failed to write notes tree to database");
18 if (!parents) {
19 /* Deduce parent commit from t->ref */
20 struct object_id parent_oid;
21 if (!read_ref(t->ref, parent_oid.hash)) {
22 struct commit *parent = lookup_commit(&parent_oid);
23 if (parse_commit(parent))
24 die("Failed to find/parse commit %s", t->ref);
25 commit_list_insert(parent, &parents);
27 /* else: t->ref points to nothing, assume root/orphan commit */
30 if (commit_tree(msg, msg_len, tree_oid.hash, parents, result_sha1, NULL, NULL))
31 die("Failed to commit notes tree to database");
34 void commit_notes(struct notes_tree *t, const char *msg)
36 struct strbuf buf = STRBUF_INIT;
37 struct object_id commit_oid;
39 if (!t)
40 t = &default_notes_tree;
41 if (!t->initialized || !t->update_ref || !*t->update_ref)
42 die(_("Cannot commit uninitialized/unreferenced notes tree"));
43 if (!t->dirty)
44 return; /* don't have to commit an unchanged tree */
46 /* Prepare commit message and reflog message */
47 strbuf_addstr(&buf, msg);
48 strbuf_complete_line(&buf);
50 create_notes_commit(t, NULL, buf.buf, buf.len, commit_oid.hash);
51 strbuf_insert(&buf, 0, "notes: ", 7); /* commit message starts at index 7 */
52 update_ref(buf.buf, t->update_ref, commit_oid.hash, NULL, 0,
53 UPDATE_REFS_DIE_ON_ERR);
55 strbuf_release(&buf);
58 int parse_notes_merge_strategy(const char *v, enum notes_merge_strategy *s)
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;
73 return 0;
76 static combine_notes_fn parse_combine_notes_fn(const char *v)
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;
90 static int notes_rewrite_config(const char *k, const char *v, void *cb)
92 struct notes_rewrite_cfg *c = cb;
93 if (starts_with(k, "notes.rewrite.") && !strcmp(k+14, c->cmd)) {
94 c->enabled = git_config_bool(k, v);
95 return 0;
96 } else if (!c->mode_from_env && !strcmp(k, "notes.rewritemode")) {
97 if (!v)
98 return config_error_nonbool(k);
99 c->combine = parse_combine_notes_fn(v);
100 if (!c->combine) {
101 error(_("Bad notes.rewriteMode value: '%s'"), v);
102 return 1;
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 */
108 if (starts_with(v, "refs/notes/"))
109 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;
116 return 0;
120 struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd)
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)
137 * TRANSLATORS: The first %s is the name of
138 * the environment variable, the second %s is
139 * its value.
141 error(_("Bad %s value: '%s'"), GIT_NOTES_REWRITE_MODE_ENVIRONMENT,
142 rewrite_mode_env);
144 if (rewrite_refs_env) {
145 c->refs_from_env = 1;
146 string_list_add_refs_from_colon_sep(c->refs, rewrite_refs_env);
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;
155 c->trees = load_notes_trees(c->refs, NOTES_INIT_WRITABLE);
156 string_list_clear(c->refs, 0);
157 free(c->refs);
158 return c;
161 int copy_note_for_rewrite(struct notes_rewrite_cfg *c,
162 const struct object_id *from_obj, const struct object_id *to_obj)
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;
171 void finish_copy_notes_for_rewrite(struct notes_rewrite_cfg *c, const char *msg)
173 int i;
174 for (i = 0; c->trees[i]; i++) {
175 commit_notes(c->trees[i], msg);
176 free_notes(c->trees[i]);
178 free(c->trees);
179 free(c);