forked from wei/git-sync
-
Notifications
You must be signed in to change notification settings - Fork 1
/
git-sync.sh
executable file
·57 lines (46 loc) · 1.79 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
52
53
54
55
56
57
#!/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
eval `ssh-agent -s`
echo '#!/bin/sh' > ~/.ssh_askpass
echo 'echo $SOURCE_SSH_PASSPHRASE' >> ~/.ssh_askpass && chmod +x ~/.ssh_askpass
echo "$(~/.ssh_askpass)"
echo "$SOURCE_SSH_PRIVATE_KEY" | tr -d '\r' | DISPLAY=None SSH_ASKPASS=~/.ssh_askpass ssh-add - >/dev/null
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 --all
git push destination -f --tags