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