-
Notifications
You must be signed in to change notification settings - Fork 95
/
MirrorChanges.sh
executable file
·62 lines (49 loc) · 1.89 KB
/
MirrorChanges.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
#!/bin/bash
# This script helps automate mirroring all changes from a "master" repository to a "mirror" repository.
# Changes should not be made in the mirror but all changes should flow to the mirror repository via the master repository.
# Inputs needed for the script are : 1. Remote URL for the master repository 2. Remote URL for the mirror repository
set -e
mirrorDirName="repoMirror"
errorMessage="Invalid arguments passed to script 1>&2
MirrorChanges.bat MasterRepositoryURL MirrorRepositoryURL LocalRepoRootDir
MasterRepositoryURL is the remote URL of the Git repository which contains the changes to be mirrored
MirrorRepositoryURL is the remote URL of the Git repository which will mirror the changes in the master repository
LocalRepoRootDir (Optional) is the root directory where the repository will be cloned. Defaults to the current directory."
# check args
if [[ -z "$1" ]]; then
echo "Master repository URL is not provided"
echo "${errorMessage}"
exit 1
fi
if [[ -z "$2" ]]; then
echo "Mirror repository URL is not provided"
echo "${errorMessage}"
exit 1
fi
masterRepoUrl="$1"
mirrorRepoURL="$2"
if [[ -n "$3" ]]; then
cd "$3"
fi
# remove old directory
echo "Delete local mirror directory"
if [[ -d "${mirrorDirName}" ]]; then
rm -rf "${mirrorDirName}"
fi
# clone the master repo
echo "Clone master repository to mirror directory"
git clone --no-checkout "${masterRepoUrl}" "${mirrorDirName}" || { echo "clone failed"; exit 1; }
if [[ ! -d "${mirrorDirName}" ]]; then
echo -e "Errors during script execution 1>&2\n${mirrorDirName} does not exist"
exit 1
fi
cd "${mirrorDirName}"
# sync mirror
echo "Set remote push url to the mirror repository url"
git remote set-url --push origin "${mirrorRepoURL}"
echo "Push to the mirror repository"
git push --mirror || { echo "push failed"; exit 1; }
# clean up directory
echo "Delete local mirror directory"
cd ..
rm -rf "${mirrorDirName}"