-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.sh
102 lines (78 loc) · 3.04 KB
/
main.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/bin/bash
# The script is pretty simple. It takes two arguments: the source directory and the new origin. It then adds the new origin as a remote to the source directory and pushes all branches to the new origin.
# After that, it asks if you want to replace the CodeCommit remote with the new origin. If you choose to replace it, the script will remove the CodeCommit remote and rename the new origin to origin.
# To run the script, you can use the following command:
# bash main.sh /path/to/source/directory
sourceDir=$1
newRemote=$2
newRemoteName=$3
$help="
Usage: bash main.sh /path/to/source/directory newRemoteUrl newRemoteName
newRemoteUrl - full url to new remote repository
newRemoteName - optional - name of the new remote. overwritten if you decide to set it to origin
You will be prompted to
- confirm the values before the script runs
- confirm if you want to push to the new remote
- confirm if you want to replace the CodeCommit remote with the new remote"
if [ $sourceDir == "--help" ]; then
echo "$help"
exit 0
fi
echo "Source directory: $sourceDir"
echo "New remote url: $newRemote"
echo "New remote name: $newRemoteName"
read -p "Do these values look correct? If so, press enter to continue. Otherwise, press ctrl+c to exit."
if [ -z "$sourceDir" ]; then
echo "Source directory is required"
exit 1
fi
if [ -z "$newRemote" ]; then
echo "New remote is required"
exit 1
fi
if [ -z "$newRemoteName" ]; then
newRemoteName="newRemote"
fi
if [ ! -d "$sourceDir" ]; then
echo "Source directory does not exist"
exit 1
fi
if [ ! -d "$sourceDir/.git" ]; then
echo "Source directory is not a git repository"
exit 1
fi
echo -e "Warning: prior to running, ensure that...\nall local branches on ALL machines have been pushed to the CodeCommit remote\nYou have fetched the codecommit remote\nYou have the correct permissions to push to the new remote\n"
read -p "If you have done so, press enter to continue. Otherwise, press ctrl+c to exit."
cd $sourceDir
git fetch origin
git remote add $newRemoteName $newRemote
echo "Remote $newRemoteName added with url $newRemote"
read -p "Would you like push to the new remote (y/n)?" choice1
echo ""
case "$choice1" in
y|Y ) push=1;;
n|N ) push=0;;
* ) echo "invalid";;
esac
if [ $push -eq 1 ]; then
git push $newRemoteName --all
echo "Pushed to $newRemote"
else
echo "Not pushing to $newRemote"
fi
read -p "Would you like this to replace CodeCommit (y/n)?" choice
echo ""
case "$choice" in
y|Y ) replace=1;;
n|N ) replace=0;;
* ) echo "invalid";;
esac
if [ $replace -eq 1 ]; then
git remote rm origin
git remote rename $newRemoteName origin
echo "CodeCommit remote replaced with $newRemote"
else
echo "CodeCommit remote not replaced"
fi
echo -e "Final steps:\n 1. Ensure that the new remote is added to all local repositories\n 2. Ensure that all local branches are pushed to the new remote\n 3. Ensure that the CodeCommit remote is removed from all local repositories\n 4. Ensure that the new remote is renamed to origin in all local repositories\n"
echo "Finished updating $sourceDir"