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