]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/notes.c
gitweb: Don't die_error in git_tag after already printing headers
[thirdparty/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>]",
a9f2adff 29 "git notes [--ref <notes_ref>] prune [-n | -v]",
74884b52
SB
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[] = {
a9f2adff 70 "git notes prune [<options>]",
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
348f199b 216 strbuf_grow(&(msg->buf), strlen(arg) + 2);
cd067d3b 217 if (msg->buf.len)
43a61b84 218 strbuf_addch(&(msg->buf), '\n');
cd067d3b 219 strbuf_addstr(&(msg->buf), arg);
348f199b
JH
220 stripspace(&(msg->buf), 0);
221
222 msg->given = 1;
223 return 0;
224}
225
226static int parse_file_arg(const struct option *opt, const char *arg, int unset)
227{
228 struct msg_arg *msg = opt->value;
229
348f199b 230 if (msg->buf.len)
43a61b84 231 strbuf_addch(&(msg->buf), '\n');
348f199b
JH
232 if (!strcmp(arg, "-")) {
233 if (strbuf_read(&(msg->buf), 0, 1024) < 0)
234 die_errno("cannot read '%s'", arg);
235 } else if (strbuf_read_file(&(msg->buf), arg, 1024) < 0)
236 die_errno("could not open or read '%s'", arg);
237 stripspace(&(msg->buf), 0);
238
cd067d3b
JH
239 msg->given = 1;
240 return 0;
241}
242
0691cff7
JH
243static int parse_reuse_arg(const struct option *opt, const char *arg, int unset)
244{
245 struct msg_arg *msg = opt->value;
246 char *buf;
247 unsigned char object[20];
248 enum object_type type;
249 unsigned long len;
250
0691cff7 251 if (msg->buf.len)
43a61b84 252 strbuf_addch(&(msg->buf), '\n');
0691cff7
JH
253
254 if (get_sha1(arg, object))
255 die("Failed to resolve '%s' as a valid ref.", arg);
256 if (!(buf = read_sha1_file(object, &type, &len)) || !len) {
257 free(buf);
258 die("Failed to read object '%s'.", arg);;
259 }
260 strbuf_add(&(msg->buf), buf, len);
261 free(buf);
262
263 msg->given = 1;
264 return 0;
265}
266
267static int parse_reedit_arg(const struct option *opt, const char *arg, int unset)
268{
269 struct msg_arg *msg = opt->value;
270 msg->use_editor = 1;
271 return parse_reuse_arg(opt, arg, unset);
272}
273
cd067d3b
JH
274int commit_notes(struct notes_tree *t, const char *msg)
275{
276 struct commit_list *parent;
277 unsigned char tree_sha1[20], prev_commit[20], new_commit[20];
278 struct strbuf buf = STRBUF_INIT;
279
280 if (!t)
281 t = &default_notes_tree;
282 if (!t->initialized || !t->ref || !*t->ref)
283 die("Cannot commit uninitialized/unreferenced notes tree");
7f710ea9
TR
284 if (!t->dirty)
285 return 0; /* don't have to commit an unchanged tree */
cd067d3b
JH
286
287 /* Prepare commit message and reflog message */
288 strbuf_addstr(&buf, "notes: "); /* commit message starts at index 7 */
289 strbuf_addstr(&buf, msg);
290 if (buf.buf[buf.len - 1] != '\n')
291 strbuf_addch(&buf, '\n'); /* Make sure msg ends with newline */
292
293 /* Convert notes tree to tree object */
294 if (write_notes_tree(t, tree_sha1))
295 die("Failed to write current notes tree to database");
296
297 /* Create new commit for the tree object */
298 if (!read_ref(t->ref, prev_commit)) { /* retrieve parent commit */
299 parent = xmalloc(sizeof(*parent));
300 parent->item = lookup_commit(prev_commit);
301 parent->next = NULL;
302 } else {
303 hashclr(prev_commit);
304 parent = NULL;
305 }
306 if (commit_tree(buf.buf + 7, tree_sha1, parent, new_commit, NULL))
307 die("Failed to commit notes tree to database");
308
309 /* Update notes ref with new commit */
310 update_ref(buf.buf, t->ref, new_commit, prev_commit, 0, DIE_ON_ERR);
311
312 strbuf_release(&buf);
313 return 0;
314}
315
4e0d7a80 316combine_notes_fn parse_combine_notes_fn(const char *v)
6956f858
TR
317{
318 if (!strcasecmp(v, "overwrite"))
319 return combine_notes_overwrite;
320 else if (!strcasecmp(v, "ignore"))
321 return combine_notes_ignore;
322 else if (!strcasecmp(v, "concatenate"))
323 return combine_notes_concatenate;
324 else
325 return NULL;
326}
327
328static int notes_rewrite_config(const char *k, const char *v, void *cb)
329{
330 struct notes_rewrite_cfg *c = cb;
331 if (!prefixcmp(k, "notes.rewrite.") && !strcmp(k+14, c->cmd)) {
332 c->enabled = git_config_bool(k, v);
333 return 0;
334 } else if (!c->mode_from_env && !strcmp(k, "notes.rewritemode")) {
335 if (!v)
336 config_error_nonbool(k);
337 c->combine = parse_combine_notes_fn(v);
338 if (!c->combine) {
339 error("Bad notes.rewriteMode value: '%s'", v);
340 return 1;
341 }
342 return 0;
343 } else if (!c->refs_from_env && !strcmp(k, "notes.rewriteref")) {
344 /* note that a refs/ prefix is implied in the
345 * underlying for_each_glob_ref */
346 if (!prefixcmp(v, "refs/notes/"))
347 string_list_add_refs_by_glob(c->refs, v);
348 else
349 warning("Refusing to rewrite notes in %s"
350 " (outside of refs/notes/)", v);
351 return 0;
352 }
353
354 return 0;
355}
356
357
358struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd)
359{
360 struct notes_rewrite_cfg *c = xmalloc(sizeof(struct notes_rewrite_cfg));
361 const char *rewrite_mode_env = getenv(GIT_NOTES_REWRITE_MODE_ENVIRONMENT);
362 const char *rewrite_refs_env = getenv(GIT_NOTES_REWRITE_REF_ENVIRONMENT);
363 c->cmd = cmd;
364 c->enabled = 1;
365 c->combine = combine_notes_concatenate;
366 c->refs = xcalloc(1, sizeof(struct string_list));
367 c->refs->strdup_strings = 1;
368 c->refs_from_env = 0;
369 c->mode_from_env = 0;
370 if (rewrite_mode_env) {
371 c->mode_from_env = 1;
372 c->combine = parse_combine_notes_fn(rewrite_mode_env);
373 if (!c->combine)
374 error("Bad " GIT_NOTES_REWRITE_MODE_ENVIRONMENT
375 " value: '%s'", rewrite_mode_env);
376 }
377 if (rewrite_refs_env) {
378 c->refs_from_env = 1;
379 string_list_add_refs_from_colon_sep(c->refs, rewrite_refs_env);
380 }
381 git_config(notes_rewrite_config, c);
382 if (!c->enabled || !c->refs->nr) {
383 string_list_clear(c->refs, 0);
384 free(c->refs);
385 free(c);
386 return NULL;
387 }
388 c->trees = load_notes_trees(c->refs);
389 string_list_clear(c->refs, 0);
390 free(c->refs);
391 return c;
392}
393
394int copy_note_for_rewrite(struct notes_rewrite_cfg *c,
395 const unsigned char *from_obj, const unsigned char *to_obj)
396{
397 int ret = 0;
398 int i;
399 for (i = 0; c->trees[i]; i++)
400 ret = copy_note(c->trees[i], from_obj, to_obj, 1, c->combine) || ret;
401 return ret;
402}
403
404void finish_copy_notes_for_rewrite(struct notes_rewrite_cfg *c)
405{
406 int i;
407 for (i = 0; c->trees[i]; i++) {
408 commit_notes(c->trees[i], "Notes added by 'git notes copy'");
409 free_notes(c->trees[i]);
410 }
411 free(c->trees);
412 free(c);
413}
414
415int notes_copy_from_stdin(int force, const char *rewrite_cmd)
160baa0d
TR
416{
417 struct strbuf buf = STRBUF_INIT;
6956f858 418 struct notes_rewrite_cfg *c = NULL;
ef7a8e3b 419 struct notes_tree *t = NULL;
160baa0d
TR
420 int ret = 0;
421
6956f858
TR
422 if (rewrite_cmd) {
423 c = init_copy_notes_for_rewrite(rewrite_cmd);
424 if (!c)
425 return 0;
426 } else {
427 init_notes(NULL, NULL, NULL, 0);
428 t = &default_notes_tree;
429 }
160baa0d
TR
430
431 while (strbuf_getline(&buf, stdin, '\n') != EOF) {
432 unsigned char from_obj[20], to_obj[20];
433 struct strbuf **split;
434 int err;
435
436 split = strbuf_split(&buf, ' ');
437 if (!split[0] || !split[1])
438 die("Malformed input line: '%s'.", buf.buf);
439 strbuf_rtrim(split[0]);
440 strbuf_rtrim(split[1]);
441 if (get_sha1(split[0]->buf, from_obj))
442 die("Failed to resolve '%s' as a valid ref.", split[0]->buf);
443 if (get_sha1(split[1]->buf, to_obj))
444 die("Failed to resolve '%s' as a valid ref.", split[1]->buf);
445
6956f858
TR
446 if (rewrite_cmd)
447 err = copy_note_for_rewrite(c, from_obj, to_obj);
448 else
449 err = copy_note(t, from_obj, to_obj, force,
450 combine_notes_overwrite);
160baa0d
TR
451
452 if (err) {
453 error("Failed to copy notes from '%s' to '%s'",
454 split[0]->buf, split[1]->buf);
455 ret = 1;
456 }
457
458 strbuf_list_free(split);
459 }
460
6956f858
TR
461 if (!rewrite_cmd) {
462 commit_notes(t, "Notes added by 'git notes copy'");
463 free_notes(t);
464 } else {
465 finish_copy_notes_for_rewrite(c);
466 }
160baa0d
TR
467 return ret;
468}
469
74884b52 470static struct notes_tree *init_notes_check(const char *subcommand)
cd067d3b 471{
cd067d3b 472 struct notes_tree *t;
74884b52
SB
473 init_notes(NULL, NULL, NULL, 0);
474 t = &default_notes_tree;
a0b4dfa9 475
74884b52
SB
476 if (prefixcmp(t->ref, "refs/notes/"))
477 die("Refusing to %s notes in %s (outside of refs/notes/)",
478 subcommand, t->ref);
479 return t;
480}
481
482static int list(int argc, const char **argv, const char *prefix)
cd067d3b 483{
cd067d3b 484 struct notes_tree *t;
74884b52 485 unsigned char object[20];
cd067d3b 486 const unsigned char *note;
74884b52 487 int retval = -1;
cd067d3b 488 struct option options[] = {
cd067d3b
JH
489 OPT_END()
490 };
491
74884b52
SB
492 if (argc)
493 argc = parse_options(argc, argv, prefix, options,
494 git_notes_list_usage, 0);
cd067d3b 495
74884b52
SB
496 if (1 < argc) {
497 error("too many parameters");
498 usage_with_options(git_notes_list_usage, options);
dcf783a2
TR
499 }
500
74884b52
SB
501 t = init_notes_check("list");
502 if (argc) {
503 if (get_sha1(argv[0], object))
504 die("Failed to resolve '%s' as a valid ref.", argv[0]);
505 note = get_note(t, object);
506 if (note) {
507 puts(sha1_to_hex(note));
508 retval = 0;
509 } else
510 retval = error("No note found for object %s.",
511 sha1_to_hex(object));
512 } else
513 retval = for_each_note(t, 0, list_each_note, NULL);
cd067d3b 514
74884b52
SB
515 free_notes(t);
516 return retval;
517}
cd067d3b 518
74884b52
SB
519static int add(int argc, const char **argv, const char *prefix)
520{
521 int retval = 0, force = 0;
92b3385f 522 const char *object_ref;
74884b52
SB
523 struct notes_tree *t;
524 unsigned char object[20], new_note[20];
92b3385f 525 char logmsg[100];
74884b52 526 const unsigned char *note;
0691cff7 527 struct msg_arg msg = { 0, 0, STRBUF_INIT };
cd067d3b 528 struct option options[] = {
43a61b84
JH
529 { OPTION_CALLBACK, 'm', "message", &msg, "MSG",
530 "note contents as a string", PARSE_OPT_NONEG,
531 parse_msg_arg},
532 { OPTION_CALLBACK, 'F', "file", &msg, "FILE",
533 "note contents in a file", PARSE_OPT_NONEG,
534 parse_file_arg},
535 { OPTION_CALLBACK, 'c', "reedit-message", &msg, "OBJECT",
536 "reuse and edit specified note object", PARSE_OPT_NONEG,
537 parse_reedit_arg},
538 { OPTION_CALLBACK, 'C', "reuse-message", &msg, "OBJECT",
539 "reuse specified note object", PARSE_OPT_NONEG,
540 parse_reuse_arg},
7aa4754e 541 OPT_BOOLEAN('f', "force", &force, "replace existing notes"),
cd067d3b
JH
542 OPT_END()
543 };
544
74884b52
SB
545 argc = parse_options(argc, argv, prefix, options, git_notes_add_usage,
546 0);
cd067d3b 547
74884b52
SB
548 if (1 < argc) {
549 error("too many parameters");
550 usage_with_options(git_notes_add_usage, options);
dcf783a2
TR
551 }
552
74884b52 553 object_ref = argc ? argv[0] : "HEAD";
cd067d3b 554
74884b52
SB
555 if (get_sha1(object_ref, object))
556 die("Failed to resolve '%s' as a valid ref.", object_ref);
cd067d3b 557
74884b52
SB
558 t = init_notes_check("add");
559 note = get_note(t, object);
92b3385f 560
74884b52
SB
561 if (note) {
562 if (!force) {
563 retval = error("Cannot add notes. Found existing notes "
564 "for object %s. Use '-f' to overwrite "
565 "existing notes", sha1_to_hex(object));
566 goto out;
567 }
568 fprintf(stderr, "Overwriting existing notes for object %s\n",
569 sha1_to_hex(object));
aaec9bcf
JH
570 }
571
74884b52 572 create_note(object, &msg, 0, note, new_note);
7aa4754e 573
74884b52
SB
574 if (is_null_sha1(new_note))
575 remove_note(t, object);
576 else
577 add_note(t, object, new_note, combine_notes_overwrite);
160baa0d 578
74884b52
SB
579 snprintf(logmsg, sizeof(logmsg), "Notes %s by 'git notes %s'",
580 is_null_sha1(new_note) ? "removed" : "added", "add");
581 commit_notes(t, logmsg);
582out:
583 free_notes(t);
584 strbuf_release(&(msg.buf));
585 return retval;
586}
cd067d3b 587
74884b52
SB
588static int copy(int argc, const char **argv, const char *prefix)
589{
590 int retval = 0, force = 0, from_stdin = 0;
591 const unsigned char *from_note, *note;
592 const char *object_ref;
593 unsigned char object[20], from_obj[20];
594 struct notes_tree *t;
595 const char *rewrite_cmd = NULL;
596 struct option options[] = {
597 OPT_BOOLEAN('f', "force", &force, "replace existing notes"),
598 OPT_BOOLEAN(0, "stdin", &from_stdin, "read objects from stdin"),
599 OPT_STRING(0, "for-rewrite", &rewrite_cmd, "command",
600 "load rewriting config for <command> (implies "
601 "--stdin)"),
602 OPT_END()
603 };
cd067d3b 604
74884b52
SB
605 argc = parse_options(argc, argv, prefix, options, git_notes_copy_usage,
606 0);
e397421a 607
74884b52
SB
608 if (from_stdin || rewrite_cmd) {
609 if (argc) {
610 error("too many parameters");
611 usage_with_options(git_notes_copy_usage, options);
5848769f 612 } else {
74884b52 613 return notes_copy_from_stdin(force, rewrite_cmd);
e73bbd96 614 }
e73bbd96
JH
615 }
616
bbb1b8a3
JK
617 if (argc < 2) {
618 error("too few parameters");
619 usage_with_options(git_notes_copy_usage, options);
620 }
74884b52 621 if (2 < argc) {
cd067d3b 622 error("too many parameters");
74884b52 623 usage_with_options(git_notes_copy_usage, options);
cd067d3b
JH
624 }
625
74884b52
SB
626 if (get_sha1(argv[0], from_obj))
627 die("Failed to resolve '%s' as a valid ref.", argv[0]);
cd067d3b 628
74884b52 629 object_ref = 1 < argc ? argv[1] : "HEAD";
cd067d3b 630
74884b52
SB
631 if (get_sha1(object_ref, object))
632 die("Failed to resolve '%s' as a valid ref.", object_ref);
cd067d3b 633
74884b52 634 t = init_notes_check("copy");
cd067d3b
JH
635 note = get_note(t, object);
636
74884b52 637 if (note) {
5848769f 638 if (!force) {
74884b52
SB
639 retval = error("Cannot copy notes. Found existing "
640 "notes for object %s. Use '-f' to "
641 "overwrite existing notes",
642 sha1_to_hex(object));
643 goto out;
5848769f 644 }
5848769f
JH
645 fprintf(stderr, "Overwriting existing notes for object %s\n",
646 sha1_to_hex(object));
e397421a
JH
647 }
648
74884b52
SB
649 from_note = get_note(t, from_obj);
650 if (!from_note) {
651 retval = error("Missing notes on source object %s. Cannot "
652 "copy.", sha1_to_hex(from_obj));
653 goto out;
cd067d3b
JH
654 }
655
74884b52
SB
656 add_note(t, object, from_note, combine_notes_overwrite);
657 commit_notes(t, "Notes added by 'git notes copy'");
658out:
659 free_notes(t);
660 return retval;
661}
7aa4754e 662
74884b52
SB
663static int append_edit(int argc, const char **argv, const char *prefix)
664{
665 const char *object_ref;
666 struct notes_tree *t;
667 unsigned char object[20], new_note[20];
668 const unsigned char *note;
669 char logmsg[100];
670 const char * const *usage;
671 struct msg_arg msg = { 0, 0, STRBUF_INIT };
672 struct option options[] = {
673 { OPTION_CALLBACK, 'm', "message", &msg, "MSG",
674 "note contents as a string", PARSE_OPT_NONEG,
675 parse_msg_arg},
676 { OPTION_CALLBACK, 'F', "file", &msg, "FILE",
677 "note contents in a file", PARSE_OPT_NONEG,
678 parse_file_arg},
679 { OPTION_CALLBACK, 'c', "reedit-message", &msg, "OBJECT",
680 "reuse and edit specified note object", PARSE_OPT_NONEG,
681 parse_reedit_arg},
682 { OPTION_CALLBACK, 'C', "reuse-message", &msg, "OBJECT",
683 "reuse specified note object", PARSE_OPT_NONEG,
684 parse_reuse_arg},
685 OPT_END()
686 };
687 int edit = !strcmp(argv[0], "edit");
688
689 usage = edit ? git_notes_edit_usage : git_notes_append_usage;
690 argc = parse_options(argc, argv, prefix, options, usage,
691 PARSE_OPT_KEEP_ARGV0);
92b3385f 692
74884b52
SB
693 if (2 < argc) {
694 error("too many parameters");
695 usage_with_options(usage, options);
cd067d3b
JH
696 }
697
74884b52
SB
698 if (msg.given && edit)
699 fprintf(stderr, "The -m/-F/-c/-C options have been deprecated "
700 "for the 'edit' subcommand.\n"
701 "Please use 'git notes add -f -m/-F/-c/-C' instead.\n");
702
703 object_ref = 1 < argc ? argv[1] : "HEAD";
704
705 if (get_sha1(object_ref, object))
706 die("Failed to resolve '%s' as a valid ref.", object_ref);
707
708 t = init_notes_check(argv[0]);
709 note = get_note(t, object);
710
711 create_note(object, &msg, !edit, note, new_note);
5848769f
JH
712
713 if (is_null_sha1(new_note))
714 remove_note(t, object);
715 else
716 add_note(t, object, new_note, combine_notes_overwrite);
717
5848769f 718 snprintf(logmsg, sizeof(logmsg), "Notes %s by 'git notes %s'",
92b3385f 719 is_null_sha1(new_note) ? "removed" : "added", argv[0]);
a0b4dfa9 720 commit_notes(t, logmsg);
cd067d3b 721 free_notes(t);
348f199b 722 strbuf_release(&(msg.buf));
74884b52
SB
723 return 0;
724}
725
726static int show(int argc, const char **argv, const char *prefix)
727{
728 const char *object_ref;
729 struct notes_tree *t;
730 unsigned char object[20];
731 const unsigned char *note;
732 int retval;
733 struct option options[] = {
734 OPT_END()
735 };
736
737 argc = parse_options(argc, argv, prefix, options, git_notes_show_usage,
738 0);
739
740 if (1 < argc) {
741 error("too many parameters");
742 usage_with_options(git_notes_show_usage, options);
743 }
744
745 object_ref = argc ? argv[0] : "HEAD";
746
747 if (get_sha1(object_ref, object))
748 die("Failed to resolve '%s' as a valid ref.", object_ref);
749
750 t = init_notes_check("show");
751 note = get_note(t, object);
752
753 if (!note)
754 retval = error("No note found for object %s.",
755 sha1_to_hex(object));
756 else {
757 const char *show_args[3] = {"show", sha1_to_hex(note), NULL};
758 retval = execv_git_cmd(show_args);
759 }
760 free_notes(t);
5848769f 761 return retval;
cd067d3b 762}
74884b52
SB
763
764static int remove_cmd(int argc, const char **argv, const char *prefix)
765{
766 struct option options[] = {
767 OPT_END()
768 };
769 const char *object_ref;
770 struct notes_tree *t;
771 unsigned char object[20];
772
773 argc = parse_options(argc, argv, prefix, options,
774 git_notes_remove_usage, 0);
775
776 if (1 < argc) {
777 error("too many parameters");
778 usage_with_options(git_notes_remove_usage, options);
779 }
780
781 object_ref = argc ? argv[0] : "HEAD";
782
783 if (get_sha1(object_ref, object))
784 die("Failed to resolve '%s' as a valid ref.", object_ref);
785
786 t = init_notes_check("remove");
787
788 fprintf(stderr, "Removing note for object %s\n", sha1_to_hex(object));
789 remove_note(t, object);
790
791 commit_notes(t, "Notes removed by 'git notes remove'");
792 free_notes(t);
793 return 0;
794}
795
796static int prune(int argc, const char **argv, const char *prefix)
797{
798 struct notes_tree *t;
a9f2adff 799 int show_only = 0, verbose = 0;
74884b52 800 struct option options[] = {
e93487d2
RS
801 OPT_BOOLEAN('n', "dry-run", &show_only,
802 "do not remove, show only"),
803 OPT_BOOLEAN('v', "verbose", &verbose, "report pruned notes"),
74884b52
SB
804 OPT_END()
805 };
806
807 argc = parse_options(argc, argv, prefix, options, git_notes_prune_usage,
808 0);
809
810 if (argc) {
811 error("too many parameters");
812 usage_with_options(git_notes_prune_usage, options);
813 }
814
815 t = init_notes_check("prune");
816
a9f2adff
MG
817 prune_notes(t, (verbose ? NOTES_PRUNE_VERBOSE : 0) |
818 (show_only ? NOTES_PRUNE_VERBOSE|NOTES_PRUNE_DRYRUN : 0) );
819 if (!show_only)
820 commit_notes(t, "Notes removed by 'git notes prune'");
74884b52
SB
821 free_notes(t);
822 return 0;
823}
824
825int cmd_notes(int argc, const char **argv, const char *prefix)
826{
827 int result;
828 const char *override_notes_ref = NULL;
829 struct option options[] = {
830 OPT_STRING(0, "ref", &override_notes_ref, "notes_ref",
831 "use notes from <notes_ref>"),
832 OPT_END()
833 };
834
835 git_config(git_default_config, NULL);
836 argc = parse_options(argc, argv, prefix, options, git_notes_usage,
837 PARSE_OPT_STOP_AT_NON_OPTION);
838
839 if (override_notes_ref) {
840 struct strbuf sb = STRBUF_INIT;
841 if (!prefixcmp(override_notes_ref, "refs/notes/"))
842 /* we're happy */;
843 else if (!prefixcmp(override_notes_ref, "notes/"))
844 strbuf_addstr(&sb, "refs/");
845 else
846 strbuf_addstr(&sb, "refs/notes/");
847 strbuf_addstr(&sb, override_notes_ref);
848 setenv("GIT_NOTES_REF", sb.buf, 1);
849 strbuf_release(&sb);
850 }
851
852 if (argc < 1 || !strcmp(argv[0], "list"))
853 result = list(argc, argv, prefix);
854 else if (!strcmp(argv[0], "add"))
855 result = add(argc, argv, prefix);
856 else if (!strcmp(argv[0], "copy"))
857 result = copy(argc, argv, prefix);
858 else if (!strcmp(argv[0], "append") || !strcmp(argv[0], "edit"))
859 result = append_edit(argc, argv, prefix);
860 else if (!strcmp(argv[0], "show"))
861 result = show(argc, argv, prefix);
862 else if (!strcmp(argv[0], "remove"))
863 result = remove_cmd(argc, argv, prefix);
864 else if (!strcmp(argv[0], "prune"))
865 result = prune(argc, argv, prefix);
866 else {
867 result = error("Unknown subcommand: %s", argv[0]);
868 usage_with_options(git_notes_usage, options);
869 }
870
871 return result ? 1 : 0;
872}