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 edit [-m <msg> | -F <file>] [<object>]",
22 "git notes show [<object>]",
23 "git notes remove [<object>]",
27 static const char note_template
[] =
30 "# Write/edit the notes for the following object:\n"
33 static void write_note_data(int fd
, const unsigned char *sha1
)
36 enum object_type type
;
37 char *buf
= read_sha1_file(sha1
, &type
, &size
);
40 write_or_die(fd
, buf
, size
);
45 static void write_commented_object(int fd
, const unsigned char *object
)
47 const char *show_args
[5] =
48 {"show", "--stat", "--no-notes", sha1_to_hex(object
), NULL
};
49 struct child_process show
;
50 struct strbuf buf
= STRBUF_INIT
;
53 /* Invoke "git show --stat --no-notes $object" */
54 memset(&show
, 0, sizeof(show
));
55 show
.argv
= show_args
;
60 if (start_command(&show
))
61 die("unable to start 'show' for object '%s'",
64 /* Open the output as FILE* so strbuf_getline() can be used. */
65 show_out
= xfdopen(show
.out
, "r");
67 die_errno("can't fdopen 'show' output fd");
69 /* Prepend "# " to each output line and write result to 'fd' */
70 while (strbuf_getline(&buf
, show_out
, '\n') != EOF
) {
71 write_or_die(fd
, "# ", 2);
72 write_or_die(fd
, buf
.buf
, buf
.len
);
73 write_or_die(fd
, "\n", 1);
77 die_errno("failed to close pipe to 'show' for object '%s'",
79 if (finish_command(&show
))
80 die("failed to finish 'show' for object '%s'",
84 static void create_note(const unsigned char *object
,
87 const unsigned char *prev
,
88 unsigned char *result
)
95 /* write the template message before editing: */
96 path
= git_pathdup("NOTES_EDITMSG");
97 fd
= open(path
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
99 die_errno("could not create file '%s'", path
);
102 write_note_data(fd
, prev
);
103 write_or_die(fd
, note_template
, strlen(note_template
));
105 write_commented_object(fd
, object
);
109 if (launch_editor(path
, buf
, NULL
)) {
110 die("Please supply the note contents using either -m" \
118 fprintf(stderr
, "Removing note for object %s\n",
119 sha1_to_hex(object
));
122 if (write_sha1_file(buf
->buf
, buf
->len
, blob_type
, result
)) {
123 error("unable to write note object");
125 error("The note contents has been left in %s",
132 unlink_or_warn(path
);
142 static int parse_msg_arg(const struct option
*opt
, const char *arg
, int unset
)
144 struct msg_arg
*msg
= opt
->value
;
149 strbuf_addstr(&(msg
->buf
), "\n\n");
150 strbuf_addstr(&(msg
->buf
), arg
);
155 int commit_notes(struct notes_tree
*t
, const char *msg
)
157 struct commit_list
*parent
;
158 unsigned char tree_sha1
[20], prev_commit
[20], new_commit
[20];
159 struct strbuf buf
= STRBUF_INIT
;
162 t
= &default_notes_tree
;
163 if (!t
->initialized
|| !t
->ref
|| !*t
->ref
)
164 die("Cannot commit uninitialized/unreferenced notes tree");
166 /* Prepare commit message and reflog message */
167 strbuf_addstr(&buf
, "notes: "); /* commit message starts at index 7 */
168 strbuf_addstr(&buf
, msg
);
169 if (buf
.buf
[buf
.len
- 1] != '\n')
170 strbuf_addch(&buf
, '\n'); /* Make sure msg ends with newline */
172 /* Convert notes tree to tree object */
173 if (write_notes_tree(t
, tree_sha1
))
174 die("Failed to write current notes tree to database");
176 /* Create new commit for the tree object */
177 if (!read_ref(t
->ref
, prev_commit
)) { /* retrieve parent commit */
178 parent
= xmalloc(sizeof(*parent
));
179 parent
->item
= lookup_commit(prev_commit
);
182 hashclr(prev_commit
);
185 if (commit_tree(buf
.buf
+ 7, tree_sha1
, parent
, new_commit
, NULL
))
186 die("Failed to commit notes tree to database");
188 /* Update notes ref with new commit */
189 update_ref(buf
.buf
, t
->ref
, new_commit
, prev_commit
, 0, DIE_ON_ERR
);
191 strbuf_release(&buf
);
195 int cmd_notes(int argc
, const char **argv
, const char *prefix
)
197 struct strbuf buf
= STRBUF_INIT
;
198 struct notes_tree
*t
;
199 unsigned char object
[20], new_note
[20];
200 const unsigned char *note
;
201 const char *object_ref
;
204 int edit
= 0, show
= 0, remove
= 0;
205 const char *msgfile
= NULL
;
206 struct msg_arg msg
= { 0, STRBUF_INIT
};
207 struct option options
[] = {
208 OPT_GROUP("Notes edit options"),
209 OPT_CALLBACK('m', NULL
, &msg
, "msg",
210 "note contents as a string", parse_msg_arg
),
211 OPT_FILENAME('F', NULL
, &msgfile
, "note contents in a file"),
215 git_config(git_default_config
, NULL
);
217 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_usage
, 0);
219 if (argc
&& !strcmp(argv
[0], "edit"))
221 else if (argc
&& !strcmp(argv
[0], "show"))
223 else if (argc
&& !strcmp(argv
[0], "remove"))
226 if (edit
+ show
+ remove
!= 1)
227 usage_with_options(git_notes_usage
, options
);
229 if ((msg
.given
|| msgfile
) && !edit
) {
230 error("cannot use -m/-F options with %s subcommand.", argv
[0]);
231 usage_with_options(git_notes_usage
, options
);
234 if (msg
.given
&& msgfile
) {
235 error("mixing -m and -F options is not allowed.");
236 usage_with_options(git_notes_usage
, options
);
239 object_ref
= argc
== 2 ? argv
[1] : "HEAD";
241 error("too many parameters");
242 usage_with_options(git_notes_usage
, options
);
245 if (get_sha1(object_ref
, object
))
246 die("Failed to resolve '%s' as a valid ref.", object_ref
);
248 init_notes(NULL
, NULL
, NULL
, 0);
249 t
= &default_notes_tree
;
251 if (prefixcmp(t
->ref
, "refs/notes/"))
252 die("Refusing to %s notes in %s (outside of refs/notes/)",
255 note
= get_note(t
, object
);
260 error("No note found for object %s.", sha1_to_hex(object
));
263 const char *show_args
[3] = {"show", sha1_to_hex(note
), NULL
};
264 return execv_git_cmd(show_args
);
267 /* edit/remove command */
272 strbuf_addbuf(&buf
, &(msg
.buf
));
274 if (!strcmp(msgfile
, "-")) {
275 if (strbuf_read(&buf
, 0, 1024) < 0)
276 die_errno("cannot read '%s'", msgfile
);
277 } else if (strbuf_read_file(&buf
, msgfile
, 1024) < 0)
278 die_errno("could not open or read '%s'", msgfile
);
281 create_note(object
, &buf
, msg
.given
|| msgfile
|| remove
, note
,
283 if (is_null_sha1(new_note
))
284 remove_note(t
, object
);
286 add_note(t
, object
, new_note
, combine_notes_overwrite
);
287 snprintf(logmsg
, sizeof(logmsg
), "Note %s by 'git notes %s'",
288 is_null_sha1(new_note
) ? "removed" : "added", argv
[0]);
289 commit_notes(t
, logmsg
);
292 strbuf_release(&buf
);