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