aboutsummaryrefslogtreecommitdiff
path: root/gdb/contrib/pre-commit-setup.py
blob: bd1f7af0a4991f7b54528af50b30188655e921e8 (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
#!/usr/bin/env python3

# Copyright (C) 2025-2026 Free Software Foundation, Inc.
# This program 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.
#
# This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.

# Occasionally we add new stages to default_install_hook_types in
# .pre-commit-config.yaml.  The new stages are not used until somebody runs
# pre-commit install again.  This script, meant to run as a pre-commit hook in
# the pre-commit stage, detects this situation.

import os
import re
import sys

import yaml

cfg = ".pre-commit-config.yaml"
with open(cfg, "r") as f:
    data = yaml.load(f, Loader=yaml.SafeLoader)
stages = data.get("default_install_hook_types", ["pre-commit"])

if os.path.isfile(".git"):
    # Handle worktrees.
    fp = open(".git")
    text = fp.read()
    m = re.search("gitdir: (.*)", text)
    dir = os.path.join(m.group(1), "..", "..")
else:
    dir = ".git"

if not os.path.isdir(dir):
    # Not a git repository.
    print("no .git dir found, skipping")
    sys.exit(0)

for val in stages:
    f = os.path.join(dir, "hooks", val)

    if not (os.path.isfile(f)):
        if val == "pre-commit":
            print("pre-commit framework hooks not installed, skipping")
            sys.exit(0)
        print("missing hook: " + val + " (please run pre-commit install)")
        sys.exit(1)

    fp = open(f)
    text = fp.read()
    m = re.search("File generated by pre-commit", text)
    if not m:
        print("not a pre-commit framework hook: " + f)
        sys.exit(1)