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.
17 #include "run-command.h"
18 #include "parse-options.h"
20 static const char * const git_notes_usage[] = {
21 "git notes edit [-m <msg> | -F <file>] [<object>]",
22 "git notes show [<object>]",
23 "git notes remove [<object>]",
28 static const char note_template[] =
31 "# Write/edit the notes for the following object:\n"
34 static void write_note_data(int fd, const unsigned char *sha1)
37 enum object_type type;
38 char *buf = read_sha1_file(sha1, &type, &size);
41 write_or_die(fd, buf, size);
46 static void write_commented_object(int fd, const unsigned char *object)
48 const char *show_args[5] =
49 {"show", "--stat", "--no-notes", sha1_to_hex(object), NULL};
50 struct child_process show;
51 struct strbuf buf = STRBUF_INIT;
54 /* Invoke "git show --stat --no-notes $object" */
55 memset(&show, 0, sizeof(show));
56 show.argv = show_args;
61 if (start_command(&show))
62 die("unable to start 'show' for object '%s'",
65 /* Open the output as FILE* so strbuf_getline() can be used. */
66 show_out = xfdopen(show.out, "r");
68 die_errno("can't fdopen 'show' output fd");
70 /* Prepend "# " to each output line and write result to 'fd' */
71 while (strbuf_getline(&buf, show_out, '\n') != EOF) {
72 write_or_die(fd, "# ", 2);
73 write_or_die(fd, buf.buf, buf.len);
74 write_or_die(fd, "\n", 1);
78 die_errno("failed to close pipe to 'show' for object '%s'",
80 if (finish_command(&show))
81 die("failed to finish 'show' for object '%s'",
85 static void create_note(const unsigned char *object,
88 const unsigned char *prev,
89 unsigned char *result)
96 /* write the template message before editing: */
97 path = git_pathdup("NOTES_EDITMSG");
98 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
100 die_errno("could not create file '%s'", path);
103 write_note_data(fd, prev);
104 write_or_die(fd, note_template, strlen(note_template));
106 write_commented_object(fd, object);
110 if (launch_editor(path, buf, NULL)) {
111 die("Please supply the note contents using either -m" \
119 fprintf(stderr, "Removing note for object %s\n",
120 sha1_to_hex(object));
123 if (write_sha1_file(buf->buf, buf->len, blob_type, result)) {
124 error("unable to write note object");
126 error("The note contents has been left in %s",
133 unlink_or_warn(path);
143 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
145 struct msg_arg *msg = opt->value;
150 strbuf_addstr(&(msg->buf), "\n\n");
151 strbuf_addstr(&(msg->buf), arg);
156 int commit_notes(struct notes_tree *t, const char *msg)
158 struct commit_list *parent;
159 unsigned char tree_sha1[20], prev_commit[20], new_commit[20];
160 struct strbuf buf = STRBUF_INIT;
163 t = &default_notes_tree;
164 if (!t->initialized || !t->ref || !*t->ref)
165 die("Cannot commit uninitialized/unreferenced notes tree");
167 /* Prepare commit message and reflog message */
168 strbuf_addstr(&buf, "notes: "); /* commit message starts at index 7 */
169 strbuf_addstr(&buf, msg);
170 if (buf.buf[buf.len - 1] != '\n')
171 strbuf_addch(&buf, '\n'); /* Make sure msg ends with newline */
173 /* Convert notes tree to tree object */
174 if (write_notes_tree(t, tree_sha1))
175 die("Failed to write current notes tree to database");
177 /* Create new commit for the tree object */
178 if (!read_ref(t->ref, prev_commit)) { /* retrieve parent commit */
179 parent = xmalloc(sizeof(*parent));
180 parent->item = lookup_commit(prev_commit);
183 hashclr(prev_commit);
186 if (commit_tree(buf.buf + 7, tree_sha1, parent, new_commit, NULL))
187 die("Failed to commit notes tree to database");
189 /* Update notes ref with new commit */
190 update_ref(buf.buf, t->ref, new_commit, prev_commit, 0, DIE_ON_ERR);
192 strbuf_release(&buf);
196 int cmd_notes(int argc, const char **argv, const char *prefix)
198 struct strbuf buf = STRBUF_INIT;
199 struct notes_tree *t;
200 unsigned char object[20], new_note[20];
201 const unsigned char *note;
202 const char *object_ref;
205 int edit = 0, show = 0, remove = 0, prune = 0;
206 const char *msgfile = NULL;
207 struct msg_arg msg = { 0, STRBUF_INIT };
208 struct option options[] = {
209 OPT_GROUP("Notes edit options"),
210 OPT_CALLBACK('m', NULL, &msg, "msg",
211 "note contents as a string", parse_msg_arg),
212 OPT_FILENAME('F', NULL, &msgfile, "note contents in a file"),
216 git_config(git_default_config, NULL);
218 argc = parse_options(argc, argv, prefix, options, git_notes_usage, 0);
220 if (argc && !strcmp(argv[0], "edit"))
222 else if (argc && !strcmp(argv[0], "show"))
224 else if (argc && !strcmp(argv[0], "remove"))
226 else if (argc && !strcmp(argv[0], "prune"))
229 if (edit + show + remove + prune != 1)
230 usage_with_options(git_notes_usage, options);
232 if ((msg.given || msgfile) && !edit) {
233 error("cannot use -m/-F options with %s subcommand.", argv[0]);
234 usage_with_options(git_notes_usage, options);
237 if (msg.given && msgfile) {
238 error("mixing -m and -F options is not allowed.");
239 usage_with_options(git_notes_usage, options);
242 object_ref = argc == 2 ? argv[1] : "HEAD";
243 if (argc > 2 || (prune && argc > 1)) {
244 error("too many parameters");
245 usage_with_options(git_notes_usage, options);
248 if (get_sha1(object_ref, object))
249 die("Failed to resolve '%s' as a valid ref.", object_ref);
251 init_notes(NULL, NULL, NULL, 0);
252 t = &default_notes_tree;
254 if (prefixcmp(t->ref, "refs/notes/"))
255 die("Refusing to %s notes in %s (outside of refs/notes/)",
258 note = get_note(t, object);
263 error("No note found for object %s.", sha1_to_hex(object));
266 const char *show_args[3] = {"show", sha1_to_hex(note), NULL};
267 return execv_git_cmd(show_args);
270 /* edit/remove/prune command */
275 strbuf_addbuf(&buf, &(msg.buf));
277 if (!strcmp(msgfile, "-")) {
278 if (strbuf_read(&buf, 0, 1024) < 0)
279 die_errno("cannot read '%s'", msgfile);
280 } else if (strbuf_read_file(&buf, msgfile, 1024) < 0)
281 die_errno("could not open or read '%s'", msgfile);
288 create_note(object, &buf, msg.given || msgfile || remove, note,
290 if (is_null_sha1(new_note))
291 remove_note(t, object);
293 add_note(t, object, new_note, combine_notes_overwrite);
295 snprintf(logmsg, sizeof(logmsg), "Note %s by 'git notes %s'",
296 is_null_sha1(new_note) ? "removed" : "added", argv[0]);
297 commit_notes(t, logmsg);
300 strbuf_release(&buf);