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> | (-c | -C) <object>] [<object>]",
23 "git notes copy [-f] <from-object> <to-object>",
24 "git notes append [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]",
25 "git notes edit [<object>]",
26 "git notes show [<object>]",
27 "git notes remove [<object>]",
32 static const char note_template
[] =
35 "# Write/edit the notes for the following object:\n"
44 static int list_each_note(const unsigned char *object_sha1
,
45 const unsigned char *note_sha1
, char *note_path
,
48 printf("%s %s\n", sha1_to_hex(note_sha1
), sha1_to_hex(object_sha1
));
52 static void write_note_data(int fd
, const unsigned char *sha1
)
55 enum object_type type
;
56 char *buf
= read_sha1_file(sha1
, &type
, &size
);
59 write_or_die(fd
, buf
, size
);
64 static void write_commented_object(int fd
, const unsigned char *object
)
66 const char *show_args
[5] =
67 {"show", "--stat", "--no-notes", sha1_to_hex(object
), NULL
};
68 struct child_process show
;
69 struct strbuf buf
= STRBUF_INIT
;
72 /* Invoke "git show --stat --no-notes $object" */
73 memset(&show
, 0, sizeof(show
));
74 show
.argv
= show_args
;
79 if (start_command(&show
))
80 die("unable to start 'show' for object '%s'",
83 /* Open the output as FILE* so strbuf_getline() can be used. */
84 show_out
= xfdopen(show
.out
, "r");
86 die_errno("can't fdopen 'show' output fd");
88 /* Prepend "# " to each output line and write result to 'fd' */
89 while (strbuf_getline(&buf
, show_out
, '\n') != EOF
) {
90 write_or_die(fd
, "# ", 2);
91 write_or_die(fd
, buf
.buf
, buf
.len
);
92 write_or_die(fd
, "\n", 1);
96 die_errno("failed to close pipe to 'show' for object '%s'",
98 if (finish_command(&show
))
99 die("failed to finish 'show' for object '%s'",
100 sha1_to_hex(object
));
103 static void create_note(const unsigned char *object
, struct msg_arg
*msg
,
104 int append_only
, const unsigned char *prev
,
105 unsigned char *result
)
109 if (msg
->use_editor
|| !msg
->given
) {
112 /* write the template message before editing: */
113 path
= git_pathdup("NOTES_EDITMSG");
114 fd
= open(path
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
116 die_errno("could not create file '%s'", path
);
119 write_or_die(fd
, msg
->buf
.buf
, msg
->buf
.len
);
120 else if (prev
&& !append_only
)
121 write_note_data(fd
, prev
);
122 write_or_die(fd
, note_template
, strlen(note_template
));
124 write_commented_object(fd
, object
);
127 strbuf_reset(&(msg
->buf
));
129 if (launch_editor(path
, &(msg
->buf
), NULL
)) {
130 die("Please supply the note contents using either -m" \
133 stripspace(&(msg
->buf
), 1);
136 if (prev
&& append_only
) {
137 /* Append buf to previous note contents */
139 enum object_type type
;
140 char *prev_buf
= read_sha1_file(prev
, &type
, &size
);
142 strbuf_grow(&(msg
->buf
), size
+ 1);
143 if (msg
->buf
.len
&& prev_buf
&& size
)
144 strbuf_insert(&(msg
->buf
), 0, "\n", 1);
145 if (prev_buf
&& size
)
146 strbuf_insert(&(msg
->buf
), 0, prev_buf
, size
);
151 fprintf(stderr
, "Removing note for object %s\n",
152 sha1_to_hex(object
));
155 if (write_sha1_file(msg
->buf
.buf
, msg
->buf
.len
, blob_type
, result
)) {
156 error("unable to write note object");
158 error("The note contents has been left in %s",
165 unlink_or_warn(path
);
170 static int parse_msg_arg(const struct option
*opt
, const char *arg
, int unset
)
172 struct msg_arg
*msg
= opt
->value
;
174 strbuf_grow(&(msg
->buf
), strlen(arg
) + 2);
176 strbuf_addch(&(msg
->buf
), '\n');
177 strbuf_addstr(&(msg
->buf
), arg
);
178 stripspace(&(msg
->buf
), 0);
184 static int parse_file_arg(const struct option
*opt
, const char *arg
, int unset
)
186 struct msg_arg
*msg
= opt
->value
;
189 strbuf_addch(&(msg
->buf
), '\n');
190 if (!strcmp(arg
, "-")) {
191 if (strbuf_read(&(msg
->buf
), 0, 1024) < 0)
192 die_errno("cannot read '%s'", arg
);
193 } else if (strbuf_read_file(&(msg
->buf
), arg
, 1024) < 0)
194 die_errno("could not open or read '%s'", arg
);
195 stripspace(&(msg
->buf
), 0);
201 static int parse_reuse_arg(const struct option
*opt
, const char *arg
, int unset
)
203 struct msg_arg
*msg
= opt
->value
;
205 unsigned char object
[20];
206 enum object_type type
;
210 strbuf_addch(&(msg
->buf
), '\n');
212 if (get_sha1(arg
, object
))
213 die("Failed to resolve '%s' as a valid ref.", arg
);
214 if (!(buf
= read_sha1_file(object
, &type
, &len
)) || !len
) {
216 die("Failed to read object '%s'.", arg
);;
218 strbuf_add(&(msg
->buf
), buf
, len
);
225 static int parse_reedit_arg(const struct option
*opt
, const char *arg
, int unset
)
227 struct msg_arg
*msg
= opt
->value
;
229 return parse_reuse_arg(opt
, arg
, unset
);
232 int commit_notes(struct notes_tree
*t
, const char *msg
)
234 struct commit_list
*parent
;
235 unsigned char tree_sha1
[20], prev_commit
[20], new_commit
[20];
236 struct strbuf buf
= STRBUF_INIT
;
239 t
= &default_notes_tree
;
240 if (!t
->initialized
|| !t
->ref
|| !*t
->ref
)
241 die("Cannot commit uninitialized/unreferenced notes tree");
243 /* Prepare commit message and reflog message */
244 strbuf_addstr(&buf
, "notes: "); /* commit message starts at index 7 */
245 strbuf_addstr(&buf
, msg
);
246 if (buf
.buf
[buf
.len
- 1] != '\n')
247 strbuf_addch(&buf
, '\n'); /* Make sure msg ends with newline */
249 /* Convert notes tree to tree object */
250 if (write_notes_tree(t
, tree_sha1
))
251 die("Failed to write current notes tree to database");
253 /* Create new commit for the tree object */
254 if (!read_ref(t
->ref
, prev_commit
)) { /* retrieve parent commit */
255 parent
= xmalloc(sizeof(*parent
));
256 parent
->item
= lookup_commit(prev_commit
);
259 hashclr(prev_commit
);
262 if (commit_tree(buf
.buf
+ 7, tree_sha1
, parent
, new_commit
, NULL
))
263 die("Failed to commit notes tree to database");
265 /* Update notes ref with new commit */
266 update_ref(buf
.buf
, t
->ref
, new_commit
, prev_commit
, 0, DIE_ON_ERR
);
268 strbuf_release(&buf
);
272 int cmd_notes(int argc
, const char **argv
, const char *prefix
)
274 struct notes_tree
*t
;
275 unsigned char object
[20], from_obj
[20], new_note
[20];
276 const unsigned char *note
;
277 const char *object_ref
;
280 int list
= 0, add
= 0, copy
= 0, append
= 0, edit
= 0, show
= 0,
281 remove
= 0, prune
= 0, force
= 0;
282 int given_object
= 0, i
= 1, retval
= 0;
283 struct msg_arg msg
= { 0, 0, STRBUF_INIT
};
284 struct option options
[] = {
285 OPT_GROUP("Notes contents options"),
286 { OPTION_CALLBACK
, 'm', "message", &msg
, "MSG",
287 "note contents as a string", PARSE_OPT_NONEG
,
289 { OPTION_CALLBACK
, 'F', "file", &msg
, "FILE",
290 "note contents in a file", PARSE_OPT_NONEG
,
292 { OPTION_CALLBACK
, 'c', "reedit-message", &msg
, "OBJECT",
293 "reuse and edit specified note object", PARSE_OPT_NONEG
,
295 { OPTION_CALLBACK
, 'C', "reuse-message", &msg
, "OBJECT",
296 "reuse specified note object", PARSE_OPT_NONEG
,
298 OPT_GROUP("Other options"),
299 OPT_BOOLEAN('f', "force", &force
, "replace existing notes"),
303 git_config(git_default_config
, NULL
);
305 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_usage
, 0);
307 if (argc
&& !strcmp(argv
[0], "list"))
309 else if (argc
&& !strcmp(argv
[0], "add"))
311 else if (argc
&& !strcmp(argv
[0], "copy"))
313 else if (argc
&& !strcmp(argv
[0], "append"))
315 else if (argc
&& !strcmp(argv
[0], "edit"))
317 else if (argc
&& !strcmp(argv
[0], "show"))
319 else if (argc
&& !strcmp(argv
[0], "remove"))
321 else if (argc
&& !strcmp(argv
[0], "prune"))
324 list
= 1; /* Default to 'list' if no other subcommand given */
328 if (list
+ add
+ copy
+ append
+ edit
+ show
+ remove
+ prune
!= 1)
329 usage_with_options(git_notes_usage
, options
);
331 if (msg
.given
&& !(add
|| append
|| edit
)) {
332 error("cannot use -m/-F/-c/-C options with %s subcommand.",
334 usage_with_options(git_notes_usage
, options
);
337 if (msg
.given
&& edit
) {
338 fprintf(stderr
, "The -m/-F/-c/-C options have been deprecated "
339 "for the 'edit' subcommand.\n"
340 "Please use 'git notes add -f -m/-F/-c/-C' instead.\n");
343 if (force
&& !(add
|| copy
)) {
344 error("cannot use -f option with %s subcommand.", argv
[0]);
345 usage_with_options(git_notes_usage
, options
);
349 const char *from_ref
;
351 error("too few parameters");
352 usage_with_options(git_notes_usage
, options
);
354 from_ref
= argv
[i
++];
355 if (get_sha1(from_ref
, from_obj
))
356 die("Failed to resolve '%s' as a valid ref.", from_ref
);
359 given_object
= argc
> i
;
360 object_ref
= given_object
? argv
[i
++] : "HEAD";
362 if (argc
> i
|| (prune
&& given_object
)) {
363 error("too many parameters");
364 usage_with_options(git_notes_usage
, options
);
367 if (get_sha1(object_ref
, object
))
368 die("Failed to resolve '%s' as a valid ref.", object_ref
);
370 init_notes(NULL
, NULL
, NULL
, 0);
371 t
= &default_notes_tree
;
373 if (prefixcmp(t
->ref
, "refs/notes/"))
374 die("Refusing to %s notes in %s (outside of refs/notes/)",
377 note
= get_note(t
, object
);
384 puts(sha1_to_hex(note
));
388 retval
= for_each_note(t
, 0, list_each_note
, NULL
);
395 if ((list
|| show
) && !note
) {
396 error("No note found for object %s.", sha1_to_hex(object
));
400 const char *show_args
[3] = {"show", sha1_to_hex(note
), NULL
};
401 retval
= execv_git_cmd(show_args
);
405 /* add/append/edit/remove/prune command */
407 if ((add
|| copy
) && note
) {
409 error("Cannot %s notes. Found existing notes for object"
410 " %s. Use '-f' to overwrite existing notes",
411 argv
[0], sha1_to_hex(object
));
415 fprintf(stderr
, "Overwriting existing notes for object %s\n",
416 sha1_to_hex(object
));
422 strbuf_reset(&(msg
.buf
));
430 const unsigned char *from_note
= get_note(t
, from_obj
);
432 error("Missing notes on source object %s. Cannot copy.",
433 sha1_to_hex(from_obj
));
437 hashcpy(new_note
, from_note
);
439 create_note(object
, &msg
, append
, note
, new_note
);
441 if (is_null_sha1(new_note
))
442 remove_note(t
, object
);
444 add_note(t
, object
, new_note
, combine_notes_overwrite
);
447 snprintf(logmsg
, sizeof(logmsg
), "Notes %s by 'git notes %s'",
448 is_null_sha1(new_note
) ? "removed" : "added", argv
[0]);
449 commit_notes(t
, logmsg
);
453 strbuf_release(&(msg
.buf
));