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 append [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]",
24 "git notes edit [<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"
43 static int list_each_note(const unsigned char *object_sha1
,
44 const unsigned char *note_sha1
, char *note_path
,
47 printf("%s %s\n", sha1_to_hex(note_sha1
), sha1_to_hex(object_sha1
));
51 static void write_note_data(int fd
, const unsigned char *sha1
)
54 enum object_type type
;
55 char *buf
= read_sha1_file(sha1
, &type
, &size
);
58 write_or_die(fd
, buf
, size
);
63 static void write_commented_object(int fd
, const unsigned char *object
)
65 const char *show_args
[5] =
66 {"show", "--stat", "--no-notes", sha1_to_hex(object
), NULL
};
67 struct child_process show
;
68 struct strbuf buf
= STRBUF_INIT
;
71 /* Invoke "git show --stat --no-notes $object" */
72 memset(&show
, 0, sizeof(show
));
73 show
.argv
= show_args
;
78 if (start_command(&show
))
79 die("unable to start 'show' for object '%s'",
82 /* Open the output as FILE* so strbuf_getline() can be used. */
83 show_out
= xfdopen(show
.out
, "r");
85 die_errno("can't fdopen 'show' output fd");
87 /* Prepend "# " to each output line and write result to 'fd' */
88 while (strbuf_getline(&buf
, show_out
, '\n') != EOF
) {
89 write_or_die(fd
, "# ", 2);
90 write_or_die(fd
, buf
.buf
, buf
.len
);
91 write_or_die(fd
, "\n", 1);
95 die_errno("failed to close pipe to 'show' for object '%s'",
97 if (finish_command(&show
))
98 die("failed to finish 'show' for object '%s'",
102 static void create_note(const unsigned char *object
, struct msg_arg
*msg
,
103 int append_only
, const unsigned char *prev
,
104 unsigned char *result
)
108 if (msg
->use_editor
|| !msg
->given
) {
111 /* write the template message before editing: */
112 path
= git_pathdup("NOTES_EDITMSG");
113 fd
= open(path
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
115 die_errno("could not create file '%s'", path
);
118 write_or_die(fd
, msg
->buf
.buf
, msg
->buf
.len
);
119 else if (prev
&& !append_only
)
120 write_note_data(fd
, prev
);
121 write_or_die(fd
, note_template
, strlen(note_template
));
123 write_commented_object(fd
, object
);
126 strbuf_reset(&(msg
->buf
));
128 if (launch_editor(path
, &(msg
->buf
), NULL
)) {
129 die("Please supply the note contents using either -m" \
132 stripspace(&(msg
->buf
), 1);
135 if (prev
&& append_only
) {
136 /* Append buf to previous note contents */
138 enum object_type type
;
139 char *prev_buf
= read_sha1_file(prev
, &type
, &size
);
141 strbuf_grow(&(msg
->buf
), size
+ 1);
142 if (msg
->buf
.len
&& prev_buf
&& size
)
143 strbuf_insert(&(msg
->buf
), 0, "\n", 1);
144 if (prev_buf
&& size
)
145 strbuf_insert(&(msg
->buf
), 0, prev_buf
, size
);
150 fprintf(stderr
, "Removing note for object %s\n",
151 sha1_to_hex(object
));
154 if (write_sha1_file(msg
->buf
.buf
, msg
->buf
.len
, blob_type
, result
)) {
155 error("unable to write note object");
157 error("The note contents has been left in %s",
164 unlink_or_warn(path
);
169 static int parse_msg_arg(const struct option
*opt
, const char *arg
, int unset
)
171 struct msg_arg
*msg
= opt
->value
;
176 strbuf_grow(&(msg
->buf
), strlen(arg
) + 2);
178 strbuf_addstr(&(msg
->buf
), "\n");
179 strbuf_addstr(&(msg
->buf
), arg
);
180 stripspace(&(msg
->buf
), 0);
186 static int parse_file_arg(const struct option
*opt
, const char *arg
, int unset
)
188 struct msg_arg
*msg
= opt
->value
;
194 strbuf_addstr(&(msg
->buf
), "\n");
195 if (!strcmp(arg
, "-")) {
196 if (strbuf_read(&(msg
->buf
), 0, 1024) < 0)
197 die_errno("cannot read '%s'", arg
);
198 } else if (strbuf_read_file(&(msg
->buf
), arg
, 1024) < 0)
199 die_errno("could not open or read '%s'", arg
);
200 stripspace(&(msg
->buf
), 0);
206 static int parse_reuse_arg(const struct option
*opt
, const char *arg
, int unset
)
208 struct msg_arg
*msg
= opt
->value
;
210 unsigned char object
[20];
211 enum object_type type
;
218 strbuf_addstr(&(msg
->buf
), "\n");
220 if (get_sha1(arg
, object
))
221 die("Failed to resolve '%s' as a valid ref.", arg
);
222 if (!(buf
= read_sha1_file(object
, &type
, &len
)) || !len
) {
224 die("Failed to read object '%s'.", arg
);;
226 strbuf_add(&(msg
->buf
), buf
, len
);
233 static int parse_reedit_arg(const struct option
*opt
, const char *arg
, int unset
)
235 struct msg_arg
*msg
= opt
->value
;
237 return parse_reuse_arg(opt
, arg
, unset
);
240 int commit_notes(struct notes_tree
*t
, const char *msg
)
242 struct commit_list
*parent
;
243 unsigned char tree_sha1
[20], prev_commit
[20], new_commit
[20];
244 struct strbuf buf
= STRBUF_INIT
;
247 t
= &default_notes_tree
;
248 if (!t
->initialized
|| !t
->ref
|| !*t
->ref
)
249 die("Cannot commit uninitialized/unreferenced notes tree");
251 /* Prepare commit message and reflog message */
252 strbuf_addstr(&buf
, "notes: "); /* commit message starts at index 7 */
253 strbuf_addstr(&buf
, msg
);
254 if (buf
.buf
[buf
.len
- 1] != '\n')
255 strbuf_addch(&buf
, '\n'); /* Make sure msg ends with newline */
257 /* Convert notes tree to tree object */
258 if (write_notes_tree(t
, tree_sha1
))
259 die("Failed to write current notes tree to database");
261 /* Create new commit for the tree object */
262 if (!read_ref(t
->ref
, prev_commit
)) { /* retrieve parent commit */
263 parent
= xmalloc(sizeof(*parent
));
264 parent
->item
= lookup_commit(prev_commit
);
267 hashclr(prev_commit
);
270 if (commit_tree(buf
.buf
+ 7, tree_sha1
, parent
, new_commit
, NULL
))
271 die("Failed to commit notes tree to database");
273 /* Update notes ref with new commit */
274 update_ref(buf
.buf
, t
->ref
, new_commit
, prev_commit
, 0, DIE_ON_ERR
);
276 strbuf_release(&buf
);
280 int cmd_notes(int argc
, const char **argv
, const char *prefix
)
282 struct notes_tree
*t
;
283 unsigned char object
[20], new_note
[20];
284 const unsigned char *note
;
285 const char *object_ref
;
288 int list
= 0, add
= 0, append
= 0, edit
= 0, show
= 0, remove
= 0,
289 prune
= 0, force
= 0;
290 int given_object
= 0, i
= 1, retval
= 0;
291 struct msg_arg msg
= { 0, 0, STRBUF_INIT
};
292 struct option options
[] = {
293 OPT_GROUP("Notes options"),
294 OPT_CALLBACK('m', "message", &msg
, "MSG",
295 "note contents as a string", parse_msg_arg
),
296 OPT_CALLBACK('F', "file", &msg
, "FILE",
297 "note contents in a file", parse_file_arg
),
298 OPT_CALLBACK('c', "reedit-message", &msg
, "OBJECT",
299 "reuse and edit specified note object", parse_reedit_arg
),
300 OPT_CALLBACK('C', "reuse-message", &msg
, "OBJECT",
301 "reuse specified note object", parse_reuse_arg
),
302 OPT_BOOLEAN('f', "force", &force
, "replace existing notes"),
306 git_config(git_default_config
, NULL
);
308 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_usage
, 0);
310 if (argc
&& !strcmp(argv
[0], "list"))
312 else if (argc
&& !strcmp(argv
[0], "add"))
314 else if (argc
&& !strcmp(argv
[0], "append"))
316 else if (argc
&& !strcmp(argv
[0], "edit"))
318 else if (argc
&& !strcmp(argv
[0], "show"))
320 else if (argc
&& !strcmp(argv
[0], "remove"))
322 else if (argc
&& !strcmp(argv
[0], "prune"))
325 list
= 1; /* Default to 'list' if no other subcommand given */
329 if (list
+ add
+ append
+ edit
+ show
+ remove
+ prune
!= 1)
330 usage_with_options(git_notes_usage
, options
);
332 if (msg
.given
&& !(add
|| append
|| edit
)) {
333 error("cannot use -m/-F/-c/-C options with %s subcommand.",
335 usage_with_options(git_notes_usage
, options
);
338 if (msg
.given
&& edit
) {
339 fprintf(stderr
, "The -m/-F/-c/-C options have been deprecated "
340 "for the 'edit' subcommand.\n"
341 "Please use 'git notes add -f -m/-F/-c/-C' instead.\n");
345 error("cannot use -f option with %s subcommand.", argv
[0]);
346 usage_with_options(git_notes_usage
, options
);
349 given_object
= argc
> i
;
350 object_ref
= given_object
? argv
[i
++] : "HEAD";
352 if (argc
> i
|| (prune
&& given_object
)) {
353 error("too many parameters");
354 usage_with_options(git_notes_usage
, options
);
357 if (get_sha1(object_ref
, object
))
358 die("Failed to resolve '%s' as a valid ref.", object_ref
);
360 init_notes(NULL
, NULL
, NULL
, 0);
361 t
= &default_notes_tree
;
363 if (prefixcmp(t
->ref
, "refs/notes/"))
364 die("Refusing to %s notes in %s (outside of refs/notes/)",
367 note
= get_note(t
, object
);
374 puts(sha1_to_hex(note
));
378 retval
= for_each_note(t
, 0, list_each_note
, NULL
);
385 if ((list
|| show
) && !note
) {
386 error("No note found for object %s.", sha1_to_hex(object
));
390 const char *show_args
[3] = {"show", sha1_to_hex(note
), NULL
};
391 retval
= execv_git_cmd(show_args
);
395 /* add/append/edit/remove/prune command */
399 error("Cannot add notes. Found existing notes for object"
400 " %s. Use '-f' to overwrite existing notes",
401 sha1_to_hex(object
));
405 fprintf(stderr
, "Overwriting existing notes for object %s\n",
406 sha1_to_hex(object
));
412 strbuf_reset(&(msg
.buf
));
420 create_note(object
, &msg
, append
, note
, new_note
);
422 if (is_null_sha1(new_note
))
423 remove_note(t
, object
);
425 add_note(t
, object
, new_note
, combine_notes_overwrite
);
428 snprintf(logmsg
, sizeof(logmsg
), "Notes %s by 'git notes %s'",
429 is_null_sha1(new_note
) ? "removed" : "added", argv
[0]);
430 commit_notes(t
, logmsg
);
434 strbuf_release(&(msg
.buf
));