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