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