Skip to content

Commit b21989a

Browse files
authored
internal: Add Git hooks to protect secrets (#193)
* Replace old hooks with internal hooks * Fix line endings * Create README.md
1 parent 090071b commit b21989a

File tree

8 files changed

+228
-177
lines changed

8 files changed

+228
-177
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@
1111
# use lf for files that are read in by scripts to deal with reading variables in different environments
1212
*.txt text eol=lf
1313
*.sh text eol=lf
14+
git_hooks/** text eol=lf
1415
.editorconfig text

.githooks/pre-commit

Lines changed: 0 additions & 11 deletions
This file was deleted.

.githooks/pre-commit.d/01-API_Key_Check

Lines changed: 0 additions & 44 deletions
This file was deleted.

.githooks/pythonScripts/check_api_key.py

Lines changed: 0 additions & 115 deletions
This file was deleted.

.githooks/readme.md

Lines changed: 0 additions & 7 deletions
This file was deleted.

git_hooks/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
All dependencies for using git hooks are installed in `install_dependencies.sh`. Simply run `./git_hooks/git-hooks --install` to install

git_hooks/git-hooks

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Copyright (c) 2010-2014, Benjamin C. Meyer <ben@meyerhome.net>
4+
# All rights reserved.
5+
#
6+
# Redistribution and use in source and binary forms, with or without
7+
# modification, are permitted provided that the following conditions are met:
8+
# 1. Redistributions of source code must retain the above copyright
9+
# notice, this list of conditions and the following disclaimer.
10+
# 2. Redistributions in binary form must reproduce the above copyright
11+
# notice, this list of conditions and the following disclaimer in the
12+
# documentation and/or other materials provided with the distribution.
13+
# 3. Neither the name of the project nor the
14+
# names of its contributors may be used to endorse or promote products
15+
# derived from this software without specific prior written permission.
16+
#
17+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ''AS IS'' AND ANY
18+
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
21+
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22+
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23+
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24+
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26+
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27+
#
28+
{
29+
# Uncomment set -xv for debugging
30+
#set -xv
31+
32+
function hook_dirs
33+
{
34+
if [ ! -z "${1}" ] ; then
35+
hook="/${1}"
36+
else
37+
hook=""
38+
fi
39+
40+
git rev-parse --git-dir &> /dev/null
41+
if [ $? -eq 0 ]; then
42+
if [ $(git rev-parse --is-bare-repository) = 'false' ]; then
43+
cd $(git rev-parse --show-toplevel)
44+
echo "${PWD}/git_hooks${hook}"
45+
fi
46+
fi
47+
}
48+
49+
function list_hooks_in_dir
50+
{
51+
path="${1}"
52+
level="${2}"
53+
find --help 2>&1 | grep -- '-L' 2>/dev/null >/dev/null
54+
if [ $? -eq 1 ] ; then
55+
find "${path}/" -mindepth ${level} -maxdepth ${level} -perm -111 -type f 2>/dev/null | grep -v "^.$" | sort
56+
else
57+
find -L "${path}/" -mindepth ${level} -maxdepth ${level} -perm -111 -type f 2>/dev/null | grep -v "^.$" | sort
58+
fi
59+
}
60+
61+
function list_hooks
62+
{
63+
GITDIR=`git rev-parse --git-dir`
64+
cat "${GITDIR}/hooks/pre-commit" 2> /dev/null | grep 'git-hooks' > /dev/null 2> /dev/null
65+
if [ $? = 0 ]; then
66+
echo "Git hooks ARE installed in this repository."
67+
echo ""
68+
else
69+
echo "Git hooks are NOT installed in this repository. (Run 'git hooks --install' to install it)"
70+
exit 1
71+
fi
72+
73+
echo 'Listing Project hooks:'
74+
echo '---'
75+
for dir in `hook_dirs`; do
76+
echo "${dir}:"
77+
for hook in `list_hooks_in_dir "${dir}" 2`; do
78+
echo -n *`basename \`dirname "${hook}"\``
79+
echo -e "/`basename "${hook}"` - `${hook} --about`"
80+
done
81+
echo ""
82+
done
83+
}
84+
85+
function run_hooks
86+
{
87+
dir="${1}"
88+
if [[ -z ${dir} || ! -d "${dir}" ]]; then
89+
echo "run_hooks requires a directory name as an argument."
90+
return 1
91+
fi
92+
shift 1
93+
for hook in `list_hooks_in_dir "${dir}" 1`
94+
do
95+
export last_run_hook="${hook} $@"
96+
if [ ! -z ${GIT_HOOKS_VERBOSE} ]; then
97+
echo -n "@@ Running hook: "
98+
echo -n `basename \`dirname "${hook}"\``
99+
echo "/`basename "${hook}"`"
100+
fi
101+
${hook} "$@"
102+
done
103+
}
104+
105+
function run_hook
106+
{
107+
set -e
108+
hook=`basename "${1}"`
109+
if [ -z ${hook} ] ; then
110+
echo "run requires a hook argument"
111+
return 1
112+
fi
113+
shift 1
114+
for dir in `hook_dirs "${hook}"`; do
115+
if [ ! -d "${dir}" ] ; then
116+
continue
117+
fi
118+
run_hooks "${dir}" "$@"
119+
done
120+
set +e
121+
}
122+
123+
function install_hooks_into
124+
{
125+
DIR=$1
126+
cd "${DIR}"
127+
128+
set -e
129+
mkdir -p hooks # some git clients do not automatically create a sample hooks directory
130+
mv hooks hooks.old
131+
set +e
132+
mkdir hooks
133+
cd hooks
134+
for file in applypatch-msg commit-msg post-applypatch post-checkout post-commit post-merge post-receive pre-applypatch pre-auto-gc pre-commit prepare-commit-msg pre-rebase pre-receive update pre-push
135+
do
136+
echo "${2}" > "${file}"
137+
chmod +x "${file}"
138+
done
139+
}
140+
141+
function install_hooks
142+
{
143+
GITDIR=`git rev-parse --git-dir`
144+
if [ ! $? -eq 0 ]; then
145+
echo "${1} must be run inside a git repository"
146+
return 1
147+
fi
148+
cd "${GITDIR}"
149+
if [ "${1}" = "--install" ]; then
150+
if [ -d hooks.old ]; then
151+
echo "hooks.old already exists, perhaps you already installed?"
152+
return 0
153+
fi
154+
cmd='#!/usr/bin/env bash
155+
if git rev-parse -q --verify MERGE_HEAD; then
156+
exit 0
157+
fi
158+
git_hooks/git-hooks run "$0" "$@"';
159+
install_hooks_into "${PWD}" "${cmd}"
160+
else
161+
if [ ! -d hooks.old ]; then
162+
echo "Error, hooks.old doesn't exists, aborting uninstall to not destroy something"
163+
return 1
164+
fi
165+
rm -rf hooks
166+
mv hooks.old hooks
167+
fi
168+
}
169+
170+
function report_error
171+
{
172+
echo "Hook failed: ${last_run_hook}"
173+
exit 1
174+
}
175+
176+
case $1 in
177+
run)
178+
if [ ! -z "${GIT_DIR}" ]; then
179+
unset GIT_DIR
180+
fi
181+
shift
182+
trap report_error ERR
183+
run_hook "$@"
184+
;;
185+
--install | --uninstall)
186+
install_hooks "$1"
187+
;;
188+
-h | --help | -?)
189+
echo 'Git Hooks'
190+
echo ' A tool to manage project, user, and global Git hooks for multiple git repositories.'
191+
echo ' https://github.com/icefox/git-hooks'
192+
echo ''
193+
echo 'Options:'
194+
echo ' --install Replace existing hooks in this repository with a call to'
195+
echo ' git hooks run [hook]. Move old hooks directory to hooks.old'
196+
echo ' --uninstall Remove existing hooks in this repository and rename hooks.old'
197+
echo ' back to hooks'
198+
echo " run <cmd> Run the hooks for <cmd> (such as pre-commit)"
199+
echo " (no arguments) Show currently installed hooks"
200+
;;
201+
*)
202+
list_hooks
203+
;;
204+
esac
205+
}

0 commit comments

Comments
 (0)