forked from azz/prettier-config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre-push.bak.sh
executable file
·82 lines (73 loc) · 3.14 KB
/
pre-push.bak.sh
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
#!/bin/sh
# An example hook script to verify what is about to be pushed. Called by "git
# push" after it has checked the remote status, but before anything has been
# pushed. If this script exits with a non-zero status nothing will be pushed.
#
# This hook is called with the following parameters:
#
# $1 -- Name of the remote to which the push is being done
# $2 -- URL to which the push is being done
#
# If pushing without using a named remote those arguments will be equal.
#
# Information about the commits which are being pushed is supplied as lines to
# the standard input in the form:
#
# <local ref> <local oid> <remote ref> <remote oid>
#
# This sample shows how to prevent push of commits where the log message starts
# with "WIP" (work in progress).
remote="$1"
url="$2"
logfile=$(git log -n 1 --format='%H' | cut -c 1-7)
logfile=".$logfile.hook.log"
echo "[test] logfile:$logfile" >$logfile
echo "[test] remote:$1" >>$logfile
echo "[test] url:$2" >>$logfile
zero=$(git hash-object --stdin </dev/null | tr '[0-9a-f]' '0')
# echo "[test] zero:$zero" >> $logfile
# zero值是0000000000000000000000000000000000000000
# # npm 默认又新增了一个commit,但是当前运行的钩子的local_oid还是运行npm version前的commit id
# npm version patch -m "NEWVERSION: %s"
while read local_ref local_oid remote_ref remote_oid; do
echo "[test] 本地要提交的分支:$local_ref" >>$logfile
echo "[test] 本地要提交的commit id:$local_oid" >>$logfile
echo "[test] 要提交到的远程分支:$remote_ref" >>$logfile
echo "[test] 要提交到的远程分支的当前commit id:$remote_oid" >>$logfile
if test "$local_oid" = "$zero"; then
# Handle delete
:
else
if test "$remote_oid" = "$zero"; then
# New branch, examine all commits
range="$local_oid"
else
# Update to existing branch, examine new commits
range="$remote_oid..$local_oid"
fi
echo "[test] range:$range" >>$logfile
# Check for WIP commit
# 搜索出最新的一条commit message以WIP开头的commit id
# commit=$(git rev-list -n 1 --grep '^WIP' "$range")
commit=$(git rev-list -n 1 --grep '^NEWVERSION' "$range")
echo "[test] commit:$commit" >>$logfile
if test -n "$commit"; then
# echo >&2 "Found WIP commit in $local_ref, not pushing"
# exit 1
# 空的ifelse block必须加一个冒号,否则报错:Syntax error: "else" unexpected
:
else
# npm 默认又新增了一个commit,但是当前运行的钩子的local_oid还是运行npm version前的commit id
npm version patch -m "NEWVERSION: %s"
# !!!⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️这里不能用git push,可能会死循环,因为git push会再次触发pre-push钩子
# 用git push --no-verify可以跳过pre-push钩子,但是这样就不能检查commit message了
# 这样虽然结果符合预期,但还是报了一个错误
# git push --no-verify
# echo >&1 "自动更新了版本号,请重新提交~" #echo >&1 "message" 和 echo "message" 效果一样, 输出的信息和其他信息混在一起不方便查找
echo >&2 "自动更新了版本号,请重新提交~"
# 注释掉的话,导致外面运行了空的push,会报错,但结果符合预期
exit 1
fi
fi
done
exit 0