builtin-notes: Refactor handling of -F option to allow combining -m and -F
[git/mingw.git] / builtin-notes.c
blob190c46c3bebc8150a65460add564e5e6d87f1e3b
1 /*
2 * Builtin "git notes"
4 * Copyright (c) 2010 Johan Herland <johan@herland.net>
6 * Based on git-notes.sh by Johannes Schindelin,
7 * and builtin-tag.c by Kristian Høgsberg and Carlos Rica.
8 */
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"
20 static const char * const git_notes_usage[] = {
21 "git notes [list [<object>]]",
22 "git notes add [-f] [-m <msg> | -F <file>] [<object>]",
23 "git notes append [-m <msg> | -F <file>] [<object>]",
24 "git notes edit [<object>]",
25 "git notes show [<object>]",
26 "git notes remove [<object>]",
27 "git notes prune",
28 NULL
31 static const char note_template[] =
32 "\n"
33 "#\n"
34 "# Write/edit the notes for the following object:\n"
35 "#\n";
37 struct msg_arg {
38 int given;
39 struct strbuf buf;
42 static int list_each_note(const unsigned char *object_sha1,
43 const unsigned char *note_sha1, char *note_path,
44 void *cb_data)
46 printf("%s %s\n", sha1_to_hex(note_sha1), sha1_to_hex(object_sha1));
47 return 0;
50 static void write_note_data(int fd, const unsigned char *sha1)
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);
62 static void write_commented_object(int fd, const unsigned char *object)
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;
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));
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");
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);
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));
101 static void create_note(const unsigned char *object, struct msg_arg *msg,
102 int append_only, const unsigned char *prev,
103 unsigned char *result)
105 char *path = NULL;
107 if (!msg->given) {
108 int fd;
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);
116 if (prev && !append_only)
117 write_note_data(fd, prev);
118 write_or_die(fd, note_template, strlen(note_template));
120 write_commented_object(fd, object);
122 close(fd);
124 if (launch_editor(path, &(msg->buf), NULL)) {
125 die("Please supply the note contents using either -m" \
126 " or -F option");
128 stripspace(&(msg->buf), 1);
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);
137 strbuf_grow(&(msg->buf), size + 1);
138 if (msg->buf.len && prev_buf && size)
139 strbuf_insert(&(msg->buf), 0, "\n", 1);
140 if (prev_buf && size)
141 strbuf_insert(&(msg->buf), 0, prev_buf, size);
142 free(prev_buf);
145 if (!msg->buf.len) {
146 fprintf(stderr, "Removing note for object %s\n",
147 sha1_to_hex(object));
148 hashclr(result);
149 } else {
150 if (write_sha1_file(msg->buf.buf, msg->buf.len, blob_type, result)) {
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);
159 if (path) {
160 unlink_or_warn(path);
161 free(path);
165 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
167 struct msg_arg *msg = opt->value;
169 if (!arg)
170 return -1;
172 strbuf_grow(&(msg->buf), strlen(arg) + 2);
173 if (msg->buf.len)
174 strbuf_addstr(&(msg->buf), "\n");
175 strbuf_addstr(&(msg->buf), arg);
176 stripspace(&(msg->buf), 0);
178 msg->given = 1;
179 return 0;
182 static int parse_file_arg(const struct option *opt, const char *arg, int unset)
184 struct msg_arg *msg = opt->value;
186 if (!arg)
187 return -1;
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);
198 msg->given = 1;
199 return 0;
202 int commit_notes(struct notes_tree *t, const char *msg)
204 struct commit_list *parent;
205 unsigned char tree_sha1[20], prev_commit[20], new_commit[20];
206 struct strbuf buf = STRBUF_INIT;
208 if (!t)
209 t = &default_notes_tree;
210 if (!t->initialized || !t->ref || !*t->ref)
211 die("Cannot commit uninitialized/unreferenced notes tree");
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 */
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");
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;
232 if (commit_tree(buf.buf + 7, tree_sha1, parent, new_commit, NULL))
233 die("Failed to commit notes tree to database");
235 /* Update notes ref with new commit */
236 update_ref(buf.buf, t->ref, new_commit, prev_commit, 0, DIE_ON_ERR);
238 strbuf_release(&buf);
239 return 0;
242 int cmd_notes(int argc, const char **argv, const char *prefix)
244 struct notes_tree *t;
245 unsigned char object[20], new_note[20];
246 const unsigned char *note;
247 const char *object_ref;
248 char logmsg[100];
250 int list = 0, add = 0, append = 0, edit = 0, show = 0, remove = 0,
251 prune = 0, force = 0;
252 int given_object;
253 struct msg_arg msg = { 0, STRBUF_INIT };
254 struct option options[] = {
255 OPT_GROUP("Notes options"),
256 OPT_CALLBACK('m', "message", &msg, "MSG",
257 "note contents as a string", parse_msg_arg),
258 OPT_CALLBACK('F', "file", &msg, "FILE",
259 "note contents in a file", parse_file_arg),
260 OPT_BOOLEAN('f', "force", &force, "replace existing notes"),
261 OPT_END()
264 git_config(git_default_config, NULL);
266 argc = parse_options(argc, argv, prefix, options, git_notes_usage, 0);
268 if (argc && !strcmp(argv[0], "list"))
269 list = 1;
270 else if (argc && !strcmp(argv[0], "add"))
271 add = 1;
272 else if (argc && !strcmp(argv[0], "append"))
273 append = 1;
274 else if (argc && !strcmp(argv[0], "edit"))
275 edit = 1;
276 else if (argc && !strcmp(argv[0], "show"))
277 show = 1;
278 else if (argc && !strcmp(argv[0], "remove"))
279 remove = 1;
280 else if (argc && !strcmp(argv[0], "prune"))
281 prune = 1;
282 else if (!argc)
283 list = 1; /* Default to 'list' if no other subcommand given */
285 if (list + add + append + edit + show + remove + prune != 1)
286 usage_with_options(git_notes_usage, options);
288 if (msg.given && !(add || append || edit)) {
289 error("cannot use -m/-F options with %s subcommand.", argv[0]);
290 usage_with_options(git_notes_usage, options);
293 if (msg.given && edit) {
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");
300 if (force && !add) {
301 error("cannot use -f option with %s subcommand.", argv[0]);
302 usage_with_options(git_notes_usage, options);
305 given_object = argc == 2;
306 object_ref = given_object ? argv[1] : "HEAD";
307 if (argc > 2 || (prune && argc > 1)) {
308 error("too many parameters");
309 usage_with_options(git_notes_usage, options);
312 if (get_sha1(object_ref, object))
313 die("Failed to resolve '%s' as a valid ref.", object_ref);
315 init_notes(NULL, NULL, NULL, 0);
316 t = &default_notes_tree;
318 if (prefixcmp(t->ref, "refs/notes/"))
319 die("Refusing to %s notes in %s (outside of refs/notes/)",
320 argv[0], t->ref);
322 note = get_note(t, object);
324 /* list command */
326 if (list) {
327 if (given_object) {
328 if (note) {
329 puts(sha1_to_hex(note));
330 return 0;
332 } else
333 return for_each_note(t, 0, list_each_note, NULL);
336 /* show command */
338 if ((list || show) && !note) {
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);
346 /* add/append/edit/remove/prune command */
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;
360 if (remove) {
361 msg.given = 1;
362 strbuf_reset(&(msg.buf));
365 if (prune) {
366 hashclr(new_note);
367 prune_notes(t);
368 } else {
369 create_note(object, &msg, append, note, new_note);
370 if (is_null_sha1(new_note))
371 remove_note(t, object);
372 else
373 add_note(t, object, new_note, combine_notes_overwrite);
375 snprintf(logmsg, sizeof(logmsg), "Note %s by 'git notes %s'",
376 is_null_sha1(new_note) ? "removed" : "added", argv[0]);
377 commit_notes(t, logmsg);
379 free_notes(t);
380 strbuf_release(&(msg.buf));
381 return 0;