-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgithooks-pre-push
More file actions
executable file
·97 lines (91 loc) · 3.17 KB
/
githooks-pre-push
File metadata and controls
executable file
·97 lines (91 loc) · 3.17 KB
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
87
88
89
90
91
92
93
94
95
96
97
#!/bin/bash
z40=0000000000000000000000000000000000000000
while read local_ref local_sha remote_ref remote_sha
do
echo "$0 $@"
echo "local_ref ${local_ref}"
echo "local_sha ${local_sha}"
echo "remote_ref ${remote_ref}"
echo "remote_sha ${remote_sha}"
# Don't allow pushing to remote "mywork" unless the branch name starts
# with my username. Only works if the Github CLI is installed
if type -t gh 2>&1 >/dev/null
then
username=$( gh api user --jq ".login" )
echo "Pushing to $1 for $username"
if [[ "$local_ref" == "(delete)" ]]
then
branch=$(
git rev-parse --abbrev-ref --symbolic-full-name "${remote_ref}"
)
else
branch=$(
git rev-parse --abbrev-ref --symbolic-full-name "${local_ref}"
)
fi
echo "---- Branch name is ${branch}"
if [[ -n "${username}" && "$1" == "mywork" ]]
then
if ! [[ "${branch}" =~ ^${username}/ ]]
then
echo "---- Bad branch name"
exit 1
fi
elif [[ -n "${username}" && "$1" == "origin" ]]
then
if [[ "${branch}" =~ ^${username}/ ]]
then
echo "---- Bad branch name"
exit 1
fi
fi
fi
# Format all files that are different from the remote, committed or not
if [[ -f .clang-format ]] && type -t git-clang-format >& /dev/null \
&& [[ ! -f "diff.$(git rev-parse --abbrev-ref HEAD | sed 's/\//./g')" ]]
then
if [[ "${#branches}" -gt 0 || "${local_ref}" == "(delete)" ]]
then
continue
fi
if [ "${remote_sha}" = ${z40} ]
then
# new branch
base="upstream/$( git head-branch upstream )" || \
base="origin/$( git head-branch origin )" || \
( r=$( git remote | head -1 ) && \
base="$r/$( git head-branch $r )" )
branches=( "${base}" "${local_ref}" )
else
branches=( "${remote_sha}" "${local_ref}" )
if type -t gh 2>&1 >/dev/null && \
[[ "$( gh base )" != "" ]]
then
branches=( $( gh base ) "${local_ref}" )
elif false && [[ "${compare}" != "${remote_sha}" ]] && \
git branch -r | grep -q "upstream/develop"
then
branches=( "upstream/develop" "${local_ref}" )
fi
fi
if [[ -e .pre-commit-config.yaml && \
-e $( git config core.hookspath )/pre-commit ]] && \
type -t pre-commit 2>&1 >/dev/null
then
pre-commit run -a
else
echo "Get common ancestor of ${branches[@]}"
compare=$( git merge-base --octopus "${branches[@]}" )
echo "Checking formatting of files changed since ${compare}:"
git log -1 ${compare} | head -10
git-clang-format --extensions c,cpp,h,cxx,ipp "${compare}"
fi
# Only uncomment the following line if you want to aggressively ensure
# that all formatting across the project is correct, and your version
# of `clang-format` doesn't have any incompatibilities.
# find src/ripple src/test -type f \( -name '*.cpp' -o -name '*.h' -o -name '*.ipp' \) -print0 | xargs -0 clang-format -i
# Comment the following line if you want to let the push complete even if
# there are formatting problems.
git diff --exit-code
fi
done