git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Johan Herland <johan@herland.net>
To: git@vger.kernel.org
Cc: Linus Torvalds <torvalds@linux-foundation.org>,
	Junio C Hamano <junkio@cox.net>
Subject: [PATCH 01/15] git-note: Add git-note command for adding/listing/deleting git notes
Date: Sun, 27 May 2007 16:09:32 +0200	[thread overview]
Message-ID: <200705271609.33033.johan@herland.net> (raw)
In-Reply-To: <200705271608.02122.johan@herland.net>

Synopsis:
'git-note' [-m <msg> | -F <file>] [<head>]
'git-note' -l [<name>...]
'git-note' -d <name>...

'git-note' without any parameters creates 'note' associated with HEAD.
Specifying an object name/ref to 'git-note' will cause the note to
be associated with that object instead.

`git-note -l [<name>...]` lists notes associated with the given <name>s.
If no <name> is given, all notes are listed. The given <name> may itself
be a note object, in which case it is listed along with its associated
notes (if any).

`git-note -d <name>...` deletes the notes associated with the given
<name>s. The given <name> may itself be a note object, in which case it is
deleted along with its associated notes (if any).

git-note.sh is partly based on git-tag.sh

Signed-off-by: Johan Herland <johan@herland.net>
---
 git-note.sh |  227 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 227 insertions(+), 0 deletions(-)
 create mode 100755 git-note.sh

diff --git a/git-note.sh b/git-note.sh
new file mode 100755
index 0000000..4f539e2
--- /dev/null
+++ b/git-note.sh
@@ -0,0 +1,227 @@
+#!/bin/sh
+
+USAGE='-l [<name>...] | -d <name>... | [-m <msg> | -F <file>] [<head>]'
+LONG_USAGE='Add, delete and show git object notes
+	-l	list/show notes associated with the given object.
+	-d	delete the notes associated with the given object.
+	-m	use the given note message (instead of prompting).
+	-F 	Take the note message from the given file (instead of prompting).
+For -l and -d, the given <name> may be a note object itself, in which case
+it will be included in the listing/deletion.
+When <head> is not given, it defaults to HEAD.'
+SUBDIRECTORY_OK='Yes'
+. git-sh-setup
+
+
+# Show the note identified by $1
+show_note () {
+	authordate=$(git-cat-file -p $1 | grep -m1 '^tagger ' | cut -c8-)
+	echo
+	echo "--- by $authordate"
+	git-cat-file -p $1 | tail -n+6
+	return 0
+}
+
+# Show notes below the refs/notes directory given in $1
+show_notes_below_dir () {
+	prev_object=
+	for note_data in $(git-for-each-ref --shell \
+			--sort=-taggerdate --sort=*authordate \
+			--format='note=%(objectname);object=%(*objectname);object_type=%(*objecttype);object_tag=%(*tag);' \
+			$1)
+	do
+		eval "$note_data"
+
+		# Separate note objects from regular tags
+		if test "$object_type" = "tag" -a -n "$(echo "$object_tag" | grep '^note-')"; then
+			object_type="note"
+			object_tag=""
+		fi
+
+		description=
+		# Create description based on object type
+		case "$object_type" in
+		commit)
+			description="$(git-show --pretty='format:%h (%s) by %an <%ae> on %aD' $object | head -n1)"
+			;;
+		tag)
+			description="$object ($object_tag)"
+			;;
+		*)
+			description="$object"
+			;;
+		esac
+
+		if test "$object" != "$prev_object"; then
+			test -z "$prev_object" || echo
+			echo "=== Notes on $object_type $description"
+		fi
+		show_note $note
+		prev_object="$object"
+	done
+	return 0
+}
+
+# Show the notes associated with the objects in $@
+show_associated_notes () {
+	if test "$#" = "0"; then
+		show_notes_below_dir "refs/notes"
+		return 0
+	fi
+
+	while case "$#" in 0) break ;; esac
+	do
+		obj=$(git-rev-parse --verify "$1") || \
+				die "error: failed to find object named '$1'"
+
+		# If $obj is a note itself, handle it first
+		if test "$(git-cat-file -t $obj)" = "tag"; then
+			if test -n "$(git-cat-file -p $obj | grep '^tag note-')"; then
+				echo
+				echo "=== Note $obj"
+				show_note "$obj"
+			fi
+		fi
+
+		# List all notes associated with $obj.
+		show_notes_below_dir "refs/notes/$obj"
+
+		shift
+	done
+	return 0
+}
+
+# Delete the noteref in $1 pointing to the note identified by the SHA1 in $2
+delete_note () {
+	git-update-ref -m "note: delete" -d "$1" "$2"
+	return 0
+}
+
+# Delete all notes associated with object in $1
+delete_object_notes () {
+	for note_data in $(git-for-each-ref --shell \
+			--sort=taggerdate --sort=*authordate \
+			--format='note=%(objectname);object=%(*objectname);object_type=%(*objecttype);noteref=%(refname)' \
+			refs/notes/$1)
+	do
+		eval "$note_data"
+		echo "Deleting note $note associated with $object_type $object"
+		delete_note "$noteref" "$note"
+	done
+
+	# Try to get rid of $object's directory below refs/notes as well
+	if test -d "$GIT_DIR/refs/notes/$1"; then
+		rmdir "$GIT_DIR/refs/notes/$1"
+	fi
+	return 0
+}
+
+# Delete the notes associated with the objects in $@
+delete_associated_notes () {
+	while case "$#" in 0) break ;; esac
+	do
+		obj=$(git-rev-parse --verify "$1") || \
+				die "error: failed to find object named '$1'"
+
+		# If $obj is a note itself, handle it first
+		if test "$(git-cat-file -t $obj)" = "tag"; then
+			objref="$(git-cat-file -p $obj | grep -m1 '^object ' | cut -c8-)"
+			if test -n "$objref"; then
+				echo "Deleting note $obj"
+				delete_note "refs/notes/$objref/$obj" "$obj"
+			fi
+		fi
+
+		# Delete all notes associated with $obj
+		delete_object_notes "$obj"
+
+		shift
+	done
+	return 0
+}
+
+message_given=
+message=
+while case "$#" in 0) break ;; esac
+do
+	case "$1" in
+	-l)
+		shift
+		show_associated_notes "$@"
+		exit $?
+		;;
+	-d)
+		shift
+		if test "$#" = "0"; then
+			die "error: option -d needs one or more objects"
+		fi
+		delete_associated_notes "$@"
+		exit $?
+		;;
+	-m)
+		shift
+		message="$1"
+		if test "$#" = "0"; then
+			die "error: option -m needs an argument"
+		else
+			message_given=1
+		fi
+		;;
+	-F)
+		shift
+		if test "$#" = "0"; then
+			die "error: option -F needs an argument"
+		else
+			message="$(cat "$1")"
+			message_given=1
+		fi
+		;;
+	-*)
+		usage
+		;;
+	*)
+		break
+		;;
+	esac
+	shift
+done
+
+object=$(git-rev-parse --verify --default HEAD "$@") || exit 1
+type=$(git-cat-file -t $object) || exit 1
+tagger=$(git-var GIT_COMMITTER_IDENT) || exit 1
+
+# Process note message
+trap 'rm -f "$GIT_DIR"/NOTE_TMP* "$GIT_DIR"/NOTE_FINALMSG "$GIT_DIR"/NOTE_EDITMSG' 0
+
+if test -z "$message_given"; then
+	( echo ""
+	  echo "#"
+	  echo "# Write a note on $type $object"
+	  echo "#" ) > "$GIT_DIR/NOTE_EDITMSG"
+	${VISUAL:-${EDITOR:-vi}} "$GIT_DIR/NOTE_EDITMSG" || exit
+else
+	printf '%s\n' "$message" >"$GIT_DIR/NOTE_EDITMSG"
+fi
+
+grep -v '^#' <"$GIT_DIR/NOTE_EDITMSG" |
+git-stripspace >"$GIT_DIR/NOTE_FINALMSG"
+
+test -s "$GIT_DIR/NOTE_FINALMSG" -o -n "$message_given" || {
+	echo >&2 "No note?"
+	exit 1
+}
+
+name="note-`( printf 'object %s\ntagger %s\n\n' "$tagger"; \
+              cat "$GIT_DIR/NOTE_FINALMSG" ) \
+            | sha1sum | cut -d" " -f1`"
+
+git-check-ref-format "notes/$object/$name" ||
+	die "Failed to create a valid note name. (internal git-note error)"
+
+( printf 'object %s\ntype %s\ntag %s\ntagger %s\n\n' \
+		"$object"  "$type"  "$name" "$tagger";
+		cat "$GIT_DIR/NOTE_FINALMSG" ) >"$GIT_DIR/NOTE_TMP"
+rm -f "$GIT_DIR/NOTE_FINALMSG"
+note=$(git-mktag < "$GIT_DIR/NOTE_TMP")
+
+git-update-ref "refs/notes/$object/$note" "$note"
-- 
1.5.2.101.gee49f

  reply	other threads:[~2007-05-27 14:09 UTC|newest]

Thread overview: 53+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-05-09 19:20 [RFC] Second parent for reverts Daniel Barkalow
2007-05-09 20:07 ` Johannes Schindelin
2007-05-09 20:22   ` Shawn O. Pearce
2007-05-09 22:26     ` Johan Herland
2007-05-09 21:54 ` Junio C Hamano
2007-05-09 22:16   ` Linus Torvalds
2007-05-10 16:35     ` Linus Torvalds
2007-05-10 18:06       ` Johan Herland
2007-05-10 18:22         ` Linus Torvalds
2007-05-27 14:08           ` [PATCH 00/15] git-note: A mechanisim for providing free-form after-the-fact annotations on commits Johan Herland
2007-05-27 14:09             ` Johan Herland [this message]
2007-05-27 14:10             ` [PATCH 02/15] git-note: (Documentation) Add git-note manual page Johan Herland
2007-05-27 14:11             ` [PATCH 03/15] git-note: (Administrivia) Add git-note to Makefile, .gitignore, etc Johan Herland
2007-05-27 14:11             ` [PATCH 04/15] git-note: (Plumbing) Add plumbing-level support for git notes Johan Herland
2007-05-27 14:12             ` [PATCH 05/15] git-note: (Plumbing) Add support for git notes to git-rev-parse and git-show-ref Johan Herland
2007-05-27 14:13             ` [PATCH 06/15] git-note: (Documentation) Explain the new '--notes' option " Johan Herland
2007-05-27 14:13             ` [PATCH 07/15] git-note: (Almost plumbing) Add support for git notes to git-pack-refs and git-fsck Johan Herland
2007-05-27 14:14             ` [PATCH 08/15] git-note: (Decorations) Add note decorations to "git-{log,show,whatchanged} --decorate" Johan Herland
2007-05-27 14:14             ` [PATCH 09/15] git-note: (Documentation) Explain new behaviour of --decorate in git-{log,show,whatchanged} Johan Herland
2007-05-27 14:15             ` [PATCH 10/15] git-note: (Transfer) Teach git-clone how to clone notes Johan Herland
2007-05-27 14:15             ` [PATCH 11/15] git-note: (Transfer) Teach git-fetch to auto-follow notes Johan Herland
2007-05-27 14:15             ` [PATCH 12/15] git-note: (Transfer) Teach git-push to push notes when --all or --notes is given Johan Herland
2007-05-27 14:16             ` [PATCH 13/15] git-note: (Documentation) Explain the new --notes option to git-push Johan Herland
2007-05-27 14:16             ` [PATCH 14/15] git-note: (Tests) Add tests for git-note and associated functionality Johan Herland
2007-05-27 14:17             ` [PATCH 15/15] git-note: Add display of notes to gitk Johan Herland
2007-05-27 20:09             ` [PATCH 00/15] git-note: A mechanisim for providing free-form after-the-fact annotations on commits Junio C Hamano
2007-05-28  0:29               ` Johan Herland
2007-05-28  0:59               ` Jakub Narebski
2007-05-28  4:37             ` Linus Torvalds
2007-05-28 10:54               ` Johan Herland
2007-05-28 16:28                 ` Linus Torvalds
2007-05-28 16:40                   ` Johan Herland
2007-05-28 16:58                     ` Linus Torvalds
2007-05-28 17:48                       ` Johan Herland
2007-05-28 20:45                         ` Junio C Hamano
2007-05-28 21:35                           ` Shawn O. Pearce
2007-05-28 23:37                             ` Johannes Schindelin
2007-05-29  3:12                             ` Linus Torvalds
2007-05-29  3:22                               ` Shawn O. Pearce
2007-05-29  7:04                                 ` Jakub Narebski
2007-05-29 11:04                               ` Andy Parkins
2007-05-29 11:12                                 ` Johannes Schindelin
2007-05-29  7:06                           ` Johan Herland
2007-05-29  8:22                             ` Jeff King
2007-05-29  9:23                               ` Johan Herland
2007-05-28 20:45                 ` Junio C Hamano
2007-05-28 21:19                   ` Shawn O. Pearce
2007-05-28 23:46                   ` [PATCH] Add fsck_verify_ref_to_tag_object() to verify that refname matches name stored in tag object Johan Herland
2007-05-28 17:29               ` [PATCH 00/15] git-note: A mechanisim for providing free-form after-the-fact annotations on commits Michael S. Tsirkin
2007-05-28 17:42                 ` Michael S. Tsirkin
2007-05-28 17:58                   ` Johan Herland
2007-05-10 22:33       ` [RFC] Second parent for reverts Martin Langhoff
2007-05-10  1:43   ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200705271609.33033.johan@herland.net \
    --to=johan@herland.net \
    --cc=git@vger.kernel.org \
    --cc=junkio@cox.net \
    --cc=torvalds@linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).