forked from yaoya/ScriptableRenderLoop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre-push
More file actions
101 lines (95 loc) · 3.8 KB
/
pre-push
File metadata and controls
101 lines (95 loc) · 3.8 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
98
99
100
101
#!/bin/sh
#This is a hook for git hub repository.
#For installing this hook, put it in .git/hooks
#
#Feature provided:
# - confirmation asked when attempting to push on a protected branch (see protected_branches below)
# - forbide push when pushing new branche where name do not follow case convention
# * In this case, propose an automatic renaming to follow convention
# * And also rename folders in .git/refs/heads to have the right case (could cause issue on some OS)
#
#Note: Current case convention is all in lowercase except HDRP which will be always in uppercase
#Note: If you attempt to push a new branch hdrp/something, it will conflict with your current folder HDRP in .git/refs/heads.
# The git error will be 'fatal: hdrp/something cannot be resolved to branch.'
# This error is raized prior this hook and cannot be handled here. Please rename your branch:
# git branch -m hdrp/something HDRP/something
# This should be sufficient to resolve this issue.
protected_branches=('master' 'HDRP/staging')
z40=0000000000000000000000000000000000000000 #this is the returned sha1 when there is no remote of the branch
current_branch=$(git branch | sed -n -e 's/^\* \(.*\)/\1/p')
current_branch_under_convention=$(sed -e 's/\(.*\)/\L\1/' -e 's/hdrp/HDRP/g' <<< $current_branch)
tmp="${current_branch_under_convention}_tmp"
while read local_ref local_sha remote_ref remote_sha
do
#echo "local_ref: $local_ref"
#echo "local_sha: $local_sha"
#echo "remote_ref: $remote_ref"
#echo "remote_sha: $remote_sha"
#echo "current_branch: $current_branch"
#echo "current_branch_under_convention: $current_branch_under_convention"
#echo "tmp: $tmp"
#echo "head: $(git symbolic-ref HEAD)"
for (( i=0; i<${#protected_branches[@]}; i++))
do
if [ "$current_branch" = "${protected_branches[i]}" ]
then
read -p "You're about to push to a protected branch. Are you sure? (Y/N) > " -n 2 -r < /dev/tty
echo
if echo $REPLY | grep -E '^[Yy]$' > /dev/null
then
echo "If you say so..."
else
echo "Aborting"
exit 1
fi
fi
done
if [ "$remote_sha" = "$z40" ] #pushing new branch
then
if [ "$current_branch" != "$current_branch_under_convention" ]
then
echo "Your branch does not follow naming convention."
echo -n "Do you want to rename it to ${current_branch_under_convention}"
read -p "? (Y/N) > " -n 2 -r < /dev/tty
if echo $REPLY | grep -E '^[Yy]$' > /dev/null
then
#do a two time renaming to deals with OS don't carrying about case
git branch -m $current_branch $tmp
git branch -m $tmp $current_branch_under_convention
#manually rename folder if there is anything
# no need to rewind as PWD is always on the directory root
tmp_PWD="${PWD}"
# go to heads
cd "${PWD}/.git/refs/heads"
# check folders created with the local creation of this branch
tmpIFS= $IFS
IFS='/'
read -r -a array_current <<< "$current_branch"
read -r -a array_current_under_convention <<< "$current_branch_under_convention"
IFS= $tmpIFS
for (( i=0; i<${#array_current[@]}-1; i++))
do
if [ "${array_current[i]}" != "${array_current_under_convention[i]}" ]
then
if [ -d "${array_current[i]}" ]
then
mv ${array_current[i]} ${array_current_under_convention[i]}
echo "renamed .git/refs/heads/${array_current[i]} -> .git/refs/heads/${array_current_under_convention[i]}"
fi
fi
cd ${array_current_under_convention[i]}
done
cd $tmp_PWD
echo "Renamed."
echo "This command will fail as it supposed to. Relaunching it."
#launch the command again
git push --set-upstream origin $current_branch_under_convention
else
echo "Aborting..."
echo "If you are sure about your naming, you can force this security with git push --no-verify"
fi
exit 1
fi
fi
done
exit 0