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