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.
17 #include "run-command.h"
18 #include "parse-options.h"
20 static const char * const git_notes_usage
[] = {
21 "git notes [list [<object>]]",
22 "git notes add [-f] [-m <msg> | -F <file>] [<object>]",
23 "git notes append [-m <msg> | -F <file>] [<object>]",
24 "git notes edit [-m <msg> | -F <file>] [<object>]",
25 "git notes show [<object>]",
26 "git notes remove [<object>]",
31 static const char note_template
[] =
34 "# Write/edit the notes for the following object:\n"
37 static int list_each_note(const unsigned char *object_sha1
,
38 const unsigned char *note_sha1
, char *note_path
,
41 printf("%s %s\n", sha1_to_hex(note_sha1
), sha1_to_hex(object_sha1
));
45 static void write_note_data(int fd
, const unsigned char *sha1
)
48 enum object_type type
;
49 char *buf
= read_sha1_file(sha1
, &type
, &size
);
52 write_or_die(fd
, buf
, size
);
57 static void write_commented_object(int fd
, const unsigned char *object
)
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
;
65 /* Invoke "git show --stat --no-notes $object" */
66 memset(&show
, 0, sizeof(show
));
67 show
.argv
= show_args
;
72 if (start_command(&show
))
73 die("unable to start 'show' for object '%s'",
76 /* Open the output as FILE* so strbuf_getline() can be used. */
77 show_out
= xfdopen(show
.out
, "r");
79 die_errno("can't fdopen 'show' output fd");
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);
89 die_errno("failed to close pipe to 'show' for object '%s'",
91 if (finish_command(&show
))
92 die("failed to finish 'show' for object '%s'",
96 static void create_note(const unsigned char *object
,
98 int skip_editor
, int append_only
,
99 const unsigned char *prev
,
100 unsigned char *result
)
107 /* write the template message before editing: */
108 path
= git_pathdup("NOTES_EDITMSG");
109 fd
= open(path
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
111 die_errno("could not create file '%s'", path
);
113 if (prev
&& !append_only
)
114 write_note_data(fd
, prev
);
115 write_or_die(fd
, note_template
, strlen(note_template
));
117 write_commented_object(fd
, object
);
121 if (launch_editor(path
, buf
, NULL
)) {
122 die("Please supply the note contents using either -m" \
129 if (prev
&& append_only
) {
130 /* Append buf to previous note contents */
132 enum object_type type
;
133 char *prev_buf
= read_sha1_file(prev
, &type
, &size
);
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
);
144 fprintf(stderr
, "Removing note for object %s\n",
145 sha1_to_hex(object
));
148 if (write_sha1_file(buf
->buf
, buf
->len
, blob_type
, result
)) {
149 error("unable to write note object");
151 error("The note contents has been left in %s",
158 unlink_or_warn(path
);
168 static int parse_msg_arg(const struct option
*opt
, const char *arg
, int unset
)
170 struct msg_arg
*msg
= opt
->value
;
175 strbuf_addstr(&(msg
->buf
), "\n\n");
176 strbuf_addstr(&(msg
->buf
), arg
);
181 int commit_notes(struct notes_tree
*t
, const char *msg
)
183 struct commit_list
*parent
;
184 unsigned char tree_sha1
[20], prev_commit
[20], new_commit
[20];
185 struct strbuf buf
= STRBUF_INIT
;
188 t
= &default_notes_tree
;
189 if (!t
->initialized
|| !t
->ref
|| !*t
->ref
)
190 die("Cannot commit uninitialized/unreferenced notes tree");
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 */
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");
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
);
208 hashclr(prev_commit
);
211 if (commit_tree(buf
.buf
+ 7, tree_sha1
, parent
, new_commit
, NULL
))
212 die("Failed to commit notes tree to database");
214 /* Update notes ref with new commit */
215 update_ref(buf
.buf
, t
->ref
, new_commit
, prev_commit
, 0, DIE_ON_ERR
);
217 strbuf_release(&buf
);
221 int cmd_notes(int argc
, const char **argv
, const char *prefix
)
223 struct strbuf buf
= STRBUF_INIT
;
224 struct notes_tree
*t
;
225 unsigned char object
[20], new_note
[20];
226 const unsigned char *note
;
227 const char *object_ref
;
230 int list
= 0, add
= 0, append
= 0, edit
= 0, show
= 0, remove
= 0,
231 prune
= 0, force
= 0;
233 const char *msgfile
= NULL
;
234 struct msg_arg msg
= { 0, STRBUF_INIT
};
235 struct option options
[] = {
236 OPT_GROUP("Notes edit options"),
237 OPT_CALLBACK('m', "message", &msg
, "msg",
238 "note contents as a string", parse_msg_arg
),
239 OPT_FILENAME('F', "file", &msgfile
, "note contents in a file"),
240 OPT_BOOLEAN('f', "force", &force
, "replace existing notes"),
244 git_config(git_default_config
, NULL
);
246 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_usage
, 0);
248 if (argc
&& !strcmp(argv
[0], "list"))
250 else if (argc
&& !strcmp(argv
[0], "add"))
252 else if (argc
&& !strcmp(argv
[0], "append"))
254 else if (argc
&& !strcmp(argv
[0], "edit"))
256 else if (argc
&& !strcmp(argv
[0], "show"))
258 else if (argc
&& !strcmp(argv
[0], "remove"))
260 else if (argc
&& !strcmp(argv
[0], "prune"))
263 list
= 1; /* Default to 'list' if no other subcommand given */
265 if (list
+ add
+ append
+ edit
+ show
+ remove
+ prune
!= 1)
266 usage_with_options(git_notes_usage
, options
);
268 if ((msg
.given
|| msgfile
) && !(add
|| append
|| edit
)) {
269 error("cannot use -m/-F options with %s subcommand.", argv
[0]);
270 usage_with_options(git_notes_usage
, options
);
273 if (msg
.given
&& msgfile
) {
274 error("mixing -m and -F options is not allowed.");
275 usage_with_options(git_notes_usage
, options
);
279 error("cannot use -f option with %s subcommand.", argv
[0]);
280 usage_with_options(git_notes_usage
, options
);
283 given_object
= argc
== 2;
284 object_ref
= given_object
? argv
[1] : "HEAD";
285 if (argc
> 2 || (prune
&& argc
> 1)) {
286 error("too many parameters");
287 usage_with_options(git_notes_usage
, options
);
290 if (get_sha1(object_ref
, object
))
291 die("Failed to resolve '%s' as a valid ref.", object_ref
);
293 init_notes(NULL
, NULL
, NULL
, 0);
294 t
= &default_notes_tree
;
296 if (prefixcmp(t
->ref
, "refs/notes/"))
297 die("Refusing to %s notes in %s (outside of refs/notes/)",
300 note
= get_note(t
, object
);
307 puts(sha1_to_hex(note
));
311 return for_each_note(t
, 0, list_each_note
, NULL
);
316 if ((list
|| show
) && !note
) {
317 error("No note found for object %s.", sha1_to_hex(object
));
320 const char *show_args
[3] = {"show", sha1_to_hex(note
), NULL
};
321 return execv_git_cmd(show_args
);
324 /* add/append/edit/remove/prune command */
328 fprintf(stderr
, "Overwriting existing notes for object %s\n",
329 sha1_to_hex(object
));
331 error("Cannot add notes. Found existing notes for object %s. "
332 "Use '-f' to overwrite existing notes",
333 sha1_to_hex(object
));
341 strbuf_addbuf(&buf
, &(msg
.buf
));
343 if (!strcmp(msgfile
, "-")) {
344 if (strbuf_read(&buf
, 0, 1024) < 0)
345 die_errno("cannot read '%s'", msgfile
);
346 } else if (strbuf_read_file(&buf
, msgfile
, 1024) < 0)
347 die_errno("could not open or read '%s'", msgfile
);
354 create_note(object
, &buf
, msg
.given
|| msgfile
|| remove
,
355 append
, note
, new_note
);
356 if (is_null_sha1(new_note
))
357 remove_note(t
, object
);
359 add_note(t
, object
, new_note
, combine_notes_overwrite
);
361 snprintf(logmsg
, sizeof(logmsg
), "Note %s by 'git notes %s'",
362 is_null_sha1(new_note
) ? "removed" : "added", argv
[0]);
363 commit_notes(t
, logmsg
);
366 strbuf_release(&buf
);