notes: implement 'git notes copy --stdin'
[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"
19
20static const char * const git_notes_usage[] = {
e397421a 21 "git notes [list [<object>]]",
0691cff7 22 "git notes add [-f] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]",
e73bbd96 23 "git notes copy [-f] <from-object> <to-object>",
0691cff7 24 "git notes append [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]",
aaec9bcf 25 "git notes edit [<object>]",
cd067d3b 26 "git notes show [<object>]",
92b3385f 27 "git notes remove [<object>]",
d6576e1f 28 "git notes prune",
cd067d3b
JH
29 NULL
30};
31
32static const char note_template[] =
33 "\n"
34 "#\n"
35 "# Write/edit the notes for the following object:\n"
36 "#\n";
37
348f199b
JH
38struct msg_arg {
39 int given;
0691cff7 40 int use_editor;
348f199b
JH
41 struct strbuf buf;
42};
43
e397421a
JH
44static int list_each_note(const unsigned char *object_sha1,
45 const unsigned char *note_sha1, char *note_path,
46 void *cb_data)
47{
48 printf("%s %s\n", sha1_to_hex(note_sha1), sha1_to_hex(object_sha1));
49 return 0;
50}
51
cd067d3b
JH
52static void write_note_data(int fd, const unsigned char *sha1)
53{
54 unsigned long size;
55 enum object_type type;
56 char *buf = read_sha1_file(sha1, &type, &size);
57 if (buf) {
58 if (size)
59 write_or_die(fd, buf, size);
60 free(buf);
61 }
62}
63
64static void write_commented_object(int fd, const unsigned char *object)
65{
66 const char *show_args[5] =
67 {"show", "--stat", "--no-notes", sha1_to_hex(object), NULL};
68 struct child_process show;
69 struct strbuf buf = STRBUF_INIT;
70 FILE *show_out;
71
72 /* Invoke "git show --stat --no-notes $object" */
73 memset(&show, 0, sizeof(show));
74 show.argv = show_args;
75 show.no_stdin = 1;
76 show.out = -1;
77 show.err = 0;
78 show.git_cmd = 1;
79 if (start_command(&show))
80 die("unable to start 'show' for object '%s'",
81 sha1_to_hex(object));
82
83 /* Open the output as FILE* so strbuf_getline() can be used. */
84 show_out = xfdopen(show.out, "r");
85 if (show_out == NULL)
86 die_errno("can't fdopen 'show' output fd");
87
88 /* Prepend "# " to each output line and write result to 'fd' */
89 while (strbuf_getline(&buf, show_out, '\n') != EOF) {
90 write_or_die(fd, "# ", 2);
91 write_or_die(fd, buf.buf, buf.len);
92 write_or_die(fd, "\n", 1);
93 }
94 strbuf_release(&buf);
95 if (fclose(show_out))
96 die_errno("failed to close pipe to 'show' for object '%s'",
97 sha1_to_hex(object));
98 if (finish_command(&show))
99 die("failed to finish 'show' for object '%s'",
100 sha1_to_hex(object));
101}
102
348f199b
JH
103static void create_note(const unsigned char *object, struct msg_arg *msg,
104 int append_only, const unsigned char *prev,
cd067d3b
JH
105 unsigned char *result)
106{
107 char *path = NULL;
108
0691cff7 109 if (msg->use_editor || !msg->given) {
cd067d3b
JH
110 int fd;
111
112 /* write the template message before editing: */
113 path = git_pathdup("NOTES_EDITMSG");
114 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
115 if (fd < 0)
116 die_errno("could not create file '%s'", path);
117
0691cff7
JH
118 if (msg->given)
119 write_or_die(fd, msg->buf.buf, msg->buf.len);
120 else if (prev && !append_only)
cd067d3b
JH
121 write_note_data(fd, prev);
122 write_or_die(fd, note_template, strlen(note_template));
123
124 write_commented_object(fd, object);
125
126 close(fd);
0691cff7 127 strbuf_reset(&(msg->buf));
cd067d3b 128
348f199b 129 if (launch_editor(path, &(msg->buf), NULL)) {
cd067d3b
JH
130 die("Please supply the note contents using either -m" \
131 " or -F option");
132 }
348f199b 133 stripspace(&(msg->buf), 1);
cd067d3b
JH
134 }
135
2347fae5
JH
136 if (prev && append_only) {
137 /* Append buf to previous note contents */
138 unsigned long size;
139 enum object_type type;
140 char *prev_buf = read_sha1_file(prev, &type, &size);
141
348f199b
JH
142 strbuf_grow(&(msg->buf), size + 1);
143 if (msg->buf.len && prev_buf && size)
144 strbuf_insert(&(msg->buf), 0, "\n", 1);
2347fae5 145 if (prev_buf && size)
348f199b 146 strbuf_insert(&(msg->buf), 0, prev_buf, size);
2347fae5
JH
147 free(prev_buf);
148 }
149
348f199b 150 if (!msg->buf.len) {
cd067d3b
JH
151 fprintf(stderr, "Removing note for object %s\n",
152 sha1_to_hex(object));
153 hashclr(result);
154 } else {
348f199b 155 if (write_sha1_file(msg->buf.buf, msg->buf.len, blob_type, result)) {
cd067d3b
JH
156 error("unable to write note object");
157 if (path)
158 error("The note contents has been left in %s",
159 path);
160 exit(128);
161 }
162 }
163
164 if (path) {
165 unlink_or_warn(path);
166 free(path);
167 }
168}
169
cd067d3b
JH
170static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
171{
172 struct msg_arg *msg = opt->value;
173
174 if (!arg)
175 return -1;
348f199b
JH
176
177 strbuf_grow(&(msg->buf), strlen(arg) + 2);
cd067d3b 178 if (msg->buf.len)
348f199b 179 strbuf_addstr(&(msg->buf), "\n");
cd067d3b 180 strbuf_addstr(&(msg->buf), arg);
348f199b
JH
181 stripspace(&(msg->buf), 0);
182
183 msg->given = 1;
184 return 0;
185}
186
187static int parse_file_arg(const struct option *opt, const char *arg, int unset)
188{
189 struct msg_arg *msg = opt->value;
190
191 if (!arg)
192 return -1;
193
194 if (msg->buf.len)
195 strbuf_addstr(&(msg->buf), "\n");
196 if (!strcmp(arg, "-")) {
197 if (strbuf_read(&(msg->buf), 0, 1024) < 0)
198 die_errno("cannot read '%s'", arg);
199 } else if (strbuf_read_file(&(msg->buf), arg, 1024) < 0)
200 die_errno("could not open or read '%s'", arg);
201 stripspace(&(msg->buf), 0);
202
cd067d3b
JH
203 msg->given = 1;
204 return 0;
205}
206
0691cff7
JH
207static int parse_reuse_arg(const struct option *opt, const char *arg, int unset)
208{
209 struct msg_arg *msg = opt->value;
210 char *buf;
211 unsigned char object[20];
212 enum object_type type;
213 unsigned long len;
214
215 if (!arg)
216 return -1;
217
218 if (msg->buf.len)
219 strbuf_addstr(&(msg->buf), "\n");
220
221 if (get_sha1(arg, object))
222 die("Failed to resolve '%s' as a valid ref.", arg);
223 if (!(buf = read_sha1_file(object, &type, &len)) || !len) {
224 free(buf);
225 die("Failed to read object '%s'.", arg);;
226 }
227 strbuf_add(&(msg->buf), buf, len);
228 free(buf);
229
230 msg->given = 1;
231 return 0;
232}
233
234static int parse_reedit_arg(const struct option *opt, const char *arg, int unset)
235{
236 struct msg_arg *msg = opt->value;
237 msg->use_editor = 1;
238 return parse_reuse_arg(opt, arg, unset);
239}
240
cd067d3b
JH
241int commit_notes(struct notes_tree *t, const char *msg)
242{
243 struct commit_list *parent;
244 unsigned char tree_sha1[20], prev_commit[20], new_commit[20];
245 struct strbuf buf = STRBUF_INIT;
246
247 if (!t)
248 t = &default_notes_tree;
249 if (!t->initialized || !t->ref || !*t->ref)
250 die("Cannot commit uninitialized/unreferenced notes tree");
251
252 /* Prepare commit message and reflog message */
253 strbuf_addstr(&buf, "notes: "); /* commit message starts at index 7 */
254 strbuf_addstr(&buf, msg);
255 if (buf.buf[buf.len - 1] != '\n')
256 strbuf_addch(&buf, '\n'); /* Make sure msg ends with newline */
257
258 /* Convert notes tree to tree object */
259 if (write_notes_tree(t, tree_sha1))
260 die("Failed to write current notes tree to database");
261
262 /* Create new commit for the tree object */
263 if (!read_ref(t->ref, prev_commit)) { /* retrieve parent commit */
264 parent = xmalloc(sizeof(*parent));
265 parent->item = lookup_commit(prev_commit);
266 parent->next = NULL;
267 } else {
268 hashclr(prev_commit);
269 parent = NULL;
270 }
271 if (commit_tree(buf.buf + 7, tree_sha1, parent, new_commit, NULL))
272 die("Failed to commit notes tree to database");
273
274 /* Update notes ref with new commit */
275 update_ref(buf.buf, t->ref, new_commit, prev_commit, 0, DIE_ON_ERR);
276
277 strbuf_release(&buf);
278 return 0;
279}
280
160baa0d
TR
281int notes_copy_from_stdin(int force)
282{
283 struct strbuf buf = STRBUF_INIT;
284 struct notes_tree *t;
285 int ret = 0;
286
287 init_notes(NULL, NULL, NULL, 0);
288 t = &default_notes_tree;
289
290 while (strbuf_getline(&buf, stdin, '\n') != EOF) {
291 unsigned char from_obj[20], to_obj[20];
292 struct strbuf **split;
293 int err;
294
295 split = strbuf_split(&buf, ' ');
296 if (!split[0] || !split[1])
297 die("Malformed input line: '%s'.", buf.buf);
298 strbuf_rtrim(split[0]);
299 strbuf_rtrim(split[1]);
300 if (get_sha1(split[0]->buf, from_obj))
301 die("Failed to resolve '%s' as a valid ref.", split[0]->buf);
302 if (get_sha1(split[1]->buf, to_obj))
303 die("Failed to resolve '%s' as a valid ref.", split[1]->buf);
304
305 err = copy_note(t, from_obj, to_obj, force, combine_notes_overwrite);
306
307 if (err) {
308 error("Failed to copy notes from '%s' to '%s'",
309 split[0]->buf, split[1]->buf);
310 ret = 1;
311 }
312
313 strbuf_list_free(split);
314 }
315
316 commit_notes(t, "Notes added by 'git notes copy'");
317 free_notes(t);
318 return ret;
319}
320
cd067d3b
JH
321int cmd_notes(int argc, const char **argv, const char *prefix)
322{
cd067d3b 323 struct notes_tree *t;
e73bbd96 324 unsigned char object[20], from_obj[20], new_note[20];
cd067d3b 325 const unsigned char *note;
92b3385f
JH
326 const char *object_ref;
327 char logmsg[100];
a0b4dfa9 328
e73bbd96 329 int list = 0, add = 0, copy = 0, append = 0, edit = 0, show = 0,
160baa0d 330 remove = 0, prune = 0, force = 0, from_stdin = 0;
5848769f 331 int given_object = 0, i = 1, retval = 0;
0691cff7 332 struct msg_arg msg = { 0, 0, STRBUF_INIT };
cd067d3b 333 struct option options[] = {
aaec9bcf 334 OPT_GROUP("Notes options"),
348f199b 335 OPT_CALLBACK('m', "message", &msg, "MSG",
cd067d3b 336 "note contents as a string", parse_msg_arg),
348f199b
JH
337 OPT_CALLBACK('F', "file", &msg, "FILE",
338 "note contents in a file", parse_file_arg),
0691cff7
JH
339 OPT_CALLBACK('c', "reedit-message", &msg, "OBJECT",
340 "reuse and edit specified note object", parse_reedit_arg),
341 OPT_CALLBACK('C', "reuse-message", &msg, "OBJECT",
342 "reuse specified note object", parse_reuse_arg),
7aa4754e 343 OPT_BOOLEAN('f', "force", &force, "replace existing notes"),
160baa0d 344 OPT_BOOLEAN(0, "stdin", &from_stdin, "read objects from stdin"),
cd067d3b
JH
345 OPT_END()
346 };
347
348 git_config(git_default_config, NULL);
349
350 argc = parse_options(argc, argv, prefix, options, git_notes_usage, 0);
351
e397421a
JH
352 if (argc && !strcmp(argv[0], "list"))
353 list = 1;
7aa4754e
JH
354 else if (argc && !strcmp(argv[0], "add"))
355 add = 1;
e73bbd96
JH
356 else if (argc && !strcmp(argv[0], "copy"))
357 copy = 1;
2347fae5
JH
358 else if (argc && !strcmp(argv[0], "append"))
359 append = 1;
e397421a 360 else if (argc && !strcmp(argv[0], "edit"))
cd067d3b
JH
361 edit = 1;
362 else if (argc && !strcmp(argv[0], "show"))
363 show = 1;
92b3385f
JH
364 else if (argc && !strcmp(argv[0], "remove"))
365 remove = 1;
d6576e1f
JH
366 else if (argc && !strcmp(argv[0], "prune"))
367 prune = 1;
5848769f 368 else if (!argc) {
e397421a 369 list = 1; /* Default to 'list' if no other subcommand given */
5848769f
JH
370 i = 0;
371 }
cd067d3b 372
e73bbd96 373 if (list + add + copy + append + edit + show + remove + prune != 1)
cd067d3b
JH
374 usage_with_options(git_notes_usage, options);
375
348f199b 376 if (msg.given && !(add || append || edit)) {
0691cff7
JH
377 error("cannot use -m/-F/-c/-C options with %s subcommand.",
378 argv[0]);
92b3385f
JH
379 usage_with_options(git_notes_usage, options);
380 }
381
348f199b 382 if (msg.given && edit) {
0691cff7
JH
383 fprintf(stderr, "The -m/-F/-c/-C options have been deprecated "
384 "for the 'edit' subcommand.\n"
385 "Please use 'git notes add -f -m/-F/-c/-C' instead.\n");
aaec9bcf
JH
386 }
387
e73bbd96 388 if (force && !(add || copy)) {
7aa4754e
JH
389 error("cannot use -f option with %s subcommand.", argv[0]);
390 usage_with_options(git_notes_usage, options);
391 }
392
160baa0d
TR
393 if (!copy && from_stdin) {
394 error("cannot use --stdin with %s subcommand.", argv[0]);
395 usage_with_options(git_notes_usage, options);
396 }
397
e73bbd96
JH
398 if (copy) {
399 const char *from_ref;
160baa0d
TR
400 if (from_stdin) {
401 if (argc > 1) {
402 error("too many parameters");
403 usage_with_options(git_notes_usage, options);
404 } else {
405 return notes_copy_from_stdin(force);
406 }
407 }
e73bbd96
JH
408 if (argc < 3) {
409 error("too few parameters");
410 usage_with_options(git_notes_usage, options);
411 }
412 from_ref = argv[i++];
413 if (get_sha1(from_ref, from_obj))
414 die("Failed to resolve '%s' as a valid ref.", from_ref);
415 }
416
5848769f
JH
417 given_object = argc > i;
418 object_ref = given_object ? argv[i++] : "HEAD";
419
420 if (argc > i || (prune && given_object)) {
cd067d3b
JH
421 error("too many parameters");
422 usage_with_options(git_notes_usage, options);
423 }
424
425 if (get_sha1(object_ref, object))
426 die("Failed to resolve '%s' as a valid ref.", object_ref);
427
428 init_notes(NULL, NULL, NULL, 0);
429 t = &default_notes_tree;
430
431 if (prefixcmp(t->ref, "refs/notes/"))
432 die("Refusing to %s notes in %s (outside of refs/notes/)",
92b3385f 433 argv[0], t->ref);
cd067d3b
JH
434
435 note = get_note(t, object);
436
e397421a
JH
437 /* list command */
438
439 if (list) {
440 if (given_object) {
441 if (note) {
442 puts(sha1_to_hex(note));
5848769f 443 goto end;
e397421a 444 }
5848769f
JH
445 } else {
446 retval = for_each_note(t, 0, list_each_note, NULL);
447 goto end;
448 }
e397421a
JH
449 }
450
cd067d3b
JH
451 /* show command */
452
e397421a 453 if ((list || show) && !note) {
cd067d3b 454 error("No note found for object %s.", sha1_to_hex(object));
5848769f
JH
455 retval = 1;
456 goto end;
cd067d3b
JH
457 } else if (show) {
458 const char *show_args[3] = {"show", sha1_to_hex(note), NULL};
5848769f
JH
459 retval = execv_git_cmd(show_args);
460 goto end;
cd067d3b
JH
461 }
462
2347fae5 463 /* add/append/edit/remove/prune command */
7aa4754e 464
e73bbd96 465 if ((add || copy) && note) {
5848769f 466 if (!force) {
e73bbd96 467 error("Cannot %s notes. Found existing notes for object"
5848769f 468 " %s. Use '-f' to overwrite existing notes",
e73bbd96 469 argv[0], sha1_to_hex(object));
5848769f
JH
470 retval = 1;
471 goto end;
7aa4754e 472 }
5848769f
JH
473 fprintf(stderr, "Overwriting existing notes for object %s\n",
474 sha1_to_hex(object));
7aa4754e 475 }
92b3385f 476
348f199b
JH
477 if (remove) {
478 msg.given = 1;
0691cff7 479 msg.use_editor = 0;
348f199b 480 strbuf_reset(&(msg.buf));
cd067d3b
JH
481 }
482
d6576e1f
JH
483 if (prune) {
484 hashclr(new_note);
485 prune_notes(t);
5848769f 486 goto commit;
e73bbd96
JH
487 } else if (copy) {
488 const unsigned char *from_note = get_note(t, from_obj);
489 if (!from_note) {
490 error("Missing notes on source object %s. Cannot copy.",
491 sha1_to_hex(from_obj));
492 retval = 1;
493 goto end;
494 }
495 hashcpy(new_note, from_note);
5848769f 496 } else
348f199b 497 create_note(object, &msg, append, note, new_note);
5848769f
JH
498
499 if (is_null_sha1(new_note))
500 remove_note(t, object);
501 else
502 add_note(t, object, new_note, combine_notes_overwrite);
503
504commit:
505 snprintf(logmsg, sizeof(logmsg), "Notes %s by 'git notes %s'",
92b3385f 506 is_null_sha1(new_note) ? "removed" : "added", argv[0]);
a0b4dfa9 507 commit_notes(t, logmsg);
cd067d3b 508
5848769f 509end:
cd067d3b 510 free_notes(t);
348f199b 511 strbuf_release(&(msg.buf));
5848769f 512 return retval;
cd067d3b 513}