]>
Commit | Line | Data |
---|---|---|
cd067d3b JH |
1 | /* |
2 | * Builtin "git notes" | |
3 | * | |
4 | * Copyright (c) 2010 Johan Herland <johan@herland.net> | |
5 | * | |
6 | * Based on git-notes.sh by Johannes Schindelin, | |
09b7e220 | 7 | * and builtin/tag.c by Kristian Høgsberg and Carlos Rica. |
cd067d3b JH |
8 | */ |
9 | ||
cd067d3b | 10 | #include "builtin.h" |
bc5c5ec0 | 11 | #include "config.h" |
a9cc3b8f | 12 | #include "alloc.h" |
4e120823 | 13 | #include "editor.h" |
787cb8a4 | 14 | #include "environment.h" |
f394e093 | 15 | #include "gettext.h" |
41771fa4 | 16 | #include "hex.h" |
cd067d3b | 17 | #include "notes.h" |
dabab1d6 | 18 | #include "object-name.h" |
a034e910 | 19 | #include "object-store-ll.h" |
c339932b | 20 | #include "path.h" |
2122f675 | 21 | #include "repository.h" |
cd067d3b | 22 | #include "blob.h" |
cf394719 | 23 | #include "pretty.h" |
cd067d3b | 24 | #include "refs.h" |
d807c4a0 | 25 | #include "exec-cmd.h" |
cd067d3b JH |
26 | #include "run-command.h" |
27 | #include "parse-options.h" | |
6956f858 | 28 | #include "string-list.h" |
75ef3f4a | 29 | #include "notes-merge.h" |
49c24704 | 30 | #include "notes-utils.h" |
ac6c561b | 31 | #include "worktree.h" |
d48be35c | 32 | #include "write-or-die.h" |
f50fee4a | 33 | |
3d6a3164 | 34 | static const char *separator = "\n"; |
cd067d3b | 35 | static const char * const git_notes_usage[] = { |
9c9b4f2f | 36 | N_("git notes [--ref <notes-ref>] [list [<object>]]"), |
3d6a3164 | 37 | N_("git notes [--ref <notes-ref>] add [-f] [--allow-empty] [--[no-]separator|--separator=<paragraph-break>] [--[no-]stripspace] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"), |
9c9b4f2f | 38 | N_("git notes [--ref <notes-ref>] copy [-f] <from-object> <to-object>"), |
3d6a3164 | 39 | N_("git notes [--ref <notes-ref>] append [--allow-empty] [--[no-]separator|--separator=<paragraph-break>] [--[no-]stripspace] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"), |
9c9b4f2f AH |
40 | N_("git notes [--ref <notes-ref>] edit [--allow-empty] [<object>]"), |
41 | N_("git notes [--ref <notes-ref>] show [<object>]"), | |
42 | N_("git notes [--ref <notes-ref>] merge [-v | -q] [-s <strategy>] <notes-ref>"), | |
959d670d JNA |
43 | "git notes merge --commit [-v | -q]", |
44 | "git notes merge --abort [-v | -q]", | |
9c9b4f2f | 45 | N_("git notes [--ref <notes-ref>] remove [<object>...]"), |
e54b6335 | 46 | N_("git notes [--ref <notes-ref>] prune [-n] [-v]"), |
9c9b4f2f | 47 | N_("git notes [--ref <notes-ref>] get-ref"), |
74884b52 SB |
48 | NULL |
49 | }; | |
50 | ||
51 | static const char * const git_notes_list_usage[] = { | |
e6b895ef | 52 | N_("git notes [list [<object>]]"), |
74884b52 SB |
53 | NULL |
54 | }; | |
55 | ||
56 | static const char * const git_notes_add_usage[] = { | |
e6b895ef | 57 | N_("git notes add [<options>] [<object>]"), |
74884b52 SB |
58 | NULL |
59 | }; | |
60 | ||
61 | static const char * const git_notes_copy_usage[] = { | |
e6b895ef NTND |
62 | N_("git notes copy [<options>] <from-object> <to-object>"), |
63 | N_("git notes copy --stdin [<from-object> <to-object>]..."), | |
74884b52 SB |
64 | NULL |
65 | }; | |
66 | ||
67 | static const char * const git_notes_append_usage[] = { | |
e6b895ef | 68 | N_("git notes append [<options>] [<object>]"), |
74884b52 SB |
69 | NULL |
70 | }; | |
71 | ||
72 | static const char * const git_notes_edit_usage[] = { | |
e6b895ef | 73 | N_("git notes edit [<object>]"), |
74884b52 SB |
74 | NULL |
75 | }; | |
76 | ||
77 | static const char * const git_notes_show_usage[] = { | |
e6b895ef | 78 | N_("git notes show [<object>]"), |
74884b52 SB |
79 | NULL |
80 | }; | |
81 | ||
75ef3f4a | 82 | static const char * const git_notes_merge_usage[] = { |
9c9b4f2f | 83 | N_("git notes merge [<options>] <notes-ref>"), |
e6b895ef NTND |
84 | N_("git notes merge --commit [<options>]"), |
85 | N_("git notes merge --abort [<options>]"), | |
75ef3f4a JH |
86 | NULL |
87 | }; | |
88 | ||
74884b52 | 89 | static const char * const git_notes_remove_usage[] = { |
e6b895ef | 90 | N_("git notes remove [<object>]"), |
74884b52 SB |
91 | NULL |
92 | }; | |
93 | ||
94 | static const char * const git_notes_prune_usage[] = { | |
e6b895ef | 95 | N_("git notes prune [<options>]"), |
cd067d3b JH |
96 | NULL |
97 | }; | |
98 | ||
618cd757 | 99 | static const char * const git_notes_get_ref_usage[] = { |
959d670d | 100 | "git notes get-ref", |
618cd757 JH |
101 | NULL |
102 | }; | |
103 | ||
cd067d3b | 104 | static const char note_template[] = |
996ee6d2 | 105 | N_("Write/edit the notes for the following object:"); |
cd067d3b | 106 | |
c4e2aa7d TL |
107 | enum notes_stripspace { |
108 | UNSPECIFIED = -1, | |
109 | NO_STRIPSPACE = 0, | |
110 | STRIPSPACE = 1, | |
111 | }; | |
112 | ||
90bc19b3 | 113 | struct note_msg { |
c4e2aa7d | 114 | enum notes_stripspace stripspace; |
90bc19b3 TL |
115 | struct strbuf buf; |
116 | }; | |
117 | ||
bebf5c04 | 118 | struct note_data { |
348f199b | 119 | int given; |
0691cff7 | 120 | int use_editor; |
c4e2aa7d | 121 | int stripspace; |
4282af0f | 122 | char *edit_path; |
348f199b | 123 | struct strbuf buf; |
90bc19b3 TL |
124 | struct note_msg **messages; |
125 | size_t msg_nr; | |
126 | size_t msg_alloc; | |
348f199b JH |
127 | }; |
128 | ||
4282af0f JH |
129 | static void free_note_data(struct note_data *d) |
130 | { | |
131 | if (d->edit_path) { | |
132 | unlink_or_warn(d->edit_path); | |
133 | free(d->edit_path); | |
134 | } | |
135 | strbuf_release(&d->buf); | |
90bc19b3 TL |
136 | |
137 | while (d->msg_nr--) { | |
138 | strbuf_release(&d->messages[d->msg_nr]->buf); | |
139 | free(d->messages[d->msg_nr]); | |
140 | } | |
141 | free(d->messages); | |
4282af0f JH |
142 | } |
143 | ||
490bc83a | 144 | static int list_each_note(const struct object_id *object_oid, |
3c50c88f JK |
145 | const struct object_id *note_oid, |
146 | char *note_path UNUSED, | |
147 | void *cb_data UNUSED) | |
e397421a | 148 | { |
490bc83a | 149 | printf("%s %s\n", oid_to_hex(note_oid), oid_to_hex(object_oid)); |
e397421a JH |
150 | return 0; |
151 | } | |
152 | ||
e8adba25 | 153 | static void copy_obj_to_fd(int fd, const struct object_id *oid) |
cd067d3b JH |
154 | { |
155 | unsigned long size; | |
156 | enum object_type type; | |
bc726bd0 | 157 | char *buf = repo_read_object_file(the_repository, oid, &type, &size); |
cd067d3b JH |
158 | if (buf) { |
159 | if (size) | |
160 | write_or_die(fd, buf, size); | |
161 | free(buf); | |
162 | } | |
163 | } | |
164 | ||
bb7e4739 | 165 | static void write_commented_object(int fd, const struct object_id *object) |
cd067d3b | 166 | { |
d3180279 | 167 | struct child_process show = CHILD_PROCESS_INIT; |
cd067d3b | 168 | struct strbuf buf = STRBUF_INIT; |
eff80a9f | 169 | struct strbuf cbuf = STRBUF_INIT; |
cd067d3b JH |
170 | |
171 | /* Invoke "git show --stat --no-notes $object" */ | |
2b709893 ÆAB |
172 | strvec_pushl(&show.args, "show", "--stat", "--no-notes", |
173 | oid_to_hex(object), NULL); | |
cd067d3b JH |
174 | show.no_stdin = 1; |
175 | show.out = -1; | |
176 | show.err = 0; | |
177 | show.git_cmd = 1; | |
178 | if (start_command(&show)) | |
caeba0ef | 179 | die(_("unable to start 'show' for object '%s'"), |
bb7e4739 | 180 | oid_to_hex(object)); |
cd067d3b | 181 | |
eff80a9f JH |
182 | if (strbuf_read(&buf, show.out, 0) < 0) |
183 | die_errno(_("could not read 'show' output")); | |
787cb8a4 | 184 | strbuf_add_commented_lines(&cbuf, buf.buf, buf.len, comment_line_char); |
eff80a9f | 185 | write_or_die(fd, cbuf.buf, cbuf.len); |
cd067d3b | 186 | |
eff80a9f | 187 | strbuf_release(&cbuf); |
cd067d3b | 188 | strbuf_release(&buf); |
eff80a9f | 189 | |
cd067d3b | 190 | if (finish_command(&show)) |
caeba0ef | 191 | die(_("failed to finish 'show' for object '%s'"), |
bb7e4739 | 192 | oid_to_hex(object)); |
cd067d3b JH |
193 | } |
194 | ||
bb7e4739 | 195 | static void prepare_note_data(const struct object_id *object, struct note_data *d, |
e8adba25 | 196 | const struct object_id *old_note) |
cd067d3b | 197 | { |
bebf5c04 | 198 | if (d->use_editor || !d->given) { |
cd067d3b | 199 | int fd; |
eff80a9f | 200 | struct strbuf buf = STRBUF_INIT; |
cd067d3b JH |
201 | |
202 | /* write the template message before editing: */ | |
4282af0f | 203 | d->edit_path = git_pathdup("NOTES_EDITMSG"); |
66e905b7 | 204 | fd = xopen(d->edit_path, O_CREAT | O_TRUNC | O_WRONLY, 0600); |
cd067d3b | 205 | |
bebf5c04 JH |
206 | if (d->given) |
207 | write_or_die(fd, d->buf.buf, d->buf.len); | |
52694cda JH |
208 | else if (old_note) |
209 | copy_obj_to_fd(fd, old_note); | |
eff80a9f JH |
210 | |
211 | strbuf_addch(&buf, '\n'); | |
787cb8a4 CW |
212 | strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_char); |
213 | strbuf_add_commented_lines(&buf, _(note_template), strlen(_(note_template)), | |
214 | comment_line_char); | |
215 | strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_char); | |
eff80a9f | 216 | write_or_die(fd, buf.buf, buf.len); |
cd067d3b JH |
217 | |
218 | write_commented_object(fd, object); | |
219 | ||
220 | close(fd); | |
eff80a9f | 221 | strbuf_release(&buf); |
bebf5c04 | 222 | strbuf_reset(&d->buf); |
cd067d3b | 223 | |
4282af0f | 224 | if (launch_editor(d->edit_path, &d->buf, NULL)) { |
8d79589a | 225 | die(_("please supply the note contents using either -m or -F option")); |
cd067d3b | 226 | } |
c4e2aa7d | 227 | if (d->stripspace) |
a9cc3b8f | 228 | strbuf_stripspace(&d->buf, comment_line_char); |
cd067d3b | 229 | } |
52694cda | 230 | } |
cd067d3b | 231 | |
a09c985e | 232 | static void write_note_data(struct note_data *d, struct object_id *oid) |
52694cda | 233 | { |
c80d226a | 234 | if (write_object_file(d->buf.buf, d->buf.len, OBJ_BLOB, oid)) { |
adcd4d4c ÆAB |
235 | int status = die_message(_("unable to write note object")); |
236 | ||
52694cda | 237 | if (d->edit_path) |
adcd4d4c ÆAB |
238 | die_message(_("the note contents have been left in %s"), |
239 | d->edit_path); | |
240 | exit(status); | |
cd067d3b | 241 | } |
cd067d3b JH |
242 | } |
243 | ||
b7d87ad5 | 244 | static void append_separator(struct strbuf *message) |
90bc19b3 | 245 | { |
3d6a3164 TL |
246 | size_t sep_len = 0; |
247 | ||
248 | if (!separator) | |
249 | return; | |
250 | else if ((sep_len = strlen(separator)) && separator[sep_len - 1] == '\n') | |
b7d87ad5 | 251 | strbuf_addstr(message, separator); |
90bc19b3 | 252 | else |
b7d87ad5 | 253 | strbuf_addf(message, "%s%s", separator, "\n"); |
90bc19b3 TL |
254 | } |
255 | ||
256 | static void concat_messages(struct note_data *d) | |
257 | { | |
258 | struct strbuf msg = STRBUF_INIT; | |
90bc19b3 | 259 | size_t i; |
3d6a3164 | 260 | |
90bc19b3 TL |
261 | for (i = 0; i < d->msg_nr ; i++) { |
262 | if (d->buf.len) | |
b7d87ad5 | 263 | append_separator(&d->buf); |
90bc19b3 TL |
264 | strbuf_add(&msg, d->messages[i]->buf.buf, d->messages[i]->buf.len); |
265 | strbuf_addbuf(&d->buf, &msg); | |
c4e2aa7d TL |
266 | if ((d->stripspace == UNSPECIFIED && |
267 | d->messages[i]->stripspace == STRIPSPACE) || | |
268 | d->stripspace == STRIPSPACE) | |
90bc19b3 TL |
269 | strbuf_stripspace(&d->buf, 0); |
270 | strbuf_reset(&msg); | |
271 | } | |
272 | strbuf_release(&msg); | |
273 | } | |
274 | ||
cd067d3b JH |
275 | static int parse_msg_arg(const struct option *opt, const char *arg, int unset) |
276 | { | |
bebf5c04 | 277 | struct note_data *d = opt->value; |
90bc19b3 | 278 | struct note_msg *msg = xmalloc(sizeof(*msg)); |
cd067d3b | 279 | |
517fe807 JK |
280 | BUG_ON_OPT_NEG(unset); |
281 | ||
90bc19b3 TL |
282 | strbuf_init(&msg->buf, strlen(arg)); |
283 | strbuf_addstr(&msg->buf, arg); | |
284 | ALLOC_GROW_BY(d->messages, d->msg_nr, 1, d->msg_alloc); | |
285 | d->messages[d->msg_nr - 1] = msg; | |
c4e2aa7d | 286 | msg->stripspace = STRIPSPACE; |
348f199b JH |
287 | return 0; |
288 | } | |
289 | ||
290 | static int parse_file_arg(const struct option *opt, const char *arg, int unset) | |
291 | { | |
bebf5c04 | 292 | struct note_data *d = opt->value; |
90bc19b3 | 293 | struct note_msg *msg = xmalloc(sizeof(*msg)); |
348f199b | 294 | |
517fe807 JK |
295 | BUG_ON_OPT_NEG(unset); |
296 | ||
90bc19b3 | 297 | strbuf_init(&msg->buf , 0); |
348f199b | 298 | if (!strcmp(arg, "-")) { |
90bc19b3 | 299 | if (strbuf_read(&msg->buf, 0, 1024) < 0) |
caeba0ef | 300 | die_errno(_("cannot read '%s'"), arg); |
90bc19b3 | 301 | } else if (strbuf_read_file(&msg->buf, arg, 1024) < 0) |
caeba0ef | 302 | die_errno(_("could not open or read '%s'"), arg); |
348f199b | 303 | |
90bc19b3 TL |
304 | ALLOC_GROW_BY(d->messages, d->msg_nr, 1, d->msg_alloc); |
305 | d->messages[d->msg_nr - 1] = msg; | |
c4e2aa7d | 306 | msg->stripspace = STRIPSPACE; |
cd067d3b JH |
307 | return 0; |
308 | } | |
309 | ||
0691cff7 JH |
310 | static int parse_reuse_arg(const struct option *opt, const char *arg, int unset) |
311 | { | |
bebf5c04 | 312 | struct note_data *d = opt->value; |
90bc19b3 TL |
313 | struct note_msg *msg = xmalloc(sizeof(*msg)); |
314 | char *value; | |
bb7e4739 | 315 | struct object_id object; |
0691cff7 JH |
316 | enum object_type type; |
317 | unsigned long len; | |
318 | ||
517fe807 JK |
319 | BUG_ON_OPT_NEG(unset); |
320 | ||
90bc19b3 | 321 | strbuf_init(&msg->buf, 0); |
d850b7a5 | 322 | if (repo_get_oid(the_repository, arg, &object)) |
8d79589a | 323 | die(_("failed to resolve '%s' as a valid ref."), arg); |
90bc19b3 | 324 | if (!(value = repo_read_object_file(the_repository, &object, &type, &len))) |
8d79589a | 325 | die(_("failed to read object '%s'."), arg); |
ce8daa1e | 326 | if (type != OBJ_BLOB) { |
90bc19b3 TL |
327 | strbuf_release(&msg->buf); |
328 | free(value); | |
329 | free(msg); | |
8d79589a | 330 | die(_("cannot read note data from non-blob object '%s'."), arg); |
0691cff7 | 331 | } |
0691cff7 | 332 | |
90bc19b3 TL |
333 | strbuf_add(&msg->buf, value, len); |
334 | free(value); | |
335 | ||
336 | msg->buf.len = len; | |
337 | ALLOC_GROW_BY(d->messages, d->msg_nr, 1, d->msg_alloc); | |
338 | d->messages[d->msg_nr - 1] = msg; | |
c4e2aa7d | 339 | msg->stripspace = NO_STRIPSPACE; |
0691cff7 JH |
340 | return 0; |
341 | } | |
342 | ||
343 | static int parse_reedit_arg(const struct option *opt, const char *arg, int unset) | |
344 | { | |
bebf5c04 | 345 | struct note_data *d = opt->value; |
517fe807 | 346 | BUG_ON_OPT_NEG(unset); |
bebf5c04 | 347 | d->use_editor = 1; |
0691cff7 JH |
348 | return parse_reuse_arg(opt, arg, unset); |
349 | } | |
350 | ||
3d6a3164 TL |
351 | static int parse_separator_arg(const struct option *opt, const char *arg, |
352 | int unset) | |
353 | { | |
354 | if (unset) | |
355 | *(const char **)opt->value = NULL; | |
356 | else | |
357 | *(const char **)opt->value = arg ? arg : "\n"; | |
358 | return 0; | |
359 | } | |
360 | ||
c2e86add | 361 | static int notes_copy_from_stdin(int force, const char *rewrite_cmd) |
160baa0d TR |
362 | { |
363 | struct strbuf buf = STRBUF_INIT; | |
6956f858 | 364 | struct notes_rewrite_cfg *c = NULL; |
ef7a8e3b | 365 | struct notes_tree *t = NULL; |
160baa0d | 366 | int ret = 0; |
80a14665 | 367 | const char *msg = "Notes added by 'git notes copy'"; |
160baa0d | 368 | |
6956f858 TR |
369 | if (rewrite_cmd) { |
370 | c = init_copy_notes_for_rewrite(rewrite_cmd); | |
371 | if (!c) | |
372 | return 0; | |
373 | } else { | |
ee76f92f | 374 | init_notes(NULL, NULL, NULL, NOTES_INIT_WRITABLE); |
6956f858 TR |
375 | t = &default_notes_tree; |
376 | } | |
160baa0d | 377 | |
8f309aeb | 378 | while (strbuf_getline_lf(&buf, stdin) != EOF) { |
bb7e4739 | 379 | struct object_id from_obj, to_obj; |
160baa0d TR |
380 | struct strbuf **split; |
381 | int err; | |
382 | ||
383 | split = strbuf_split(&buf, ' '); | |
384 | if (!split[0] || !split[1]) | |
8d79589a | 385 | die(_("malformed input line: '%s'."), buf.buf); |
160baa0d TR |
386 | strbuf_rtrim(split[0]); |
387 | strbuf_rtrim(split[1]); | |
d850b7a5 | 388 | if (repo_get_oid(the_repository, split[0]->buf, &from_obj)) |
8d79589a | 389 | die(_("failed to resolve '%s' as a valid ref."), split[0]->buf); |
d850b7a5 | 390 | if (repo_get_oid(the_repository, split[1]->buf, &to_obj)) |
8d79589a | 391 | die(_("failed to resolve '%s' as a valid ref."), split[1]->buf); |
160baa0d | 392 | |
6956f858 | 393 | if (rewrite_cmd) |
bb7e4739 | 394 | err = copy_note_for_rewrite(c, &from_obj, &to_obj); |
6956f858 | 395 | else |
5ee8a954 | 396 | err = copy_note(t, &from_obj, &to_obj, force, |
6956f858 | 397 | combine_notes_overwrite); |
160baa0d TR |
398 | |
399 | if (err) { | |
8d79589a | 400 | error(_("failed to copy notes from '%s' to '%s'"), |
160baa0d TR |
401 | split[0]->buf, split[1]->buf); |
402 | ret = 1; | |
403 | } | |
404 | ||
405 | strbuf_list_free(split); | |
406 | } | |
407 | ||
6956f858 | 408 | if (!rewrite_cmd) { |
1d18d758 | 409 | commit_notes(the_repository, t, msg); |
6956f858 TR |
410 | free_notes(t); |
411 | } else { | |
1d18d758 | 412 | finish_copy_notes_for_rewrite(the_repository, c, msg); |
6956f858 | 413 | } |
1f3992f4 | 414 | strbuf_release(&buf); |
160baa0d TR |
415 | return ret; |
416 | } | |
417 | ||
ee76f92f MH |
418 | static struct notes_tree *init_notes_check(const char *subcommand, |
419 | int flags) | |
cd067d3b | 420 | { |
cd067d3b | 421 | struct notes_tree *t; |
ee76f92f MH |
422 | const char *ref; |
423 | init_notes(NULL, NULL, NULL, flags); | |
74884b52 | 424 | t = &default_notes_tree; |
a0b4dfa9 | 425 | |
ee76f92f MH |
426 | ref = (flags & NOTES_INIT_WRITABLE) ? t->update_ref : t->ref; |
427 | if (!starts_with(ref, "refs/notes/")) | |
66f5f6dc ÆAB |
428 | /* |
429 | * TRANSLATORS: the first %s will be replaced by a git | |
430 | * notes command: 'add', 'merge', 'remove', etc. | |
431 | */ | |
8d79589a | 432 | die(_("refusing to %s notes in %s (outside of refs/notes/)"), |
ee76f92f | 433 | subcommand, ref); |
74884b52 SB |
434 | return t; |
435 | } | |
436 | ||
437 | static int list(int argc, const char **argv, const char *prefix) | |
cd067d3b | 438 | { |
cd067d3b | 439 | struct notes_tree *t; |
bb7e4739 | 440 | struct object_id object; |
9ef72230 | 441 | const struct object_id *note; |
74884b52 | 442 | int retval = -1; |
cd067d3b | 443 | struct option options[] = { |
cd067d3b JH |
444 | OPT_END() |
445 | }; | |
446 | ||
74884b52 SB |
447 | if (argc) |
448 | argc = parse_options(argc, argv, prefix, options, | |
449 | git_notes_list_usage, 0); | |
cd067d3b | 450 | |
74884b52 | 451 | if (1 < argc) { |
b8657347 | 452 | error(_("too many arguments")); |
74884b52 | 453 | usage_with_options(git_notes_list_usage, options); |
dcf783a2 TR |
454 | } |
455 | ||
ee76f92f | 456 | t = init_notes_check("list", 0); |
74884b52 | 457 | if (argc) { |
d850b7a5 | 458 | if (repo_get_oid(the_repository, argv[0], &object)) |
8d79589a | 459 | die(_("failed to resolve '%s' as a valid ref."), argv[0]); |
5ee8a954 | 460 | note = get_note(t, &object); |
74884b52 | 461 | if (note) { |
9ef72230 | 462 | puts(oid_to_hex(note)); |
74884b52 SB |
463 | retval = 0; |
464 | } else | |
8d79589a | 465 | retval = error(_("no note found for object %s."), |
bb7e4739 | 466 | oid_to_hex(&object)); |
74884b52 SB |
467 | } else |
468 | retval = for_each_note(t, 0, list_each_note, NULL); | |
cd067d3b | 469 | |
74884b52 SB |
470 | free_notes(t); |
471 | return retval; | |
472 | } | |
cd067d3b | 473 | |
84a7e35e JH |
474 | static int append_edit(int argc, const char **argv, const char *prefix); |
475 | ||
74884b52 SB |
476 | static int add(int argc, const char **argv, const char *prefix) |
477 | { | |
d73a5b93 | 478 | int force = 0, allow_empty = 0; |
92b3385f | 479 | const char *object_ref; |
74884b52 | 480 | struct notes_tree *t; |
bb7e4739 | 481 | struct object_id object, new_note; |
9ef72230 | 482 | const struct object_id *note; |
c4e2aa7d | 483 | struct note_data d = { .buf = STRBUF_INIT, .stripspace = UNSPECIFIED }; |
90bc19b3 | 484 | |
cd067d3b | 485 | struct option options[] = { |
203c8533 | 486 | OPT_CALLBACK_F('m', "message", &d, N_("message"), |
e6b895ef | 487 | N_("note contents as a string"), PARSE_OPT_NONEG, |
203c8533 DL |
488 | parse_msg_arg), |
489 | OPT_CALLBACK_F('F', "file", &d, N_("file"), | |
e6b895ef | 490 | N_("note contents in a file"), PARSE_OPT_NONEG, |
203c8533 DL |
491 | parse_file_arg), |
492 | OPT_CALLBACK_F('c', "reedit-message", &d, N_("object"), | |
e6b895ef | 493 | N_("reuse and edit specified note object"), PARSE_OPT_NONEG, |
203c8533 DL |
494 | parse_reedit_arg), |
495 | OPT_CALLBACK_F('C', "reuse-message", &d, N_("object"), | |
e6b895ef | 496 | N_("reuse specified note object"), PARSE_OPT_NONEG, |
203c8533 | 497 | parse_reuse_arg), |
d73a5b93 JH |
498 | OPT_BOOL(0, "allow-empty", &allow_empty, |
499 | N_("allow storing empty note")), | |
7a60e3bb | 500 | OPT__FORCE(&force, N_("replace existing notes"), PARSE_OPT_NOCOMPLETE), |
3d6a3164 TL |
501 | OPT_CALLBACK_F(0, "separator", &separator, |
502 | N_("<paragraph-break>"), | |
503 | N_("insert <paragraph-break> between paragraphs"), | |
504 | PARSE_OPT_OPTARG, parse_separator_arg), | |
c4e2aa7d TL |
505 | OPT_BOOL(0, "stripspace", &d.stripspace, |
506 | N_("remove unnecessary whitespace")), | |
cd067d3b JH |
507 | OPT_END() |
508 | }; | |
509 | ||
74884b52 | 510 | argc = parse_options(argc, argv, prefix, options, git_notes_add_usage, |
84a7e35e | 511 | PARSE_OPT_KEEP_ARGV0); |
cd067d3b | 512 | |
84a7e35e | 513 | if (2 < argc) { |
b8657347 | 514 | error(_("too many arguments")); |
74884b52 | 515 | usage_with_options(git_notes_add_usage, options); |
dcf783a2 TR |
516 | } |
517 | ||
90bc19b3 TL |
518 | if (d.msg_nr) |
519 | concat_messages(&d); | |
520 | d.given = !!d.buf.len; | |
521 | ||
84a7e35e | 522 | object_ref = argc > 1 ? argv[1] : "HEAD"; |
cd067d3b | 523 | |
d850b7a5 | 524 | if (repo_get_oid(the_repository, object_ref, &object)) |
8d79589a | 525 | die(_("failed to resolve '%s' as a valid ref."), object_ref); |
cd067d3b | 526 | |
ee76f92f | 527 | t = init_notes_check("add", NOTES_INIT_WRITABLE); |
5ee8a954 | 528 | note = get_note(t, &object); |
92b3385f | 529 | |
74884b52 SB |
530 | if (note) { |
531 | if (!force) { | |
b0de56c6 JH |
532 | free_notes(t); |
533 | if (d.given) { | |
4282af0f | 534 | free_note_data(&d); |
b0de56c6 JH |
535 | return error(_("Cannot add notes. " |
536 | "Found existing notes for object %s. " | |
537 | "Use '-f' to overwrite existing notes"), | |
bb7e4739 | 538 | oid_to_hex(&object)); |
84a7e35e | 539 | } |
b0de56c6 JH |
540 | /* |
541 | * Redirect to "edit" subcommand. | |
542 | * | |
543 | * We only end up here if none of -m/-F/-c/-C or -f are | |
544 | * given. The original args are therefore still in | |
545 | * argv[0-1]. | |
546 | */ | |
547 | argv[0] = "edit"; | |
548 | return append_edit(argc, argv, prefix); | |
74884b52 | 549 | } |
caeba0ef | 550 | fprintf(stderr, _("Overwriting existing notes for object %s\n"), |
bb7e4739 | 551 | oid_to_hex(&object)); |
aaec9bcf JH |
552 | } |
553 | ||
e8adba25 | 554 | prepare_note_data(&object, &d, note); |
d73a5b93 | 555 | if (d.buf.len || allow_empty) { |
a09c985e | 556 | write_note_data(&d, &new_note); |
5ee8a954 | 557 | if (add_note(t, &object, &new_note, combine_notes_overwrite)) |
033abf97 | 558 | BUG("combine_notes_overwrite failed"); |
1d18d758 NTND |
559 | commit_notes(the_repository, t, |
560 | "Notes added by 'git notes add'"); | |
52694cda JH |
561 | } else { |
562 | fprintf(stderr, _("Removing note for object %s\n"), | |
bb7e4739 | 563 | oid_to_hex(&object)); |
564 | remove_note(t, object.hash); | |
1d18d758 NTND |
565 | commit_notes(the_repository, t, |
566 | "Notes removed by 'git notes add'"); | |
52694cda | 567 | } |
160baa0d | 568 | |
52694cda | 569 | free_note_data(&d); |
74884b52 | 570 | free_notes(t); |
b0de56c6 | 571 | return 0; |
74884b52 | 572 | } |
cd067d3b | 573 | |
74884b52 SB |
574 | static int copy(int argc, const char **argv, const char *prefix) |
575 | { | |
576 | int retval = 0, force = 0, from_stdin = 0; | |
9ef72230 | 577 | const struct object_id *from_note, *note; |
74884b52 | 578 | const char *object_ref; |
bb7e4739 | 579 | struct object_id object, from_obj; |
74884b52 SB |
580 | struct notes_tree *t; |
581 | const char *rewrite_cmd = NULL; | |
582 | struct option options[] = { | |
7a60e3bb | 583 | OPT__FORCE(&force, N_("replace existing notes"), PARSE_OPT_NOCOMPLETE), |
d5d09d47 | 584 | OPT_BOOL(0, "stdin", &from_stdin, N_("read objects from stdin")), |
e6b895ef NTND |
585 | OPT_STRING(0, "for-rewrite", &rewrite_cmd, N_("command"), |
586 | N_("load rewriting config for <command> (implies " | |
587 | "--stdin)")), | |
74884b52 SB |
588 | OPT_END() |
589 | }; | |
cd067d3b | 590 | |
74884b52 SB |
591 | argc = parse_options(argc, argv, prefix, options, git_notes_copy_usage, |
592 | 0); | |
e397421a | 593 | |
74884b52 SB |
594 | if (from_stdin || rewrite_cmd) { |
595 | if (argc) { | |
b8657347 | 596 | error(_("too many arguments")); |
74884b52 | 597 | usage_with_options(git_notes_copy_usage, options); |
5848769f | 598 | } else { |
74884b52 | 599 | return notes_copy_from_stdin(force, rewrite_cmd); |
e73bbd96 | 600 | } |
e73bbd96 JH |
601 | } |
602 | ||
d58deb9c | 603 | if (argc < 1) { |
b8657347 | 604 | error(_("too few arguments")); |
bbb1b8a3 JK |
605 | usage_with_options(git_notes_copy_usage, options); |
606 | } | |
74884b52 | 607 | if (2 < argc) { |
b8657347 | 608 | error(_("too many arguments")); |
74884b52 | 609 | usage_with_options(git_notes_copy_usage, options); |
cd067d3b JH |
610 | } |
611 | ||
d850b7a5 | 612 | if (repo_get_oid(the_repository, argv[0], &from_obj)) |
8d79589a | 613 | die(_("failed to resolve '%s' as a valid ref."), argv[0]); |
cd067d3b | 614 | |
74884b52 | 615 | object_ref = 1 < argc ? argv[1] : "HEAD"; |
cd067d3b | 616 | |
d850b7a5 | 617 | if (repo_get_oid(the_repository, object_ref, &object)) |
8d79589a | 618 | die(_("failed to resolve '%s' as a valid ref."), object_ref); |
cd067d3b | 619 | |
ee76f92f | 620 | t = init_notes_check("copy", NOTES_INIT_WRITABLE); |
5ee8a954 | 621 | note = get_note(t, &object); |
cd067d3b | 622 | |
74884b52 | 623 | if (note) { |
5848769f | 624 | if (!force) { |
caeba0ef | 625 | retval = error(_("Cannot copy notes. Found existing " |
74884b52 | 626 | "notes for object %s. Use '-f' to " |
caeba0ef | 627 | "overwrite existing notes"), |
bb7e4739 | 628 | oid_to_hex(&object)); |
74884b52 | 629 | goto out; |
5848769f | 630 | } |
caeba0ef | 631 | fprintf(stderr, _("Overwriting existing notes for object %s\n"), |
bb7e4739 | 632 | oid_to_hex(&object)); |
e397421a JH |
633 | } |
634 | ||
5ee8a954 | 635 | from_note = get_note(t, &from_obj); |
74884b52 | 636 | if (!from_note) { |
8d79589a | 637 | retval = error(_("missing notes on source object %s. Cannot " |
bb7e4739 | 638 | "copy."), oid_to_hex(&from_obj)); |
74884b52 | 639 | goto out; |
cd067d3b JH |
640 | } |
641 | ||
5ee8a954 | 642 | if (add_note(t, &object, from_note, combine_notes_overwrite)) |
033abf97 | 643 | BUG("combine_notes_overwrite failed"); |
1d18d758 NTND |
644 | commit_notes(the_repository, t, |
645 | "Notes added by 'git notes copy'"); | |
74884b52 SB |
646 | out: |
647 | free_notes(t); | |
648 | return retval; | |
649 | } | |
7aa4754e | 650 | |
74884b52 SB |
651 | static int append_edit(int argc, const char **argv, const char *prefix) |
652 | { | |
d73a5b93 | 653 | int allow_empty = 0; |
74884b52 SB |
654 | const char *object_ref; |
655 | struct notes_tree *t; | |
bb7e4739 | 656 | struct object_id object, new_note; |
9ef72230 | 657 | const struct object_id *note; |
5b1ef2ce | 658 | char *logmsg; |
74884b52 | 659 | const char * const *usage; |
c4e2aa7d | 660 | struct note_data d = { .buf = STRBUF_INIT, .stripspace = UNSPECIFIED }; |
74884b52 | 661 | struct option options[] = { |
203c8533 | 662 | OPT_CALLBACK_F('m', "message", &d, N_("message"), |
e6b895ef | 663 | N_("note contents as a string"), PARSE_OPT_NONEG, |
203c8533 DL |
664 | parse_msg_arg), |
665 | OPT_CALLBACK_F('F', "file", &d, N_("file"), | |
e6b895ef | 666 | N_("note contents in a file"), PARSE_OPT_NONEG, |
203c8533 DL |
667 | parse_file_arg), |
668 | OPT_CALLBACK_F('c', "reedit-message", &d, N_("object"), | |
e6b895ef | 669 | N_("reuse and edit specified note object"), PARSE_OPT_NONEG, |
203c8533 DL |
670 | parse_reedit_arg), |
671 | OPT_CALLBACK_F('C', "reuse-message", &d, N_("object"), | |
e6b895ef | 672 | N_("reuse specified note object"), PARSE_OPT_NONEG, |
203c8533 | 673 | parse_reuse_arg), |
d73a5b93 JH |
674 | OPT_BOOL(0, "allow-empty", &allow_empty, |
675 | N_("allow storing empty note")), | |
3d6a3164 TL |
676 | OPT_CALLBACK_F(0, "separator", &separator, |
677 | N_("<paragraph-break>"), | |
678 | N_("insert <paragraph-break> between paragraphs"), | |
679 | PARSE_OPT_OPTARG, parse_separator_arg), | |
c4e2aa7d TL |
680 | OPT_BOOL(0, "stripspace", &d.stripspace, |
681 | N_("remove unnecessary whitespace")), | |
74884b52 SB |
682 | OPT_END() |
683 | }; | |
684 | int edit = !strcmp(argv[0], "edit"); | |
685 | ||
686 | usage = edit ? git_notes_edit_usage : git_notes_append_usage; | |
687 | argc = parse_options(argc, argv, prefix, options, usage, | |
688 | PARSE_OPT_KEEP_ARGV0); | |
92b3385f | 689 | |
74884b52 | 690 | if (2 < argc) { |
b8657347 | 691 | error(_("too many arguments")); |
74884b52 | 692 | usage_with_options(usage, options); |
cd067d3b JH |
693 | } |
694 | ||
90bc19b3 TL |
695 | if (d.msg_nr) |
696 | concat_messages(&d); | |
697 | d.given = !!d.buf.len; | |
698 | ||
bebf5c04 | 699 | if (d.given && edit) |
caeba0ef | 700 | fprintf(stderr, _("The -m/-F/-c/-C options have been deprecated " |
74884b52 | 701 | "for the 'edit' subcommand.\n" |
caeba0ef | 702 | "Please use 'git notes add -f -m/-F/-c/-C' instead.\n")); |
74884b52 SB |
703 | |
704 | object_ref = 1 < argc ? argv[1] : "HEAD"; | |
705 | ||
d850b7a5 | 706 | if (repo_get_oid(the_repository, object_ref, &object)) |
8d79589a | 707 | die(_("failed to resolve '%s' as a valid ref."), object_ref); |
74884b52 | 708 | |
ee76f92f | 709 | t = init_notes_check(argv[0], NOTES_INIT_WRITABLE); |
5ee8a954 | 710 | note = get_note(t, &object); |
74884b52 | 711 | |
e8adba25 | 712 | prepare_note_data(&object, &d, edit && note ? note : NULL); |
5848769f | 713 | |
52694cda JH |
714 | if (note && !edit) { |
715 | /* Append buf to previous note contents */ | |
716 | unsigned long size; | |
717 | enum object_type type; | |
b7d87ad5 TL |
718 | struct strbuf buf = STRBUF_INIT; |
719 | char *prev_buf = repo_read_object_file(the_repository, note, &type, &size); | |
52694cda | 720 | |
52694cda | 721 | if (prev_buf && size) |
b7d87ad5 TL |
722 | strbuf_add(&buf, prev_buf, size); |
723 | if (d.buf.len && prev_buf && size) | |
724 | append_separator(&buf); | |
725 | strbuf_insert(&d.buf, 0, buf.buf, buf.len); | |
726 | ||
52694cda | 727 | free(prev_buf); |
b7d87ad5 | 728 | strbuf_release(&buf); |
52694cda | 729 | } |
5848769f | 730 | |
d73a5b93 | 731 | if (d.buf.len || allow_empty) { |
a09c985e | 732 | write_note_data(&d, &new_note); |
5ee8a954 | 733 | if (add_note(t, &object, &new_note, combine_notes_overwrite)) |
033abf97 | 734 | BUG("combine_notes_overwrite failed"); |
5b1ef2ce | 735 | logmsg = xstrfmt("Notes added by 'git notes %s'", argv[0]); |
52694cda JH |
736 | } else { |
737 | fprintf(stderr, _("Removing note for object %s\n"), | |
bb7e4739 | 738 | oid_to_hex(&object)); |
739 | remove_note(t, object.hash); | |
5b1ef2ce | 740 | logmsg = xstrfmt("Notes removed by 'git notes %s'", argv[0]); |
52694cda | 741 | } |
1d18d758 | 742 | commit_notes(the_repository, t, logmsg); |
52694cda | 743 | |
5b1ef2ce | 744 | free(logmsg); |
52694cda | 745 | free_note_data(&d); |
cd067d3b | 746 | free_notes(t); |
74884b52 SB |
747 | return 0; |
748 | } | |
749 | ||
750 | static int show(int argc, const char **argv, const char *prefix) | |
751 | { | |
752 | const char *object_ref; | |
753 | struct notes_tree *t; | |
bb7e4739 | 754 | struct object_id object; |
9ef72230 | 755 | const struct object_id *note; |
74884b52 SB |
756 | int retval; |
757 | struct option options[] = { | |
758 | OPT_END() | |
759 | }; | |
760 | ||
761 | argc = parse_options(argc, argv, prefix, options, git_notes_show_usage, | |
762 | 0); | |
763 | ||
764 | if (1 < argc) { | |
b8657347 | 765 | error(_("too many arguments")); |
74884b52 SB |
766 | usage_with_options(git_notes_show_usage, options); |
767 | } | |
768 | ||
769 | object_ref = argc ? argv[0] : "HEAD"; | |
770 | ||
d850b7a5 | 771 | if (repo_get_oid(the_repository, object_ref, &object)) |
8d79589a | 772 | die(_("failed to resolve '%s' as a valid ref."), object_ref); |
74884b52 | 773 | |
ee76f92f | 774 | t = init_notes_check("show", 0); |
5ee8a954 | 775 | note = get_note(t, &object); |
74884b52 SB |
776 | |
777 | if (!note) | |
8d79589a | 778 | retval = error(_("no note found for object %s."), |
bb7e4739 | 779 | oid_to_hex(&object)); |
74884b52 | 780 | else { |
9ef72230 | 781 | const char *show_args[3] = {"show", oid_to_hex(note), NULL}; |
74884b52 SB |
782 | retval = execv_git_cmd(show_args); |
783 | } | |
784 | free_notes(t); | |
5848769f | 785 | return retval; |
cd067d3b | 786 | } |
74884b52 | 787 | |
6abb3655 JH |
788 | static int merge_abort(struct notes_merge_options *o) |
789 | { | |
790 | int ret = 0; | |
791 | ||
792 | /* | |
793 | * Remove .git/NOTES_MERGE_PARTIAL and .git/NOTES_MERGE_REF, and call | |
794 | * notes_merge_abort() to remove .git/NOTES_MERGE_WORKTREE. | |
795 | */ | |
796 | ||
755b49ae | 797 | if (delete_ref(NULL, "NOTES_MERGE_PARTIAL", NULL, 0)) |
8d79589a | 798 | ret += error(_("failed to delete ref NOTES_MERGE_PARTIAL")); |
91774afc | 799 | if (delete_ref(NULL, "NOTES_MERGE_REF", NULL, REF_NO_DEREF)) |
8d79589a | 800 | ret += error(_("failed to delete ref NOTES_MERGE_REF")); |
6abb3655 | 801 | if (notes_merge_abort(o)) |
8d79589a | 802 | ret += error(_("failed to remove 'git notes merge' worktree")); |
6abb3655 JH |
803 | return ret; |
804 | } | |
805 | ||
806 | static int merge_commit(struct notes_merge_options *o) | |
807 | { | |
808 | struct strbuf msg = STRBUF_INIT; | |
2928325f | 809 | struct object_id oid, parent_oid; |
6abb3655 JH |
810 | struct notes_tree *t; |
811 | struct commit *partial; | |
812 | struct pretty_print_context pretty_ctx; | |
96ec7b1e | 813 | void *local_ref_to_free; |
d5a35c11 | 814 | int ret; |
6abb3655 JH |
815 | |
816 | /* | |
817 | * Read partial merge result from .git/NOTES_MERGE_PARTIAL, | |
818 | * and target notes ref from .git/NOTES_MERGE_REF. | |
819 | */ | |
820 | ||
d850b7a5 | 821 | if (repo_get_oid(the_repository, "NOTES_MERGE_PARTIAL", &oid)) |
8d79589a | 822 | die(_("failed to read ref NOTES_MERGE_PARTIAL")); |
2122f675 | 823 | else if (!(partial = lookup_commit_reference(the_repository, &oid))) |
8d79589a | 824 | die(_("could not find commit from NOTES_MERGE_PARTIAL.")); |
ecb5091f | 825 | else if (repo_parse_commit(the_repository, partial)) |
8d79589a | 826 | die(_("could not parse commit from NOTES_MERGE_PARTIAL.")); |
6abb3655 | 827 | |
6cfd6a9d | 828 | if (partial->parents) |
2928325f | 829 | oidcpy(&parent_oid, &partial->parents->item->object.oid); |
6cfd6a9d | 830 | else |
2928325f | 831 | oidclr(&parent_oid); |
6cfd6a9d | 832 | |
ca56dadb | 833 | CALLOC_ARRAY(t, 1); |
6abb3655 JH |
834 | init_notes(t, "NOTES_MERGE_PARTIAL", combine_notes_overwrite, 0); |
835 | ||
96ec7b1e | 836 | o->local_ref = local_ref_to_free = |
0f2dc722 | 837 | resolve_refdup("NOTES_MERGE_REF", 0, &oid, NULL); |
6abb3655 | 838 | if (!o->local_ref) |
8d79589a | 839 | die(_("failed to resolve NOTES_MERGE_REF")); |
6abb3655 | 840 | |
5237e0eb | 841 | if (notes_merge_commit(o, t, partial, &oid)) |
8d79589a | 842 | die(_("failed to finalize notes merge")); |
6abb3655 JH |
843 | |
844 | /* Reuse existing commit message in reflog message */ | |
845 | memset(&pretty_ctx, 0, sizeof(pretty_ctx)); | |
bab82164 ÆAB |
846 | repo_format_commit_message(the_repository, partial, "%s", &msg, |
847 | &pretty_ctx); | |
6abb3655 | 848 | strbuf_trim(&msg); |
a91cc7fa | 849 | strbuf_insertstr(&msg, 0, "notes: "); |
ae077771 | 850 | update_ref(msg.buf, o->local_ref, &oid, |
851 | is_null_oid(&parent_oid) ? NULL : &parent_oid, | |
f4124112 | 852 | 0, UPDATE_REFS_DIE_ON_ERR); |
6abb3655 JH |
853 | |
854 | free_notes(t); | |
855 | strbuf_release(&msg); | |
d5a35c11 | 856 | ret = merge_abort(o); |
96ec7b1e | 857 | free(local_ref_to_free); |
d5a35c11 | 858 | return ret; |
6abb3655 JH |
859 | } |
860 | ||
d2d68d99 JK |
861 | static int git_config_get_notes_strategy(const char *key, |
862 | enum notes_merge_strategy *strategy) | |
863 | { | |
344b5484 | 864 | char *value; |
d2d68d99 | 865 | |
344b5484 | 866 | if (git_config_get_string(key, &value)) |
d2d68d99 JK |
867 | return 1; |
868 | if (parse_notes_merge_strategy(value, strategy)) | |
5313827f | 869 | git_die_config(key, _("unknown notes merge strategy %s"), value); |
d2d68d99 | 870 | |
344b5484 | 871 | free(value); |
d2d68d99 JK |
872 | return 0; |
873 | } | |
874 | ||
75ef3f4a JH |
875 | static int merge(int argc, const char **argv, const char *prefix) |
876 | { | |
877 | struct strbuf remote_ref = STRBUF_INIT, msg = STRBUF_INIT; | |
bb7e4739 | 878 | struct object_id result_oid; |
2085b16a | 879 | struct notes_tree *t; |
75ef3f4a | 880 | struct notes_merge_options o; |
6abb3655 | 881 | int do_merge = 0, do_commit = 0, do_abort = 0; |
75ef3f4a | 882 | int verbosity = 0, result; |
3228e671 | 883 | const char *strategy = NULL; |
75ef3f4a | 884 | struct option options[] = { |
e6b895ef | 885 | OPT_GROUP(N_("General options")), |
75ef3f4a | 886 | OPT__VERBOSITY(&verbosity), |
e6b895ef NTND |
887 | OPT_GROUP(N_("Merge options")), |
888 | OPT_STRING('s', "strategy", &strategy, N_("strategy"), | |
889 | N_("resolve notes conflicts using the given strategy " | |
890 | "(manual/ours/theirs/union/cat_sort_uniq)")), | |
891 | OPT_GROUP(N_("Committing unmerged notes")), | |
3e4a67b4 NTND |
892 | OPT_SET_INT_F(0, "commit", &do_commit, |
893 | N_("finalize notes merge by committing unmerged notes"), | |
894 | 1, PARSE_OPT_NONEG), | |
e6b895ef | 895 | OPT_GROUP(N_("Aborting notes merge resolution")), |
3e4a67b4 NTND |
896 | OPT_SET_INT_F(0, "abort", &do_abort, |
897 | N_("abort notes merge"), | |
898 | 1, PARSE_OPT_NONEG), | |
75ef3f4a JH |
899 | OPT_END() |
900 | }; | |
901 | ||
902 | argc = parse_options(argc, argv, prefix, options, | |
903 | git_notes_merge_usage, 0); | |
904 | ||
6abb3655 JH |
905 | if (strategy || do_commit + do_abort == 0) |
906 | do_merge = 1; | |
907 | if (do_merge + do_commit + do_abort != 1) { | |
5313827f | 908 | error(_("cannot mix --commit, --abort or -s/--strategy")); |
6abb3655 JH |
909 | usage_with_options(git_notes_merge_usage, options); |
910 | } | |
911 | ||
912 | if (do_merge && argc != 1) { | |
8d79589a | 913 | error(_("must specify a notes ref to merge")); |
75ef3f4a | 914 | usage_with_options(git_notes_merge_usage, options); |
6abb3655 | 915 | } else if (!do_merge && argc) { |
b8657347 | 916 | error(_("too many arguments")); |
6abb3655 | 917 | usage_with_options(git_notes_merge_usage, options); |
75ef3f4a JH |
918 | } |
919 | ||
5684200f | 920 | init_notes_merge_options(the_repository, &o); |
75ef3f4a JH |
921 | o.verbosity = verbosity + NOTES_MERGE_VERBOSITY_DEFAULT; |
922 | ||
6abb3655 JH |
923 | if (do_abort) |
924 | return merge_abort(&o); | |
925 | if (do_commit) | |
926 | return merge_commit(&o); | |
927 | ||
75ef3f4a JH |
928 | o.local_ref = default_notes_ref(); |
929 | strbuf_addstr(&remote_ref, argv[0]); | |
b3715b75 | 930 | expand_loose_notes_ref(&remote_ref); |
75ef3f4a JH |
931 | o.remote_ref = remote_ref.buf; |
932 | ||
ee76f92f | 933 | t = init_notes_check("merge", NOTES_INIT_WRITABLE); |
d2d68d99 | 934 | |
3228e671 | 935 | if (strategy) { |
93efcad3 | 936 | if (parse_notes_merge_strategy(strategy, &o.strategy)) { |
8d79589a | 937 | error(_("unknown -s/--strategy: %s"), strategy); |
3228e671 JH |
938 | usage_with_options(git_notes_merge_usage, options); |
939 | } | |
d2d68d99 | 940 | } else { |
4f655e22 JK |
941 | struct strbuf merge_key = STRBUF_INIT; |
942 | const char *short_ref = NULL; | |
3228e671 | 943 | |
4f655e22 | 944 | if (!skip_prefix(o.local_ref, "refs/notes/", &short_ref)) |
033abf97 | 945 | BUG("local ref %s is outside of refs/notes/", |
4f655e22 JK |
946 | o.local_ref); |
947 | ||
948 | strbuf_addf(&merge_key, "notes.%s.mergeStrategy", short_ref); | |
949 | ||
950 | if (git_config_get_notes_strategy(merge_key.buf, &o.strategy)) | |
951 | git_config_get_notes_strategy("notes.mergeStrategy", &o.strategy); | |
952 | ||
953 | strbuf_release(&merge_key); | |
3228e671 | 954 | } |
75ef3f4a JH |
955 | |
956 | strbuf_addf(&msg, "notes: Merged notes from %s into %s", | |
957 | remote_ref.buf, default_notes_ref()); | |
443259cf | 958 | strbuf_add(&(o.commit_msg), msg.buf + 7, msg.len - 7); /* skip "notes: " */ |
2085b16a | 959 | |
5237e0eb | 960 | result = notes_merge(&o, t, &result_oid); |
2085b16a | 961 | |
5237e0eb | 962 | if (result >= 0) /* Merge resulted (trivially) in result_oid */ |
75ef3f4a | 963 | /* Update default notes ref with new commit */ |
ae077771 | 964 | update_ref(msg.buf, default_notes_ref(), &result_oid, NULL, 0, |
965 | UPDATE_REFS_DIE_ON_ERR); | |
6abb3655 | 966 | else { /* Merge has unresolved conflicts */ |
c8dd491f | 967 | struct worktree **worktrees; |
d3b9ac07 | 968 | const struct worktree *wt; |
6abb3655 | 969 | /* Update .git/NOTES_MERGE_PARTIAL with partial merge result */ |
ae077771 | 970 | update_ref(msg.buf, "NOTES_MERGE_PARTIAL", &result_oid, NULL, |
f4124112 | 971 | 0, UPDATE_REFS_DIE_ON_ERR); |
6abb3655 | 972 | /* Store ref-to-be-updated into .git/NOTES_MERGE_REF */ |
c8dd491f AK |
973 | worktrees = get_worktrees(); |
974 | wt = find_shared_symref(worktrees, "NOTES_MERGE_REF", | |
975 | default_notes_ref()); | |
d3b9ac07 | 976 | if (wt) |
8d79589a | 977 | die(_("a notes merge into %s is already in-progress at %s"), |
d3b9ac07 | 978 | default_notes_ref(), wt->path); |
c8dd491f | 979 | free_worktrees(worktrees); |
6abb3655 | 980 | if (create_symref("NOTES_MERGE_REF", default_notes_ref(), NULL)) |
8d79589a | 981 | die(_("failed to store link to current notes ref (%s)"), |
6abb3655 | 982 | default_notes_ref()); |
89b9e31d TZ |
983 | fprintf(stderr, _("Automatic notes merge failed. Fix conflicts in %s " |
984 | "and commit the result with 'git notes merge --commit', " | |
985 | "or abort the merge with 'git notes merge --abort'.\n"), | |
986 | git_path(NOTES_MERGE_WORKTREE)); | |
6abb3655 | 987 | } |
75ef3f4a | 988 | |
2085b16a | 989 | free_notes(t); |
75ef3f4a JH |
990 | strbuf_release(&remote_ref); |
991 | strbuf_release(&msg); | |
809f38c8 | 992 | return result < 0; /* return non-zero on conflicts */ |
75ef3f4a JH |
993 | } |
994 | ||
46538012 | 995 | #define IGNORE_MISSING 1 |
2d370d2f JH |
996 | |
997 | static int remove_one_note(struct notes_tree *t, const char *name, unsigned flag) | |
c3ab1a8e JH |
998 | { |
999 | int status; | |
bb7e4739 | 1000 | struct object_id oid; |
d850b7a5 | 1001 | if (repo_get_oid(the_repository, name, &oid)) |
c3ab1a8e | 1002 | return error(_("Failed to resolve '%s' as a valid ref."), name); |
bb7e4739 | 1003 | status = remove_note(t, oid.hash); |
c3ab1a8e JH |
1004 | if (status) |
1005 | fprintf(stderr, _("Object %s has no note\n"), name); | |
1006 | else | |
1007 | fprintf(stderr, _("Removing note for object %s\n"), name); | |
46538012 | 1008 | return (flag & IGNORE_MISSING) ? 0 : status; |
c3ab1a8e JH |
1009 | } |
1010 | ||
74884b52 SB |
1011 | static int remove_cmd(int argc, const char **argv, const char *prefix) |
1012 | { | |
2d370d2f | 1013 | unsigned flag = 0; |
46538012 | 1014 | int from_stdin = 0; |
74884b52 | 1015 | struct option options[] = { |
2d370d2f | 1016 | OPT_BIT(0, "ignore-missing", &flag, |
e6b895ef | 1017 | N_("attempt to remove non-existent note is not an error"), |
46538012 | 1018 | IGNORE_MISSING), |
d5d09d47 | 1019 | OPT_BOOL(0, "stdin", &from_stdin, |
e6b895ef | 1020 | N_("read object names from the standard input")), |
74884b52 SB |
1021 | OPT_END() |
1022 | }; | |
74884b52 | 1023 | struct notes_tree *t; |
c3ab1a8e | 1024 | int retval = 0; |
74884b52 SB |
1025 | |
1026 | argc = parse_options(argc, argv, prefix, options, | |
1027 | git_notes_remove_usage, 0); | |
1028 | ||
ee76f92f | 1029 | t = init_notes_check("remove", NOTES_INIT_WRITABLE); |
74884b52 | 1030 | |
46538012 | 1031 | if (!argc && !from_stdin) { |
2d370d2f | 1032 | retval = remove_one_note(t, "HEAD", flag); |
c3ab1a8e JH |
1033 | } else { |
1034 | while (*argv) { | |
2d370d2f | 1035 | retval |= remove_one_note(t, *argv, flag); |
c3ab1a8e JH |
1036 | argv++; |
1037 | } | |
1ee1e43d | 1038 | } |
46538012 JH |
1039 | if (from_stdin) { |
1040 | struct strbuf sb = STRBUF_INIT; | |
1041 | while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) { | |
1042 | strbuf_rtrim(&sb); | |
1043 | retval |= remove_one_note(t, sb.buf, flag); | |
1044 | } | |
1045 | strbuf_release(&sb); | |
1046 | } | |
c3ab1a8e | 1047 | if (!retval) |
1d18d758 NTND |
1048 | commit_notes(the_repository, t, |
1049 | "Notes removed by 'git notes remove'"); | |
74884b52 | 1050 | free_notes(t); |
1ee1e43d | 1051 | return retval; |
74884b52 SB |
1052 | } |
1053 | ||
1054 | static int prune(int argc, const char **argv, const char *prefix) | |
1055 | { | |
1056 | struct notes_tree *t; | |
a9f2adff | 1057 | int show_only = 0, verbose = 0; |
74884b52 | 1058 | struct option options[] = { |
b34c77e3 VA |
1059 | OPT__DRY_RUN(&show_only, N_("do not remove, show only")), |
1060 | OPT__VERBOSE(&verbose, N_("report pruned notes")), | |
74884b52 SB |
1061 | OPT_END() |
1062 | }; | |
1063 | ||
1064 | argc = parse_options(argc, argv, prefix, options, git_notes_prune_usage, | |
1065 | 0); | |
1066 | ||
1067 | if (argc) { | |
b8657347 | 1068 | error(_("too many arguments")); |
74884b52 SB |
1069 | usage_with_options(git_notes_prune_usage, options); |
1070 | } | |
1071 | ||
ee76f92f | 1072 | t = init_notes_check("prune", NOTES_INIT_WRITABLE); |
74884b52 | 1073 | |
a9f2adff MG |
1074 | prune_notes(t, (verbose ? NOTES_PRUNE_VERBOSE : 0) | |
1075 | (show_only ? NOTES_PRUNE_VERBOSE|NOTES_PRUNE_DRYRUN : 0) ); | |
1076 | if (!show_only) | |
1d18d758 NTND |
1077 | commit_notes(the_repository, t, |
1078 | "Notes removed by 'git notes prune'"); | |
74884b52 SB |
1079 | free_notes(t); |
1080 | return 0; | |
1081 | } | |
1082 | ||
618cd757 JH |
1083 | static int get_ref(int argc, const char **argv, const char *prefix) |
1084 | { | |
1085 | struct option options[] = { OPT_END() }; | |
1086 | argc = parse_options(argc, argv, prefix, options, | |
1087 | git_notes_get_ref_usage, 0); | |
1088 | ||
1089 | if (argc) { | |
b8657347 | 1090 | error(_("too many arguments")); |
618cd757 JH |
1091 | usage_with_options(git_notes_get_ref_usage, options); |
1092 | } | |
1093 | ||
1094 | puts(default_notes_ref()); | |
1095 | return 0; | |
1096 | } | |
1097 | ||
74884b52 SB |
1098 | int cmd_notes(int argc, const char **argv, const char *prefix) |
1099 | { | |
74884b52 | 1100 | const char *override_notes_ref = NULL; |
1c7c25ae | 1101 | parse_opt_subcommand_fn *fn = NULL; |
74884b52 | 1102 | struct option options[] = { |
e703d711 | 1103 | OPT_STRING(0, "ref", &override_notes_ref, N_("notes-ref"), |
9c9b4f2f | 1104 | N_("use notes from <notes-ref>")), |
54ef7676 SG |
1105 | OPT_SUBCOMMAND("list", &fn, list), |
1106 | OPT_SUBCOMMAND("add", &fn, add), | |
1107 | OPT_SUBCOMMAND("copy", &fn, copy), | |
1108 | OPT_SUBCOMMAND("append", &fn, append_edit), | |
1109 | OPT_SUBCOMMAND("edit", &fn, append_edit), | |
1110 | OPT_SUBCOMMAND("show", &fn, show), | |
1111 | OPT_SUBCOMMAND("merge", &fn, merge), | |
1112 | OPT_SUBCOMMAND("remove", &fn, remove_cmd), | |
1113 | OPT_SUBCOMMAND("prune", &fn, prune), | |
1114 | OPT_SUBCOMMAND("get-ref", &fn, get_ref), | |
74884b52 SB |
1115 | OPT_END() |
1116 | }; | |
1117 | ||
1118 | git_config(git_default_config, NULL); | |
1119 | argc = parse_options(argc, argv, prefix, options, git_notes_usage, | |
54ef7676 | 1120 | PARSE_OPT_SUBCOMMAND_OPTIONAL); |
1c7c25ae SG |
1121 | if (!fn) { |
1122 | if (argc) { | |
dd834d75 | 1123 | error(_("unknown subcommand: `%s'"), argv[0]); |
1c7c25ae SG |
1124 | usage_with_options(git_notes_usage, options); |
1125 | } | |
1126 | fn = list; | |
54ef7676 | 1127 | } |
74884b52 SB |
1128 | |
1129 | if (override_notes_ref) { | |
1130 | struct strbuf sb = STRBUF_INIT; | |
74884b52 | 1131 | strbuf_addstr(&sb, override_notes_ref); |
8ef313e1 | 1132 | expand_notes_ref(&sb); |
74884b52 SB |
1133 | setenv("GIT_NOTES_REF", sb.buf, 1); |
1134 | strbuf_release(&sb); | |
1135 | } | |
1136 | ||
54ef7676 | 1137 | return !!fn(argc, argv, prefix); |
74884b52 | 1138 | } |