Teach builtin-notes to remove empty notes
[alt-git.git] / builtin-notes.c
blob7b4cb1367ee6c68390b11d2d35ef8233d20a7204
1 /*
2 * Builtin "git notes"
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.
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 NULL
26 static const char note_template[] =
27 "\n"
28 "#\n"
29 "# Write/edit the notes for the following object:\n"
30 "#\n";
32 static void write_note_data(int fd, const unsigned char *sha1)
34 unsigned long size;
35 enum object_type type;
36 char *buf = read_sha1_file(sha1, &type, &size);
37 if (buf) {
38 if (size)
39 write_or_die(fd, buf, size);
40 free(buf);
44 static void write_commented_object(int fd, const unsigned char *object)
46 const char *show_args[5] =
47 {"show", "--stat", "--no-notes", sha1_to_hex(object), NULL};
48 struct child_process show;
49 struct strbuf buf = STRBUF_INIT;
50 FILE *show_out;
52 /* Invoke "git show --stat --no-notes $object" */
53 memset(&show, 0, sizeof(show));
54 show.argv = show_args;
55 show.no_stdin = 1;
56 show.out = -1;
57 show.err = 0;
58 show.git_cmd = 1;
59 if (start_command(&show))
60 die("unable to start 'show' for object '%s'",
61 sha1_to_hex(object));
63 /* Open the output as FILE* so strbuf_getline() can be used. */
64 show_out = xfdopen(show.out, "r");
65 if (show_out == NULL)
66 die_errno("can't fdopen 'show' output fd");
68 /* Prepend "# " to each output line and write result to 'fd' */
69 while (strbuf_getline(&buf, show_out, '\n') != EOF) {
70 write_or_die(fd, "# ", 2);
71 write_or_die(fd, buf.buf, buf.len);
72 write_or_die(fd, "\n", 1);
74 strbuf_release(&buf);
75 if (fclose(show_out))
76 die_errno("failed to close pipe to 'show' for object '%s'",
77 sha1_to_hex(object));
78 if (finish_command(&show))
79 die("failed to finish 'show' for object '%s'",
80 sha1_to_hex(object));
83 static void create_note(const unsigned char *object,
84 struct strbuf *buf,
85 int skip_editor,
86 const unsigned char *prev,
87 unsigned char *result)
89 char *path = NULL;
91 if (!skip_editor) {
92 int fd;
94 /* write the template message before editing: */
95 path = git_pathdup("NOTES_EDITMSG");
96 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
97 if (fd < 0)
98 die_errno("could not create file '%s'", path);
100 if (prev)
101 write_note_data(fd, prev);
102 write_or_die(fd, note_template, strlen(note_template));
104 write_commented_object(fd, object);
106 close(fd);
108 if (launch_editor(path, buf, NULL)) {
109 die("Please supply the note contents using either -m" \
110 " or -F option");
114 stripspace(buf, 1);
116 if (!buf->len) {
117 fprintf(stderr, "Removing note for object %s\n",
118 sha1_to_hex(object));
119 hashclr(result);
120 } else {
121 if (write_sha1_file(buf->buf, buf->len, blob_type, result)) {
122 error("unable to write note object");
123 if (path)
124 error("The note contents has been left in %s",
125 path);
126 exit(128);
130 if (path) {
131 unlink_or_warn(path);
132 free(path);
136 struct msg_arg {
137 int given;
138 struct strbuf buf;
141 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
143 struct msg_arg *msg = opt->value;
145 if (!arg)
146 return -1;
147 if (msg->buf.len)
148 strbuf_addstr(&(msg->buf), "\n\n");
149 strbuf_addstr(&(msg->buf), arg);
150 msg->given = 1;
151 return 0;
154 int commit_notes(struct notes_tree *t, const char *msg)
156 struct commit_list *parent;
157 unsigned char tree_sha1[20], prev_commit[20], new_commit[20];
158 struct strbuf buf = STRBUF_INIT;
160 if (!t)
161 t = &default_notes_tree;
162 if (!t->initialized || !t->ref || !*t->ref)
163 die("Cannot commit uninitialized/unreferenced notes tree");
165 /* Prepare commit message and reflog message */
166 strbuf_addstr(&buf, "notes: "); /* commit message starts at index 7 */
167 strbuf_addstr(&buf, msg);
168 if (buf.buf[buf.len - 1] != '\n')
169 strbuf_addch(&buf, '\n'); /* Make sure msg ends with newline */
171 /* Convert notes tree to tree object */
172 if (write_notes_tree(t, tree_sha1))
173 die("Failed to write current notes tree to database");
175 /* Create new commit for the tree object */
176 if (!read_ref(t->ref, prev_commit)) { /* retrieve parent commit */
177 parent = xmalloc(sizeof(*parent));
178 parent->item = lookup_commit(prev_commit);
179 parent->next = NULL;
180 } else {
181 hashclr(prev_commit);
182 parent = NULL;
184 if (commit_tree(buf.buf + 7, tree_sha1, parent, new_commit, NULL))
185 die("Failed to commit notes tree to database");
187 /* Update notes ref with new commit */
188 update_ref(buf.buf, t->ref, new_commit, prev_commit, 0, DIE_ON_ERR);
190 strbuf_release(&buf);
191 return 0;
194 int cmd_notes(int argc, const char **argv, const char *prefix)
196 struct strbuf buf = STRBUF_INIT;
197 struct notes_tree *t;
198 unsigned char object[20], new_note[20];
199 const unsigned char *note;
200 const char *object_ref, *logmsg;
202 int edit = 0, show = 0;
203 const char *msgfile = NULL;
204 struct msg_arg msg = { 0, STRBUF_INIT };
205 struct option options[] = {
206 OPT_GROUP("Notes edit options"),
207 OPT_CALLBACK('m', NULL, &msg, "msg",
208 "note contents as a string", parse_msg_arg),
209 OPT_FILENAME('F', NULL, &msgfile, "note contents in a file"),
210 OPT_END()
213 git_config(git_default_config, NULL);
215 argc = parse_options(argc, argv, prefix, options, git_notes_usage, 0);
217 if (argc && !strcmp(argv[0], "edit"))
218 edit = 1;
219 else if (argc && !strcmp(argv[0], "show"))
220 show = 1;
222 if (edit + show != 1)
223 usage_with_options(git_notes_usage, options);
225 object_ref = argc == 2 ? argv[1] : "HEAD";
226 if (argc > 2) {
227 error("too many parameters");
228 usage_with_options(git_notes_usage, options);
231 if (get_sha1(object_ref, object))
232 die("Failed to resolve '%s' as a valid ref.", object_ref);
234 init_notes(NULL, NULL, NULL, 0);
235 t = &default_notes_tree;
237 if (prefixcmp(t->ref, "refs/notes/"))
238 die("Refusing to %s notes in %s (outside of refs/notes/)",
239 edit ? "edit" : "show", t->ref);
241 note = get_note(t, object);
243 /* show command */
245 if (show && !note) {
246 error("No note found for object %s.", sha1_to_hex(object));
247 return 1;
248 } else if (show) {
249 const char *show_args[3] = {"show", sha1_to_hex(note), NULL};
250 return execv_git_cmd(show_args);
253 /* edit command */
255 if (msg.given || msgfile) {
256 if (msg.given && msgfile) {
257 error("mixing -m and -F options is not allowed.");
258 usage_with_options(git_notes_usage, options);
260 if (msg.given)
261 strbuf_addbuf(&buf, &(msg.buf));
262 else {
263 if (!strcmp(msgfile, "-")) {
264 if (strbuf_read(&buf, 0, 1024) < 0)
265 die_errno("cannot read '%s'", msgfile);
266 } else {
267 if (strbuf_read_file(&buf, msgfile, 1024) < 0)
268 die_errno("could not open or read '%s'",
269 msgfile);
274 create_note(object, &buf, msg.given || msgfile, note, new_note);
275 if (is_null_sha1(new_note)) {
276 remove_note(t, object);
277 logmsg = "Note removed by 'git notes edit'";
278 } else {
279 add_note(t, object, new_note, combine_notes_overwrite);
280 logmsg = "Note added by 'git notes edit'";
282 commit_notes(t, logmsg);
284 free_notes(t);
285 strbuf_release(&buf);
286 return 0;