tag: add --edit option
[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                          unsigned char *result_sha1)
10 {
11         struct object_id tree_oid;
12
13         assert(t->initialized);
14
15         if (write_notes_tree(t, tree_oid.hash))
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.hash, parents, result_sha1, NULL, NULL))
31                 die("Failed to commit notes tree to database");
32 }
33
34 void commit_notes(struct notes_tree *t, const char *msg)
35 {
36         struct strbuf buf = STRBUF_INIT;
37         struct object_id commit_oid;
38
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 */
45
46         /* Prepare commit message and reflog message */
47         strbuf_addstr(&buf, msg);
48         strbuf_complete_line(&buf);
49
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, NULL, 0,
53                    UPDATE_REFS_DIE_ON_ERR);
54
55         strbuf_release(&buf);
56 }
57
58 int parse_notes_merge_strategy(const char *v, enum notes_merge_strategy *s)
59 {
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;
72
73         return 0;
74 }
75
76 static combine_notes_fn parse_combine_notes_fn(const char *v)
77 {
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;
88 }
89
90 static int notes_rewrite_config(const char *k, const char *v, void *cb)
91 {
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;
103                 }
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;
114         }
115
116         return 0;
117 }
118
119
120 struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd)
121 {
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)
136                         /*
137                          * TRANSLATORS: The first %s is the name of
138                          * the environment variable, the second %s is
139                          * its value.
140                          */
141                         error(_("Bad %s value: '%s'"), GIT_NOTES_REWRITE_MODE_ENVIRONMENT,
142                                         rewrite_mode_env);
143         }
144         if (rewrite_refs_env) {
145                 c->refs_from_env = 1;
146                 string_list_add_refs_from_colon_sep(c->refs, rewrite_refs_env);
147         }
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;
154         }
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;
159 }
160
161 int copy_note_for_rewrite(struct notes_rewrite_cfg *c,
162                           const struct object_id *from_obj, const struct object_id *to_obj)
163 {
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;
169 }
170
171 void finish_copy_notes_for_rewrite(struct notes_rewrite_cfg *c, const char *msg)
172 {
173         int i;
174         for (i = 0; c->trees[i]; i++) {
175                 commit_notes(c->trees[i], msg);
176                 free_notes(c->trees[i]);
177         }
178         free(c->trees);
179         free(c);
180 }