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