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 edit [-m <msg> | -F <file>] [<object>]",
24 "git notes show [<object>]",
25 "git notes remove [<object>]",
30 static const char note_template
[] =
33 "# Write/edit the notes for the following object:\n"
36 static int list_each_note(const unsigned char *object_sha1
,
37 const unsigned char *note_sha1
, char *note_path
,
40 printf("%s %s\n", sha1_to_hex(note_sha1
), sha1_to_hex(object_sha1
));
44 static void write_note_data(int fd
, const unsigned char *sha1
)
47 enum object_type type
;
48 char *buf
= read_sha1_file(sha1
, &type
, &size
);
51 write_or_die(fd
, buf
, size
);
56 static void write_commented_object(int fd
, const unsigned char *object
)
58 const char *show_args
[5] =
59 {"show", "--stat", "--no-notes", sha1_to_hex(object
), NULL
};
60 struct child_process show
;
61 struct strbuf buf
= STRBUF_INIT
;
64 /* Invoke "git show --stat --no-notes $object" */
65 memset(&show
, 0, sizeof(show
));
66 show
.argv
= show_args
;
71 if (start_command(&show
))
72 die("unable to start 'show' for object '%s'",
75 /* Open the output as FILE* so strbuf_getline() can be used. */
76 show_out
= xfdopen(show
.out
, "r");
78 die_errno("can't fdopen 'show' output fd");
80 /* Prepend "# " to each output line and write result to 'fd' */
81 while (strbuf_getline(&buf
, show_out
, '\n') != EOF
) {
82 write_or_die(fd
, "# ", 2);
83 write_or_die(fd
, buf
.buf
, buf
.len
);
84 write_or_die(fd
, "\n", 1);
88 die_errno("failed to close pipe to 'show' for object '%s'",
90 if (finish_command(&show
))
91 die("failed to finish 'show' for object '%s'",
95 static void create_note(const unsigned char *object
,
98 const unsigned char *prev
,
99 unsigned char *result
)
106 /* write the template message before editing: */
107 path
= git_pathdup("NOTES_EDITMSG");
108 fd
= open(path
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
110 die_errno("could not create file '%s'", path
);
113 write_note_data(fd
, prev
);
114 write_or_die(fd
, note_template
, strlen(note_template
));
116 write_commented_object(fd
, object
);
120 if (launch_editor(path
, buf
, NULL
)) {
121 die("Please supply the note contents using either -m" \
129 fprintf(stderr
, "Removing note for object %s\n",
130 sha1_to_hex(object
));
133 if (write_sha1_file(buf
->buf
, buf
->len
, blob_type
, result
)) {
134 error("unable to write note object");
136 error("The note contents has been left in %s",
143 unlink_or_warn(path
);
153 static int parse_msg_arg(const struct option
*opt
, const char *arg
, int unset
)
155 struct msg_arg
*msg
= opt
->value
;
160 strbuf_addstr(&(msg
->buf
), "\n\n");
161 strbuf_addstr(&(msg
->buf
), arg
);
166 int commit_notes(struct notes_tree
*t
, const char *msg
)
168 struct commit_list
*parent
;
169 unsigned char tree_sha1
[20], prev_commit
[20], new_commit
[20];
170 struct strbuf buf
= STRBUF_INIT
;
173 t
= &default_notes_tree
;
174 if (!t
->initialized
|| !t
->ref
|| !*t
->ref
)
175 die("Cannot commit uninitialized/unreferenced notes tree");
177 /* Prepare commit message and reflog message */
178 strbuf_addstr(&buf
, "notes: "); /* commit message starts at index 7 */
179 strbuf_addstr(&buf
, msg
);
180 if (buf
.buf
[buf
.len
- 1] != '\n')
181 strbuf_addch(&buf
, '\n'); /* Make sure msg ends with newline */
183 /* Convert notes tree to tree object */
184 if (write_notes_tree(t
, tree_sha1
))
185 die("Failed to write current notes tree to database");
187 /* Create new commit for the tree object */
188 if (!read_ref(t
->ref
, prev_commit
)) { /* retrieve parent commit */
189 parent
= xmalloc(sizeof(*parent
));
190 parent
->item
= lookup_commit(prev_commit
);
193 hashclr(prev_commit
);
196 if (commit_tree(buf
.buf
+ 7, tree_sha1
, parent
, new_commit
, NULL
))
197 die("Failed to commit notes tree to database");
199 /* Update notes ref with new commit */
200 update_ref(buf
.buf
, t
->ref
, new_commit
, prev_commit
, 0, DIE_ON_ERR
);
202 strbuf_release(&buf
);
206 int cmd_notes(int argc
, const char **argv
, const char *prefix
)
208 struct strbuf buf
= STRBUF_INIT
;
209 struct notes_tree
*t
;
210 unsigned char object
[20], new_note
[20];
211 const unsigned char *note
;
212 const char *object_ref
;
215 int list
= 0, add
= 0, edit
= 0, show
= 0, remove
= 0, prune
= 0,
218 const char *msgfile
= NULL
;
219 struct msg_arg msg
= { 0, STRBUF_INIT
};
220 struct option options
[] = {
221 OPT_GROUP("Notes edit options"),
222 OPT_CALLBACK('m', "message", &msg
, "msg",
223 "note contents as a string", parse_msg_arg
),
224 OPT_FILENAME('F', "file", &msgfile
, "note contents in a file"),
225 OPT_BOOLEAN('f', "force", &force
, "replace existing notes"),
229 git_config(git_default_config
, NULL
);
231 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_usage
, 0);
233 if (argc
&& !strcmp(argv
[0], "list"))
235 else if (argc
&& !strcmp(argv
[0], "add"))
237 else if (argc
&& !strcmp(argv
[0], "edit"))
239 else if (argc
&& !strcmp(argv
[0], "show"))
241 else if (argc
&& !strcmp(argv
[0], "remove"))
243 else if (argc
&& !strcmp(argv
[0], "prune"))
246 list
= 1; /* Default to 'list' if no other subcommand given */
248 if (list
+ add
+ edit
+ show
+ remove
+ prune
!= 1)
249 usage_with_options(git_notes_usage
, options
);
251 if ((msg
.given
|| msgfile
) && !(add
|| edit
)) {
252 error("cannot use -m/-F options with %s subcommand.", argv
[0]);
253 usage_with_options(git_notes_usage
, options
);
256 if (msg
.given
&& msgfile
) {
257 error("mixing -m and -F options is not allowed.");
258 usage_with_options(git_notes_usage
, options
);
262 error("cannot use -f option with %s subcommand.", argv
[0]);
263 usage_with_options(git_notes_usage
, options
);
266 given_object
= argc
== 2;
267 object_ref
= given_object
? argv
[1] : "HEAD";
268 if (argc
> 2 || (prune
&& argc
> 1)) {
269 error("too many parameters");
270 usage_with_options(git_notes_usage
, options
);
273 if (get_sha1(object_ref
, object
))
274 die("Failed to resolve '%s' as a valid ref.", object_ref
);
276 init_notes(NULL
, NULL
, NULL
, 0);
277 t
= &default_notes_tree
;
279 if (prefixcmp(t
->ref
, "refs/notes/"))
280 die("Refusing to %s notes in %s (outside of refs/notes/)",
283 note
= get_note(t
, object
);
290 puts(sha1_to_hex(note
));
294 return for_each_note(t
, 0, list_each_note
, NULL
);
299 if ((list
|| show
) && !note
) {
300 error("No note found for object %s.", sha1_to_hex(object
));
303 const char *show_args
[3] = {"show", sha1_to_hex(note
), NULL
};
304 return execv_git_cmd(show_args
);
307 /* add/edit/remove/prune command */
311 fprintf(stderr
, "Overwriting existing notes for object %s\n",
312 sha1_to_hex(object
));
314 error("Cannot add notes. Found existing notes for object %s. "
315 "Use '-f' to overwrite existing notes",
316 sha1_to_hex(object
));
324 strbuf_addbuf(&buf
, &(msg
.buf
));
326 if (!strcmp(msgfile
, "-")) {
327 if (strbuf_read(&buf
, 0, 1024) < 0)
328 die_errno("cannot read '%s'", msgfile
);
329 } else if (strbuf_read_file(&buf
, msgfile
, 1024) < 0)
330 die_errno("could not open or read '%s'", msgfile
);
337 create_note(object
, &buf
, msg
.given
|| msgfile
|| remove
, note
,
339 if (is_null_sha1(new_note
))
340 remove_note(t
, object
);
342 add_note(t
, object
, new_note
, combine_notes_overwrite
);
344 snprintf(logmsg
, sizeof(logmsg
), "Note %s by 'git notes %s'",
345 is_null_sha1(new_note
) ? "removed" : "added", argv
[0]);
346 commit_notes(t
, logmsg
);
349 strbuf_release(&buf
);