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 [<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"
42 static int list_each_note(const unsigned char *object_sha1
,
43 const unsigned char *note_sha1
, char *note_path
,
46 printf("%s %s\n", sha1_to_hex(note_sha1
), sha1_to_hex(object_sha1
));
50 static void write_note_data(int fd
, const unsigned char *sha1
)
53 enum object_type type
;
54 char *buf
= read_sha1_file(sha1
, &type
, &size
);
57 write_or_die(fd
, buf
, size
);
62 static void write_commented_object(int fd
, const unsigned char *object
)
64 const char *show_args
[5] =
65 {"show", "--stat", "--no-notes", sha1_to_hex(object
), NULL
};
66 struct child_process show
;
67 struct strbuf buf
= STRBUF_INIT
;
70 /* Invoke "git show --stat --no-notes $object" */
71 memset(&show
, 0, sizeof(show
));
72 show
.argv
= show_args
;
77 if (start_command(&show
))
78 die("unable to start 'show' for object '%s'",
81 /* Open the output as FILE* so strbuf_getline() can be used. */
82 show_out
= xfdopen(show
.out
, "r");
84 die_errno("can't fdopen 'show' output fd");
86 /* Prepend "# " to each output line and write result to 'fd' */
87 while (strbuf_getline(&buf
, show_out
, '\n') != EOF
) {
88 write_or_die(fd
, "# ", 2);
89 write_or_die(fd
, buf
.buf
, buf
.len
);
90 write_or_die(fd
, "\n", 1);
94 die_errno("failed to close pipe to 'show' for object '%s'",
96 if (finish_command(&show
))
97 die("failed to finish 'show' for object '%s'",
101 static void create_note(const unsigned char *object
, struct msg_arg
*msg
,
102 int append_only
, const unsigned char *prev
,
103 unsigned char *result
)
110 /* write the template message before editing: */
111 path
= git_pathdup("NOTES_EDITMSG");
112 fd
= open(path
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
114 die_errno("could not create file '%s'", path
);
116 if (prev
&& !append_only
)
117 write_note_data(fd
, prev
);
118 write_or_die(fd
, note_template
, strlen(note_template
));
120 write_commented_object(fd
, object
);
124 if (launch_editor(path
, &(msg
->buf
), NULL
)) {
125 die("Please supply the note contents using either -m" \
128 stripspace(&(msg
->buf
), 1);
131 if (prev
&& append_only
) {
132 /* Append buf to previous note contents */
134 enum object_type type
;
135 char *prev_buf
= read_sha1_file(prev
, &type
, &size
);
137 strbuf_grow(&(msg
->buf
), size
+ 1);
138 if (msg
->buf
.len
&& prev_buf
&& size
)
139 strbuf_insert(&(msg
->buf
), 0, "\n", 1);
140 if (prev_buf
&& size
)
141 strbuf_insert(&(msg
->buf
), 0, prev_buf
, size
);
146 fprintf(stderr
, "Removing note for object %s\n",
147 sha1_to_hex(object
));
150 if (write_sha1_file(msg
->buf
.buf
, msg
->buf
.len
, blob_type
, result
)) {
151 error("unable to write note object");
153 error("The note contents has been left in %s",
160 unlink_or_warn(path
);
165 static int parse_msg_arg(const struct option
*opt
, const char *arg
, int unset
)
167 struct msg_arg
*msg
= opt
->value
;
172 strbuf_grow(&(msg
->buf
), strlen(arg
) + 2);
174 strbuf_addstr(&(msg
->buf
), "\n");
175 strbuf_addstr(&(msg
->buf
), arg
);
176 stripspace(&(msg
->buf
), 0);
182 static int parse_file_arg(const struct option
*opt
, const char *arg
, int unset
)
184 struct msg_arg
*msg
= opt
->value
;
190 strbuf_addstr(&(msg
->buf
), "\n");
191 if (!strcmp(arg
, "-")) {
192 if (strbuf_read(&(msg
->buf
), 0, 1024) < 0)
193 die_errno("cannot read '%s'", arg
);
194 } else if (strbuf_read_file(&(msg
->buf
), arg
, 1024) < 0)
195 die_errno("could not open or read '%s'", arg
);
196 stripspace(&(msg
->buf
), 0);
202 int commit_notes(struct notes_tree
*t
, const char *msg
)
204 struct commit_list
*parent
;
205 unsigned char tree_sha1
[20], prev_commit
[20], new_commit
[20];
206 struct strbuf buf
= STRBUF_INIT
;
209 t
= &default_notes_tree
;
210 if (!t
->initialized
|| !t
->ref
|| !*t
->ref
)
211 die("Cannot commit uninitialized/unreferenced notes tree");
213 /* Prepare commit message and reflog message */
214 strbuf_addstr(&buf
, "notes: "); /* commit message starts at index 7 */
215 strbuf_addstr(&buf
, msg
);
216 if (buf
.buf
[buf
.len
- 1] != '\n')
217 strbuf_addch(&buf
, '\n'); /* Make sure msg ends with newline */
219 /* Convert notes tree to tree object */
220 if (write_notes_tree(t
, tree_sha1
))
221 die("Failed to write current notes tree to database");
223 /* Create new commit for the tree object */
224 if (!read_ref(t
->ref
, prev_commit
)) { /* retrieve parent commit */
225 parent
= xmalloc(sizeof(*parent
));
226 parent
->item
= lookup_commit(prev_commit
);
229 hashclr(prev_commit
);
232 if (commit_tree(buf
.buf
+ 7, tree_sha1
, parent
, new_commit
, NULL
))
233 die("Failed to commit notes tree to database");
235 /* Update notes ref with new commit */
236 update_ref(buf
.buf
, t
->ref
, new_commit
, prev_commit
, 0, DIE_ON_ERR
);
238 strbuf_release(&buf
);
242 int cmd_notes(int argc
, const char **argv
, const char *prefix
)
244 struct notes_tree
*t
;
245 unsigned char object
[20], new_note
[20];
246 const unsigned char *note
;
247 const char *object_ref
;
250 int list
= 0, add
= 0, append
= 0, edit
= 0, show
= 0, remove
= 0,
251 prune
= 0, force
= 0;
253 struct msg_arg msg
= { 0, STRBUF_INIT
};
254 struct option options
[] = {
255 OPT_GROUP("Notes options"),
256 OPT_CALLBACK('m', "message", &msg
, "MSG",
257 "note contents as a string", parse_msg_arg
),
258 OPT_CALLBACK('F', "file", &msg
, "FILE",
259 "note contents in a file", parse_file_arg
),
260 OPT_BOOLEAN('f', "force", &force
, "replace existing notes"),
264 git_config(git_default_config
, NULL
);
266 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_usage
, 0);
268 if (argc
&& !strcmp(argv
[0], "list"))
270 else if (argc
&& !strcmp(argv
[0], "add"))
272 else if (argc
&& !strcmp(argv
[0], "append"))
274 else if (argc
&& !strcmp(argv
[0], "edit"))
276 else if (argc
&& !strcmp(argv
[0], "show"))
278 else if (argc
&& !strcmp(argv
[0], "remove"))
280 else if (argc
&& !strcmp(argv
[0], "prune"))
283 list
= 1; /* Default to 'list' if no other subcommand given */
285 if (list
+ add
+ append
+ edit
+ show
+ remove
+ prune
!= 1)
286 usage_with_options(git_notes_usage
, options
);
288 if (msg
.given
&& !(add
|| append
|| edit
)) {
289 error("cannot use -m/-F options with %s subcommand.", argv
[0]);
290 usage_with_options(git_notes_usage
, options
);
293 if (msg
.given
&& edit
) {
294 fprintf(stderr
, "The -m and -F options has been deprecated for"
295 " the 'edit' subcommand.\n"
296 "Please use 'git notes add -f -m/-F' instead.\n");
301 error("cannot use -f option with %s subcommand.", argv
[0]);
302 usage_with_options(git_notes_usage
, options
);
305 given_object
= argc
== 2;
306 object_ref
= given_object
? argv
[1] : "HEAD";
307 if (argc
> 2 || (prune
&& argc
> 1)) {
308 error("too many parameters");
309 usage_with_options(git_notes_usage
, options
);
312 if (get_sha1(object_ref
, object
))
313 die("Failed to resolve '%s' as a valid ref.", object_ref
);
315 init_notes(NULL
, NULL
, NULL
, 0);
316 t
= &default_notes_tree
;
318 if (prefixcmp(t
->ref
, "refs/notes/"))
319 die("Refusing to %s notes in %s (outside of refs/notes/)",
322 note
= get_note(t
, object
);
329 puts(sha1_to_hex(note
));
333 return for_each_note(t
, 0, list_each_note
, NULL
);
338 if ((list
|| show
) && !note
) {
339 error("No note found for object %s.", sha1_to_hex(object
));
342 const char *show_args
[3] = {"show", sha1_to_hex(note
), NULL
};
343 return execv_git_cmd(show_args
);
346 /* add/append/edit/remove/prune command */
350 fprintf(stderr
, "Overwriting existing notes for object %s\n",
351 sha1_to_hex(object
));
353 error("Cannot add notes. Found existing notes for object %s. "
354 "Use '-f' to overwrite existing notes",
355 sha1_to_hex(object
));
362 strbuf_reset(&(msg
.buf
));
369 create_note(object
, &msg
, append
, note
, new_note
);
370 if (is_null_sha1(new_note
))
371 remove_note(t
, object
);
373 add_note(t
, object
, new_note
, combine_notes_overwrite
);
375 snprintf(logmsg
, sizeof(logmsg
), "Note %s by 'git notes %s'",
376 is_null_sha1(new_note
) ? "removed" : "added", argv
[0]);
377 commit_notes(t
, logmsg
);
380 strbuf_release(&(msg
.buf
));