summaryrefslogtreecommitdiff
path: root/build-aux/git-hooks/pre-push
blob: 6ff59102fd79ebcb79360f2646afad26b3dc5b1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/bin/sh
# Check the file list of GNU Emacs change log entries before pushing.

# 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 <https://www.gnu.org/licenses/>.

### Commentary:

# This hook runs before pushing a series of commits and checks that
# the files mentioned in each commit message match the diffs.  This
# helps ensure that the resulting change logs are correct, which
# should prevent errors when generating etc/AUTHORS.

# These checks also happen in the "post-commit" hook (which see), but
# that hook can't abort a commit; it just advises the committer to fix
# the commit so that this hook runs without errors.

### Code:

# Prefer gawk if available, as it handles NUL bytes properly.
if type gawk >/dev/null 2>&1; then
  awk="gawk"
else
  awk="awk"
fi

# Standard input receives lines of the form:
#   <local ref> SP <local sha> SP <remote ref> SP <remote sha> LF
$awk -v origin_name="$1" '
  # If the local SHA is all zeroes, ignore it.
  $2 ~ /^0{40}$/ {
    next
  }

  # Check any lines with a valid local SHA and whose remote ref is
  # master or an emacs-NN release branch.  (We want to avoid checking
  # feature or scratch branches here.)
  $2 ~ /^[a-z0-9]{40}$/ && $3 ~ /^refs\/heads\/(master|emacs-[0-9]+)$/ {
    newref = $2
    # If the remote SHA is all zeroes, this is a new object to be
    # pushed (likely a branch)...
    if ($4 ~ /^0{40}$/) {
      back = 0
      # ... Go backwards until we find a SHA on an origin branch.
      # Stop trying after 1000 commits, just in case...
      for (back = 0; back < 1000; back++) {
        cmd = ("git branch -r -l '\''" origin_name "/*'\''" \
               " --contains " newref "~" back)
        rv = (cmd | getline)
        close(cmd)
        if (rv > 0)
          break;
      }

      cmd = ("git rev-parse " newref "~" back)
      cmd | getline oldref
      if (!(oldref ~ /^[a-z0-9]{40}$/)) {
        # The SHA is misformatted!  Skip this line.
        next
      }
      close(cmd)
    } else if ($4 ~ /^[a-z0-9]{40}$/)  {
      oldref = $4
    } else {
      # The SHA is misformatted!  Skip this line.
      next
    }

    # Print every SHA after oldref, up to (and including) newref.
    system("git rev-list --first-parent --reverse " oldref ".." newref)
  }
' | $awk -v reason=pre-push -f ${GIT_DIR:-.git}/hooks/commit-msg-files.awk