Builtin-ify git-notes
The builtin-ification includes some minor behavioural changes to the command-line interface: It is no longer allowed to mix the -m and -F arguments, and it is not allowed to use multiple -F options. As part of the builtin-ification, we add the commit_notes() function to the builtin API. This function (together with the notes.h API) can be easily used from other builtins to manipulate the notes tree. Also includes needed changes to t3301. This patch has been improved by the following contributions: - Stephen Boyd: Use die() instead of fprintf(stderr, ...) followed by exit(1) Cc: Stephen Boyd <bebarino@gmail.com> Signed-off-by: Johan Herland <johan@herland.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Johan Herland committed
Feb 13, 2010 at 22:28 UTC
cd067d3bf4ea3f89969cd143be3e281e1c5ac58a
7 files changed
+350
-40
Documentation/git-notes.txt
+2
-4
@@ -37,14 +37,12 @@ OPTIONS
37
-------
38
-m <msg>::
39
Use the given note message (instead of prompting).
40
- If multiple `-m` (or `-F`) options are given, their
41
- values are concatenated as separate paragraphs.
40
+ If multiple `-m` options are given, their values
41
+ are concatenated as separate paragraphs.
42
43
-F <file>::
44
Take the note message from the given file. Use '-' to
45
read the note message from the standard input.
46
- If multiple `-F` (or `-m`) options are given, their
47
- values are concatenated as separate paragraphs.
46
47
48
Author
Makefile
+1
-1
@@ -353,7 +353,6 @@ SCRIPT_SH += git-merge-one-file.sh
353
SCRIPT_SH += git-merge-resolve.sh
354
SCRIPT_SH += git-mergetool.sh
355
SCRIPT_SH += git-mergetool--lib.sh
356
-SCRIPT_SH += git-notes.sh
356
SCRIPT_SH += git-parse-remote.sh
357
SCRIPT_SH += git-pull.sh
358
SCRIPT_SH += git-quiltimport.sh
@@ -671,6 +670,7 @@ BUILTIN_OBJS += builtin-mktag.o
670
BUILTIN_OBJS += builtin-mktree.o
671
BUILTIN_OBJS += builtin-mv.o
672
BUILTIN_OBJS += builtin-name-rev.o
673
+BUILTIN_OBJS += builtin-notes.o
674
BUILTIN_OBJS += builtin-pack-objects.o
675
BUILTIN_OBJS += builtin-pack-redundant.o
676
BUILTIN_OBJS += builtin-pack-refs.o
builtin-notes.c
new
+280
@@ -0,0 +1,280 @@
1
+/*
2
+ * Builtin "git notes"
3
+ *
4
+ * Copyright (c) 2010 Johan Herland <johan@herland.net>
5
+ *
6
+ * Based on git-notes.sh by Johannes Schindelin,
7
+ * and builtin-tag.c by Kristian Høgsberg and Carlos Rica.
8
+ */
9
+
10
+#include "cache.h"
11
+#include "builtin.h"
12
+#include "notes.h"
13
+#include "blob.h"
14
+#include "commit.h"
15
+#include "refs.h"
16
+#include "exec_cmd.h"
17
+#include "run-command.h"
18
+#include "parse-options.h"
19
+
20
+static const char * const git_notes_usage[] = {
21
+ "git notes edit [-m <msg> | -F <file>] [<object>]",
22
+ "git notes show [<object>]",
23
+ NULL
24
+};
25
+
26
+static const char note_template[] =
27
+ "\n"
28
+ "#\n"
29
+ "# Write/edit the notes for the following object:\n"
30
+ "#\n";
31
+
32
+static void write_note_data(int fd, const unsigned char *sha1)
33
+{
34
+ unsigned long size;
35
+ enum object_type type;
36
+ char *buf = read_sha1_file(sha1, &type, &size);
37
+ if (buf) {
38
+ if (size)
39
+ write_or_die(fd, buf, size);
40
+ free(buf);
41
+ }
42
+}
43
+
44
+static void write_commented_object(int fd, const unsigned char *object)
45
+{
46
+ const char *show_args[5] =
47
+ {"show", "--stat", "--no-notes", sha1_to_hex(object), NULL};
48
+ struct child_process show;
49
+ struct strbuf buf = STRBUF_INIT;
50
+ FILE *show_out;
51
+
52
+ /* Invoke "git show --stat --no-notes $object" */
53
+ memset(&show, 0, sizeof(show));
54
+ show.argv = show_args;
55
+ show.no_stdin = 1;
56
+ show.out = -1;
57
+ show.err = 0;
58
+ show.git_cmd = 1;
59
+ if (start_command(&show))
60
+ die("unable to start 'show' for object '%s'",
61
+ sha1_to_hex(object));
62
+
63
+ /* Open the output as FILE* so strbuf_getline() can be used. */
64
+ show_out = xfdopen(show.out, "r");
65
+ if (show_out == NULL)
66
+ die_errno("can't fdopen 'show' output fd");
67
+
68
+ /* Prepend "# " to each output line and write result to 'fd' */
69
+ while (strbuf_getline(&buf, show_out, '\n') != EOF) {
70
+ write_or_die(fd, "# ", 2);
71
+ write_or_die(fd, buf.buf, buf.len);
72
+ write_or_die(fd, "\n", 1);
73
+ }
74
+ strbuf_release(&buf);
75
+ if (fclose(show_out))
76
+ die_errno("failed to close pipe to 'show' for object '%s'",
77
+ sha1_to_hex(object));
78
+ if (finish_command(&show))
79
+ die("failed to finish 'show' for object '%s'",
80
+ sha1_to_hex(object));
81
+}
82
+
83
+static void create_note(const unsigned char *object,
84
+ struct strbuf *buf,
85
+ int skip_editor,
86
+ const unsigned char *prev,
87
+ unsigned char *result)
88
+{
89
+ char *path = NULL;
90
+
91
+ if (!skip_editor) {
92
+ int fd;
93
+
94
+ /* write the template message before editing: */
95
+ path = git_pathdup("NOTES_EDITMSG");
96
+ fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
97
+ if (fd < 0)
98
+ die_errno("could not create file '%s'", path);
99
+
100
+ if (prev)
101
+ write_note_data(fd, prev);
102
+ write_or_die(fd, note_template, strlen(note_template));
103
+
104
+ write_commented_object(fd, object);
105
+
106
+ close(fd);
107
+
108
+ if (launch_editor(path, buf, NULL)) {
109
+ die("Please supply the note contents using either -m" \
110
+ " or -F option");
111
+ }
112
+ }
113
+
114
+ stripspace(buf, 1);
115
+
116
+ if (!skip_editor && !buf->len) {
117
+ fprintf(stderr, "Removing note for object %s\n",
118
+ sha1_to_hex(object));
119
+ hashclr(result);
120
+ } else {
121
+ if (write_sha1_file(buf->buf, buf->len, blob_type, result)) {
122
+ error("unable to write note object");
123
+ if (path)
124
+ error("The note contents has been left in %s",
125
+ path);
126
+ exit(128);
127
+ }
128
+ }
129
+
130
+ if (path) {
131
+ unlink_or_warn(path);
132
+ free(path);
133
+ }
134
+}
135
+
136
+struct msg_arg {
137
+ int given;
138
+ struct strbuf buf;
139
+};
140
+
141
+static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
142
+{
143
+ struct msg_arg *msg = opt->value;
144
+
145
+ if (!arg)
146
+ return -1;
147
+ if (msg->buf.len)
148
+ strbuf_addstr(&(msg->buf), "\n\n");
149
+ strbuf_addstr(&(msg->buf), arg);
150
+ msg->given = 1;
151
+ return 0;
152
+}
153
+
154
+int commit_notes(struct notes_tree *t, const char *msg)
155
+{
156
+ struct commit_list *parent;
157
+ unsigned char tree_sha1[20], prev_commit[20], new_commit[20];
158
+ struct strbuf buf = STRBUF_INIT;
159
+
160
+ if (!t)
161
+ t = &default_notes_tree;
162
+ if (!t->initialized || !t->ref || !*t->ref)
163
+ die("Cannot commit uninitialized/unreferenced notes tree");
164
+
165
+ /* Prepare commit message and reflog message */
166
+ strbuf_addstr(&buf, "notes: "); /* commit message starts at index 7 */
167
+ strbuf_addstr(&buf, msg);
168
+ if (buf.buf[buf.len - 1] != '\n')
169
+ strbuf_addch(&buf, '\n'); /* Make sure msg ends with newline */
170
+
171
+ /* Convert notes tree to tree object */
172
+ if (write_notes_tree(t, tree_sha1))
173
+ die("Failed to write current notes tree to database");
174
+
175
+ /* Create new commit for the tree object */
176
+ if (!read_ref(t->ref, prev_commit)) { /* retrieve parent commit */
177
+ parent = xmalloc(sizeof(*parent));
178
+ parent->item = lookup_commit(prev_commit);
179
+ parent->next = NULL;
180
+ } else {
181
+ hashclr(prev_commit);
182
+ parent = NULL;
183
+ }
184
+ if (commit_tree(buf.buf + 7, tree_sha1, parent, new_commit, NULL))
185
+ die("Failed to commit notes tree to database");
186
+
187
+ /* Update notes ref with new commit */
188
+ update_ref(buf.buf, t->ref, new_commit, prev_commit, 0, DIE_ON_ERR);
189
+
190
+ strbuf_release(&buf);
191
+ return 0;
192
+}
193
+
194
+int cmd_notes(int argc, const char **argv, const char *prefix)
195
+{
196
+ struct strbuf buf = STRBUF_INIT;
197
+ struct notes_tree *t;
198
+ unsigned char object[20], new_note[20];
199
+ const unsigned char *note;
200
+ const char *object_ref;
201
+ int edit = 0, show = 0;
202
+ const char *msgfile = NULL;
203
+ struct msg_arg msg = { 0, STRBUF_INIT };
204
+ struct option options[] = {
205
+ OPT_GROUP("Notes edit options"),
206
+ OPT_CALLBACK('m', NULL, &msg, "msg",
207
+ "note contents as a string", parse_msg_arg),
208
+ OPT_FILENAME('F', NULL, &msgfile, "note contents in a file"),
209
+ OPT_END()
210
+ };
211
+
212
+ git_config(git_default_config, NULL);
213
+
214
+ argc = parse_options(argc, argv, prefix, options, git_notes_usage, 0);
215
+
216
+ if (argc && !strcmp(argv[0], "edit"))
217
+ edit = 1;
218
+ else if (argc && !strcmp(argv[0], "show"))
219
+ show = 1;
220
+
221
+ if (edit + show != 1)
222
+ usage_with_options(git_notes_usage, options);
223
+
224
+ object_ref = argc == 2 ? argv[1] : "HEAD";
225
+ if (argc > 2) {
226
+ error("too many parameters");
227
+ usage_with_options(git_notes_usage, options);
228
+ }
229
+
230
+ if (get_sha1(object_ref, object))
231
+ die("Failed to resolve '%s' as a valid ref.", object_ref);
232
+
233
+ init_notes(NULL, NULL, NULL, 0);
234
+ t = &default_notes_tree;
235
+
236
+ if (prefixcmp(t->ref, "refs/notes/"))
237
+ die("Refusing to %s notes in %s (outside of refs/notes/)",
238
+ edit ? "edit" : "show", t->ref);
239
+
240
+ note = get_note(t, object);
241
+
242
+ /* show command */
243
+
244
+ if (show && !note) {
245
+ error("No note found for object %s.", sha1_to_hex(object));
246
+ return 1;
247
+ } else if (show) {
248
+ const char *show_args[3] = {"show", sha1_to_hex(note), NULL};
249
+ return execv_git_cmd(show_args);
250
+ }
251
+
252
+ /* edit command */
253
+
254
+ if (msg.given || msgfile) {
255
+ if (msg.given && msgfile) {
256
+ error("mixing -m and -F options is not allowed.");
257
+ usage_with_options(git_notes_usage, options);
258
+ }
259
+ if (msg.given)
260
+ strbuf_addbuf(&buf, &(msg.buf));
261
+ else {
262
+ if (!strcmp(msgfile, "-")) {
263
+ if (strbuf_read(&buf, 0, 1024) < 0)
264
+ die_errno("cannot read '%s'", msgfile);
265
+ } else {
266
+ if (strbuf_read_file(&buf, msgfile, 1024) < 0)
267
+ die_errno("could not open or read '%s'",
268
+ msgfile);
269
+ }
270
+ }
271
+ }
272
+
273
+ create_note(object, &buf, msg.given || msgfile, note, new_note);
274
+ add_note(t, object, new_note, combine_notes_overwrite);
275
+ commit_notes(t, "Note added by 'git notes edit'");
276
+
277
+ free_notes(t);
278
+ strbuf_release(&buf);
279
+ return 0;
280
+}
builtin.h
+3
@@ -5,6 +5,7 @@
5
#include "strbuf.h"
6
#include "cache.h"
7
#include "commit.h"
8
+#include "notes.h"
9
10
extern const char git_version_string[];
11
extern const char git_usage_string[];
@@ -18,6 +19,7 @@ extern int fmt_merge_msg(int merge_summary, struct strbuf *in,
19
extern int commit_tree(const char *msg, unsigned char *tree,
20
struct commit_list *parents, unsigned char *ret,
21
const char *author);
22
+extern int commit_notes(struct notes_tree *t, const char *msg);
23
extern int check_pager_config(const char *cmd);
24
25
extern int cmd_add(int argc, const char **argv, const char *prefix);
@@ -78,6 +80,7 @@ extern int cmd_mktag(int argc, const char **argv, const char *prefix);
80
extern int cmd_mktree(int argc, const char **argv, const char *prefix);
81
extern int cmd_mv(int argc, const char **argv, const char *prefix);
82
extern int cmd_name_rev(int argc, const char **argv, const char *prefix);
83
+extern int cmd_notes(int argc, const char **argv, const char *prefix);
84
extern int cmd_pack_objects(int argc, const char **argv, const char *prefix);
85
extern int cmd_pack_redundant(int argc, const char **argv, const char *prefix);
86
extern int cmd_patch_id(int argc, const char **argv, const char *prefix);
contrib/examples/git-notes.sh
renamed
git.c
+1
@@ -343,6 +343,7 @@ static void handle_internal_command(int argc, const char **argv)
343
{ "mktree", cmd_mktree, RUN_SETUP },
344
{ "mv", cmd_mv, RUN_SETUP | NEED_WORK_TREE },
345
{ "name-rev", cmd_name_rev, RUN_SETUP },
346
+ { "notes", cmd_notes, RUN_SETUP },
347
{ "pack-objects", cmd_pack_objects, RUN_SETUP },
348
{ "pack-redundant", cmd_pack_redundant, RUN_SETUP },
349
{ "patch-id", cmd_patch_id },
t/t3301-notes.sh
+63
-35
@@ -12,8 +12,8 @@ echo "$MSG" > "$1"
12
echo "$MSG" >& 2
13
EOF
14
chmod a+x fake_editor.sh
15
-VISUAL=./fake_editor.sh
16
-export VISUAL
15
+GIT_EDITOR=./fake_editor.sh
16
+export GIT_EDITOR
17
18
test_expect_success 'cannot annotate non-existing HEAD' '
19
(MSG=3 && export MSG && test_must_fail git notes edit)
@@ -56,8 +56,17 @@ test_expect_success 'handle empty notes gracefully' '
56
57
test_expect_success 'create notes' '
58
git config core.notesRef refs/notes/commits &&
59
+ MSG=b0 git notes edit &&
60
+ test ! -f .git/NOTES_EDITMSG &&
61
+ test 1 = $(git ls-tree refs/notes/commits | wc -l) &&
62
+ test b0 = $(git notes show) &&
63
+ git show HEAD^ &&
64
+ test_must_fail git notes show HEAD^
65
+'
66
+
67
+test_expect_success 'edit existing notes' '
68
MSG=b1 git notes edit &&
60
- test ! -f .git/new-notes &&
69
+ test ! -f .git/NOTES_EDITMSG &&
70
test 1 = $(git ls-tree refs/notes/commits | wc -l) &&
71
test b1 = $(git notes show) &&
72
git show HEAD^ &&
@@ -110,19 +119,16 @@ test_expect_success 'show multi-line notes' '
119
git log -2 > output &&
120
test_cmp expect-multiline output
121
'
113
-test_expect_success 'create -m and -F notes (setup)' '
122
+test_expect_success 'create -F notes (setup)' '
123
: > a4 &&
124
git add a4 &&
125
test_tick &&
126
git commit -m 4th &&
127
echo "xyzzy" > note5 &&
119
- git notes edit -m spam -F note5 -m "foo
120
-bar
121
-baz"
128
+ git notes edit -F note5
129
'
130
124
-whitespace=" "
125
-cat > expect-m-and-F << EOF
131
+cat > expect-F << EOF
132
commit 15023535574ded8b1a89052b32673f84cf9582b8
133
Author: A U Thor <author@example.com>
134
Date: Thu Apr 7 15:16:13 2005 -0700
@@ -130,21 +136,15 @@ Date: Thu Apr 7 15:16:13 2005 -0700
136
4th
137
138
Notes:
133
- spam
134
-$whitespace
139
xyzzy
136
-$whitespace
137
- foo
138
- bar
139
- baz
140
EOF
141
142
-printf "\n" >> expect-m-and-F
143
-cat expect-multiline >> expect-m-and-F
142
+printf "\n" >> expect-F
143
+cat expect-multiline >> expect-F
144
145
-test_expect_success 'show -m and -F notes' '
145
+test_expect_success 'show -F notes' '
146
git log -3 > output &&
147
- test_cmp expect-m-and-F output
147
+ test_cmp expect-F output
148
'
149
150
cat >expect << EOF
@@ -164,13 +164,7 @@ test_expect_success 'git log --pretty=raw does not show notes' '
164
cat >>expect <<EOF
165
166
Notes:
167
- spam
168
-$whitespace
167
xyzzy
170
-$whitespace
171
- foo
172
- bar
173
- baz
168
EOF
169
test_expect_success 'git log --show-notes' '
170
git log -1 --pretty=raw --show-notes >output &&
@@ -179,17 +173,17 @@ test_expect_success 'git log --show-notes' '
173
174
test_expect_success 'git log --no-notes' '
175
git log -1 --no-notes >output &&
182
- ! grep spam output
176
+ ! grep xyzzy output
177
'
178
179
test_expect_success 'git format-patch does not show notes' '
180
git format-patch -1 --stdout >output &&
187
- ! grep spam output
181
+ ! grep xyzzy output
182
'
183
184
test_expect_success 'git format-patch --show-notes does show notes' '
185
git format-patch --show-notes -1 --stdout >output &&
192
- grep spam output
186
+ grep xyzzy output
187
'
188
189
for pretty in \
@@ -202,35 +196,69 @@ do
196
esac
197
test_expect_success "git show $pretty does$not show notes" '
198
git show $p >output &&
205
- eval "$negate grep spam output"
199
+ eval "$negate grep xyzzy output"
200
'
201
done
202
209
-test_expect_success 'create other note on a different notes ref (setup)' '
203
+test_expect_success 'create -m notes (setup)' '
204
: > a5 &&
205
git add a5 &&
206
test_tick &&
207
git commit -m 5th &&
214
- GIT_NOTES_REF="refs/notes/other" git notes edit -m "other note"
208
+ git notes edit -m spam -m "foo
209
+bar
210
+baz"
211
'
212
217
-cat > expect-other << EOF
213
+whitespace=" "
214
+cat > expect-m << EOF
215
commit bd1753200303d0a0344be813e504253b3d98e74d
216
Author: A U Thor <author@example.com>
217
Date: Thu Apr 7 15:17:13 2005 -0700
218
219
5th
220
221
+Notes:
222
+ spam
223
+$whitespace
224
+ foo
225
+ bar
226
+ baz
227
+EOF
228
+
229
+printf "\n" >> expect-m
230
+cat expect-F >> expect-m
231
+
232
+test_expect_success 'show -m notes' '
233
+ git log -4 > output &&
234
+ test_cmp expect-m output
235
+'
236
+
237
+test_expect_success 'create other note on a different notes ref (setup)' '
238
+ : > a6 &&
239
+ git add a6 &&
240
+ test_tick &&
241
+ git commit -m 6th &&
242
+ GIT_NOTES_REF="refs/notes/other" git notes edit -m "other note"
243
+'
244
+
245
+cat > expect-other << EOF
246
+commit 387a89921c73d7ed72cd94d179c1c7048ca47756
247
+Author: A U Thor <author@example.com>
248
+Date: Thu Apr 7 15:18:13 2005 -0700
249
+
250
+ 6th
251
+
252
Notes:
253
other note
254
EOF
255
256
cat > expect-not-other << EOF
229
-commit bd1753200303d0a0344be813e504253b3d98e74d
257
+commit 387a89921c73d7ed72cd94d179c1c7048ca47756
258
Author: A U Thor <author@example.com>
231
-Date: Thu Apr 7 15:17:13 2005 -0700
259
+Date: Thu Apr 7 15:18:13 2005 -0700
260
233
- 5th
261
+ 6th
262
EOF
263
264
test_expect_success 'Do not show note on other ref by default' '