-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync_public_repo.sh
63 lines (52 loc) · 2.27 KB
/
sync_public_repo.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
#!/bin/bash
# Config
BASE_REPONAME=nl-rdo-app-android-modules
# Helpers
TIMESTAMP=`date '+%Y%m%d-%H%M%S'`
PR_TITLE="Sync+public+repo+from+private+repository" # Use + for spaces as this is used in a URL
PR_BODY="This+PR+proposes+the+latest+changes+from+private+to+public+repository.+Timestamp:+${TIMESTAMP}"
RED="\033[1;31m"
GREEN="\033[1;32m"
ENDCOL="\033[0m"
echo -e "${GREEN}Ensuring a safe environment${ENDCOL}"
if [ -z "$(git status --porcelain)" ]; then
# Working directory clean
echo "Working directory clean"
else
# Uncommitted changes
echo
echo -e "${RED}Your working directory contains changes.${ENDCOL}"
echo "To avoid losing changes, this script only works if you have a clean directory."
echo "Commit any work to the current branch, and try again."
echo
exit
fi
# To ensure the script works regardless of whether you run this from private or public, we ignore origin, and
# add 2 remotes, one for public, one for private
if ! git config remote.public-repo.url > /dev/null; then
git remote add public-repo [email protected]:minvws/$BASE_REPONAME
echo -e "${GREEN}Public-repo remote added${ENDCOL}"
fi
if ! git config remote.private-repo.url > /dev/null; then
git remote add private-repo [email protected]:minvws/$BASE_REPONAME-private
echo -e "${GREEN}Private-repo remote added${ENDCOL}"
fi
# Create a branch where we sync everything from current private main
echo -e "${GREEN}Ensuring we are in sync with the private repo${ENDCOL}"
git fetch private-repo
echo -e "${GREEN}Creating a new sync branch based on private/main${ENDCOL}"
git branch sync/$TIMESTAMP private-repo/main
# Todo: this could be optimized to only push if there actually are changes between the two branches (if not, this currently creates an empty PR)
echo -e "${GREEN}Pushing the sync branch to public repo${ENDCOL}"
git push public-repo sync/$TIMESTAMP
echo -e "${GREEN}Constructing a PR request and opening it in the browser${ENDCOL}"
PR_URL="https://github.com/minvws/$BASE_REPONAME/compare/sync/$TIMESTAMP?quick_pull=1&title=${PR_TITLE}&body=${PR_BODY}"
open $PR_URL
# Sync tags
# delete all local tags
git tag -d $(git tag -l)
# fetch all tags from private-repo
git fetch private-repo --tags --force
# push all tags to public-repo
git push -f --tags public-repo
echo -e "${GREEN}Done.${ENDCOL}"