builtin-notes: Deprecate the -m/-F options for "git notes edit"
[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>]",
2347fae5 23 "git notes append [-m <msg> | -F <file>] [<object>]",
aaec9bcf 24 "git notes edit [<object>]",
cd067d3b 25 "git notes show [<object>]",
92b3385f 26 "git notes remove [<object>]",
d6576e1f 27 "git notes prune",
cd067d3b
JH
28 NULL
29};
30
31static const char note_template[] =
32 "\n"
33 "#\n"
34 "# Write/edit the notes for the following object:\n"
35 "#\n";
36
e397421a
JH
37static int list_each_note(const unsigned char *object_sha1,
38 const unsigned char *note_sha1, char *note_path,
39 void *cb_data)
40{
41 printf("%s %s\n", sha1_to_hex(note_sha1), sha1_to_hex(object_sha1));
42 return 0;
43}
44
cd067d3b
JH
45static void write_note_data(int fd, const unsigned char *sha1)
46{
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);
54 }
55}
56
57static void write_commented_object(int fd, const unsigned char *object)
58{
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;
64
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));
75
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");
80
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);
86 }
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));
94}
95
96static void create_note(const unsigned char *object,
97 struct strbuf *buf,
2347fae5 98 int skip_editor, int append_only,
cd067d3b
JH
99 const unsigned char *prev,
100 unsigned char *result)
101{
102 char *path = NULL;
103
104 if (!skip_editor) {
105 int fd;
106
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);
112
2347fae5 113 if (prev && !append_only)
cd067d3b
JH
114 write_note_data(fd, prev);
115 write_or_die(fd, note_template, strlen(note_template));
116
117 write_commented_object(fd, object);
118
119 close(fd);
120
121 if (launch_editor(path, buf, NULL)) {
122 die("Please supply the note contents using either -m" \
123 " or -F option");
124 }
125 }
126
127 stripspace(buf, 1);
128
2347fae5
JH
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);
134
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);
141 }
142
a0b4dfa9 143 if (!buf->len) {
cd067d3b
JH
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);
154 }
155 }
156
157 if (path) {
158 unlink_or_warn(path);
159 free(path);
160 }
161}
162
163struct msg_arg {
164 int given;
165 struct strbuf buf;
166};
167
168static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
169{
170 struct msg_arg *msg = opt->value;
171
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;
179}
180
181int commit_notes(struct notes_tree *t, const char *msg)
182{
183 struct commit_list *parent;
184 unsigned char tree_sha1[20], prev_commit[20], new_commit[20];
185 struct strbuf buf = STRBUF_INIT;
186
187 if (!t)
188 t = &default_notes_tree;
189 if (!t->initialized || !t->ref || !*t->ref)
190 die("Cannot commit uninitialized/unreferenced notes tree");
191
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 */
197
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");
201
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;
210 }
211 if (commit_tree(buf.buf + 7, tree_sha1, parent, new_commit, NULL))
212 die("Failed to commit notes tree to database");
213
214 /* Update notes ref with new commit */
215 update_ref(buf.buf, t->ref, new_commit, prev_commit, 0, DIE_ON_ERR);
216
217 strbuf_release(&buf);
218 return 0;
219}
220
221int cmd_notes(int argc, const char **argv, const char *prefix)
222{
223 struct strbuf buf = STRBUF_INIT;
224 struct notes_tree *t;
225 unsigned char object[20], new_note[20];
226 const unsigned char *note;
92b3385f
JH
227 const char *object_ref;
228 char logmsg[100];
a0b4dfa9 229
2347fae5
JH
230 int list = 0, add = 0, append = 0, edit = 0, show = 0, remove = 0,
231 prune = 0, force = 0;
e397421a 232 int given_object;
cd067d3b
JH
233 const char *msgfile = NULL;
234 struct msg_arg msg = { 0, STRBUF_INIT };
235 struct option options[] = {
aaec9bcf 236 OPT_GROUP("Notes options"),
ba20f15e 237 OPT_CALLBACK('m', "message", &msg, "msg",
cd067d3b 238 "note contents as a string", parse_msg_arg),
ba20f15e 239 OPT_FILENAME('F', "file", &msgfile, "note contents in a file"),
7aa4754e 240 OPT_BOOLEAN('f', "force", &force, "replace existing notes"),
cd067d3b
JH
241 OPT_END()
242 };
243
244 git_config(git_default_config, NULL);
245
246 argc = parse_options(argc, argv, prefix, options, git_notes_usage, 0);
247
e397421a
JH
248 if (argc && !strcmp(argv[0], "list"))
249 list = 1;
7aa4754e
JH
250 else if (argc && !strcmp(argv[0], "add"))
251 add = 1;
2347fae5
JH
252 else if (argc && !strcmp(argv[0], "append"))
253 append = 1;
e397421a 254 else if (argc && !strcmp(argv[0], "edit"))
cd067d3b
JH
255 edit = 1;
256 else if (argc && !strcmp(argv[0], "show"))
257 show = 1;
92b3385f
JH
258 else if (argc && !strcmp(argv[0], "remove"))
259 remove = 1;
d6576e1f
JH
260 else if (argc && !strcmp(argv[0], "prune"))
261 prune = 1;
e397421a
JH
262 else if (!argc)
263 list = 1; /* Default to 'list' if no other subcommand given */
cd067d3b 264
2347fae5 265 if (list + add + append + edit + show + remove + prune != 1)
cd067d3b
JH
266 usage_with_options(git_notes_usage, options);
267
2347fae5 268 if ((msg.given || msgfile) && !(add || append || edit)) {
92b3385f
JH
269 error("cannot use -m/-F options with %s subcommand.", argv[0]);
270 usage_with_options(git_notes_usage, options);
271 }
272
aaec9bcf
JH
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");
277 }
278
92b3385f
JH
279 if (msg.given && msgfile) {
280 error("mixing -m and -F options is not allowed.");
281 usage_with_options(git_notes_usage, options);
282 }
283
7aa4754e
JH
284 if (force && !add) {
285 error("cannot use -f option with %s subcommand.", argv[0]);
286 usage_with_options(git_notes_usage, options);
287 }
288
e397421a
JH
289 given_object = argc == 2;
290 object_ref = given_object ? argv[1] : "HEAD";
d6576e1f 291 if (argc > 2 || (prune && argc > 1)) {
cd067d3b
JH
292 error("too many parameters");
293 usage_with_options(git_notes_usage, options);
294 }
295
296 if (get_sha1(object_ref, object))
297 die("Failed to resolve '%s' as a valid ref.", object_ref);
298
299 init_notes(NULL, NULL, NULL, 0);
300 t = &default_notes_tree;
301
302 if (prefixcmp(t->ref, "refs/notes/"))
303 die("Refusing to %s notes in %s (outside of refs/notes/)",
92b3385f 304 argv[0], t->ref);
cd067d3b
JH
305
306 note = get_note(t, object);
307
e397421a
JH
308 /* list command */
309
310 if (list) {
311 if (given_object) {
312 if (note) {
313 puts(sha1_to_hex(note));
314 return 0;
315 }
316 } else
317 return for_each_note(t, 0, list_each_note, NULL);
318 }
319
cd067d3b
JH
320 /* show command */
321
e397421a 322 if ((list || show) && !note) {
cd067d3b
JH
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);
328 }
329
2347fae5 330 /* add/append/edit/remove/prune command */
7aa4754e
JH
331
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;
341 }
342 }
92b3385f
JH
343
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);
cd067d3b
JH
354 }
355
d6576e1f
JH
356 if (prune) {
357 hashclr(new_note);
358 prune_notes(t);
359 } else {
2347fae5
JH
360 create_note(object, &buf, msg.given || msgfile || remove,
361 append, note, new_note);
d6576e1f
JH
362 if (is_null_sha1(new_note))
363 remove_note(t, object);
364 else
365 add_note(t, object, new_note, combine_notes_overwrite);
366 }
92b3385f
JH
367 snprintf(logmsg, sizeof(logmsg), "Note %s by 'git notes %s'",
368 is_null_sha1(new_note) ? "removed" : "added", argv[0]);
a0b4dfa9 369 commit_notes(t, logmsg);
cd067d3b
JH
370
371 free_notes(t);
372 strbuf_release(&buf);
373 return 0;
374}