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

Subscribers

People subscribed via source and target branches