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 edit [-m <msg> | -F <file>] [<object>]",
23 "git notes show [<object>]",
24 "git notes remove [<object>]",
29 static const char note_template
[] =
32 "# Write/edit the notes for the following object:\n"
35 static int list_each_note(const unsigned char *object_sha1
,
36 const unsigned char *note_sha1
, char *note_path
,
39 printf("%s %s\n", sha1_to_hex(note_sha1
), sha1_to_hex(object_sha1
));
43 static void write_note_data(int fd
, const unsigned char *sha1
)
46 enum object_type type
;
47 char *buf
= read_sha1_file(sha1
, &type
, &size
);
50 write_or_die(fd
, buf
, size
);
55 static void write_commented_object(int fd
, const unsigned char *object
)
57 const char *show_args
[5] =
58 {"show", "--stat", "--no-notes", sha1_to_hex(object
), NULL
};
59 struct child_process show
;
60 struct strbuf buf
= STRBUF_INIT
;
63 /* Invoke "git show --stat --no-notes $object" */
64 memset(&show
, 0, sizeof(show
));
65 show
.argv
= show_args
;
70 if (start_command(&show
))
71 die("unable to start 'show' for object '%s'",
74 /* Open the output as FILE* so strbuf_getline() can be used. */
75 show_out
= xfdopen(show
.out
, "r");
77 die_errno("can't fdopen 'show' output fd");
79 /* Prepend "# " to each output line and write result to 'fd' */
80 while (strbuf_getline(&buf
, show_out
, '\n') != EOF
) {
81 write_or_die(fd
, "# ", 2);
82 write_or_die(fd
, buf
.buf
, buf
.len
);
83 write_or_die(fd
, "\n", 1);
87 die_errno("failed to close pipe to 'show' for object '%s'",
89 if (finish_command(&show
))
90 die("failed to finish 'show' for object '%s'",
94 static void create_note(const unsigned char *object
,
97 const unsigned char *prev
,
98 unsigned char *result
)
105 /* write the template message before editing: */
106 path
= git_pathdup("NOTES_EDITMSG");
107 fd
= open(path
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
109 die_errno("could not create file '%s'", path
);
112 write_note_data(fd
, prev
);
113 write_or_die(fd
, note_template
, strlen(note_template
));
115 write_commented_object(fd
, object
);
119 if (launch_editor(path
, buf
, NULL
)) {
120 die("Please supply the note contents using either -m" \
128 fprintf(stderr
, "Removing note for object %s\n",
129 sha1_to_hex(object
));
132 if (write_sha1_file(buf
->buf
, buf
->len
, blob_type
, result
)) {
133 error("unable to write note object");
135 error("The note contents has been left in %s",
142 unlink_or_warn(path
);
152 static int parse_msg_arg(const struct option
*opt
, const char *arg
, int unset
)
154 struct msg_arg
*msg
= opt
->value
;
159 strbuf_addstr(&(msg
->buf
), "\n\n");
160 strbuf_addstr(&(msg
->buf
), arg
);
165 int commit_notes(struct notes_tree
*t
, const char *msg
)
167 struct commit_list
*parent
;
168 unsigned char tree_sha1
[20], prev_commit
[20], new_commit
[20];
169 struct strbuf buf
= STRBUF_INIT
;
172 t
= &default_notes_tree
;
173 if (!t
->initialized
|| !t
->ref
|| !*t
->ref
)
174 die("Cannot commit uninitialized/unreferenced notes tree");
176 /* Prepare commit message and reflog message */
177 strbuf_addstr(&buf
, "notes: "); /* commit message starts at index 7 */
178 strbuf_addstr(&buf
, msg
);
179 if (buf
.buf
[buf
.len
- 1] != '\n')
180 strbuf_addch(&buf
, '\n'); /* Make sure msg ends with newline */
182 /* Convert notes tree to tree object */
183 if (write_notes_tree(t
, tree_sha1
))
184 die("Failed to write current notes tree to database");
186 /* Create new commit for the tree object */
187 if (!read_ref(t
->ref
, prev_commit
)) { /* retrieve parent commit */
188 parent
= xmalloc(sizeof(*parent
));
189 parent
->item
= lookup_commit(prev_commit
);
192 hashclr(prev_commit
);
195 if (commit_tree(buf
.buf
+ 7, tree_sha1
, parent
, new_commit
, NULL
))
196 die("Failed to commit notes tree to database");
198 /* Update notes ref with new commit */
199 update_ref(buf
.buf
, t
->ref
, new_commit
, prev_commit
, 0, DIE_ON_ERR
);
201 strbuf_release(&buf
);
205 int cmd_notes(int argc
, const char **argv
, const char *prefix
)
207 struct strbuf buf
= STRBUF_INIT
;
208 struct notes_tree
*t
;
209 unsigned char object
[20], new_note
[20];
210 const unsigned char *note
;
211 const char *object_ref
;
214 int list
= 0, edit
= 0, show
= 0, remove
= 0, prune
= 0;
216 const char *msgfile
= NULL
;
217 struct msg_arg msg
= { 0, STRBUF_INIT
};
218 struct option options
[] = {
219 OPT_GROUP("Notes edit options"),
220 OPT_CALLBACK('m', NULL
, &msg
, "msg",
221 "note contents as a string", parse_msg_arg
),
222 OPT_FILENAME('F', NULL
, &msgfile
, "note contents in a file"),
226 git_config(git_default_config
, NULL
);
228 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_usage
, 0);
230 if (argc
&& !strcmp(argv
[0], "list"))
232 else if (argc
&& !strcmp(argv
[0], "edit"))
234 else if (argc
&& !strcmp(argv
[0], "show"))
236 else if (argc
&& !strcmp(argv
[0], "remove"))
238 else if (argc
&& !strcmp(argv
[0], "prune"))
241 list
= 1; /* Default to 'list' if no other subcommand given */
243 if (list
+ edit
+ show
+ remove
+ prune
!= 1)
244 usage_with_options(git_notes_usage
, options
);
246 if ((msg
.given
|| msgfile
) && !edit
) {
247 error("cannot use -m/-F options with %s subcommand.", argv
[0]);
248 usage_with_options(git_notes_usage
, options
);
251 if (msg
.given
&& msgfile
) {
252 error("mixing -m and -F options is not allowed.");
253 usage_with_options(git_notes_usage
, options
);
256 given_object
= argc
== 2;
257 object_ref
= given_object
? argv
[1] : "HEAD";
258 if (argc
> 2 || (prune
&& argc
> 1)) {
259 error("too many parameters");
260 usage_with_options(git_notes_usage
, options
);
263 if (get_sha1(object_ref
, object
))
264 die("Failed to resolve '%s' as a valid ref.", object_ref
);
266 init_notes(NULL
, NULL
, NULL
, 0);
267 t
= &default_notes_tree
;
269 if (prefixcmp(t
->ref
, "refs/notes/"))
270 die("Refusing to %s notes in %s (outside of refs/notes/)",
273 note
= get_note(t
, object
);
280 puts(sha1_to_hex(note
));
284 return for_each_note(t
, 0, list_each_note
, NULL
);
289 if ((list
|| show
) && !note
) {
290 error("No note found for object %s.", sha1_to_hex(object
));
293 const char *show_args
[3] = {"show", sha1_to_hex(note
), NULL
};
294 return execv_git_cmd(show_args
);
297 /* edit/remove/prune command */
302 strbuf_addbuf(&buf
, &(msg
.buf
));
304 if (!strcmp(msgfile
, "-")) {
305 if (strbuf_read(&buf
, 0, 1024) < 0)
306 die_errno("cannot read '%s'", msgfile
);
307 } else if (strbuf_read_file(&buf
, msgfile
, 1024) < 0)
308 die_errno("could not open or read '%s'", msgfile
);
315 create_note(object
, &buf
, msg
.given
|| msgfile
|| remove
, note
,
317 if (is_null_sha1(new_note
))
318 remove_note(t
, object
);
320 add_note(t
, object
, new_note
, combine_notes_overwrite
);
322 snprintf(logmsg
, sizeof(logmsg
), "Note %s by 'git notes %s'",
323 is_null_sha1(new_note
) ? "removed" : "added", argv
[0]);
324 commit_notes(t
, logmsg
);
327 strbuf_release(&buf
);