This repository has been archived by the owner on Nov 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
workspace.sh
executable file
·142 lines (122 loc) · 3.75 KB
/
workspace.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env bash
# vim: set expandtab sw=4 ts=4:
## This little script accompanies a go mod uber repo, that is, a Git repo that aggregates modules pertaining
## to a single project linearly through git submodules.
set -euo pipefail
IFS=$'\n'
## Load all subdirectories siblings of this script.
mods=()
while IFS='' read -r line; do mods+=("$line"); done < \
<(find "$(dirname "${0}")" -mindepth 1 -maxdepth 1 -type d -not -name '.*' -exec basename {} ';' | sort)
## Return the repo name of origin remote
## It expect the origin to be `git@github.com:<orgname>/<repo-name>.git` (ssh format).
get_repo() {
git -C "${1}" remote get-url origin | cut -d':' -f2 | cut -d'.' -f1
}
## Edits a module gomod. Args:
## $1: module to edit
## $2: array of flags to go mod edit
edit_mod() {
local mod="${1}"
shift
local flags=("$@")
go mod edit "${flags[@]}" "$mod/go.mod"
echo $mod
}
do_local() {
local flags=()
for mod in "${mods[@]}"; do
flags+=("-replace=github.com/$(get_repo $mod)=../$mod")
done
for i in "${!mods[@]}"; do
local rep=("${flags[@]}")
unset 'rep['"$i"']'
rep=("${rep[@]}")
edit_mod "${mods[$i]}" "${rep[@]}"
done
}
do_remote() {
local flags=()
for mod in "${mods[@]}"; do
flags+=("-dropreplace=github.com/$(get_repo $mod)")
done
for i in "${!mods[@]}"; do
local rep=("${flags[@]}")
unset 'rep['"$i"']'
rep=("${rep[@]}")
edit_mod "${mods[$i]}" "${rep[@]}"
done
}
do_refresh() {
cd "$(dirname "${0}")"
echo "::: Stashing all changes :::"
git submodule foreach git stash
echo "::: Checking out master on all submodules :::"
git submodule foreach git checkout master
echo "::: Rebasing all submodules origin/master :::"
exec 3>&1
if ! git submodule update --jobs 10 --remote --rebase 1>&3 2>&3; then
echo "WARN: upgrade git for faster submodule updates from origin"
git submodule update --remote --rebase
fi
echo "::: Pulling all modules in case new dependencies have been added :::"
# We might have to do multiple passes in case a new dependency adds another new dependency
added=1
while [ ${added} -eq 1 ]; do
added=0
for repo in $(
awk '/^\s\s*github.com\/(libp2p|ipld|multiformats)/ { print gensub(/^github.com\/([^/]*\/[^/]*).*/, "\\1", "g", $1) }' ./*/go.mod \
| sort -u
); do
if git submodule add "git@github.com:${repo}.git"; then
added=1
fi
done
done
echo "Done"
}
git-branch-name () {
git status 2> /dev/null | \
head -1 | \
sed 's/^# //' | \
sed 's/^On branch //' |\
sed 's/HEAD detached at //'
}
do_branches() {
for D in *;
do
if [ -d "${D}" ]; then
cd "${D}"
printf "${D}\t"
git-branch-name
cd ..
fi
done
}
do_branches_col() {
if which column &>/dev/null; then
do_branches | column -t
else
do_branches
fi
}
print_usage() {
echo "Usage: $0 {local|remote|master}" >&2
echo
echo " [l]ocal adds \`replace\` directives to all go.mod files to make dependencies point to the local workspace"
echo " [r]emote removes the \`replace\` directives introduced by \`local\`"
echo " [re]fresh refreshes all submodules from origin/master, stashing all local changes first, then checking out master"
echo " [b]ranches lists all the repos and the branch checked out"
echo ""
}
if [[ -z ${1:-} ]]; then
print_usage
exit 1
fi
case "$1" in
local | l) do_local ;;
remote | r) do_remote ;;
refresh | re) do_refresh ;;
branches | b) do_branches_col ;;
*) print_usage; exit 1; ;;
esac