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