-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.sh
executable file
·118 lines (92 loc) · 4.13 KB
/
script.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#! /bin/bash
# ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
# STEP 1: Gather user input
printf "What is the location of your monorepo?\n"
read MONOREPO
printf "What is the location of the repository you'd like to put in your monorepo (RTPIM)?\n"
read RTPIM
# printf "What Git commit prefix would you like you to add to the commits in your RTPIM? Leave blank for none."
# read commitPrefix
# Definitions
CHANGELOG='changelog.md'
TOP_LEVEL_DIRECTORY=`echo $PWD`
## GIT_BRANCH_REGEX="((([-0-9a-zA-Z\/_?:.,]+)$)|(([-0-9a-zA-Z\/_?:.,]+( ))$))"
# ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
# STEP 2: Check paths
# Create git repo if monorepo doesn't exist
if [ ! -d "$MONOREPO" ]; then
mkdir $MONOREPO
cd $MONOREPO
git init
touch $CHANGELOG
git add --all
git commit -m "initialize repository"
cd $TOP_LEVEL_DIRECTORY
printf "\nCREATED NEW GIT REPOSITORY AT \"$MONOREPO\"."
fi
# Check $RTPIM directory exists
if [ -d "$RTPIM" ]; then
cd $RTPIM
else
printf "\nWARNING! \"$RTPIM\" IS NOT A DIRECTORY! EXITING.\n"
exit
fi
# From https://stackoverflow.com/a/4754797
for branch in $(git branch --all | grep '^\s*remotes' | egrep --invert-match '(:?HEAD|master)$'); do
git branch --track "${branch##*/}" "$branch"
done
cd $TOP_LEVEL_DIRECTORY/$MONOREPO
# Create changelog
touch $CHANGELOG
CHANGELOG_HEADER="## Add branches from $RTPIM to $MONOREPO"
echo "$CHANGELOG_HEADER" >> $CHANGELOG
git add --all
git commit -m "Create changelog"
cd $TOP_LEVEL_DIRECTORY/$RTPIM
# Reset git and checkout to avoid "Working tree has modifications." issue
# https://stackoverflow.com/a/18608538
git reset
git checkout
# ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
# STEP 3: Gather all git branches from $RTPIM into array
# Get list of all local branches
RAW_GIT_BRANCHES=`git branch`
# Format list of local branches into array
# from https://unix.stackexchange.com/a/628576
set -o noglob
IFS=$'\n' ARRAY_GIT_BRANCHES=($RAW_GIT_BRANCHES)
set +o noglob
# ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
# STEP 4: Loop through array of git branches and add each branch from $RTPIM as a subtree in $MONOREPO, in directory $MONOREPO/$RTPIM/$BRANCH_NAME
for BRANCH in ${ARRAY_GIT_BRANCHES[@]}
do
# Clean up git branch name using Regex and sed
# from https://stackoverflow.com/a/6744040 and https://stackoverflow.com/a/51673105
BRANCH=$(sed 's/^[ *]*\([^ ]*\).*/\1/' <<< $BRANCH)
BRANCH_CLEAN_NAME=$(echo "$BRANCH" | awk '$1=$1')
printf "$BRANCH_CLEAN_NAME"
# Checkout the branch in the current loop, before heading back to the monorepo
git checkout $BRANCH_CLEAN_NAME
# Head to the monorepo and begin Git logic
cd $TOP_LEVEL_DIRECTORY/$MONOREPO
git subtree add --prefix=$RTPIM/$BRANCH_CLEAN_NAME ../$RTPIM $BRANCH_CLEAN_NAME
# Report results
printf "\nADDED TO CHANGELOG: %s\n"
CURRENT_DATE=`date +"%Y-%m-%d %T"`
CHANGELOG_MESSAGE="At $CURRENT_DATE, branch \"\`$BRANCH_CLEAN_NAME\`\" from repository \"\`$RTPIM\`\" was added to monorepo: \"\`$MONOREPO\`\", in path \"\`$RTPIM/$BRANCH_CLEAN_NAME\`\""
printf "⦿ $CHANGELOG_MESSAGE \n"
echo "- $CHANGELOG_MESSAGE" >> $CHANGELOG
git add --all
git commit -m "Update changelog"
# Head back to $RTPIM directory to begin loop again
cd $TOP_LEVEL_DIRECTORY/$RTPIM
done
cd $TOP_LEVEL_DIRECTORY/$MONOREPO
printf "\n\nNew tree structure of your monorepo:\n`tree -L 2`"
cd $TOP_LEVEL_DIRECTORY
printf "\n\nFINISHED!!!\n"
# A bunch of stuff I tried that didn't work
## BRANCH=$(echo "$BRANCH" | grep -E '((([-0-9a-zA-Z\/_?:.,]+)$)|(([-0-9a-zA-Z\/_?:.,]+( ))$))')
## CLEANED_GIT_BRANCHES=$(echo $RAW_GIT_BRANCHES | grep -qE "{GIT_BRANCH_REGEX}")
## IFS='\n\b' read -ra ARRAY_GIT_BRANCHES <<< "$RAW_GIT_BRANCHES"
## IFS=$'\n' read -rd '' -a ARRAY_GIT_BRANCHES <<<"$RAW_GIT_BRANCHES"