archive.c: avoid access to the_index
[git.git] / notes-utils.c
blob14ea03178e9c44c8c8a05fb70ab038df5ad1b5f8
1 #include "cache.h"
2 #include "config.h"
3 #include "commit.h"
4 #include "refs.h"
5 #include "notes-utils.h"
6 #include "repository.h"
8 void create_notes_commit(struct notes_tree *t, struct commit_list *parents,
9 const char *msg, size_t msg_len,
10 struct object_id *result_oid)
12 struct object_id tree_oid;
14 assert(t->initialized);
16 if (write_notes_tree(t, &tree_oid))
17 die("Failed to write notes tree to database");
19 if (!parents) {
20 /* Deduce parent commit from t->ref */
21 struct object_id parent_oid;
22 if (!read_ref(t->ref, &parent_oid)) {
23 struct commit *parent = lookup_commit(the_repository,
24 &parent_oid);
25 if (parse_commit(parent))
26 die("Failed to find/parse commit %s", t->ref);
27 commit_list_insert(parent, &parents);
29 /* else: t->ref points to nothing, assume root/orphan commit */
32 if (commit_tree(msg, msg_len, &tree_oid, parents, result_oid, NULL,
33 NULL))
34 die("Failed to commit notes tree to database");
37 void commit_notes(struct notes_tree *t, const char *msg)
39 struct strbuf buf = STRBUF_INIT;
40 struct object_id commit_oid;
42 if (!t)
43 t = &default_notes_tree;
44 if (!t->initialized || !t->update_ref || !*t->update_ref)
45 die(_("Cannot commit uninitialized/unreferenced notes tree"));
46 if (!t->dirty)
47 return; /* don't have to commit an unchanged tree */
49 /* Prepare commit message and reflog message */
50 strbuf_addstr(&buf, msg);
51 strbuf_complete_line(&buf);
53 create_notes_commit(t, NULL, buf.buf, buf.len, &commit_oid);
54 strbuf_insert(&buf, 0, "notes: ", 7); /* commit message starts at index 7 */
55 update_ref(buf.buf, t->update_ref, &commit_oid, NULL, 0,
56 UPDATE_REFS_DIE_ON_ERR);
58 strbuf_release(&buf);
61 int parse_notes_merge_strategy(const char *v, enum notes_merge_strategy *s)
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;
76 return 0;
79 static combine_notes_fn parse_combine_notes_fn(const char *v)
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;
93 static int notes_rewrite_config(const char *k, const char *v, void *cb)
95 struct notes_rewrite_cfg *c = cb;
96 if (starts_with(k, "notes.rewrite.") && !strcmp(k+14, c->cmd)) {
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)
101 return config_error_nonbool(k);
102 c->combine = parse_combine_notes_fn(v);
103 if (!c->combine) {
104 error(_("Bad notes.rewriteMode value: '%s'"), v);
105 return 1;
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 */
111 if (starts_with(v, "refs/notes/"))
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;
119 return 0;
123 struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd)
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)
140 * TRANSLATORS: The first %s is the name of
141 * the environment variable, the second %s is
142 * its value.
144 error(_("Bad %s value: '%s'"), GIT_NOTES_REWRITE_MODE_ENVIRONMENT,
145 rewrite_mode_env);
147 if (rewrite_refs_env) {
148 c->refs_from_env = 1;
149 string_list_add_refs_from_colon_sep(c->refs, rewrite_refs_env);
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;
158 c->trees = load_notes_trees(c->refs, NOTES_INIT_WRITABLE);
159 string_list_clear(c->refs, 0);
160 free(c->refs);
161 return c;
164 int copy_note_for_rewrite(struct notes_rewrite_cfg *c,
165 const struct object_id *from_obj, const struct object_id *to_obj)
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;
174 void finish_copy_notes_for_rewrite(struct notes_rewrite_cfg *c, const char *msg)
176 int i;
177 for (i = 0; c->trees[i]; i++) {
178 commit_notes(c->trees[i], msg);
179 free_notes(c->trees[i]);
181 free(c->trees);
182 free(c);