Merge ~sbeattie/ubuntu-cve-tracker/+git/ubuntu-cve-tracker:git-commit-hooks-pre-merge_hook into ubuntu-cve-tracker:master

Proposed by Steve Beattie
Status: Needs review
Proposed branch: ~sbeattie/ubuntu-cve-tracker/+git/ubuntu-cve-tracker:git-commit-hooks-pre-merge_hook
Merge into: ubuntu-cve-tracker:master
Diff against target: 91 lines (+15/-53)
3 files modified
Makefile (+2/-2)
dev/null (+0/-51)
scripts/git-hooks/pre-merge-commit (+13/-0)
Reviewer Review Type Date Requested Status
Eduardo Barretto Needs Information
Alex Murray Approve
Review via email: mp+457285@code.launchpad.net

Commit message

git-hooks: use pre-merge-commit over prepare-commit-msg

Replace/remove the prepare-commit-msg with a pre-merge-commit hook that
invokes the pre-commit infrastructure we already have.

When we originally started using git hooks to perform check-syntax
against UCT, the prepare-commit-msg hook was added (in 2019) because
git's pre-commit hook does not get performed on git merges, and there
were times when we would want to merge branches from people who do
not have enough of a setup for check-syntax to work.

However, git has had a pre-merge-commit hook since git 2.24 [1], and
present since at least Ubuntu 20.04 LTS, so we should make use of that.
Additionally, the prepare-commit-msg hook attempted to duplicate the
pre-commit hook but was never kept in sync, so choose here to use
the default upstream pre-merge-commit hook, which just invokes the
pre-commit hook.

[1] https://github.com/git/git/blob/da72936f544fec5a335e66432610e4cef4430991/Documentation/RelNotes/2.24.0.txt#L38

Description of the change

Thinking about git commit hooks from Amir's merge request reminded me that we were using the prepare-commit-msg hook to work around git's failure to use the pre-commit hook for merges. Git 2.24 added support for a pre-merge-commit hook, so this merge proposal converts UCT to use that, and set it up via 'make dev-setup'.

One thing this change does NOT do, however, is remove an existing prepare-commit-msg hook if it exists (which it should, because make dev-setup would install it into place).

To post a comment you must log in.
Revision history for this message
Alex Murray (alexmurray) wrote :

LGTM except I wonder if we should make this get rid of the old prepare-commit-msg hook as well to keep things cleaner.

review: Approve
Revision history for this message
Eduardo Barretto (ebarretto) wrote :

hey Steve, is there anything else for this PR?

review: Needs Information
Revision history for this message
Mark Esler (eslerm) wrote :

LGTM. Just commenting.

Not sure if this is related: currently if you recieve a warning and fix the issue, a subsequent git-commit will complete even without git-add'ing the new changes first. As in, git-hooks inspect the present state of a file, not the form they are staged as.

Unmerged commits

2963895... by Steve Beattie

git-hooks: use pre-merge-commit over prepare-commit-msg

Replace/remove the prepare-commit-msg with a pre-merge-commit hook that
invokes the pre-commit infrastructure we already have.

When we originally started using git hooks to perform check-syntax
against UCT, the prepare-commit-msg hook was added (in 2019) because
git's pre-commit hook does not get performed on git merges, and there
were times when we would want to merge branches from people who do
not have enough of a setup for check-syntax to work.

However, git has had a pre-merge-commit hook since git 2.24 [1], and
present since at least Ubuntu 20.04 LTS, so we should make use of that.
Additionally, the prepare-commit-msg hook attempted to duplicate the
pre-commit hook but was never kept in sync, so choose here to use
the default upstream pre-merge-commit hook, which just invokes the
pre-commit hook.

[1] https://github.com/git/git/blob/da72936f544fec5a335e66432610e4cef4430991/Documentation/RelNotes/2.24.0.txt#L38

Signed-off-by: Steve Beattie <email address hidden>

Succeeded
[SUCCEEDED] unit-tests:0 (build)
[SUCCEEDED] check-cves:0 (build)
12 of 2 results

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/Makefile b/Makefile
index 2c0abb7..bfbafc3 100644
--- a/Makefile
+++ b/Makefile
@@ -61,8 +61,8 @@ dev_setup:
61 # install git prepare message hook; this is used to do a \61 # install git prepare message hook; this is used to do a \
62 # check-syntax run for merge commits, which the regular git \62 # check-syntax run for merge commits, which the regular git \
63 # commit hook mind-bogglingly does not get run on \63 # commit hook mind-bogglingly does not get run on \
64 echo install -m 755 -b -S .backup scripts/git-hooks/prepare-commit-msg .git/hooks ; \64 echo install -m 755 -b -S .backup scripts/git-hooks/pre-merge-commit .git/hooks ; \
65 install -m 755 -b -S .backup scripts/git-hooks/prepare-commit-msg .git/hooks ; \65 install -m 755 -b -S .backup scripts/git-hooks/pre-merge-commit .git/hooks ; \
66 # install git commit hooks in UST repo if configured \66 # install git commit hooks in UST repo if configured \
67 if [ -n "$${UST}" ] ; then \67 if [ -n "$${UST}" ] ; then \
68 echo install -m 755 -b -S .backup scripts/git-hooks/pre-commit-pyflakes3 "$${UST}/.git/hooks/pre-commit" ; \68 echo install -m 755 -b -S .backup scripts/git-hooks/pre-commit-pyflakes3 "$${UST}/.git/hooks/pre-commit" ; \
diff --git a/scripts/git-hooks/pre-merge-commit b/scripts/git-hooks/pre-merge-commit
69new file mode 10075569new file mode 100755
index 0000000..399eab1
--- /dev/null
+++ b/scripts/git-hooks/pre-merge-commit
@@ -0,0 +1,13 @@
1#!/bin/sh
2#
3# An example hook script to verify what is about to be committed.
4# Called by "git merge" with no arguments. The hook should
5# exit with non-zero status after issuing an appropriate message to
6# stderr if it wants to stop the merge commit.
7#
8# To enable this hook, rename this file to "pre-merge-commit".
9
10. git-sh-setup
11test -x "$GIT_DIR/hooks/pre-commit" &&
12 exec "$GIT_DIR/hooks/pre-commit"
13:
diff --git a/scripts/git-hooks/prepare-commit-msg b/scripts/git-hooks/prepare-commit-msg
0deleted file mode 10075514deleted file mode 100755
index 629035d..0000000
--- a/scripts/git-hooks/prepare-commit-msg
+++ /dev/null
@@ -1,51 +0,0 @@
1#!/bin/sh
2#
3# git prepare-commit-msg hook
4# Sadly, on git merge, the pre-commit hook is not invoked. So we use
5# this hook instead to check if a merge has borked cve syntax.
6# Unfortunately, if the hook fails, then you are left in an incomplete
7# merge state.
8#
9
10if ! [ "$1" = ".git/MERGE_MSG" ] ; then
11 # this was not triggered by a git merge, so skip check (we do it
12 # elsewhere for regular commits *and* it's handled more cleanly
13 # there.
14 exit 0
15fi
16
17if git rev-parse --verify HEAD >/dev/null 2>&1
18then
19 against=HEAD
20else
21 # Initial commit: diff against an empty tree object
22 against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
23fi
24
25# Redirect output to stderr.
26exec 1>&2
27
28cleanup() {
29 if [ -n "${modified_files}" ] && [ -f "${modified_files}" ] ; then
30 rm "${modified_files}"
31 fi
32}
33
34set -e
35
36trap cleanup EXIT INT HUP
37
38if [ -z "${UCT_IGNORE_CHECK_SYNTAX}" ] ; then
39 modified_files="$(mktemp -t ucthook-XXXXXXXXXX)"
40 # check-syntax needs full path to files so prepend it via sed
41 dir=$(git rev-parse --show-toplevel)
42 git diff --cached --name-only --diff-filter=AM $against | sed "s|^|${dir}/|" > "${modified_files}"
43
44 if ! "${UCT}/scripts/check-syntax" --verbose --filelist "${modified_files}" ; then
45 read -p 'Found syntax errors. Enter "ignore" to continue or anything else to exit: ' answer < /dev/tty
46 if ! [ "${answer}" = "ignore" ] ; then
47 echo 'Aborting, use git merge --abort to undo the merge action'
48 exit 1
49 fi
50 fi
51fi

Subscribers

People subscribed via source and target branches