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