git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCHv12 12/23] Builtin-ify git-notes

From
Johan Herland <johan@herland.net>
Date
Jan 27, 2010, 11:51 UTC
Message-ID
<1264593120-4428-13-git-send-email-johan@herland.net>
In-Reply-To
<1264593120-4428-1-git-send-email-johan@herland.net>

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>
---
 Documentation/git-notes.txt                   |    6 +-
 Makefile                                      |    2 +-
 builtin-notes.c                               |  238 +++++++++++++++++++++++++
 builtin.h                                     |    3 +
 git-notes.sh => contrib/examples/git-notes.sh |    0
 git.c                                         |    1 +
 t/t3301-notes.sh                              |   98 +++++++----
 7 files changed, 308 insertions(+), 40 deletions(-)
 create mode 100644 builtin-notes.c
 rename git-notes.sh => contrib/examples/git-notes.sh (100%)
diff --git a/Documentation/git-notes.txt b/Documentation/git-notes.txt
index d4487ca..0d1ada6 100644
--- a/Documentation/git-notes.txt
+++ b/Documentation/git-notes.txt
@@ -37,14 +37,12 @@ OPTIONS
 -------
 -m <msg>::
 	Use the given note message (instead of prompting).
-	If multiple `-m` (or `-F`) options are given, their
-	values are concatenated as separate paragraphs.
+	If multiple `-m` options are given, their values
+	are concatenated as separate paragraphs.
 
 -F <file>::
 	Take the note message from the given file.  Use '-' to
 	read the note message from the standard input.
-	If multiple `-F` (or `-m`) options are given, their
-	values are concatenated as separate paragraphs.
 
 
 Author
diff --git a/Makefile b/Makefile
index c0dbee2..1f95f93 100644
--- a/Makefile
+++ b/Makefile
@@ -353,7 +353,6 @@ SCRIPT_SH += git-merge-one-file.sh
 SCRIPT_SH += git-merge-resolve.sh
 SCRIPT_SH += git-mergetool.sh
 SCRIPT_SH += git-mergetool--lib.sh
-SCRIPT_SH += git-notes.sh
 SCRIPT_SH += git-parse-remote.sh
 SCRIPT_SH += git-pull.sh
 SCRIPT_SH += git-quiltimport.sh
@@ -671,6 +670,7 @@ BUILTIN_OBJS += builtin-mktag.o
 BUILTIN_OBJS += builtin-mktree.o
 BUILTIN_OBJS += builtin-mv.o
 BUILTIN_OBJS += builtin-name-rev.o
+BUILTIN_OBJS += builtin-notes.o
 BUILTIN_OBJS += builtin-pack-objects.o
 BUILTIN_OBJS += builtin-pack-redundant.o
 BUILTIN_OBJS += builtin-pack-refs.o
diff --git a/builtin-notes.c b/builtin-notes.c
new file mode 100644
index 0000000..a764811
--- /dev/null
+++ b/builtin-notes.c
@@ -0,0 +1,238 @@
+/*
+ * Builtin "git notes"
+ *
+ * Copyright (c) 2010 Johan Herland <johan@herland.net>
+ *
+ * Based on git-notes.sh by Johannes Schindelin,
+ * and builtin-tag.c by Kristian Høgsberg and Carlos Rica.
+ */
+
+#include "cache.h"
+#include "builtin.h"
+#include "notes.h"
+#include "blob.h"
+#include "commit.h"
+#include "refs.h"
+#include "exec_cmd.h"
+#include "parse-options.h"
+
+static const char * const git_notes_usage[] = {
+	"git notes edit [-m <msg> | -F <file>] [<object>]",
+	"git notes show [<object>]",
+	NULL
+};
+
+static const char note_template[] =
+	"\n"
+	"#\n"
+	"# Write/edit the note contents\n"
+	"#\n";
+
+static void write_note_data(int fd, const unsigned char *sha1)
+{
+	unsigned long size;
+	enum object_type type;
+	char *buf = read_sha1_file(sha1, &type, &size);
+	if (buf) {
+		if (size)
+			write_or_die(fd, buf, size);
+		free(buf);
+	}
+}
+
+static void create_note(const unsigned char *object,
+			struct strbuf *buf,
+			int skip_editor,
+			const unsigned char *prev,
+			unsigned char *result)
+{
+	char *path = NULL;
+
+	if (!skip_editor) {
+		int fd;
+
+		/* write the template message before editing: */
+		path = git_pathdup("NOTES_EDITMSG");
+		fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
+		if (fd < 0)
+			die_errno("could not create file '%s'", path);
+
+		if (prev)
+			write_note_data(fd, prev);
+		write_or_die(fd, note_template, strlen(note_template));
+
+		close(fd);
+
+		if (launch_editor(path, buf, NULL)) {
+			die("Please supply the note contents using either -m" \
+			    " or -F option");
+		}
+	}
+
+	stripspace(buf, 1);
+
+	if (!skip_editor && !buf->len) {
+		fprintf(stderr, "Removing note for object %s\n",
+			sha1_to_hex(object));
+		hashclr(result);
+	} else {
+		if (write_sha1_file(buf->buf, buf->len, blob_type, result)) {
+			error("unable to write note object");
+			if (path)
+				error("The note contents has been left in %s",
+				      path);
+			exit(128);
+		}
+	}
+
+	if (path) {
+		unlink_or_warn(path);
+		free(path);
+	}
+}
+
+struct msg_arg {
+	int given;
+	struct strbuf buf;
+};
+
+static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
+{
+	struct msg_arg *msg = opt->value;
+
+	if (!arg)
+		return -1;
+	if (msg->buf.len)
+		strbuf_addstr(&(msg->buf), "\n\n");
+	strbuf_addstr(&(msg->buf), arg);
+	msg->given = 1;
+	return 0;
+}
+
+int commit_notes(struct notes_tree *t, const char *msg)
+{
+	struct commit_list *parent;
+	unsigned char tree_sha1[20], prev_commit[20], new_commit[20];
+	struct strbuf buf = STRBUF_INIT;
+
+	if (!t)
+		t = &default_notes_tree;
+	if (!t->initialized || !t->ref || !*t->ref)
+		die("Cannot commit uninitialized/unreferenced notes tree");
+
+	/* Prepare commit message and reflog message */
+	strbuf_addstr(&buf, "notes: "); /* commit message starts at index 7 */
+	strbuf_addstr(&buf, msg);
+	if (buf.buf[buf.len - 1] != '\n')
+		strbuf_addch(&buf, '\n'); /* Make sure msg ends with newline */
+
+	/* Convert notes tree to tree object */
+	if (write_notes_tree(t, tree_sha1))
+		die("Failed to write current notes tree to database");
+
+	/* Create new commit for the tree object */
+	if (!read_ref(t->ref, prev_commit)) { /* retrieve parent commit */
+		parent = xmalloc(sizeof(*parent));
+		parent->item = lookup_commit(prev_commit);
+		parent->next = NULL;
+	} else {
+		hashclr(prev_commit);
+		parent = NULL;
+	}
+	if (commit_tree(buf.buf + 7, tree_sha1, parent, new_commit, NULL))
+		die("Failed to commit notes tree to database");
+
+	/* Update notes ref with new commit */
+	update_ref(buf.buf, t->ref, new_commit, prev_commit, 0, DIE_ON_ERR);
+
+	strbuf_release(&buf);
+	return 0;
+}
+
+int cmd_notes(int argc, const char **argv, const char *prefix)
+{
+	struct strbuf buf = STRBUF_INIT;
+	struct notes_tree *t;
+	unsigned char object[20], new_note[20];
+	const unsigned char *note;
+	const char *object_ref;
+	int edit = 0, show = 0;
+	const char *msgfile = NULL;
+	struct msg_arg msg = { 0, STRBUF_INIT };
+	struct option options[] = {
+		OPT_GROUP("Notes edit options"),
+		OPT_CALLBACK('m', NULL, &msg, "msg",
+			     "note contents as a string", parse_msg_arg),
+		OPT_FILENAME('F', NULL, &msgfile, "note contents in a file"),
+		OPT_END()
+	};
+
+	git_config(git_default_config, NULL);
+
+	argc = parse_options(argc, argv, prefix, options, git_notes_usage, 0);
+
+	if (argc && !strcmp(argv[0], "edit"))
+		edit = 1;
+	else if (argc && !strcmp(argv[0], "show"))
+		show = 1;
+
+	if (edit + show != 1)
+		usage_with_options(git_notes_usage, options);
+
+	object_ref = argc == 2 ? argv[1] : "HEAD";
+	if (argc > 2) {
+		error("too many parameters");
+		usage_with_options(git_notes_usage, options);
+	}
+
+	if (get_sha1(object_ref, object))
+		die("Failed to resolve '%s' as a valid ref.", object_ref);
+
+	init_notes(NULL, NULL, NULL, 0);
+	t = &default_notes_tree;
+
+	if (prefixcmp(t->ref, "refs/notes/"))
+		die("Refusing to %s notes in %s (outside of refs/notes/)",
+		    edit ? "edit" : "show", t->ref);
+
+	note = get_note(t, object);
+
+	/* show command */
+
+	if (show && !note) {
+		error("No note found for object %s.", sha1_to_hex(object));
+		return 1;
+	} else if (show) {
+		const char *show_args[3] = {"show", sha1_to_hex(note), NULL};
+		return execv_git_cmd(show_args);
+	}
+
+	/* edit command */
+
+	if (msg.given || msgfile) {
+		if (msg.given && msgfile) {
+			error("mixing -m and -F options is not allowed.");
+			usage_with_options(git_notes_usage, options);
+		}
+		if (msg.given)
+			strbuf_addbuf(&buf, &(msg.buf));
+		else {
+			if (!strcmp(msgfile, "-")) {
+				if (strbuf_read(&buf, 0, 1024) < 0)
+					die_errno("cannot read '%s'", msgfile);
+			} else {
+				if (strbuf_read_file(&buf, msgfile, 1024) < 0)
+					die_errno("could not open or read '%s'",
+						msgfile);
+			}
+		}
+	}
+
+	create_note(object, &buf, msg.given || msgfile, note, new_note);
+	add_note(t, object, new_note, combine_notes_overwrite);
+	commit_notes(t, "Note added by 'git notes edit'");
+
+	free_notes(t);
+	strbuf_release(&buf);
+	return 0;
+}
diff --git a/builtin.h b/builtin.h
index e8202f3..cdf9847 100644
--- a/builtin.h
+++ b/builtin.h
@@ -5,6 +5,7 @@
 #include "strbuf.h"
 #include "cache.h"
 #include "commit.h"
+#include "notes.h"
 
 extern const char git_version_string[];
 extern const char git_usage_string[];
@@ -18,6 +19,7 @@ extern int fmt_merge_msg(int merge_summary, struct strbuf *in,
 extern int commit_tree(const char *msg, unsigned char *tree,
 		struct commit_list *parents, unsigned char *ret,
 		const char *author);
+extern int commit_notes(struct notes_tree *t, const char *msg);
 extern int check_pager_config(const char *cmd);
 
 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);
 extern int cmd_mktree(int argc, const char **argv, const char *prefix);
 extern int cmd_mv(int argc, const char **argv, const char *prefix);
 extern int cmd_name_rev(int argc, const char **argv, const char *prefix);
+extern int cmd_notes(int argc, const char **argv, const char *prefix);
 extern int cmd_pack_objects(int argc, const char **argv, const char *prefix);
 extern int cmd_pack_redundant(int argc, const char **argv, const char *prefix);
 extern int cmd_patch_id(int argc, const char **argv, const char *prefix);
diff --git a/git-notes.sh b/contrib/examples/git-notes.sh
similarity index 100%
rename from git-notes.sh
rename to contrib/examples/git-notes.sh
diff --git a/git.c b/git.c
index b3e23f1..32f76e1 100644
--- a/git.c
+++ b/git.c
@@ -343,6 +343,7 @@ static void handle_internal_command(int argc, const char **argv)
 		{ "mktree", cmd_mktree, RUN_SETUP },
 		{ "mv", cmd_mv, RUN_SETUP | NEED_WORK_TREE },
 		{ "name-rev", cmd_name_rev, RUN_SETUP },
+		{ "notes", cmd_notes, RUN_SETUP },
 		{ "pack-objects", cmd_pack_objects, RUN_SETUP },
 		{ "pack-redundant", cmd_pack_redundant, RUN_SETUP },
 		{ "patch-id", cmd_patch_id },
diff --git a/t/t3301-notes.sh b/t/t3301-notes.sh
index 18aad53..10f62f4 100755
--- a/t/t3301-notes.sh
+++ b/t/t3301-notes.sh
@@ -12,8 +12,8 @@ echo "$MSG" > "$1"
 echo "$MSG" >& 2
 EOF
 chmod a+x fake_editor.sh
-VISUAL=./fake_editor.sh
-export VISUAL
+GIT_EDITOR=./fake_editor.sh
+export GIT_EDITOR
 
 test_expect_success 'cannot annotate non-existing HEAD' '
 	(MSG=3 && export MSG && test_must_fail git notes edit)
@@ -56,8 +56,17 @@ test_expect_success 'handle empty notes gracefully' '
 
 test_expect_success 'create notes' '
 	git config core.notesRef refs/notes/commits &&
+	MSG=b0 git notes edit &&
+	test ! -f .git/NOTES_EDITMSG &&
+	test 1 = $(git ls-tree refs/notes/commits | wc -l) &&
+	test b0 = $(git notes show) &&
+	git show HEAD^ &&
+	test_must_fail git notes show HEAD^
+'
+
+test_expect_success 'edit existing notes' '
 	MSG=b1 git notes edit &&
-	test ! -f .git/new-notes &&
+	test ! -f .git/NOTES_EDITMSG &&
 	test 1 = $(git ls-tree refs/notes/commits | wc -l) &&
 	test b1 = $(git notes show) &&
 	git show HEAD^ &&
@@ -110,19 +119,16 @@ test_expect_success 'show multi-line notes' '
 	git log -2 > output &&
 	test_cmp expect-multiline output
 '
-test_expect_success 'create -m and -F notes (setup)' '
+test_expect_success 'create -F notes (setup)' '
 	: > a4 &&
 	git add a4 &&
 	test_tick &&
 	git commit -m 4th &&
 	echo "xyzzy" > note5 &&
-	git notes edit -m spam -F note5 -m "foo
-bar
-baz"
+	git notes edit -F note5
 '
 
-whitespace="    "
-cat > expect-m-and-F << EOF
+cat > expect-F << EOF
 commit 15023535574ded8b1a89052b32673f84cf9582b8
 Author: A U Thor <author@example.com>
 Date:   Thu Apr 7 15:16:13 2005 -0700
@@ -130,21 +136,15 @@ Date:   Thu Apr 7 15:16:13 2005 -0700
     4th
 
 Notes:
-    spam
-$whitespace
     xyzzy
-$whitespace
-    foo
-    bar
-    baz
 EOF
 
-printf "\n" >> expect-m-and-F
-cat expect-multiline >> expect-m-and-F
+printf "\n" >> expect-F
+cat expect-multiline >> expect-F
 
-test_expect_success 'show -m and -F notes' '
+test_expect_success 'show -F notes' '
 	git log -3 > output &&
-	test_cmp expect-m-and-F output
+	test_cmp expect-F output
 '
 
 cat >expect << EOF
@@ -164,13 +164,7 @@ test_expect_success 'git log --pretty=raw does not show notes' '
 cat >>expect <<EOF
 
 Notes:
-    spam
-$whitespace
     xyzzy
-$whitespace
-    foo
-    bar
-    baz
 EOF
 test_expect_success 'git log --show-notes' '
 	git log -1 --pretty=raw --show-notes >output &&
@@ -179,17 +173,17 @@ test_expect_success 'git log --show-notes' '
 
 test_expect_success 'git log --no-notes' '
 	git log -1 --no-notes >output &&
-	! grep spam output
+	! grep xyzzy output
 '
 
 test_expect_success 'git format-patch does not show notes' '
 	git format-patch -1 --stdout >output &&
-	! grep spam output
+	! grep xyzzy output
 '
 
 test_expect_success 'git format-patch --show-notes does show notes' '
 	git format-patch --show-notes -1 --stdout >output &&
-	grep spam output
+	grep xyzzy output
 '
 
 for pretty in \
@@ -202,19 +196,22 @@ do
 	esac
 	test_expect_success "git show $pretty does$not show notes" '
 		git show $p >output &&
-		eval "$negate grep spam output"
+		eval "$negate grep xyzzy output"
 	'
 done
 
-test_expect_success 'create other note on a different notes ref (setup)' '
+test_expect_success 'create -m notes (setup)' '
 	: > a5 &&
 	git add a5 &&
 	test_tick &&
 	git commit -m 5th &&
-	GIT_NOTES_REF="refs/notes/other" git notes edit -m "other note"
+	git notes edit -m spam -m "foo
+bar
+baz"
 '
 
-cat > expect-other << EOF
+whitespace="    "
+cat > expect-m << EOF
 commit bd1753200303d0a0344be813e504253b3d98e74d
 Author: A U Thor <author@example.com>
 Date:   Thu Apr 7 15:17:13 2005 -0700
@@ -222,15 +219,46 @@ Date:   Thu Apr 7 15:17:13 2005 -0700
     5th
 
 Notes:
+    spam
+$whitespace
+    foo
+    bar
+    baz
+EOF
+
+printf "\n" >> expect-m
+cat expect-F >> expect-m
+
+test_expect_success 'show -m notes' '
+	git log -4 > output &&
+	test_cmp expect-m output
+'
+
+test_expect_success 'create other note on a different notes ref (setup)' '
+	: > a6 &&
+	git add a6 &&
+	test_tick &&
+	git commit -m 6th &&
+	GIT_NOTES_REF="refs/notes/other" git notes edit -m "other note"
+'
+
+cat > expect-other << EOF
+commit 387a89921c73d7ed72cd94d179c1c7048ca47756
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:18:13 2005 -0700
+
+    6th
+
+Notes:
     other note
 EOF
 
 cat > expect-not-other << EOF
-commit bd1753200303d0a0344be813e504253b3d98e74d
+commit 387a89921c73d7ed72cd94d179c1c7048ca47756
 Author: A U Thor <author@example.com>
-Date:   Thu Apr 7 15:17:13 2005 -0700
+Date:   Thu Apr 7 15:18:13 2005 -0700
 
-    5th
+    6th
 EOF
 
 test_expect_success 'Do not show note on other ref by default' '
-- 
1.6.6.405.g80ed6
Previous: Johan HerlandNext: Johan Herland
Message 15 of 31 in “[PATCHv12 00/23] git notes”
  1. Johan HerlandJan 27, 2010
  2. 01/23 Minor non-functional fixes to notes.cJohan Herland, Jan 27, 2010
  3. Junio C HamanoJan 27, 2010
  4. Johan HerlandJan 27, 2010
  5. 02/23 Notes API: get_commit_notes() -> format_note() + remove the commit restrictionJohan Herland, Jan 27, 2010
  6. 03/23 Add tests for checking correct handling of $GIT_NOTES_REF and core.notesRefJohan Herland, Jan 27, 2010
  7. 04/23 Notes API: init_notes(): Initialize the notes tree from the given notes refJohan Herland, Jan 27, 2010
  8. 05/23 Notes API: add_note(): Add note objects to the internal notes tree structureJohan Herland, Jan 27, 2010
  9. 06/23 Notes API: remove_note(): Remove note objects from the notes tree structureJohan Herland, Jan 27, 2010
  10. 07/23 Notes API: get_note(): Return the note annotating the given objectJohan Herland, Jan 27, 2010
  11. 08/23 Notes API: for_each_note(): Traverse the entire notes tree with a callbackJohan Herland, Jan 27, 2010
  12. 09/23 Notes API: write_notes_tree(): Store the notes tree in the databaseJohan Herland, Jan 27, 2010
  13. 10/23 Notes API: Allow multiple concurrent notes trees with new struct notes_treeJohan Herland, Jan 27, 2010
  14. 11/23 Refactor notes concatenation into a flexible interface for combining notesJohan Herland, Jan 27, 2010
  15. 12/23 Builtin-ify git-notesJohan Herland, Jan 27, 2010
  16. 13/23 t3301: Verify successful annotation of non-commitsJohan Herland, Jan 27, 2010
  17. 14/23 t3305: Verify that adding many notes with git-notes triggers increased fanoutJohan Herland, Jan 27, 2010
  18. 15/23 Teach notes code to properly preserve non-notes in the notes treeJohan Herland, Jan 27, 2010
  19. 16/23 Teach builtin-notes to remove empty notesJohan Herland, Jan 27, 2010
  20. 17/23 builtin-notes: Add "remove" subcommand for removing existing notesJohan Herland, Jan 27, 2010
  21. 18/23 t3305: Verify that removing notes triggers automatic fanout consolidationJohan Herland, Jan 27, 2010
  22. 19/23 Notes API: prune_notes(): Prune notes that belong to non-existing objectsJohan Herland, Jan 27, 2010
  23. 20/23 builtin-notes: Add "prune" subcommand for removing notes for missing objectsJohan Herland, Jan 27, 2010
  24. 21/23 Documentation: Generalize git-notes docs to 'objects' instead of 'commits'Johan Herland, Jan 27, 2010
  25. 22/23 builtin-notes: Add "list" subcommand for listing note objectsJohan Herland, Jan 27, 2010
  26. 23/23 builtin-notes: Add "add" subcommand for appending to note objectsJohan Herland, Jan 27, 2010
  27. Junio C HamanoJan 27, 2010
  28. Sverre RabbelierJan 27, 2010
  29. Johan HerlandJan 27, 2010
  30. Junio C HamanoJan 28, 2010
  31. Johan HerlandJan 28, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.