-
Notifications
You must be signed in to change notification settings - Fork 124
/
git-sync.sh
executable file
·51 lines (40 loc) · 1.53 KB
/
git-sync.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
#!/bin/sh
set -e
SOURCE_REPO=$1
SOURCE_BRANCH=$2
DESTINATION_REPO=$3
DESTINATION_BRANCH=$4
if ! echo $SOURCE_REPO | grep -Eq ':|@|\.git\/?$'; then
if [[ -n "$SSH_PRIVATE_KEY" || -n "$SOURCE_SSH_PRIVATE_KEY" ]]; then
SOURCE_REPO="git@github.com:${SOURCE_REPO}.git"
GIT_SSH_COMMAND="ssh -v"
else
SOURCE_REPO="https://github.com/${SOURCE_REPO}.git"
fi
fi
if ! echo $DESTINATION_REPO | grep -Eq ':|@|\.git\/?$'; then
if [[ -n "$SSH_PRIVATE_KEY" || -n "$DESTINATION_SSH_PRIVATE_KEY" ]]; then
DESTINATION_REPO="git@github.com:${DESTINATION_REPO}.git"
GIT_SSH_COMMAND="ssh -v"
else
DESTINATION_REPO="https://github.com/${DESTINATION_REPO}.git"
fi
fi
echo "SOURCE=$SOURCE_REPO:$SOURCE_BRANCH"
echo "DESTINATION=$DESTINATION_REPO:$DESTINATION_BRANCH"
if [[ -n "$SOURCE_SSH_PRIVATE_KEY" ]]; then
# Clone using source ssh key if provided
git clone -c core.sshCommand="/usr/bin/ssh -i ~/.ssh/src_rsa" "$SOURCE_REPO" /root/source --origin source && cd /root/source
else
git clone "$SOURCE_REPO" /root/source --origin source && cd /root/source
fi
git remote add destination "$DESTINATION_REPO"
# Pull all branches references down locally so subsequent commands can see them
git fetch source '+refs/heads/*:refs/heads/*' --update-head-ok
# Print out all branches
git --no-pager branch -a -vv
if [[ -n "$DESTINATION_SSH_PRIVATE_KEY" ]]; then
# Push using destination ssh key if provided
git config --local core.sshCommand "/usr/bin/ssh -i ~/.ssh/dst_rsa"
fi
git push destination "${SOURCE_BRANCH}:${DESTINATION_BRANCH}" -f