From 4416262f59f5e74d3991fdf9c06ad776eca50663 Mon Sep 17 00:00:00 2001 From: Jim Porter Date: Wed, 12 Apr 2023 23:03:31 -0700 Subject: Add Git hooks to check filenames listed in the commit message See . * build-aux/git-hooks/commit-msg-files.awk: * build-aux/git-hooks/post-commit: * build-aux/git-hooks/pre-push: New files... * autogen.sh: ... add them. --- build-aux/git-hooks/commit-msg-files.awk | 113 +++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 build-aux/git-hooks/commit-msg-files.awk (limited to 'build-aux/git-hooks/commit-msg-files.awk') diff --git a/build-aux/git-hooks/commit-msg-files.awk b/build-aux/git-hooks/commit-msg-files.awk new file mode 100644 index 00000000000..3856e474d3e --- /dev/null +++ b/build-aux/git-hooks/commit-msg-files.awk @@ -0,0 +1,113 @@ +# Check the file list of GNU Emacs change log entries for each commit SHA. + +# Copyright 2023 Free Software Foundation, Inc. + +# This file is part of GNU Emacs. + +# GNU Emacs is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# GNU Emacs is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with GNU Emacs. If not, see . + +### Commentary: + +# This script accepts a list of (unabbreviated) Git commit SHAs, and +# will then iterate over them to check that any files mentioned in the +# commit message are actually present in the commit's diff. If not, +# it will print out the incorrect file names and return 1. + +# You can also pass "-v reason=pre-push", which will add more-verbose +# output, indicating the abbreviated commit SHA and first line of the +# commit message for any improper commits. + +### Code: + +function get_commit_changes(commit_sha, changes, cmd, i, j, len, \ + bits, filename) { + # Collect all the files touched in the specified commit. + cmd = ("git log -1 --name-status --format= " commit_sha) + while ((cmd | getline) > 0) { + for (i = 2; i <= NF; i++) { + len = split($i, bits, "/") + for (j = 1; j <= len; j++) { + if (j == 1) + filename = bits[j] + else + filename = filename "/" bits[j] + changes[filename] = 1 + } + } + } + close(cmd) +} + +function check_commit_msg_files(commit_sha, verbose, changes, good, \ + cmd, msg, filenames_str, filenames, i) { + get_commit_changes(commit_sha, changes) + good = 1 + + cmd = ("git log -1 --format=%B " commit_sha) + while ((cmd | getline) > 0) { + if (verbose && ! msg) + msg = $0 + + # Find lines that reference files. We look at any line starting + # with "*" (possibly prefixed by "; ") where the file part starts + # with an alphanumeric character. The file part ends if we + # encounter any of the following characters: [ ( < { : + if (/^(; )?\*[ \t]+[[:alnum:]]/ && match($0, /[[:alnum:]][^[(<{:]*/)) { + # There might be multiple files listed on this line, separated + # by spaces (and possibly a comma). Iterate over each of them. + split(substr($0, RSTART, RLENGTH), filenames, ",?([[:blank:]]+|$)") + + for (i in filenames) { + # Remove trailing slashes from any directory entries. + sub(/\/$/, "", filenames[i]) + + if (length(filenames[i]) && ! (filenames[i] in changes)) { + if (good) { + # Print a header describing the error. + if (verbose) + printf("In commit %s \"%s\"...\n", substr(commit_sha, 1, 10), msg) + printf("Files listed in commit message, but not in diff:\n") + } + printf(" %s\n", filenames[i]) + good = 0 + } + } + } + } + close(cmd) + + return good +} + +BEGIN { + if (reason == "pre-push") + verbose = 1 +} + +/^[a-z0-9]{40}$/ { + if (! check_commit_msg_files($0, verbose)) { + status = 1 + } +} + +END { + if (status != 0) { + if (reason == "pre-push") + error_msg = "Push aborted" + else + error_msg = "Bad commit message" + printf("%s; please see the file 'CONTRIBUTE'\n", error_msg) + } + exit status +} -- cgit v1.2.1 From 9914de503bd39b5a8b55a472d7cee6832a72e61d Mon Sep 17 00:00:00 2001 From: Jim Porter Date: Fri, 21 Apr 2023 10:06:49 -0700 Subject: Improve the logic of the file entry Git hooks to support more cases In addition to starting with a "*", file entries now need a ":" somewhere in them. This helps reduce false positives with bulleted lists. Also, support multiple files separated by commas after a "*". * build-aux/git-hooks/commit-msg-files.awk (check_commit_msg_files): Accumulate file entries over multiple lines to support the above. --- build-aux/git-hooks/commit-msg-files.awk | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) (limited to 'build-aux/git-hooks/commit-msg-files.awk') diff --git a/build-aux/git-hooks/commit-msg-files.awk b/build-aux/git-hooks/commit-msg-files.awk index 3856e474d3e..2117681343f 100644 --- a/build-aux/git-hooks/commit-msg-files.awk +++ b/build-aux/git-hooks/commit-msg-files.awk @@ -59,15 +59,28 @@ function check_commit_msg_files(commit_sha, verbose, changes, good, \ if (verbose && ! msg) msg = $0 - # Find lines that reference files. We look at any line starting - # with "*" (possibly prefixed by "; ") where the file part starts - # with an alphanumeric character. The file part ends if we - # encounter any of the following characters: [ ( < { : - if (/^(; )?\*[ \t]+[[:alnum:]]/ && match($0, /[[:alnum:]][^[(<{:]*/)) { - # There might be multiple files listed on this line, separated + # Find file entries in the commit message. We look at any line + # starting with "*" (possibly prefixed by "; ") followed by a ":", + # possibly on a different line. If we encounter a blank line + # without seeing a ":", then we don't treat that as a file entry. + + # Accumulate the contents of a (possible) file entry. + if (/^[ \t]*$/) + filenames_str = "" + else if (/^(; )?\*[ \t]+[[:alnum:]]/) + filenames_str = $0 + else if (filenames_str) + filenames_str = (filenames_str $0) + + # We have a file entry; analyze it. + if (filenames_str && /:/) { + # Delete the leading "*" and any trailing information. + sub(/^(; )?\*[ \t]+/, "", filenames_str) + sub(/[ \t]*[[(<:].*$/, "", filenames_str) + + # There might be multiple files listed in this entry, separated # by spaces (and possibly a comma). Iterate over each of them. - split(substr($0, RSTART, RLENGTH), filenames, ",?([[:blank:]]+|$)") - + split(filenames_str, filenames, ",[ \t]+") for (i in filenames) { # Remove trailing slashes from any directory entries. sub(/\/$/, "", filenames[i]) @@ -83,6 +96,8 @@ function check_commit_msg_files(commit_sha, verbose, changes, good, \ good = 0 } } + + filenames_str = "" } } close(cmd) -- cgit v1.2.1 From 3ce462c8fda02c29e79ec80fb389fc44d550142a Mon Sep 17 00:00:00 2001 From: Jim Porter Date: Sun, 23 Apr 2023 11:43:07 -0700 Subject: When examining merge commits in our Git hooks, only check the first parent This does two things: 1. We can properly validate log entries in merge commits. 2. We don't check commits that were merged in from other branches. * build-aux/git-hooks/commit-msg-files.awk (get_commit_changes): Get the changes compared to the first parent. * build-aux/git-hooks/pre-push: Only get the first parent of merge commits when returning the rev-list, and only check "master" or "emacs-NN" branches. --- build-aux/git-hooks/commit-msg-files.awk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'build-aux/git-hooks/commit-msg-files.awk') diff --git a/build-aux/git-hooks/commit-msg-files.awk b/build-aux/git-hooks/commit-msg-files.awk index 2117681343f..5c9b70a5de5 100644 --- a/build-aux/git-hooks/commit-msg-files.awk +++ b/build-aux/git-hooks/commit-msg-files.awk @@ -33,7 +33,7 @@ function get_commit_changes(commit_sha, changes, cmd, i, j, len, \ bits, filename) { # Collect all the files touched in the specified commit. - cmd = ("git log -1 --name-status --format= " commit_sha) + cmd = ("git show --name-status --first-parent --format= " commit_sha) while ((cmd | getline) > 0) { for (i = 2; i <= NF; i++) { len = split($i, bits, "/") -- cgit v1.2.1