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