-
Notifications
You must be signed in to change notification settings - Fork 65
/
monorepoize.sh
executable file
·77 lines (68 loc) · 2.99 KB
/
monorepoize.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
#!/usr/bin/env bash
###
# converts a project with git submodules to a monorepo by importing all the
# submodules into the main repository.
#
# gets submodule info from .gitmodules file and imports each submodule into
# the main repository.
###
main() {
# checkout "current" branch and reset to latest changes
git checkout current >/dev/null 2>&1 || fail "failed to checkout master branch"
git reset --hard origin/upstream >/dev/null 2>&1 || fail "failed to reset upstream branch"
if [[ -f .gitmodules ]]; then
# make sure the .gitmodules file is not empty
if [[ ! -s .gitmodules ]]; then
echo ".gitmodules file is empty. nothing to do. exiting..."
else
echo "found .gitmodules file. attempting to convert to monorepo..."
convert
fi
else
echo "no .gitmodules file found. exiting..."
fi
# copy over the README from remote master branch
git checkout origin/master -- README.md monorepoize.sh >/dev/null 2>&1 || fail "failed to copy README.md and monorepoize.sh from remote master branch"
git add README.md monorepoize.sh >/dev/null 2>&1 || fail "failed to add README.md and monorepoize.sh"
git commit -m "use CrabLang README.md and monorepoize.sh" >/dev/null 2>&1 || fail "failed to commit README.md and monorepoize.sh"
# force push master branch to remote
git push -f origin current >/dev/null 2>&1 || fail "failed to push 'current' branch to remote"
}
convert() {
# read each submodule from .gitmodules file
while read -r i; do
if [[ $i == \[submodule* ]]; then
read -r i # next line is the path
path=$(echo "$i" | cut -d'=' -f2 | xargs)
read -r i # next line is the url
url=$(echo "$i" | cut -d'=' -f2 | xargs)
# if path equals "src/llvm-project", skip it
if [[ $path == "src/llvm-project" ]]; then
echo "skipping LLVM submodule..."
continue
fi
echo "converting $path from $url..."
# deinitialize and remove the submodule
git submodule deinit -f "$path" >/dev/null 2>&1 || fail "failed to deinit $path"
rm -rf .git/modules/"$path"
git rm -f "$path" >/dev/null 2>&1 || fail "failed to remove $path"
# clone the submodule and remove its .git directory
git clone "$url" "$path" >/dev/null 2>&1 || fail "failed to clone $url"
rm -rf "$path"/.git
# add the submodule files and commit
git add "$path" >/dev/null 2>&1 || fail "failed to add $path with git"
msg="imported $path into main repository"
echo "$msg"
git commit -m "$msg" >/dev/null 2>&1 || fail "failed to commit $path"
echo "successfully converted $path."
fi
done <.gitmodules
}
fail() {
printf '%s\n' "$1" >&2
exit "${2-1}"
}
try_and_continue() {
$1 || echo "command failed: $1, continuing..."
}
main "$@" && echo "converted to monorepo successfully."