]>
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" | |
15 | #include "commit.h" | |
16 | #include "refs.h" | |
17 | #include "exec_cmd.h" | |
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 AH |
35 | N_("git notes [--ref <notes-ref>] remove [<object>...]"), |
36 | N_("git notes [--ref <notes-ref>] prune [-n | -v]"), | |
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 | ||
bebf5c04 | 121 | static void copy_obj_to_fd(int fd, const unsigned char *sha1) |
cd067d3b JH |
122 | { |
123 | unsigned long size; | |
124 | enum object_type type; | |
125 | char *buf = read_sha1_file(sha1, &type, &size); | |
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, |
52694cda | 165 | const unsigned char *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 | |
52694cda JH |
201 | static void write_note_data(struct note_data *d, unsigned char *sha1) |
202 | { | |
203 | if (write_sha1_file(d->buf.buf, d->buf.len, blob_type, sha1)) { | |
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); |
bb7e4739 | 256 | if (!(buf = read_sha1_file(object.hash, &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 | } |
160baa0d TR |
331 | return ret; |
332 | } | |
333 | ||
ee76f92f MH |
334 | static struct notes_tree *init_notes_check(const char *subcommand, |
335 | int flags) | |
cd067d3b | 336 | { |
cd067d3b | 337 | struct notes_tree *t; |
ee76f92f MH |
338 | const char *ref; |
339 | init_notes(NULL, NULL, NULL, flags); | |
74884b52 | 340 | t = &default_notes_tree; |
a0b4dfa9 | 341 | |
ee76f92f MH |
342 | ref = (flags & NOTES_INIT_WRITABLE) ? t->update_ref : t->ref; |
343 | if (!starts_with(ref, "refs/notes/")) | |
66f5f6dc ÆAB |
344 | /* |
345 | * TRANSLATORS: the first %s will be replaced by a git | |
346 | * notes command: 'add', 'merge', 'remove', etc. | |
347 | */ | |
8d79589a | 348 | die(_("refusing to %s notes in %s (outside of refs/notes/)"), |
ee76f92f | 349 | subcommand, ref); |
74884b52 SB |
350 | return t; |
351 | } | |
352 | ||
353 | static int list(int argc, const char **argv, const char *prefix) | |
cd067d3b | 354 | { |
cd067d3b | 355 | struct notes_tree *t; |
bb7e4739 | 356 | struct object_id object; |
9ef72230 | 357 | const struct object_id *note; |
74884b52 | 358 | int retval = -1; |
cd067d3b | 359 | struct option options[] = { |
cd067d3b JH |
360 | OPT_END() |
361 | }; | |
362 | ||
74884b52 SB |
363 | if (argc) |
364 | argc = parse_options(argc, argv, prefix, options, | |
365 | git_notes_list_usage, 0); | |
cd067d3b | 366 | |
74884b52 | 367 | if (1 < argc) { |
caeba0ef | 368 | error(_("too many parameters")); |
74884b52 | 369 | usage_with_options(git_notes_list_usage, options); |
dcf783a2 TR |
370 | } |
371 | ||
ee76f92f | 372 | t = init_notes_check("list", 0); |
74884b52 | 373 | if (argc) { |
bb7e4739 | 374 | if (get_oid(argv[0], &object)) |
8d79589a | 375 | die(_("failed to resolve '%s' as a valid ref."), argv[0]); |
5ee8a954 | 376 | note = get_note(t, &object); |
74884b52 | 377 | if (note) { |
9ef72230 | 378 | puts(oid_to_hex(note)); |
74884b52 SB |
379 | retval = 0; |
380 | } else | |
8d79589a | 381 | retval = error(_("no note found for object %s."), |
bb7e4739 | 382 | oid_to_hex(&object)); |
74884b52 SB |
383 | } else |
384 | retval = for_each_note(t, 0, list_each_note, NULL); | |
cd067d3b | 385 | |
74884b52 SB |
386 | free_notes(t); |
387 | return retval; | |
388 | } | |
cd067d3b | 389 | |
84a7e35e JH |
390 | static int append_edit(int argc, const char **argv, const char *prefix); |
391 | ||
74884b52 SB |
392 | static int add(int argc, const char **argv, const char *prefix) |
393 | { | |
d73a5b93 | 394 | int force = 0, allow_empty = 0; |
92b3385f | 395 | const char *object_ref; |
74884b52 | 396 | struct notes_tree *t; |
bb7e4739 | 397 | struct object_id object, new_note; |
9ef72230 | 398 | const struct object_id *note; |
4282af0f | 399 | struct note_data d = { 0, 0, NULL, STRBUF_INIT }; |
cd067d3b | 400 | struct option options[] = { |
bebf5c04 | 401 | { OPTION_CALLBACK, 'm', "message", &d, N_("message"), |
e6b895ef | 402 | N_("note contents as a string"), PARSE_OPT_NONEG, |
43a61b84 | 403 | parse_msg_arg}, |
bebf5c04 | 404 | { OPTION_CALLBACK, 'F', "file", &d, N_("file"), |
e6b895ef | 405 | N_("note contents in a file"), PARSE_OPT_NONEG, |
43a61b84 | 406 | parse_file_arg}, |
bebf5c04 | 407 | { OPTION_CALLBACK, 'c', "reedit-message", &d, N_("object"), |
e6b895ef | 408 | N_("reuse and edit specified note object"), PARSE_OPT_NONEG, |
43a61b84 | 409 | parse_reedit_arg}, |
bebf5c04 | 410 | { OPTION_CALLBACK, 'C', "reuse-message", &d, N_("object"), |
e6b895ef | 411 | N_("reuse specified note object"), PARSE_OPT_NONEG, |
43a61b84 | 412 | parse_reuse_arg}, |
d73a5b93 JH |
413 | OPT_BOOL(0, "allow-empty", &allow_empty, |
414 | N_("allow storing empty note")), | |
e6b895ef | 415 | OPT__FORCE(&force, N_("replace existing notes")), |
cd067d3b JH |
416 | OPT_END() |
417 | }; | |
418 | ||
74884b52 | 419 | argc = parse_options(argc, argv, prefix, options, git_notes_add_usage, |
84a7e35e | 420 | PARSE_OPT_KEEP_ARGV0); |
cd067d3b | 421 | |
84a7e35e | 422 | if (2 < argc) { |
caeba0ef | 423 | error(_("too many parameters")); |
74884b52 | 424 | usage_with_options(git_notes_add_usage, options); |
dcf783a2 TR |
425 | } |
426 | ||
84a7e35e | 427 | object_ref = argc > 1 ? argv[1] : "HEAD"; |
cd067d3b | 428 | |
bb7e4739 | 429 | if (get_oid(object_ref, &object)) |
8d79589a | 430 | die(_("failed to resolve '%s' as a valid ref."), object_ref); |
cd067d3b | 431 | |
ee76f92f | 432 | t = init_notes_check("add", NOTES_INIT_WRITABLE); |
5ee8a954 | 433 | note = get_note(t, &object); |
92b3385f | 434 | |
74884b52 SB |
435 | if (note) { |
436 | if (!force) { | |
b0de56c6 JH |
437 | free_notes(t); |
438 | if (d.given) { | |
4282af0f | 439 | free_note_data(&d); |
b0de56c6 JH |
440 | return error(_("Cannot add notes. " |
441 | "Found existing notes for object %s. " | |
442 | "Use '-f' to overwrite existing notes"), | |
bb7e4739 | 443 | oid_to_hex(&object)); |
84a7e35e | 444 | } |
b0de56c6 JH |
445 | /* |
446 | * Redirect to "edit" subcommand. | |
447 | * | |
448 | * We only end up here if none of -m/-F/-c/-C or -f are | |
449 | * given. The original args are therefore still in | |
450 | * argv[0-1]. | |
451 | */ | |
452 | argv[0] = "edit"; | |
453 | return append_edit(argc, argv, prefix); | |
74884b52 | 454 | } |
caeba0ef | 455 | fprintf(stderr, _("Overwriting existing notes for object %s\n"), |
bb7e4739 | 456 | oid_to_hex(&object)); |
aaec9bcf JH |
457 | } |
458 | ||
bb7e4739 | 459 | prepare_note_data(&object, &d, note->hash); |
d73a5b93 | 460 | if (d.buf.len || allow_empty) { |
bb7e4739 | 461 | write_note_data(&d, new_note.hash); |
5ee8a954 | 462 | if (add_note(t, &object, &new_note, combine_notes_overwrite)) |
52694cda JH |
463 | die("BUG: combine_notes_overwrite failed"); |
464 | commit_notes(t, "Notes added by 'git notes add'"); | |
465 | } else { | |
466 | fprintf(stderr, _("Removing note for object %s\n"), | |
bb7e4739 | 467 | oid_to_hex(&object)); |
468 | remove_note(t, object.hash); | |
52694cda JH |
469 | commit_notes(t, "Notes removed by 'git notes add'"); |
470 | } | |
160baa0d | 471 | |
52694cda | 472 | free_note_data(&d); |
74884b52 | 473 | free_notes(t); |
b0de56c6 | 474 | return 0; |
74884b52 | 475 | } |
cd067d3b | 476 | |
74884b52 SB |
477 | static int copy(int argc, const char **argv, const char *prefix) |
478 | { | |
479 | int retval = 0, force = 0, from_stdin = 0; | |
9ef72230 | 480 | const struct object_id *from_note, *note; |
74884b52 | 481 | const char *object_ref; |
bb7e4739 | 482 | struct object_id object, from_obj; |
74884b52 SB |
483 | struct notes_tree *t; |
484 | const char *rewrite_cmd = NULL; | |
485 | struct option options[] = { | |
e6b895ef | 486 | OPT__FORCE(&force, N_("replace existing notes")), |
d5d09d47 | 487 | OPT_BOOL(0, "stdin", &from_stdin, N_("read objects from stdin")), |
e6b895ef NTND |
488 | OPT_STRING(0, "for-rewrite", &rewrite_cmd, N_("command"), |
489 | N_("load rewriting config for <command> (implies " | |
490 | "--stdin)")), | |
74884b52 SB |
491 | OPT_END() |
492 | }; | |
cd067d3b | 493 | |
74884b52 SB |
494 | argc = parse_options(argc, argv, prefix, options, git_notes_copy_usage, |
495 | 0); | |
e397421a | 496 | |
74884b52 SB |
497 | if (from_stdin || rewrite_cmd) { |
498 | if (argc) { | |
caeba0ef | 499 | error(_("too many parameters")); |
74884b52 | 500 | usage_with_options(git_notes_copy_usage, options); |
5848769f | 501 | } else { |
74884b52 | 502 | return notes_copy_from_stdin(force, rewrite_cmd); |
e73bbd96 | 503 | } |
e73bbd96 JH |
504 | } |
505 | ||
bbb1b8a3 | 506 | if (argc < 2) { |
caeba0ef | 507 | error(_("too few parameters")); |
bbb1b8a3 JK |
508 | usage_with_options(git_notes_copy_usage, options); |
509 | } | |
74884b52 | 510 | if (2 < argc) { |
caeba0ef | 511 | error(_("too many parameters")); |
74884b52 | 512 | usage_with_options(git_notes_copy_usage, options); |
cd067d3b JH |
513 | } |
514 | ||
bb7e4739 | 515 | if (get_oid(argv[0], &from_obj)) |
8d79589a | 516 | die(_("failed to resolve '%s' as a valid ref."), argv[0]); |
cd067d3b | 517 | |
74884b52 | 518 | object_ref = 1 < argc ? argv[1] : "HEAD"; |
cd067d3b | 519 | |
bb7e4739 | 520 | if (get_oid(object_ref, &object)) |
8d79589a | 521 | die(_("failed to resolve '%s' as a valid ref."), object_ref); |
cd067d3b | 522 | |
ee76f92f | 523 | t = init_notes_check("copy", NOTES_INIT_WRITABLE); |
5ee8a954 | 524 | note = get_note(t, &object); |
cd067d3b | 525 | |
74884b52 | 526 | if (note) { |
5848769f | 527 | if (!force) { |
caeba0ef | 528 | retval = error(_("Cannot copy notes. Found existing " |
74884b52 | 529 | "notes for object %s. Use '-f' to " |
caeba0ef | 530 | "overwrite existing notes"), |
bb7e4739 | 531 | oid_to_hex(&object)); |
74884b52 | 532 | goto out; |
5848769f | 533 | } |
caeba0ef | 534 | fprintf(stderr, _("Overwriting existing notes for object %s\n"), |
bb7e4739 | 535 | oid_to_hex(&object)); |
e397421a JH |
536 | } |
537 | ||
5ee8a954 | 538 | from_note = get_note(t, &from_obj); |
74884b52 | 539 | if (!from_note) { |
8d79589a | 540 | retval = error(_("missing notes on source object %s. Cannot " |
bb7e4739 | 541 | "copy."), oid_to_hex(&from_obj)); |
74884b52 | 542 | goto out; |
cd067d3b JH |
543 | } |
544 | ||
5ee8a954 | 545 | if (add_note(t, &object, from_note, combine_notes_overwrite)) |
180619a5 | 546 | die("BUG: combine_notes_overwrite failed"); |
74884b52 SB |
547 | commit_notes(t, "Notes added by 'git notes copy'"); |
548 | out: | |
549 | free_notes(t); | |
550 | return retval; | |
551 | } | |
7aa4754e | 552 | |
74884b52 SB |
553 | static int append_edit(int argc, const char **argv, const char *prefix) |
554 | { | |
d73a5b93 | 555 | int allow_empty = 0; |
74884b52 SB |
556 | const char *object_ref; |
557 | struct notes_tree *t; | |
bb7e4739 | 558 | struct object_id object, new_note; |
9ef72230 | 559 | const struct object_id *note; |
5b1ef2ce | 560 | char *logmsg; |
74884b52 | 561 | const char * const *usage; |
4282af0f | 562 | struct note_data d = { 0, 0, NULL, STRBUF_INIT }; |
74884b52 | 563 | struct option options[] = { |
bebf5c04 | 564 | { OPTION_CALLBACK, 'm', "message", &d, N_("message"), |
e6b895ef | 565 | N_("note contents as a string"), PARSE_OPT_NONEG, |
74884b52 | 566 | parse_msg_arg}, |
bebf5c04 | 567 | { OPTION_CALLBACK, 'F', "file", &d, N_("file"), |
e6b895ef | 568 | N_("note contents in a file"), PARSE_OPT_NONEG, |
74884b52 | 569 | parse_file_arg}, |
bebf5c04 | 570 | { OPTION_CALLBACK, 'c', "reedit-message", &d, N_("object"), |
e6b895ef | 571 | N_("reuse and edit specified note object"), PARSE_OPT_NONEG, |
74884b52 | 572 | parse_reedit_arg}, |
bebf5c04 | 573 | { OPTION_CALLBACK, 'C', "reuse-message", &d, N_("object"), |
e6b895ef | 574 | N_("reuse specified note object"), PARSE_OPT_NONEG, |
74884b52 | 575 | parse_reuse_arg}, |
d73a5b93 JH |
576 | OPT_BOOL(0, "allow-empty", &allow_empty, |
577 | N_("allow storing empty note")), | |
74884b52 SB |
578 | OPT_END() |
579 | }; | |
580 | int edit = !strcmp(argv[0], "edit"); | |
581 | ||
582 | usage = edit ? git_notes_edit_usage : git_notes_append_usage; | |
583 | argc = parse_options(argc, argv, prefix, options, usage, | |
584 | PARSE_OPT_KEEP_ARGV0); | |
92b3385f | 585 | |
74884b52 | 586 | if (2 < argc) { |
caeba0ef | 587 | error(_("too many parameters")); |
74884b52 | 588 | usage_with_options(usage, options); |
cd067d3b JH |
589 | } |
590 | ||
bebf5c04 | 591 | if (d.given && edit) |
caeba0ef | 592 | fprintf(stderr, _("The -m/-F/-c/-C options have been deprecated " |
74884b52 | 593 | "for the 'edit' subcommand.\n" |
caeba0ef | 594 | "Please use 'git notes add -f -m/-F/-c/-C' instead.\n")); |
74884b52 SB |
595 | |
596 | object_ref = 1 < argc ? argv[1] : "HEAD"; | |
597 | ||
bb7e4739 | 598 | if (get_oid(object_ref, &object)) |
8d79589a | 599 | die(_("failed to resolve '%s' as a valid ref."), object_ref); |
74884b52 | 600 | |
ee76f92f | 601 | t = init_notes_check(argv[0], NOTES_INIT_WRITABLE); |
5ee8a954 | 602 | note = get_note(t, &object); |
74884b52 | 603 | |
bb7e4739 | 604 | prepare_note_data(&object, &d, edit && note ? note->hash : NULL); |
5848769f | 605 | |
52694cda JH |
606 | if (note && !edit) { |
607 | /* Append buf to previous note contents */ | |
608 | unsigned long size; | |
609 | enum object_type type; | |
9ef72230 | 610 | char *prev_buf = read_sha1_file(note->hash, &type, &size); |
52694cda JH |
611 | |
612 | strbuf_grow(&d.buf, size + 1); | |
613 | if (d.buf.len && prev_buf && size) | |
614 | strbuf_insert(&d.buf, 0, "\n", 1); | |
615 | if (prev_buf && size) | |
616 | strbuf_insert(&d.buf, 0, prev_buf, size); | |
617 | free(prev_buf); | |
618 | } | |
5848769f | 619 | |
d73a5b93 | 620 | if (d.buf.len || allow_empty) { |
bb7e4739 | 621 | write_note_data(&d, new_note.hash); |
5ee8a954 | 622 | if (add_note(t, &object, &new_note, combine_notes_overwrite)) |
52694cda | 623 | die("BUG: combine_notes_overwrite failed"); |
5b1ef2ce | 624 | logmsg = xstrfmt("Notes added by 'git notes %s'", argv[0]); |
52694cda JH |
625 | } else { |
626 | fprintf(stderr, _("Removing note for object %s\n"), | |
bb7e4739 | 627 | oid_to_hex(&object)); |
628 | remove_note(t, object.hash); | |
5b1ef2ce | 629 | logmsg = xstrfmt("Notes removed by 'git notes %s'", argv[0]); |
52694cda | 630 | } |
a0b4dfa9 | 631 | commit_notes(t, logmsg); |
52694cda | 632 | |
5b1ef2ce | 633 | free(logmsg); |
52694cda | 634 | free_note_data(&d); |
cd067d3b | 635 | free_notes(t); |
74884b52 SB |
636 | return 0; |
637 | } | |
638 | ||
639 | static int show(int argc, const char **argv, const char *prefix) | |
640 | { | |
641 | const char *object_ref; | |
642 | struct notes_tree *t; | |
bb7e4739 | 643 | struct object_id object; |
9ef72230 | 644 | const struct object_id *note; |
74884b52 SB |
645 | int retval; |
646 | struct option options[] = { | |
647 | OPT_END() | |
648 | }; | |
649 | ||
650 | argc = parse_options(argc, argv, prefix, options, git_notes_show_usage, | |
651 | 0); | |
652 | ||
653 | if (1 < argc) { | |
caeba0ef | 654 | error(_("too many parameters")); |
74884b52 SB |
655 | usage_with_options(git_notes_show_usage, options); |
656 | } | |
657 | ||
658 | object_ref = argc ? argv[0] : "HEAD"; | |
659 | ||
bb7e4739 | 660 | if (get_oid(object_ref, &object)) |
8d79589a | 661 | die(_("failed to resolve '%s' as a valid ref."), object_ref); |
74884b52 | 662 | |
ee76f92f | 663 | t = init_notes_check("show", 0); |
5ee8a954 | 664 | note = get_note(t, &object); |
74884b52 SB |
665 | |
666 | if (!note) | |
8d79589a | 667 | retval = error(_("no note found for object %s."), |
bb7e4739 | 668 | oid_to_hex(&object)); |
74884b52 | 669 | else { |
9ef72230 | 670 | const char *show_args[3] = {"show", oid_to_hex(note), NULL}; |
74884b52 SB |
671 | retval = execv_git_cmd(show_args); |
672 | } | |
673 | free_notes(t); | |
5848769f | 674 | return retval; |
cd067d3b | 675 | } |
74884b52 | 676 | |
6abb3655 JH |
677 | static int merge_abort(struct notes_merge_options *o) |
678 | { | |
679 | int ret = 0; | |
680 | ||
681 | /* | |
682 | * Remove .git/NOTES_MERGE_PARTIAL and .git/NOTES_MERGE_REF, and call | |
683 | * notes_merge_abort() to remove .git/NOTES_MERGE_WORKTREE. | |
684 | */ | |
685 | ||
755b49ae | 686 | if (delete_ref(NULL, "NOTES_MERGE_PARTIAL", NULL, 0)) |
8d79589a | 687 | ret += error(_("failed to delete ref NOTES_MERGE_PARTIAL")); |
755b49ae | 688 | if (delete_ref(NULL, "NOTES_MERGE_REF", NULL, REF_NODEREF)) |
8d79589a | 689 | ret += error(_("failed to delete ref NOTES_MERGE_REF")); |
6abb3655 | 690 | if (notes_merge_abort(o)) |
8d79589a | 691 | ret += error(_("failed to remove 'git notes merge' worktree")); |
6abb3655 JH |
692 | return ret; |
693 | } | |
694 | ||
695 | static int merge_commit(struct notes_merge_options *o) | |
696 | { | |
697 | struct strbuf msg = STRBUF_INIT; | |
2928325f | 698 | struct object_id oid, parent_oid; |
6abb3655 JH |
699 | struct notes_tree *t; |
700 | struct commit *partial; | |
701 | struct pretty_print_context pretty_ctx; | |
96ec7b1e | 702 | void *local_ref_to_free; |
d5a35c11 | 703 | int ret; |
6abb3655 JH |
704 | |
705 | /* | |
706 | * Read partial merge result from .git/NOTES_MERGE_PARTIAL, | |
707 | * and target notes ref from .git/NOTES_MERGE_REF. | |
708 | */ | |
709 | ||
2928325f | 710 | if (get_oid("NOTES_MERGE_PARTIAL", &oid)) |
8d79589a | 711 | die(_("failed to read ref NOTES_MERGE_PARTIAL")); |
bc83266a | 712 | else if (!(partial = lookup_commit_reference(&oid))) |
8d79589a | 713 | die(_("could not find commit from NOTES_MERGE_PARTIAL.")); |
6abb3655 | 714 | else if (parse_commit(partial)) |
8d79589a | 715 | die(_("could not parse commit from NOTES_MERGE_PARTIAL.")); |
6abb3655 | 716 | |
6cfd6a9d | 717 | if (partial->parents) |
2928325f | 718 | oidcpy(&parent_oid, &partial->parents->item->object.oid); |
6cfd6a9d | 719 | else |
2928325f | 720 | oidclr(&parent_oid); |
6cfd6a9d | 721 | |
6abb3655 JH |
722 | t = xcalloc(1, sizeof(struct notes_tree)); |
723 | init_notes(t, "NOTES_MERGE_PARTIAL", combine_notes_overwrite, 0); | |
724 | ||
96ec7b1e | 725 | o->local_ref = local_ref_to_free = |
2928325f | 726 | resolve_refdup("NOTES_MERGE_REF", 0, oid.hash, NULL); |
6abb3655 | 727 | if (!o->local_ref) |
8d79589a | 728 | die(_("failed to resolve NOTES_MERGE_REF")); |
6abb3655 | 729 | |
5237e0eb | 730 | if (notes_merge_commit(o, t, partial, &oid)) |
8d79589a | 731 | die(_("failed to finalize notes merge")); |
6abb3655 JH |
732 | |
733 | /* Reuse existing commit message in reflog message */ | |
734 | memset(&pretty_ctx, 0, sizeof(pretty_ctx)); | |
735 | format_commit_message(partial, "%s", &msg, &pretty_ctx); | |
736 | strbuf_trim(&msg); | |
737 | strbuf_insert(&msg, 0, "notes: ", 7); | |
2928325f | 738 | update_ref(msg.buf, o->local_ref, oid.hash, |
739 | is_null_oid(&parent_oid) ? NULL : parent_oid.hash, | |
f4124112 | 740 | 0, UPDATE_REFS_DIE_ON_ERR); |
6abb3655 JH |
741 | |
742 | free_notes(t); | |
743 | strbuf_release(&msg); | |
d5a35c11 | 744 | ret = merge_abort(o); |
96ec7b1e | 745 | free(local_ref_to_free); |
d5a35c11 | 746 | return ret; |
6abb3655 JH |
747 | } |
748 | ||
d2d68d99 JK |
749 | static int git_config_get_notes_strategy(const char *key, |
750 | enum notes_merge_strategy *strategy) | |
751 | { | |
344b5484 | 752 | char *value; |
d2d68d99 | 753 | |
344b5484 | 754 | if (git_config_get_string(key, &value)) |
d2d68d99 JK |
755 | return 1; |
756 | if (parse_notes_merge_strategy(value, strategy)) | |
5313827f | 757 | git_die_config(key, _("unknown notes merge strategy %s"), value); |
d2d68d99 | 758 | |
344b5484 | 759 | free(value); |
d2d68d99 JK |
760 | return 0; |
761 | } | |
762 | ||
75ef3f4a JH |
763 | static int merge(int argc, const char **argv, const char *prefix) |
764 | { | |
765 | struct strbuf remote_ref = STRBUF_INIT, msg = STRBUF_INIT; | |
bb7e4739 | 766 | struct object_id result_oid; |
2085b16a | 767 | struct notes_tree *t; |
75ef3f4a | 768 | struct notes_merge_options o; |
6abb3655 | 769 | int do_merge = 0, do_commit = 0, do_abort = 0; |
75ef3f4a | 770 | int verbosity = 0, result; |
3228e671 | 771 | const char *strategy = NULL; |
75ef3f4a | 772 | struct option options[] = { |
e6b895ef | 773 | OPT_GROUP(N_("General options")), |
75ef3f4a | 774 | OPT__VERBOSITY(&verbosity), |
e6b895ef NTND |
775 | OPT_GROUP(N_("Merge options")), |
776 | OPT_STRING('s', "strategy", &strategy, N_("strategy"), | |
777 | N_("resolve notes conflicts using the given strategy " | |
778 | "(manual/ours/theirs/union/cat_sort_uniq)")), | |
779 | OPT_GROUP(N_("Committing unmerged notes")), | |
4741edd5 | 780 | { OPTION_SET_INT, 0, "commit", &do_commit, NULL, |
e6b895ef | 781 | N_("finalize notes merge by committing unmerged notes"), |
4741edd5 | 782 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1}, |
e6b895ef | 783 | OPT_GROUP(N_("Aborting notes merge resolution")), |
4741edd5 | 784 | { OPTION_SET_INT, 0, "abort", &do_abort, NULL, |
e6b895ef | 785 | N_("abort notes merge"), |
4741edd5 | 786 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1}, |
75ef3f4a JH |
787 | OPT_END() |
788 | }; | |
789 | ||
790 | argc = parse_options(argc, argv, prefix, options, | |
791 | git_notes_merge_usage, 0); | |
792 | ||
6abb3655 JH |
793 | if (strategy || do_commit + do_abort == 0) |
794 | do_merge = 1; | |
795 | if (do_merge + do_commit + do_abort != 1) { | |
5313827f | 796 | error(_("cannot mix --commit, --abort or -s/--strategy")); |
6abb3655 JH |
797 | usage_with_options(git_notes_merge_usage, options); |
798 | } | |
799 | ||
800 | if (do_merge && argc != 1) { | |
8d79589a | 801 | error(_("must specify a notes ref to merge")); |
75ef3f4a | 802 | usage_with_options(git_notes_merge_usage, options); |
6abb3655 | 803 | } else if (!do_merge && argc) { |
5313827f | 804 | error(_("too many parameters")); |
6abb3655 | 805 | usage_with_options(git_notes_merge_usage, options); |
75ef3f4a JH |
806 | } |
807 | ||
808 | init_notes_merge_options(&o); | |
809 | o.verbosity = verbosity + NOTES_MERGE_VERBOSITY_DEFAULT; | |
810 | ||
6abb3655 JH |
811 | if (do_abort) |
812 | return merge_abort(&o); | |
813 | if (do_commit) | |
814 | return merge_commit(&o); | |
815 | ||
75ef3f4a JH |
816 | o.local_ref = default_notes_ref(); |
817 | strbuf_addstr(&remote_ref, argv[0]); | |
b3715b75 | 818 | expand_loose_notes_ref(&remote_ref); |
75ef3f4a JH |
819 | o.remote_ref = remote_ref.buf; |
820 | ||
ee76f92f | 821 | t = init_notes_check("merge", NOTES_INIT_WRITABLE); |
d2d68d99 | 822 | |
3228e671 | 823 | if (strategy) { |
93efcad3 | 824 | if (parse_notes_merge_strategy(strategy, &o.strategy)) { |
8d79589a | 825 | error(_("unknown -s/--strategy: %s"), strategy); |
3228e671 JH |
826 | usage_with_options(git_notes_merge_usage, options); |
827 | } | |
d2d68d99 | 828 | } else { |
4f655e22 JK |
829 | struct strbuf merge_key = STRBUF_INIT; |
830 | const char *short_ref = NULL; | |
3228e671 | 831 | |
4f655e22 JK |
832 | if (!skip_prefix(o.local_ref, "refs/notes/", &short_ref)) |
833 | die("BUG: local ref %s is outside of refs/notes/", | |
834 | o.local_ref); | |
835 | ||
836 | strbuf_addf(&merge_key, "notes.%s.mergeStrategy", short_ref); | |
837 | ||
838 | if (git_config_get_notes_strategy(merge_key.buf, &o.strategy)) | |
839 | git_config_get_notes_strategy("notes.mergeStrategy", &o.strategy); | |
840 | ||
841 | strbuf_release(&merge_key); | |
3228e671 | 842 | } |
75ef3f4a JH |
843 | |
844 | strbuf_addf(&msg, "notes: Merged notes from %s into %s", | |
845 | remote_ref.buf, default_notes_ref()); | |
443259cf | 846 | strbuf_add(&(o.commit_msg), msg.buf + 7, msg.len - 7); /* skip "notes: " */ |
2085b16a | 847 | |
5237e0eb | 848 | result = notes_merge(&o, t, &result_oid); |
2085b16a | 849 | |
5237e0eb | 850 | if (result >= 0) /* Merge resulted (trivially) in result_oid */ |
75ef3f4a | 851 | /* Update default notes ref with new commit */ |
bb7e4739 | 852 | update_ref(msg.buf, default_notes_ref(), result_oid.hash, NULL, |
f4124112 | 853 | 0, UPDATE_REFS_DIE_ON_ERR); |
6abb3655 | 854 | else { /* Merge has unresolved conflicts */ |
d3b9ac07 | 855 | const struct worktree *wt; |
6abb3655 | 856 | /* Update .git/NOTES_MERGE_PARTIAL with partial merge result */ |
bb7e4739 | 857 | update_ref(msg.buf, "NOTES_MERGE_PARTIAL", result_oid.hash, NULL, |
f4124112 | 858 | 0, UPDATE_REFS_DIE_ON_ERR); |
6abb3655 | 859 | /* Store ref-to-be-updated into .git/NOTES_MERGE_REF */ |
d3b9ac07 NTND |
860 | wt = find_shared_symref("NOTES_MERGE_REF", default_notes_ref()); |
861 | if (wt) | |
8d79589a | 862 | die(_("a notes merge into %s is already in-progress at %s"), |
d3b9ac07 | 863 | default_notes_ref(), wt->path); |
6abb3655 | 864 | if (create_symref("NOTES_MERGE_REF", default_notes_ref(), NULL)) |
8d79589a | 865 | die(_("failed to store link to current notes ref (%s)"), |
6abb3655 | 866 | default_notes_ref()); |
5313827f VA |
867 | printf(_("Automatic notes merge failed. Fix conflicts in %s and " |
868 | "commit the result with 'git notes merge --commit', or " | |
869 | "abort the merge with 'git notes merge --abort'.\n"), | |
809f38c8 | 870 | git_path(NOTES_MERGE_WORKTREE)); |
6abb3655 | 871 | } |
75ef3f4a | 872 | |
2085b16a | 873 | free_notes(t); |
75ef3f4a JH |
874 | strbuf_release(&remote_ref); |
875 | strbuf_release(&msg); | |
809f38c8 | 876 | return result < 0; /* return non-zero on conflicts */ |
75ef3f4a JH |
877 | } |
878 | ||
46538012 | 879 | #define IGNORE_MISSING 1 |
2d370d2f JH |
880 | |
881 | static int remove_one_note(struct notes_tree *t, const char *name, unsigned flag) | |
c3ab1a8e JH |
882 | { |
883 | int status; | |
bb7e4739 | 884 | struct object_id oid; |
885 | if (get_oid(name, &oid)) | |
c3ab1a8e | 886 | return error(_("Failed to resolve '%s' as a valid ref."), name); |
bb7e4739 | 887 | status = remove_note(t, oid.hash); |
c3ab1a8e JH |
888 | if (status) |
889 | fprintf(stderr, _("Object %s has no note\n"), name); | |
890 | else | |
891 | fprintf(stderr, _("Removing note for object %s\n"), name); | |
46538012 | 892 | return (flag & IGNORE_MISSING) ? 0 : status; |
c3ab1a8e JH |
893 | } |
894 | ||
74884b52 SB |
895 | static int remove_cmd(int argc, const char **argv, const char *prefix) |
896 | { | |
2d370d2f | 897 | unsigned flag = 0; |
46538012 | 898 | int from_stdin = 0; |
74884b52 | 899 | struct option options[] = { |
2d370d2f | 900 | OPT_BIT(0, "ignore-missing", &flag, |
e6b895ef | 901 | N_("attempt to remove non-existent note is not an error"), |
46538012 | 902 | IGNORE_MISSING), |
d5d09d47 | 903 | OPT_BOOL(0, "stdin", &from_stdin, |
e6b895ef | 904 | N_("read object names from the standard input")), |
74884b52 SB |
905 | OPT_END() |
906 | }; | |
74884b52 | 907 | struct notes_tree *t; |
c3ab1a8e | 908 | int retval = 0; |
74884b52 SB |
909 | |
910 | argc = parse_options(argc, argv, prefix, options, | |
911 | git_notes_remove_usage, 0); | |
912 | ||
ee76f92f | 913 | t = init_notes_check("remove", NOTES_INIT_WRITABLE); |
74884b52 | 914 | |
46538012 | 915 | if (!argc && !from_stdin) { |
2d370d2f | 916 | retval = remove_one_note(t, "HEAD", flag); |
c3ab1a8e JH |
917 | } else { |
918 | while (*argv) { | |
2d370d2f | 919 | retval |= remove_one_note(t, *argv, flag); |
c3ab1a8e JH |
920 | argv++; |
921 | } | |
1ee1e43d | 922 | } |
46538012 JH |
923 | if (from_stdin) { |
924 | struct strbuf sb = STRBUF_INIT; | |
925 | while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) { | |
926 | strbuf_rtrim(&sb); | |
927 | retval |= remove_one_note(t, sb.buf, flag); | |
928 | } | |
929 | strbuf_release(&sb); | |
930 | } | |
c3ab1a8e JH |
931 | if (!retval) |
932 | commit_notes(t, "Notes removed by 'git notes remove'"); | |
74884b52 | 933 | free_notes(t); |
1ee1e43d | 934 | return retval; |
74884b52 SB |
935 | } |
936 | ||
937 | static int prune(int argc, const char **argv, const char *prefix) | |
938 | { | |
939 | struct notes_tree *t; | |
a9f2adff | 940 | int show_only = 0, verbose = 0; |
74884b52 | 941 | struct option options[] = { |
b34c77e3 VA |
942 | OPT__DRY_RUN(&show_only, N_("do not remove, show only")), |
943 | OPT__VERBOSE(&verbose, N_("report pruned notes")), | |
74884b52 SB |
944 | OPT_END() |
945 | }; | |
946 | ||
947 | argc = parse_options(argc, argv, prefix, options, git_notes_prune_usage, | |
948 | 0); | |
949 | ||
950 | if (argc) { | |
caeba0ef | 951 | error(_("too many parameters")); |
74884b52 SB |
952 | usage_with_options(git_notes_prune_usage, options); |
953 | } | |
954 | ||
ee76f92f | 955 | t = init_notes_check("prune", NOTES_INIT_WRITABLE); |
74884b52 | 956 | |
a9f2adff MG |
957 | prune_notes(t, (verbose ? NOTES_PRUNE_VERBOSE : 0) | |
958 | (show_only ? NOTES_PRUNE_VERBOSE|NOTES_PRUNE_DRYRUN : 0) ); | |
959 | if (!show_only) | |
960 | commit_notes(t, "Notes removed by 'git notes prune'"); | |
74884b52 SB |
961 | free_notes(t); |
962 | return 0; | |
963 | } | |
964 | ||
618cd757 JH |
965 | static int get_ref(int argc, const char **argv, const char *prefix) |
966 | { | |
967 | struct option options[] = { OPT_END() }; | |
968 | argc = parse_options(argc, argv, prefix, options, | |
969 | git_notes_get_ref_usage, 0); | |
970 | ||
971 | if (argc) { | |
5313827f | 972 | error(_("too many parameters")); |
618cd757 JH |
973 | usage_with_options(git_notes_get_ref_usage, options); |
974 | } | |
975 | ||
976 | puts(default_notes_ref()); | |
977 | return 0; | |
978 | } | |
979 | ||
74884b52 SB |
980 | int cmd_notes(int argc, const char **argv, const char *prefix) |
981 | { | |
982 | int result; | |
983 | const char *override_notes_ref = NULL; | |
984 | struct option options[] = { | |
e703d711 | 985 | OPT_STRING(0, "ref", &override_notes_ref, N_("notes-ref"), |
9c9b4f2f | 986 | N_("use notes from <notes-ref>")), |
74884b52 SB |
987 | OPT_END() |
988 | }; | |
989 | ||
990 | git_config(git_default_config, NULL); | |
991 | argc = parse_options(argc, argv, prefix, options, git_notes_usage, | |
992 | PARSE_OPT_STOP_AT_NON_OPTION); | |
993 | ||
994 | if (override_notes_ref) { | |
995 | struct strbuf sb = STRBUF_INIT; | |
74884b52 | 996 | strbuf_addstr(&sb, override_notes_ref); |
8ef313e1 | 997 | expand_notes_ref(&sb); |
74884b52 SB |
998 | setenv("GIT_NOTES_REF", sb.buf, 1); |
999 | strbuf_release(&sb); | |
1000 | } | |
1001 | ||
1002 | if (argc < 1 || !strcmp(argv[0], "list")) | |
1003 | result = list(argc, argv, prefix); | |
1004 | else if (!strcmp(argv[0], "add")) | |
1005 | result = add(argc, argv, prefix); | |
1006 | else if (!strcmp(argv[0], "copy")) | |
1007 | result = copy(argc, argv, prefix); | |
1008 | else if (!strcmp(argv[0], "append") || !strcmp(argv[0], "edit")) | |
1009 | result = append_edit(argc, argv, prefix); | |
1010 | else if (!strcmp(argv[0], "show")) | |
1011 | result = show(argc, argv, prefix); | |
75ef3f4a JH |
1012 | else if (!strcmp(argv[0], "merge")) |
1013 | result = merge(argc, argv, prefix); | |
74884b52 SB |
1014 | else if (!strcmp(argv[0], "remove")) |
1015 | result = remove_cmd(argc, argv, prefix); | |
1016 | else if (!strcmp(argv[0], "prune")) | |
1017 | result = prune(argc, argv, prefix); | |
618cd757 JH |
1018 | else if (!strcmp(argv[0], "get-ref")) |
1019 | result = get_ref(argc, argv, prefix); | |
74884b52 | 1020 | else { |
8d79589a | 1021 | result = error(_("unknown subcommand: %s"), argv[0]); |
74884b52 SB |
1022 | usage_with_options(git_notes_usage, options); |
1023 | } | |
1024 | ||
1025 | return result ? 1 : 0; | |
1026 | } |