-
Notifications
You must be signed in to change notification settings - Fork 101
/
pre-push
executable file
路64 lines (56 loc) 路 1.83 KB
/
pre-push
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
#!/bin/sh
# Based on:
#
# https://github.com/git/git/blob/c09b6306c6ca275ed9d0348a8c8014b2ff723cfb/templates/hooks--pre-push.sample
#
# For:
#
# https://github.com/wincent/wincent/issues/100
#
# (ie. automatically updates `master` whenever `main` is pushed)
#
# To use, install to `.git/hooks/pre-push` and `chmod +x` accordingly.
REMOTE="$1"
URL="$2"
ZERO=$(git hash-object --stdin </dev/null | tr '[0-9a-f]' '0')
# By default, don't update the remote repo, only the local ref.
# Set to `1` to update the remote as well.
SYNC_REMOTE=0
while read LOCAL_REF LOCAL_OID REMOTE_REF REMOTE_OID; do
if [ "$REMOTE_REF" = "refs/heads/main" ]; then
if [ "$LOCAL_OID" = "$ZERO" ]; then
# Branch deletion: ignore.
:
else
# Use color in interactive terminals.
if [ -t 1 ]; then
BOLD="$(tput bold)"
RESET="$(tput sgr0)"
else
BOLD=
RESET=
fi
# Respect --dry-run/-n.
DRY_RUN=0
for WORD in $(ps -p $PPID -ww -o command=); do
if [ "$WORD" = "-n" -o "$WORD" = "--dry-run" ]; then
DRY_RUN=1
fi
done
if [ $DRY_RUN -eq 1 ]; then
if [ $SYNC_REMOTE -eq 1 ]; then
echo >&2 "${BOLD}[dry-run][pre-push (main -> master)]: git push --no-verify ${REMOTE} +${LOCAL_OID}:master${RESET}"
fi
echo >&2 "${BOLD}[dry-run][pre-push (main -> master)]: git branch -f master ${LOCAL_OID}${RESET}"
else
if [ $SYNC_REMOTE -eq 1 ]; then
echo >&2 "${BOLD}[pre-push (main -> master)]: git push --no-verify ${REMOTE} +${LOCAL_OID}:master${RESET}"
git push --no-verify "${REMOTE}" "+${LOCAL_OID}:refs/heads/master"
fi
echo >&2 "${BOLD}[pre-push (main -> master)]: git branch -f master ${LOCAL_OID}${RESET}"
git branch -f master "${LOCAL_OID}"
fi
fi
fi
done
exit 0