From a949fa5d202f0a1f812d7630f3e5bf0f02ca4e98 Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Sat, 22 Aug 2009 18:17:46 -0700 Subject: Automatically install Gerrit Code Review's commit-msg hook Most users of repo are also using Gerrit Code Review, and will want the commit-msg hook to be automatically installed into their local projects so that Change-Ids are assigned when commits are created, not when they are first uploaded. Change-Id: Ide42e93b068832f099d68a79c2863d22145d05ad Signed-off-by: Shawn O. Pearce --- hooks/commit-msg | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100755 hooks/commit-msg (limited to 'hooks/commit-msg') diff --git a/hooks/commit-msg b/hooks/commit-msg new file mode 100755 index 00000000..ecd6a20b --- /dev/null +++ b/hooks/commit-msg @@ -0,0 +1,44 @@ +#!/bin/sh + +MSG="$1" + +# Check for, and add if missing, a unique Change-Id +# +add_ChangeId() { + if grep '^Change-Id: ' "$MSG" >/dev/null + then + return + fi + + id=$(_gen_ChangeId) + out="$MSG.new" + ftt="$MSG.footers" + sed -e '/^[A-Za-z][A-Za-z0-9-]*: /,$d' <"$MSG" >"$out" + sed -ne '/^[A-Za-z][A-Za-z0-9-]*: /,$p' <"$MSG" >"$ftt" + if ! [ -s "$ftt" ] + then + echo >>"$out" + fi + echo "Change-Id: I$id" >>"$out" + cat "$ftt" >>"$out" + mv -f "$out" "$MSG" + rm -f "$out" "$ftt" +} +_gen_ChangeIdInput() { + echo "tree $(git write-tree)" + if parent=$(git rev-parse HEAD^0 2>/dev/null) + then + echo "parent $parent" + fi + echo "author $(git var GIT_AUTHOR_IDENT)" + echo "committer $(git var GIT_COMMITTER_IDENT)" + echo + cat "$MSG" +} +_gen_ChangeId() { + _gen_ChangeIdInput | + git hash-object -t commit --stdin +} + + +add_ChangeId -- cgit v1.2.3-54-g00ecf