forked from dflourusso/pre-push
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hook
executable file
·118 lines (106 loc) · 2.73 KB
/
hook
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/bin/bash
echoc() {
# 输出黄色字
echo -e "\n\033[33m $1 \033[0m\n"
}
HAS_NODE=`which node 2> /dev/null || which nodejs 2> /dev/null || which iojs 2> /dev/null`
#
# There are some issues with Source Tree because paths are not set correctly for
# the given environment. Sourcing the bash_profile seems to resolve this for bash users,
# sourcing the zshrc for zshell users.
#
# https://answers.atlassian.com/questions/140339/sourcetree-hook-failing-because-paths-don-t-seem-to-be-set-correctly
#
function source_home_file {
file="$HOME/$1"
[[ -f "${file}" ]] && source "${file}"
}
if [[ -z "$HAS_NODE" ]]; then
source_home_file ".bash_profile" || source_home_file ".zshrc" || source_home_file ".bashrc" || true
fi
NODE=`which node 2> /dev/null`
NODEJS=`which nodejs 2> /dev/null`
IOJS=`which iojs 2> /dev/null`
LOCAL="/usr/local/bin/node"
BINARY=
dir=`pwd`
#
# Figure out which binary we need to use for our script execution.
#
if [[ -n "$NODE" ]]; then
BINARY="$NODE"
elif [[ -n "$NODEJS" ]]; then
BINARY="$NODEJS"
elif [[ -n "$IOJS" ]]; then
BINARY="$IOJS"
elif [[ -x "$LOCAL" ]]; then
BINARY="$LOCAL"
fi
if [[ -z "$BINARY" ]]; then
exit 1
fi
"$BINARY" -e "require('./rush.json')" 2> /dev/null
res=$?
if [[ $res -ne 0 ]]; then
if [[ $* == *--dry-run* ]]; then
echoc "--dry-run: execute this hook without side affects"
else
"$BINARY" "$("$BINARY" -e "console.log(require.resolve('pre-push-for-rush'))")"
res=$?
if [[ 0 -ne $res ]]; then
exit $res
fi
fi
else
# support rush
projects=(
`$BINARY -e "require('./rush.json').projects.forEach(item => console.log(item.projectFolder + '/'));"`
)
curBranch=`git rev-parse --abbrev-ref HEAD`
git fetch origin $curBranch 2> /dev/null
diffFiles=`git diff HEAD origin/$curBranch --name-only 2> /dev/null`
cd $dir
index=0
for project in ${projects[@]}
do
echo $diffFiles | grep -q ^$project
res=$?
if [[ 0 -eq res ]]; then
let index++
echoc "$project --changed"
fi
done
if [[ 1 -ne $index ]]; then
echoc "$index: changed"
echoc "skip for not one project changed!"
exit 0
fi
for project in ${projects[@]}
do
echo $diffFiles | grep -q ^$project
res=$?
if [[ 0 -eq $res ]]; then
# hit
offsetPath=`echo './'$project`
cd $offsetPath
"$BINARY" -e "require('pre-push-for-rush')" 2> /dev/null
res=$?
if [[ $res -ne 0 ]]; then
msg="$offsetPath Error: Cannot find module 'pre-push-for-rush'"
# echoc $msg
else
echoc $offsetPath
if [[ $* == *--dry-run* ]]; then
echoc "--dry-run: execute this hook without side affects"
else
"$BINARY" "$("$BINARY" -e "console.log(require.resolve('pre-push-for-rush'))")" $project
res=$?
if [[ 0 -ne $res ]]; then
exit $res
fi
fi
fi
cd $dir
fi
done
fi