Merge branch 'jl/commit-v-strip-marker' into maint
[git] / notes-utils.c
1 #include "cache.h"
2 #include "commit.h"
3 #include "refs.h"
4 #include "notes-utils.h"
5
6 void create_notes_commit(struct notes_tree *t, struct commit_list *parents,
7                          const struct strbuf *msg, unsigned char *result_sha1)
8 {
9         unsigned char tree_sha1[20];
10
11         assert(t->initialized);
12
13         if (write_notes_tree(t, tree_sha1))
14                 die("Failed to write notes tree to database");
15
16         if (!parents) {
17                 /* Deduce parent commit from t->ref */
18                 unsigned char parent_sha1[20];
19                 if (!read_ref(t->ref, parent_sha1)) {
20                         struct commit *parent = lookup_commit(parent_sha1);
21                         if (!parent || parse_commit(parent))
22                                 die("Failed to find/parse commit %s", t->ref);
23                         commit_list_insert(parent, &parents);
24                 }
25                 /* else: t->ref points to nothing, assume root/orphan commit */
26         }
27
28         if (commit_tree(msg, tree_sha1, parents, result_sha1, NULL, NULL))
29                 die("Failed to commit notes tree to database");
30 }
31
32 void commit_notes(struct notes_tree *t, const char *msg)
33 {
34         struct strbuf buf = STRBUF_INIT;
35         unsigned char commit_sha1[20];
36
37         if (!t)
38                 t = &default_notes_tree;
39         if (!t->initialized || !t->ref || !*t->ref)
40                 die(_("Cannot commit uninitialized/unreferenced notes tree"));
41         if (!t->dirty)
42                 return; /* don't have to commit an unchanged tree */
43
44         /* Prepare commit message and reflog message */
45         strbuf_addstr(&buf, msg);
46         if (buf.buf[buf.len - 1] != '\n')
47                 strbuf_addch(&buf, '\n'); /* Make sure msg ends with newline */
48
49         create_notes_commit(t, NULL, &buf, commit_sha1);
50         strbuf_insert(&buf, 0, "notes: ", 7); /* commit message starts at index 7 */
51         update_ref(buf.buf, t->ref, commit_sha1, NULL, 0, DIE_ON_ERR);
52
53         strbuf_release(&buf);
54 }
55
56 static combine_notes_fn parse_combine_notes_fn(const char *v)
57 {
58         if (!strcasecmp(v, "overwrite"))
59                 return combine_notes_overwrite;
60         else if (!strcasecmp(v, "ignore"))
61                 return combine_notes_ignore;
62         else if (!strcasecmp(v, "concatenate"))
63                 return combine_notes_concatenate;
64         else if (!strcasecmp(v, "cat_sort_uniq"))
65                 return combine_notes_cat_sort_uniq;
66         else
67                 return NULL;
68 }
69
70 static int notes_rewrite_config(const char *k, const char *v, void *cb)
71 {
72         struct notes_rewrite_cfg *c = cb;
73         if (!prefixcmp(k, "notes.rewrite.") && !strcmp(k+14, c->cmd)) {
74                 c->enabled = git_config_bool(k, v);
75                 return 0;
76         } else if (!c->mode_from_env && !strcmp(k, "notes.rewritemode")) {
77                 if (!v)
78                         config_error_nonbool(k);
79                 c->combine = parse_combine_notes_fn(v);
80                 if (!c->combine) {
81                         error(_("Bad notes.rewriteMode value: '%s'"), v);
82                         return 1;
83                 }
84                 return 0;
85         } else if (!c->refs_from_env && !strcmp(k, "notes.rewriteref")) {
86                 /* note that a refs/ prefix is implied in the
87                  * underlying for_each_glob_ref */
88                 if (!prefixcmp(v, "refs/notes/"))
89                         string_list_add_refs_by_glob(c->refs, v);
90                 else
91                         warning(_("Refusing to rewrite notes in %s"
92                                 " (outside of refs/notes/)"), v);
93                 return 0;
94         }
95
96         return 0;
97 }
98
99
100 struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd)
101 {
102         struct notes_rewrite_cfg *c = xmalloc(sizeof(struct notes_rewrite_cfg));
103         const char *rewrite_mode_env = getenv(GIT_NOTES_REWRITE_MODE_ENVIRONMENT);
104         const char *rewrite_refs_env = getenv(GIT_NOTES_REWRITE_REF_ENVIRONMENT);
105         c->cmd = cmd;
106         c->enabled = 1;
107         c->combine = combine_notes_concatenate;
108         c->refs = xcalloc(1, sizeof(struct string_list));
109         c->refs->strdup_strings = 1;
110         c->refs_from_env = 0;
111         c->mode_from_env = 0;
112         if (rewrite_mode_env) {
113                 c->mode_from_env = 1;
114                 c->combine = parse_combine_notes_fn(rewrite_mode_env);
115                 if (!c->combine)
116                         /* TRANSLATORS: The first %s is the name of the
117                            environment variable, the second %s is its value */
118                         error(_("Bad %s value: '%s'"), GIT_NOTES_REWRITE_MODE_ENVIRONMENT,
119                                         rewrite_mode_env);
120         }
121         if (rewrite_refs_env) {
122                 c->refs_from_env = 1;
123                 string_list_add_refs_from_colon_sep(c->refs, rewrite_refs_env);
124         }
125         git_config(notes_rewrite_config, c);
126         if (!c->enabled || !c->refs->nr) {
127                 string_list_clear(c->refs, 0);
128                 free(c->refs);
129                 free(c);
130                 return NULL;
131         }
132         c->trees = load_notes_trees(c->refs);
133         string_list_clear(c->refs, 0);
134         free(c->refs);
135         return c;
136 }
137
138 int copy_note_for_rewrite(struct notes_rewrite_cfg *c,
139                           const unsigned char *from_obj, const unsigned char *to_obj)
140 {
141         int ret = 0;
142         int i;
143         for (i = 0; c->trees[i]; i++)
144                 ret = copy_note(c->trees[i], from_obj, to_obj, 1, c->combine) || ret;
145         return ret;
146 }
147
148 void finish_copy_notes_for_rewrite(struct notes_rewrite_cfg *c, const char *msg)
149 {
150         int i;
151         for (i = 0; c->trees[i]; i++) {
152                 commit_notes(c->trees[i], msg);
153                 free_notes(c->trees[i]);
154         }
155         free(c->trees);
156         free(c);
157 }