-
Notifications
You must be signed in to change notification settings - Fork 24
/
run_update_and_commit_hive_submodules.sh
executable file
·138 lines (109 loc) · 4.53 KB
/
run_update_and_commit_hive_submodules.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
#!/bin/bash
BASE_MODULE="github.com/iotaledger/hive.go"
# Check if there are any changes in the Git repository
if [[ -n $(git status -s) ]]; then
echo "ERROR: There are pending changes in the repository. We can't update the dependencies!"
exit 1
fi
# Run git fetch
git fetch >/dev/null
# Check if the remote branch is set
current_branch=$(git symbolic-ref --short HEAD)
if ! git show-ref --verify --quiet refs/remotes/origin/$current_branch; then
echo "ERROR: Remote branch \"origin/$current_branch\" doesn't exist! Create it first by pushing to remote!"
exit 1
fi
# Check if we or remote is up to date
local_commit=$(git rev-parse "$current_branch")
remote_commit=$(git rev-parse "origin/$current_branch")
if ! [ "$local_commit" = "$remote_commit" ]; then
echo "ERROR: Current remote branch is not up to date with the local branch!"
exit 1
fi
# Find all submodules by searching for subdirectories containing go.mod files
SUBMODULES=$(find . -type f -name "go.mod" -exec dirname {} \; | sed -e 's/^\.\///' | sort)
# Declare an associative array to store submodule versions
declare -A SUBMODULE_VERSIONS
# Get the current version of each submodule
for submodule in $SUBMODULES; do
version=$(grep -E "^module " "$submodule/go.mod" | awk '{print $2}' | sed "s|^$BASE_MODULE/||")
SUBMODULE_VERSIONS["$submodule"]="$version"
done
# Declare an associative array to store submodule inter-dependencies
declare -A SUBMODULE_INTER_DEPENDENCIES
# Build the dependency graph
for submodule in $SUBMODULES; do
dependencies=$(grep -E "^\s$BASE_MODULE" "$submodule/go.mod" | awk '{print $1}')
SUBMODULE_INTER_DEPENDENCIES["$submodule"]="$dependencies"
done
# Create an empty string to store the ordered submodules
order=""
# Function to recursively resolve dependencies and add them to the order array
resolve_dependencies() {
local submodule_with_base_and_version="$1"
local submodule_with_version="${submodule_with_base_and_version#${BASE_MODULE}/}"
local submodule="${submodule_with_version%/v[0-9]*}"
local visited="$2"
local dependencies="${SUBMODULE_INTER_DEPENDENCIES["$submodule"]}"
if [[ -z "$visited" ]]; then
visited="$submodule_with_base_and_version "
else
visited+="$submodule_with_base_and_version "
fi
# dependencies are always with base and version
for dependency in $dependencies; do
if [[ ! "$visited" =~ "$dependency" ]]; then
resolve_dependencies "$dependency" "$visited"
fi
done
if [[ ! "$order" =~ "$submodule_with_base_and_version" ]]; then
order+="$submodule_with_base_and_version "
fi
}
# Resolve the dependencies between the submodules
for submodule in $SUBMODULES; do
submodule_with_version="${SUBMODULE_VERSIONS["$submodule"]}"
submodule_with_base_and_version=$BASE_MODULE/$submodule_with_version
if [[ ! "$order" =~ "$submodule_with_base_and_version" ]]; then
resolve_dependencies "$submodule_with_base_and_version"
fi
done
# Trim leading and trailing spaces
order="${order%"${order##*[![:space:]]}"}"
order="${order#"${order%%[![:space:]]*}"}"
# Function that updates the inter-dependencies in the submodule
update_submodule() {
local submodule="$1"
local dependencies="${SUBMODULE_INTER_DEPENDENCIES["$submodule"]}"
echo "Updating $submodule..."
# Enter the submodule folder
pushd "$submodule" >/dev/null
# Get the current commit hash
current_commit=$(git rev-parse HEAD)
current_commit_short=$(git rev-parse --short HEAD)
for dependency in $dependencies; do
echo " go get -u $dependency@$current_commit..."
go get -u "$dependency@$current_commit" >/dev/null
done
# Run go mod tidy
echo "Running go mod tidy..."
go mod tidy >/dev/null
# Commit the changes
commit_message="Update \"$submodule\" submodule to commit \"$current_commit_short\""
echo "Commiting the changes with commit message \"$commit_message\"..."
git add go.mod go.sum
git commit -m "$commit_message"
# Push to remote repository, so we can reference the new commits in the other submodules
git push
# Add some sleep time, so we can reference the new commits in the other modules
sleep 2
# Move back to the parent directory
popd >/dev/null
}
# Update submodules in the correct order
for submodule_with_base_and_version in $order; do
submodule_with_version="${submodule_with_base_and_version#${BASE_MODULE}/}"
submodule="${submodule_with_version%/v[0-9]*}"
update_submodule "$submodule"
done
echo "All submodules updated and committed."