notes: rework subcommands and parse options
[git/git.git] / builtin-notes.c
CommitLineData
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,
7 * and builtin-tag.c by Kristian Høgsberg and Carlos Rica.
8 */
9
10#include "cache.h"
11#include "builtin.h"
12#include "notes.h"
13#include "blob.h"
14#include "commit.h"
15#include "refs.h"
16#include "exec_cmd.h"
17#include "run-command.h"
18#include "parse-options.h"
6956f858 19#include "string-list.h"
cd067d3b
JH
20
21static const char * const git_notes_usage[] = {
74884b52
SB
22 "git notes [--ref <notes_ref>] [list [<object>]]",
23 "git notes [--ref <notes_ref>] add [-f] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]",
24 "git notes [--ref <notes_ref>] copy [-f] <from-object> <to-object>",
25 "git notes [--ref <notes_ref>] append [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]",
26 "git notes [--ref <notes_ref>] edit [<object>]",
27 "git notes [--ref <notes_ref>] show [<object>]",
28 "git notes [--ref <notes_ref>] remove [<object>]",
29 "git notes [--ref <notes_ref>] prune",
30 NULL
31};
32
33static const char * const git_notes_list_usage[] = {
e397421a 34 "git notes [list [<object>]]",
74884b52
SB
35 NULL
36};
37
38static const char * const git_notes_add_usage[] = {
39 "git notes add [<options>] [<object>]",
40 NULL
41};
42
43static const char * const git_notes_copy_usage[] = {
44 "git notes copy [<options>] <from-object> <to-object>",
45 "git notes copy --stdin [<from-object> <to-object>]...",
46 NULL
47};
48
49static const char * const git_notes_append_usage[] = {
50 "git notes append [<options>] [<object>]",
51 NULL
52};
53
54static const char * const git_notes_edit_usage[] = {
aaec9bcf 55 "git notes edit [<object>]",
74884b52
SB
56 NULL
57};
58
59static const char * const git_notes_show_usage[] = {
cd067d3b 60 "git notes show [<object>]",
74884b52
SB
61 NULL
62};
63
64static const char * const git_notes_remove_usage[] = {
92b3385f 65 "git notes remove [<object>]",
74884b52
SB
66 NULL
67};
68
69static const char * const git_notes_prune_usage[] = {
d6576e1f 70 "git notes prune",
cd067d3b
JH
71 NULL
72};
73
74static const char note_template[] =
75 "\n"
76 "#\n"
77 "# Write/edit the notes for the following object:\n"
78 "#\n";
79
348f199b
JH
80struct msg_arg {
81 int given;
0691cff7 82 int use_editor;
348f199b
JH
83 struct strbuf buf;
84};
85
e397421a
JH
86static int list_each_note(const unsigned char *object_sha1,
87 const unsigned char *note_sha1, char *note_path,
88 void *cb_data)
89{
90 printf("%s %s\n", sha1_to_hex(note_sha1), sha1_to_hex(object_sha1));
91 return 0;
92}
93
cd067d3b
JH
94static void write_note_data(int fd, const unsigned char *sha1)
95{
96 unsigned long size;
97 enum object_type type;
98 char *buf = read_sha1_file(sha1, &type, &size);
99 if (buf) {
100 if (size)
101 write_or_die(fd, buf, size);
102 free(buf);
103 }
104}
105
106static void write_commented_object(int fd, const unsigned char *object)
107{
108 const char *show_args[5] =
109 {"show", "--stat", "--no-notes", sha1_to_hex(object), NULL};
110 struct child_process show;
111 struct strbuf buf = STRBUF_INIT;
112 FILE *show_out;
113
114 /* Invoke "git show --stat --no-notes $object" */
115 memset(&show, 0, sizeof(show));
116 show.argv = show_args;
117 show.no_stdin = 1;
118 show.out = -1;
119 show.err = 0;
120 show.git_cmd = 1;
121 if (start_command(&show))
122 die("unable to start 'show' for object '%s'",
123 sha1_to_hex(object));
124
125 /* Open the output as FILE* so strbuf_getline() can be used. */
126 show_out = xfdopen(show.out, "r");
127 if (show_out == NULL)
128 die_errno("can't fdopen 'show' output fd");
129
130 /* Prepend "# " to each output line and write result to 'fd' */
131 while (strbuf_getline(&buf, show_out, '\n') != EOF) {
132 write_or_die(fd, "# ", 2);
133 write_or_die(fd, buf.buf, buf.len);
134 write_or_die(fd, "\n", 1);
135 }
136 strbuf_release(&buf);
137 if (fclose(show_out))
138 die_errno("failed to close pipe to 'show' for object '%s'",
139 sha1_to_hex(object));
140 if (finish_command(&show))
141 die("failed to finish 'show' for object '%s'",
142 sha1_to_hex(object));
143}
144
348f199b
JH
145static void create_note(const unsigned char *object, struct msg_arg *msg,
146 int append_only, const unsigned char *prev,
cd067d3b
JH
147 unsigned char *result)
148{
149 char *path = NULL;
150
0691cff7 151 if (msg->use_editor || !msg->given) {
cd067d3b
JH
152 int fd;
153
154 /* write the template message before editing: */
155 path = git_pathdup("NOTES_EDITMSG");
156 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
157 if (fd < 0)
158 die_errno("could not create file '%s'", path);
159
0691cff7
JH
160 if (msg->given)
161 write_or_die(fd, msg->buf.buf, msg->buf.len);
162 else if (prev && !append_only)
cd067d3b
JH
163 write_note_data(fd, prev);
164 write_or_die(fd, note_template, strlen(note_template));
165
166 write_commented_object(fd, object);
167
168 close(fd);
0691cff7 169 strbuf_reset(&(msg->buf));
cd067d3b 170
348f199b 171 if (launch_editor(path, &(msg->buf), NULL)) {
cd067d3b
JH
172 die("Please supply the note contents using either -m" \
173 " or -F option");
174 }
348f199b 175 stripspace(&(msg->buf), 1);
cd067d3b
JH
176 }
177
2347fae5
JH
178 if (prev && append_only) {
179 /* Append buf to previous note contents */
180 unsigned long size;
181 enum object_type type;
182 char *prev_buf = read_sha1_file(prev, &type, &size);
183
348f199b
JH
184 strbuf_grow(&(msg->buf), size + 1);
185 if (msg->buf.len && prev_buf && size)
186 strbuf_insert(&(msg->buf), 0, "\n", 1);
2347fae5 187 if (prev_buf && size)
348f199b 188 strbuf_insert(&(msg->buf), 0, prev_buf, size);
2347fae5
JH
189 free(prev_buf);
190 }
191
348f199b 192 if (!msg->buf.len) {
cd067d3b
JH
193 fprintf(stderr, "Removing note for object %s\n",
194 sha1_to_hex(object));
195 hashclr(result);
196 } else {
348f199b 197 if (write_sha1_file(msg->buf.buf, msg->buf.len, blob_type, result)) {
cd067d3b
JH
198 error("unable to write note object");
199 if (path)
200 error("The note contents has been left in %s",
201 path);
202 exit(128);
203 }
204 }
205
206 if (path) {
207 unlink_or_warn(path);
208 free(path);
209 }
210}
211
cd067d3b
JH
212static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
213{
214 struct msg_arg *msg = opt->value;
215
216 if (!arg)
217 return -1;
348f199b
JH
218
219 strbuf_grow(&(msg->buf), strlen(arg) + 2);
cd067d3b 220 if (msg->buf.len)
348f199b 221 strbuf_addstr(&(msg->buf), "\n");
cd067d3b 222 strbuf_addstr(&(msg->buf), arg);
348f199b
JH
223 stripspace(&(msg->buf), 0);
224
225 msg->given = 1;
226 return 0;
227}
228
229static int parse_file_arg(const struct option *opt, const char *arg, int unset)
230{
231 struct msg_arg *msg = opt->value;
232
233 if (!arg)
234 return -1;
235
236 if (msg->buf.len)
237 strbuf_addstr(&(msg->buf), "\n");
238 if (!strcmp(arg, "-")) {
239 if (strbuf_read(&(msg->buf), 0, 1024) < 0)
240 die_errno("cannot read '%s'", arg);
241 } else if (strbuf_read_file(&(msg->buf), arg, 1024) < 0)
242 die_errno("could not open or read '%s'", arg);
243 stripspace(&(msg->buf), 0);
244
cd067d3b
JH
245 msg->given = 1;
246 return 0;
247}
248
0691cff7
JH
249static int parse_reuse_arg(const struct option *opt, const char *arg, int unset)
250{
251 struct msg_arg *msg = opt->value;
252 char *buf;
253 unsigned char object[20];
254 enum object_type type;
255 unsigned long len;
256
257 if (!arg)
258 return -1;
259
260 if (msg->buf.len)
261 strbuf_addstr(&(msg->buf), "\n");
262
263 if (get_sha1(arg, object))
264 die("Failed to resolve '%s' as a valid ref.", arg);
265 if (!(buf = read_sha1_file(object, &type, &len)) || !len) {
266 free(buf);
267 die("Failed to read object '%s'.", arg);;
268 }
269 strbuf_add(&(msg->buf), buf, len);
270 free(buf);
271
272 msg->given = 1;
273 return 0;
274}
275
276static int parse_reedit_arg(const struct option *opt, const char *arg, int unset)
277{
278 struct msg_arg *msg = opt->value;
279 msg->use_editor = 1;
280 return parse_reuse_arg(opt, arg, unset);
281}
282
cd067d3b
JH
283int commit_notes(struct notes_tree *t, const char *msg)
284{
285 struct commit_list *parent;
286 unsigned char tree_sha1[20], prev_commit[20], new_commit[20];
287 struct strbuf buf = STRBUF_INIT;
288
289 if (!t)
290 t = &default_notes_tree;
291 if (!t->initialized || !t->ref || !*t->ref)
292 die("Cannot commit uninitialized/unreferenced notes tree");
7f710ea9
TR
293 if (!t->dirty)
294 return 0; /* don't have to commit an unchanged tree */
cd067d3b
JH
295
296 /* Prepare commit message and reflog message */
297 strbuf_addstr(&buf, "notes: "); /* commit message starts at index 7 */
298 strbuf_addstr(&buf, msg);
299 if (buf.buf[buf.len - 1] != '\n')
300 strbuf_addch(&buf, '\n'); /* Make sure msg ends with newline */
301
302 /* Convert notes tree to tree object */
303 if (write_notes_tree(t, tree_sha1))
304 die("Failed to write current notes tree to database");
305
306 /* Create new commit for the tree object */
307 if (!read_ref(t->ref, prev_commit)) { /* retrieve parent commit */
308 parent = xmalloc(sizeof(*parent));
309 parent->item = lookup_commit(prev_commit);
310 parent->next = NULL;
311 } else {
312 hashclr(prev_commit);
313 parent = NULL;
314 }
315 if (commit_tree(buf.buf + 7, tree_sha1, parent, new_commit, NULL))
316 die("Failed to commit notes tree to database");
317
318 /* Update notes ref with new commit */
319 update_ref(buf.buf, t->ref, new_commit, prev_commit, 0, DIE_ON_ERR);
320
321 strbuf_release(&buf);
322 return 0;
323}
324
6956f858
TR
325combine_notes_fn *parse_combine_notes_fn(const char *v)
326{
327 if (!strcasecmp(v, "overwrite"))
328 return combine_notes_overwrite;
329 else if (!strcasecmp(v, "ignore"))
330 return combine_notes_ignore;
331 else if (!strcasecmp(v, "concatenate"))
332 return combine_notes_concatenate;
333 else
334 return NULL;
335}
336
337static int notes_rewrite_config(const char *k, const char *v, void *cb)
338{
339 struct notes_rewrite_cfg *c = cb;
340 if (!prefixcmp(k, "notes.rewrite.") && !strcmp(k+14, c->cmd)) {
341 c->enabled = git_config_bool(k, v);
342 return 0;
343 } else if (!c->mode_from_env && !strcmp(k, "notes.rewritemode")) {
344 if (!v)
345 config_error_nonbool(k);
346 c->combine = parse_combine_notes_fn(v);
347 if (!c->combine) {
348 error("Bad notes.rewriteMode value: '%s'", v);
349 return 1;
350 }
351 return 0;
352 } else if (!c->refs_from_env && !strcmp(k, "notes.rewriteref")) {
353 /* note that a refs/ prefix is implied in the
354 * underlying for_each_glob_ref */
355 if (!prefixcmp(v, "refs/notes/"))
356 string_list_add_refs_by_glob(c->refs, v);
357 else
358 warning("Refusing to rewrite notes in %s"
359 " (outside of refs/notes/)", v);
360 return 0;
361 }
362
363 return 0;
364}
365
366
367struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd)
368{
369 struct notes_rewrite_cfg *c = xmalloc(sizeof(struct notes_rewrite_cfg));
370 const char *rewrite_mode_env = getenv(GIT_NOTES_REWRITE_MODE_ENVIRONMENT);
371 const char *rewrite_refs_env = getenv(GIT_NOTES_REWRITE_REF_ENVIRONMENT);
372 c->cmd = cmd;
373 c->enabled = 1;
374 c->combine = combine_notes_concatenate;
375 c->refs = xcalloc(1, sizeof(struct string_list));
376 c->refs->strdup_strings = 1;
377 c->refs_from_env = 0;
378 c->mode_from_env = 0;
379 if (rewrite_mode_env) {
380 c->mode_from_env = 1;
381 c->combine = parse_combine_notes_fn(rewrite_mode_env);
382 if (!c->combine)
383 error("Bad " GIT_NOTES_REWRITE_MODE_ENVIRONMENT
384 " value: '%s'", rewrite_mode_env);
385 }
386 if (rewrite_refs_env) {
387 c->refs_from_env = 1;
388 string_list_add_refs_from_colon_sep(c->refs, rewrite_refs_env);
389 }
390 git_config(notes_rewrite_config, c);
391 if (!c->enabled || !c->refs->nr) {
392 string_list_clear(c->refs, 0);
393 free(c->refs);
394 free(c);
395 return NULL;
396 }
397 c->trees = load_notes_trees(c->refs);
398 string_list_clear(c->refs, 0);
399 free(c->refs);
400 return c;
401}
402
403int copy_note_for_rewrite(struct notes_rewrite_cfg *c,
404 const unsigned char *from_obj, const unsigned char *to_obj)
405{
406 int ret = 0;
407 int i;
408 for (i = 0; c->trees[i]; i++)
409 ret = copy_note(c->trees[i], from_obj, to_obj, 1, c->combine) || ret;
410 return ret;
411}
412
413void finish_copy_notes_for_rewrite(struct notes_rewrite_cfg *c)
414{
415 int i;
416 for (i = 0; c->trees[i]; i++) {
417 commit_notes(c->trees[i], "Notes added by 'git notes copy'");
418 free_notes(c->trees[i]);
419 }
420 free(c->trees);
421 free(c);
422}
423
424int notes_copy_from_stdin(int force, const char *rewrite_cmd)
160baa0d
TR
425{
426 struct strbuf buf = STRBUF_INIT;
6956f858 427 struct notes_rewrite_cfg *c = NULL;
160baa0d
TR
428 struct notes_tree *t;
429 int ret = 0;
430
6956f858
TR
431 if (rewrite_cmd) {
432 c = init_copy_notes_for_rewrite(rewrite_cmd);
433 if (!c)
434 return 0;
435 } else {
436 init_notes(NULL, NULL, NULL, 0);
437 t = &default_notes_tree;
438 }
160baa0d
TR
439
440 while (strbuf_getline(&buf, stdin, '\n') != EOF) {
441 unsigned char from_obj[20], to_obj[20];
442 struct strbuf **split;
443 int err;
444
445 split = strbuf_split(&buf, ' ');
446 if (!split[0] || !split[1])
447 die("Malformed input line: '%s'.", buf.buf);
448 strbuf_rtrim(split[0]);
449 strbuf_rtrim(split[1]);
450 if (get_sha1(split[0]->buf, from_obj))
451 die("Failed to resolve '%s' as a valid ref.", split[0]->buf);
452 if (get_sha1(split[1]->buf, to_obj))
453 die("Failed to resolve '%s' as a valid ref.", split[1]->buf);
454
6956f858
TR
455 if (rewrite_cmd)
456 err = copy_note_for_rewrite(c, from_obj, to_obj);
457 else
458 err = copy_note(t, from_obj, to_obj, force,
459 combine_notes_overwrite);
160baa0d
TR
460
461 if (err) {
462 error("Failed to copy notes from '%s' to '%s'",
463 split[0]->buf, split[1]->buf);
464 ret = 1;
465 }
466
467 strbuf_list_free(split);
468 }
469
6956f858
TR
470 if (!rewrite_cmd) {
471 commit_notes(t, "Notes added by 'git notes copy'");
472 free_notes(t);
473 } else {
474 finish_copy_notes_for_rewrite(c);
475 }
160baa0d
TR
476 return ret;
477}
478
74884b52 479static struct notes_tree *init_notes_check(const char *subcommand)
cd067d3b 480{
cd067d3b 481 struct notes_tree *t;
74884b52
SB
482 init_notes(NULL, NULL, NULL, 0);
483 t = &default_notes_tree;
a0b4dfa9 484
74884b52
SB
485 if (prefixcmp(t->ref, "refs/notes/"))
486 die("Refusing to %s notes in %s (outside of refs/notes/)",
487 subcommand, t->ref);
488 return t;
489}
490
491static int list(int argc, const char **argv, const char *prefix)
492{
493 struct notes_tree *t;
494 unsigned char object[20];
495 const unsigned char *note;
496 int retval = -1;
cd067d3b 497 struct option options[] = {
cd067d3b
JH
498 OPT_END()
499 };
500
74884b52
SB
501 if (argc)
502 argc = parse_options(argc, argv, prefix, options,
503 git_notes_list_usage, 0);
cd067d3b 504
74884b52
SB
505 if (1 < argc) {
506 error("too many parameters");
507 usage_with_options(git_notes_list_usage, options);
dcf783a2
TR
508 }
509
74884b52
SB
510 t = init_notes_check("list");
511 if (argc) {
512 if (get_sha1(argv[0], object))
513 die("Failed to resolve '%s' as a valid ref.", argv[0]);
514 note = get_note(t, object);
515 if (note) {
516 puts(sha1_to_hex(note));
517 retval = 0;
518 } else
519 retval = error("No note found for object %s.",
520 sha1_to_hex(object));
521 } else
522 retval = for_each_note(t, 0, list_each_note, NULL);
cd067d3b 523
74884b52
SB
524 free_notes(t);
525 return retval;
526}
cd067d3b 527
74884b52
SB
528static int add(int argc, const char **argv, const char *prefix)
529{
530 int retval = 0, force = 0;
531 const char *object_ref;
532 struct notes_tree *t;
533 unsigned char object[20], new_note[20];
534 char logmsg[100];
535 const unsigned char *note;
536 struct msg_arg msg = { 0, 0, STRBUF_INIT };
537 struct option options[] = {
538 { OPTION_CALLBACK, 'm', "message", &msg, "MSG",
539 "note contents as a string", PARSE_OPT_NONEG,
540 parse_msg_arg},
541 { OPTION_CALLBACK, 'F', "file", &msg, "FILE",
542 "note contents in a file", PARSE_OPT_NONEG,
543 parse_file_arg},
544 { OPTION_CALLBACK, 'c', "reedit-message", &msg, "OBJECT",
545 "reuse and edit specified note object", PARSE_OPT_NONEG,
546 parse_reedit_arg},
547 { OPTION_CALLBACK, 'C', "reuse-message", &msg, "OBJECT",
548 "reuse specified note object", PARSE_OPT_NONEG,
549 parse_reuse_arg},
550 OPT_BOOLEAN('f', "force", &force, "replace existing notes"),
551 OPT_END()
552 };
92b3385f 553
74884b52
SB
554 argc = parse_options(argc, argv, prefix, options, git_notes_add_usage,
555 0);
aaec9bcf 556
74884b52
SB
557 if (1 < argc) {
558 error("too many parameters");
559 usage_with_options(git_notes_add_usage, options);
7aa4754e
JH
560 }
561
74884b52 562 object_ref = argc ? argv[0] : "HEAD";
160baa0d 563
74884b52
SB
564 if (get_sha1(object_ref, object))
565 die("Failed to resolve '%s' as a valid ref.", object_ref);
e73bbd96 566
74884b52
SB
567 t = init_notes_check("add");
568 note = get_note(t, object);
5848769f 569
74884b52
SB
570 if (note) {
571 if (!force) {
572 retval = error("Cannot add notes. Found existing notes "
573 "for object %s. Use '-f' to overwrite "
574 "existing notes", sha1_to_hex(object));
575 goto out;
576 }
577 fprintf(stderr, "Overwriting existing notes for object %s\n",
578 sha1_to_hex(object));
cd067d3b
JH
579 }
580
74884b52 581 create_note(object, &msg, 0, note, new_note);
cd067d3b 582
74884b52
SB
583 if (is_null_sha1(new_note))
584 remove_note(t, object);
585 else
586 add_note(t, object, new_note, combine_notes_overwrite);
cd067d3b 587
74884b52
SB
588 snprintf(logmsg, sizeof(logmsg), "Notes %s by 'git notes %s'",
589 is_null_sha1(new_note) ? "removed" : "added", "add");
590 commit_notes(t, logmsg);
591out:
592 free_notes(t);
593 strbuf_release(&(msg.buf));
594 return retval;
595}
cd067d3b 596
74884b52
SB
597static int copy(int argc, const char **argv, const char *prefix)
598{
599 int retval = 0, force = 0, from_stdin = 0;
600 const unsigned char *from_note, *note;
601 const char *object_ref;
602 unsigned char object[20], from_obj[20];
603 struct notes_tree *t;
604 const char *rewrite_cmd = NULL;
605 struct option options[] = {
606 OPT_BOOLEAN('f', "force", &force, "replace existing notes"),
607 OPT_BOOLEAN(0, "stdin", &from_stdin, "read objects from stdin"),
608 OPT_STRING(0, "for-rewrite", &rewrite_cmd, "command",
609 "load rewriting config for <command> (implies "
610 "--stdin)"),
611 OPT_END()
612 };
cd067d3b 613
74884b52
SB
614 argc = parse_options(argc, argv, prefix, options, git_notes_copy_usage,
615 0);
e397421a 616
74884b52
SB
617 if (from_stdin || rewrite_cmd) {
618 if (argc) {
619 error("too many parameters");
620 usage_with_options(git_notes_copy_usage, options);
5848769f 621 } else {
74884b52 622 return notes_copy_from_stdin(force, rewrite_cmd);
5848769f 623 }
e397421a
JH
624 }
625
74884b52
SB
626 if (2 < argc) {
627 error("too many parameters");
628 usage_with_options(git_notes_copy_usage, options);
cd067d3b
JH
629 }
630
74884b52
SB
631 if (get_sha1(argv[0], from_obj))
632 die("Failed to resolve '%s' as a valid ref.", argv[0]);
633
634 object_ref = 1 < argc ? argv[1] : "HEAD";
635
636 if (get_sha1(object_ref, object))
637 die("Failed to resolve '%s' as a valid ref.", object_ref);
7aa4754e 638
74884b52
SB
639 t = init_notes_check("copy");
640 note = get_note(t, object);
641
642 if (note) {
5848769f 643 if (!force) {
74884b52
SB
644 retval = error("Cannot copy notes. Found existing "
645 "notes for object %s. Use '-f' to "
646 "overwrite existing notes",
647 sha1_to_hex(object));
648 goto out;
7aa4754e 649 }
5848769f
JH
650 fprintf(stderr, "Overwriting existing notes for object %s\n",
651 sha1_to_hex(object));
7aa4754e 652 }
92b3385f 653
74884b52
SB
654 from_note = get_note(t, from_obj);
655 if (!from_note) {
656 retval = error("Missing notes on source object %s. Cannot "
657 "copy.", sha1_to_hex(from_obj));
658 goto out;
cd067d3b
JH
659 }
660
74884b52
SB
661 add_note(t, object, from_note, combine_notes_overwrite);
662 commit_notes(t, "Notes added by 'git notes copy'");
663out:
664 free_notes(t);
665 return retval;
666}
667
668static int append_edit(int argc, const char **argv, const char *prefix)
669{
670 const char *object_ref;
671 struct notes_tree *t;
672 unsigned char object[20], new_note[20];
673 const unsigned char *note;
674 char logmsg[100];
675 const char * const *usage;
676 struct msg_arg msg = { 0, 0, STRBUF_INIT };
677 struct option options[] = {
678 { OPTION_CALLBACK, 'm', "message", &msg, "MSG",
679 "note contents as a string", PARSE_OPT_NONEG,
680 parse_msg_arg},
681 { OPTION_CALLBACK, 'F', "file", &msg, "FILE",
682 "note contents in a file", PARSE_OPT_NONEG,
683 parse_file_arg},
684 { OPTION_CALLBACK, 'c', "reedit-message", &msg, "OBJECT",
685 "reuse and edit specified note object", PARSE_OPT_NONEG,
686 parse_reedit_arg},
687 { OPTION_CALLBACK, 'C', "reuse-message", &msg, "OBJECT",
688 "reuse specified note object", PARSE_OPT_NONEG,
689 parse_reuse_arg},
690 OPT_END()
691 };
692 int edit = !strcmp(argv[0], "edit");
693
694 usage = edit ? git_notes_edit_usage : git_notes_append_usage;
695 argc = parse_options(argc, argv, prefix, options, usage,
696 PARSE_OPT_KEEP_ARGV0);
697
698 if (2 < argc) {
699 error("too many parameters");
700 usage_with_options(usage, options);
701 }
702
703 if (msg.given && edit)
704 fprintf(stderr, "The -m/-F/-c/-C options have been deprecated "
705 "for the 'edit' subcommand.\n"
706 "Please use 'git notes add -f -m/-F/-c/-C' instead.\n");
707
708 object_ref = 1 < argc ? argv[1] : "HEAD";
709
710 if (get_sha1(object_ref, object))
711 die("Failed to resolve '%s' as a valid ref.", object_ref);
712
713 t = init_notes_check(argv[0]);
714 note = get_note(t, object);
715
716 create_note(object, &msg, !edit, note, new_note);
5848769f
JH
717
718 if (is_null_sha1(new_note))
719 remove_note(t, object);
720 else
721 add_note(t, object, new_note, combine_notes_overwrite);
722
5848769f 723 snprintf(logmsg, sizeof(logmsg), "Notes %s by 'git notes %s'",
92b3385f 724 is_null_sha1(new_note) ? "removed" : "added", argv[0]);
a0b4dfa9 725 commit_notes(t, logmsg);
cd067d3b 726 free_notes(t);
348f199b 727 strbuf_release(&(msg.buf));
74884b52
SB
728 return 0;
729}
730
731static int show(int argc, const char **argv, const char *prefix)
732{
733 const char *object_ref;
734 struct notes_tree *t;
735 unsigned char object[20];
736 const unsigned char *note;
737 int retval;
738 struct option options[] = {
739 OPT_END()
740 };
741
742 argc = parse_options(argc, argv, prefix, options, git_notes_show_usage,
743 0);
744
745 if (1 < argc) {
746 error("too many parameters");
747 usage_with_options(git_notes_show_usage, options);
748 }
749
750 object_ref = argc ? argv[0] : "HEAD";
751
752 if (get_sha1(object_ref, object))
753 die("Failed to resolve '%s' as a valid ref.", object_ref);
754
755 t = init_notes_check("show");
756 note = get_note(t, object);
757
758 if (!note)
759 retval = error("No note found for object %s.",
760 sha1_to_hex(object));
761 else {
762 const char *show_args[3] = {"show", sha1_to_hex(note), NULL};
763 retval = execv_git_cmd(show_args);
764 }
765 free_notes(t);
5848769f 766 return retval;
cd067d3b 767}
74884b52
SB
768
769static int remove_cmd(int argc, const char **argv, const char *prefix)
770{
771 struct option options[] = {
772 OPT_END()
773 };
774 const char *object_ref;
775 struct notes_tree *t;
776 unsigned char object[20];
777
778 argc = parse_options(argc, argv, prefix, options,
779 git_notes_remove_usage, 0);
780
781 if (1 < argc) {
782 error("too many parameters");
783 usage_with_options(git_notes_remove_usage, options);
784 }
785
786 object_ref = argc ? argv[0] : "HEAD";
787
788 if (get_sha1(object_ref, object))
789 die("Failed to resolve '%s' as a valid ref.", object_ref);
790
791 t = init_notes_check("remove");
792
793 fprintf(stderr, "Removing note for object %s\n", sha1_to_hex(object));
794 remove_note(t, object);
795
796 commit_notes(t, "Notes removed by 'git notes remove'");
797 free_notes(t);
798 return 0;
799}
800
801static int prune(int argc, const char **argv, const char *prefix)
802{
803 struct notes_tree *t;
804 struct option options[] = {
805 OPT_END()
806 };
807
808 argc = parse_options(argc, argv, prefix, options, git_notes_prune_usage,
809 0);
810
811 if (argc) {
812 error("too many parameters");
813 usage_with_options(git_notes_prune_usage, options);
814 }
815
816 t = init_notes_check("prune");
817
818 prune_notes(t);
819 commit_notes(t, "Notes removed by 'git notes prune'");
820 free_notes(t);
821 return 0;
822}
823
824int cmd_notes(int argc, const char **argv, const char *prefix)
825{
826 int result;
827 const char *override_notes_ref = NULL;
828 struct option options[] = {
829 OPT_STRING(0, "ref", &override_notes_ref, "notes_ref",
830 "use notes from <notes_ref>"),
831 OPT_END()
832 };
833
834 git_config(git_default_config, NULL);
835 argc = parse_options(argc, argv, prefix, options, git_notes_usage,
836 PARSE_OPT_STOP_AT_NON_OPTION);
837
838 if (override_notes_ref) {
839 struct strbuf sb = STRBUF_INIT;
840 if (!prefixcmp(override_notes_ref, "refs/notes/"))
841 /* we're happy */;
842 else if (!prefixcmp(override_notes_ref, "notes/"))
843 strbuf_addstr(&sb, "refs/");
844 else
845 strbuf_addstr(&sb, "refs/notes/");
846 strbuf_addstr(&sb, override_notes_ref);
847 setenv("GIT_NOTES_REF", sb.buf, 1);
848 strbuf_release(&sb);
849 }
850
851 if (argc < 1 || !strcmp(argv[0], "list"))
852 result = list(argc, argv, prefix);
853 else if (!strcmp(argv[0], "add"))
854 result = add(argc, argv, prefix);
855 else if (!strcmp(argv[0], "copy"))
856 result = copy(argc, argv, prefix);
857 else if (!strcmp(argv[0], "append") || !strcmp(argv[0], "edit"))
858 result = append_edit(argc, argv, prefix);
859 else if (!strcmp(argv[0], "show"))
860 result = show(argc, argv, prefix);
861 else if (!strcmp(argv[0], "remove"))
862 result = remove_cmd(argc, argv, prefix);
863 else if (!strcmp(argv[0], "prune"))
864 result = prune(argc, argv, prefix);
865 else {
866 result = error("Unknown subcommand: %s", argv[0]);
867 usage_with_options(git_notes_usage, options);
868 }
869
870 return result ? 1 : 0;
871}