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 */
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 edit [-m <msg> | -F <file>] [<object>]",
22 "git notes show [<object>]",
23 "git notes remove [<object>]",
24 "git notes prune",
25 NULL
26 };
28 static const char note_template[] =
29 "\n"
30 "#\n"
31 "# Write/edit the notes for the following object:\n"
32 "#\n";
34 static void write_note_data(int fd, const unsigned char *sha1)
35 {
36 unsigned long size;
37 enum object_type type;
38 char *buf = read_sha1_file(sha1, &type, &size);
39 if (buf) {
40 if (size)
41 write_or_die(fd, buf, size);
42 free(buf);
43 }
44 }
46 static void write_commented_object(int fd, const unsigned char *object)
47 {
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;
52 FILE *show_out;
54 /* Invoke "git show --stat --no-notes $object" */
55 memset(&show, 0, sizeof(show));
56 show.argv = show_args;
57 show.no_stdin = 1;
58 show.out = -1;
59 show.err = 0;
60 show.git_cmd = 1;
61 if (start_command(&show))
62 die("unable to start 'show' for object '%s'",
63 sha1_to_hex(object));
65 /* Open the output as FILE* so strbuf_getline() can be used. */
66 show_out = xfdopen(show.out, "r");
67 if (show_out == NULL)
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);
75 }
76 strbuf_release(&buf);
77 if (fclose(show_out))
78 die_errno("failed to close pipe to 'show' for object '%s'",
79 sha1_to_hex(object));
80 if (finish_command(&show))
81 die("failed to finish 'show' for object '%s'",
82 sha1_to_hex(object));
83 }
85 static void create_note(const unsigned char *object,
86 struct strbuf *buf,
87 int skip_editor,
88 const unsigned char *prev,
89 unsigned char *result)
90 {
91 char *path = NULL;
93 if (!skip_editor) {
94 int fd;
96 /* write the template message before editing: */
97 path = git_pathdup("NOTES_EDITMSG");
98 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
99 if (fd < 0)
100 die_errno("could not create file '%s'", path);
102 if (prev)
103 write_note_data(fd, prev);
104 write_or_die(fd, note_template, strlen(note_template));
106 write_commented_object(fd, object);
108 close(fd);
110 if (launch_editor(path, buf, NULL)) {
111 die("Please supply the note contents using either -m" \
112 " or -F option");
113 }
114 }
116 stripspace(buf, 1);
118 if (!buf->len) {
119 fprintf(stderr, "Removing note for object %s\n",
120 sha1_to_hex(object));
121 hashclr(result);
122 } else {
123 if (write_sha1_file(buf->buf, buf->len, blob_type, result)) {
124 error("unable to write note object");
125 if (path)
126 error("The note contents has been left in %s",
127 path);
128 exit(128);
129 }
130 }
132 if (path) {
133 unlink_or_warn(path);
134 free(path);
135 }
136 }
138 struct msg_arg {
139 int given;
140 struct strbuf buf;
141 };
143 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
144 {
145 struct msg_arg *msg = opt->value;
147 if (!arg)
148 return -1;
149 if (msg->buf.len)
150 strbuf_addstr(&(msg->buf), "\n\n");
151 strbuf_addstr(&(msg->buf), arg);
152 msg->given = 1;
153 return 0;
154 }
156 int commit_notes(struct notes_tree *t, const char *msg)
157 {
158 struct commit_list *parent;
159 unsigned char tree_sha1[20], prev_commit[20], new_commit[20];
160 struct strbuf buf = STRBUF_INIT;
162 if (!t)
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);
181 parent->next = NULL;
182 } else {
183 hashclr(prev_commit);
184 parent = NULL;
185 }
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);
193 return 0;
194 }
196 int cmd_notes(int argc, const char **argv, const char *prefix)
197 {
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;
203 char logmsg[100];
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"),
213 OPT_END()
214 };
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"))
221 edit = 1;
222 else if (argc && !strcmp(argv[0], "show"))
223 show = 1;
224 else if (argc && !strcmp(argv[0], "remove"))
225 remove = 1;
226 else if (argc && !strcmp(argv[0], "prune"))
227 prune = 1;
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);
235 }
237 if (msg.given && msgfile) {
238 error("mixing -m and -F options is not allowed.");
239 usage_with_options(git_notes_usage, options);
240 }
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);
246 }
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/)",
256 argv[0], t->ref);
258 note = get_note(t, object);
260 /* show command */
262 if (show && !note) {
263 error("No note found for object %s.", sha1_to_hex(object));
264 return 1;
265 } else if (show) {
266 const char *show_args[3] = {"show", sha1_to_hex(note), NULL};
267 return execv_git_cmd(show_args);
268 }
270 /* edit/remove/prune command */
272 if (remove)
273 strbuf_reset(&buf);
274 else if (msg.given)
275 strbuf_addbuf(&buf, &(msg.buf));
276 else if (msgfile) {
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);
282 }
284 if (prune) {
285 hashclr(new_note);
286 prune_notes(t);
287 } else {
288 create_note(object, &buf, msg.given || msgfile || remove, note,
289 new_note);
290 if (is_null_sha1(new_note))
291 remove_note(t, object);
292 else
293 add_note(t, object, new_note, combine_notes_overwrite);
294 }
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);
299 free_notes(t);
300 strbuf_release(&buf);
301 return 0;
302 }