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