adding reviewed-by tags (semi)automatically

Michael Adam obnox at samba.org
Fri Nov 2 09:48:12 MDT 2012


Hi,

I was looking for a means of easily adding Reviewed-by: tags
to commit messages without the need to do a lot of typing.

There is no parameter like -s/--signoff to git, so we need
something else.

Firstly, I suggest to have a local git clone for
reviews. Then you don't have to interrupt your development work
by switching branches for doing reviews.

Attached are two git hook scripts that I am currently
experimenting with. There are three hooks involved:

- The prepare-commit-msg hook script is invoked by "git commit"
  before you enter the editor.
- The commit-msg hook is invoked by "git commit" after you finished
  editing the commit message.
- The applypatch-msg is invoked by "git am".
  I did not attach it: the default sample, if activated, invokes
  the commit-msg script if present.

The hooks must be placed into the .git/hooks directory
of your git checkout and made executable. The ".obnox" suffix
must be stripped from the scripts I attached.

Then the attached "prepare-commit-msg" script and "commit-msg"
scripts both add the Reviewed-by: Tag of the committer if it
is not already present in the message. In order for them to
add the tag, you have to set the environment variable REVIEW to some
value.

- If you want to see the Reviewed-by Tag in you editor (for e.g.
  "git commit --amend"), you should enable the prepare-commit-msg hook.
- If you want to do non-editing amends, e.g. with "git commit
  --amend --no-edit", then you should activate the commit-msg
  hook.
- If you want "git am" to add the tag, you should activate the
  commit-msg hook and the default applypatch-msg hook.

Examples:

$ REVIEW=1 git commit --amend
$ REVIEW=1 git commit --amend --no-edit
$ REVIEW=1 EDITOR=/bin/true git commit --amend
$ REVIEW=1 git am patcfile


Note: if you activate both prepare-commit-msg and commit-msg
and call

$ REVIEW=1 git commit --amend

you'll see the tag in the editor, but if you remove it
then the commit-msg hook will re-add it...


If you want to modify various commit messages, you can use
this with the "x" feature of "git rebase -i".

In the editor offered by (e.g.) "git commit -i origin/master",
add this line after each commit you want to modify:

x REVIEW=1 git commit --amend --no-edit

And this should do the trick..

Cheers - Michael

-------------- next part --------------
#!/bin/sh
#
# An example hook script to check the commit log message.
# Called by git-commit with one argument, the name of the file
# that has the commit message.  The hook should exit with non-zero
# status after issuing an appropriate message if it wants to stop the
# commit.  The hook is allowed to edit the commit message file.
#
# To enable this hook, rename this file to "commit-msg".

# Uncomment the below to add a Signed-off-by line to the message.
# Doing this in a hook is a bad idea in general, but the prepare-commit-msg
# hook is more suited to it.
#
# SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p')
# grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1"

# This example catches duplicate Signed-off-by lines.

#test "" = "$(grep '^Signed-off-by: ' "$1" |
#	 sort | uniq -c | sed -e '/^[ 	]*1[ 	]/d')" || {
#	echo >&2 Duplicate Signed-off-by lines.
#	exit 1
#}


# if a the environment variable "REVIEW" is set,
# then add a Reviewed-by: tag to the commit message,
# if it is not already present:

test  "x$REVIEW" = "x" || {
	echo "commit-msg: REVIEW is set"
	RB=$(git var GIT_COMMITTER_IDENT | sed -n 's/^\(.*>\).*$/Reviewed-by: \1/p')
	grep -qs "^$RB" "$1" ||  { \
		echo "$RB" >> $1 ;
	}
}

-------------- next part --------------
#!/bin/sh
#
# An example hook script to prepare the commit log message.
# Called by git-commit with the name of the file that has the
# commit message, followed by the description of the commit
# message's source.  The hook's purpose is to edit the commit
# message file.  If the hook fails with a non-zero status,
# the commit is aborted.
#
# To enable this hook, rename this file to "prepare-commit-msg".

# This hook includes three examples.  The first comments out the
# "Conflicts:" part of a merge commit.
#
# The second includes the output of "git diff --name-status -r"
# into the message, just before the "git status" output.  It is
# commented because it doesn't cope with --amend or with squashed
# commits.
#
# The third example adds a Signed-off-by line to the message, that can
# still be edited.  This is rarely a good idea.

### case "$2,$3" in
###   merge,)
###     perl -i.bak -ne 's/^/# /, s/^# #/#/ if /^Conflicts/ .. /#/; print' "$1" ;;
### 
### # ,|template,)
### #   perl -i.bak -pe '
### #      print "\n" . `git diff --cached --name-status -r`
### #	 if /^#/ && $first++ == 0' "$1" ;;
### 
###   *) ;;
### esac

# SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p')
# grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1"

# if a the environment variable "REVIEW" is set,
# then add a Reviewed-by: tag to the commit message,
# if it is not already present:

test  "x$REVIEW" = "x" || {
	RB=$(git var GIT_COMMITTER_IDENT | sed -n 's/^\(.*>\).*$/Reviewed-by: \1/p')
	grep -qs "^$RB" "$1" ||  \
		perl -i.bak -pe 'if (/^#/ && $first++ == 0) {
			$rb = `git var GIT_COMMITTER_IDENT` ;
			$rb =~ s/^(.*>).*$/Reviewed-by: $1/ ;
			print "$rb\n";
		}' "$1"
}

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 206 bytes
Desc: not available
URL: <http://lists.samba.org/pipermail/samba-technical/attachments/20121102/9317b214/attachment.pgp>


More information about the samba-technical mailing list