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