-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PMM-13487 Move rewinding branches to the entrypoint
- Loading branch information
Showing
3 changed files
with
130 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,124 @@ | ||
#!/bin/bash | ||
set -o errexit | ||
|
||
git config --global --add safe.directory /app | ||
git config --add safe.directory /app | ||
|
||
rm -f gitmodules.yml | ||
|
||
if [ -s "ci.yml" ]; then | ||
echo "Info: ci.yml was found, we will use it in combination with defaults to resolve dependencies." | ||
echo "If you like to (re)discover the dependencies base on the '${BRANCH_NAME}' branch, please remove the ci.yml file." | ||
fi | ||
|
||
needs_to_pull() { | ||
local UPSTREAM=${1:-'@{u}'} | ||
local LOCAL=$(git rev-parse @) | ||
local BASE=$(git merge-base @ "$UPSTREAM") | ||
local REMOTE=$(git rev-parse "$UPSTREAM") | ||
|
||
if [ "$LOCAL" = "$REMOTE" ]; then | ||
return 1 # false, we are up-to-date | ||
fi | ||
|
||
if [ "$LOCAL" = "$BASE" ]; then | ||
return 0 # true, we are behind upstream | ||
fi | ||
} | ||
|
||
rewind() { | ||
local DIR="$1" | ||
local BRANCH="$2" | ||
local NAME="$3" | ||
|
||
cd "$DIR" > /dev/null | ||
local CURRENT=$(git rev-parse --abbrev-ref HEAD) | ||
echo "Rewinding submodule ${NAME}..." | ||
git fetch | ||
|
||
if [ "$CURRENT" != "$BRANCH" ]; then | ||
echo "Currently on $CURRENT, checking out $BRANCH" | ||
git checkout "$BRANCH" | ||
fi | ||
|
||
if needs_to_pull; then | ||
if ! git pull origin; then | ||
git reset --hard HEAD~30 | ||
git pull origin > /dev/null | ||
fi | ||
echo "Submodule ${NAME} has pulled from upstream" | ||
git log --oneline -n 2 | ||
cd - > /dev/null | ||
git add "$DIR" | ||
echo | ||
else | ||
cd - > /dev/null | ||
echo "Submodule ${NAME} is up-to-date with upstream" | ||
echo | ||
fi | ||
} | ||
|
||
# Join the dependencies listed in .gitmodules and ci.yml and output the result to gitmodules.yml. | ||
# This script will not fail even if ci.yml is empty. | ||
# This script accepts an empty ci.yml. | ||
if ! python ci.py --convert; then | ||
echo "Error: could not convert the ci.yml config file to gitmodules.yml, exiting..." | ||
exit 1 | ||
fi | ||
|
||
DEPS=$(yq -o=json '.' gitmodules.yml | jq -r '[.deps[] | {name: .name, branch: .branch, path: .path, url: .url}]') | ||
echo "$DEPS" > /app/build/build.json | ||
echo -n > /tmp/deps.txt | ||
|
||
DISCOVERED_BRANCHES=() | ||
|
||
while read -r item; do | ||
branch=$(echo "$item" | jq -r '.branch') | ||
path=$(echo "$item" | jq -r '.path') | ||
name=$(echo "$item" | jq -r '.name') | ||
url=$(echo "$item" | jq -r '.url') | ||
|
||
if [ -n "${BRANCH_NAME:-}" ]; then | ||
commit=$(git ls-remote --heads "$url" "$BRANCH_NAME" | cut -f1) | ||
if [ -n "$commit" ]; then | ||
echo | ||
echo "Info: branch '$BRANCH_NAME' found in '$name' submodule, will use it instead of '$branch'" | ||
branch="$BRANCH_NAME" | ||
DISCOVERED_BRANCHES+=( "$name|$branch|$path|$url" ) | ||
echo "name=${name}|path=${path}|url=${url}|branch=${branch}|commit=${commit}" >> /tmp/deps.txt | ||
else | ||
echo "name=${name}|path=${path}|url=${url}|branch=${branch}|commit=none" >> /tmp/deps.txt | ||
declare deps=$(yq -o=json '.' gitmodules.yml | jq -r '[.deps[] | {name: .name, branch: .branch, path: .path, url: .url}]') | ||
# echo "$deps" > /app/build/build.json | ||
|
||
declare -a discovered_branches | ||
declare discovered_file="/tmp/ci.yml" | ||
declare branch path name url commit | ||
declare updated_deps="[]" | ||
# BRANCHE_NAME is passed via the environment variable | ||
|
||
# Only run this block if ci.yml is not present or empty | ||
if [ ! -s "ci.yml" ]; then | ||
while read -r item; do | ||
branch=$(echo "$item" | jq -r '.branch') | ||
path=$(echo "$item" | jq -r '.path') | ||
name=$(echo "$item" | jq -r '.name') | ||
url=$(echo "$item" | jq -r '.url') | ||
|
||
if [ -n "${BRANCH_NAME:-}" ]; then | ||
commit=$(git ls-remote --heads "$url" "$BRANCH_NAME" | cut -f1) | ||
if [ -n "$commit" ]; then | ||
echo | ||
echo "Info: branch '$BRANCH_NAME' found in '$name' submodule, will use it instead of the default '$branch' branch" | ||
branch="$BRANCH_NAME" | ||
discovered_branches+=( "$name|$branch|$path|$url" ) | ||
fi | ||
fi | ||
else | ||
echo "name=${name}|path=${path}|url=${url}|branch=${branch}|commit=none" >> /tmp/deps.txt | ||
fi | ||
done < <(echo "$DEPS" | jq -c '.[]') | ||
|
||
cat /tmp/deps.txt > /app/build/deps.txt | ||
rewind "$path" "$branch" "$name" | ||
commit=$(git -C "$path" rev-parse HEAD) | ||
|
||
DISCOVERED_FILE="/tmp/discovered.yml" | ||
updated_deps=$(echo "$updated_deps" | jq ". += [{name: \"$name\", branch: \"$branch\", commit: \"$commit\", path: \"$path\", url: \"$url\"}]") | ||
|
||
# echo "name=${name}|path=${path}|url=${url}|branch=${branch}" >> /tmp/deps.txt | ||
done < <(echo "$deps" | jq -c '.[]') | ||
|
||
# cat /tmp/deps.txt > /app/build/deps.txt | ||
echo "$updated_deps" > /app/build/build.json | ||
fi | ||
|
||
if [[ "${#DISCOVERED_BRANCHES[@]}" -gt 0 ]]; then | ||
echo "Generating... $DISCOVERED_FILE" | ||
echo "deps:" > "$DISCOVERED_FILE" | ||
for item in "${DISCOVERED_BRANCHES[@]}"; do | ||
if [[ "${#discovered_branches[@]}" -gt 0 ]]; then | ||
echo "Generating... $discovered_file" | ||
echo "deps:" > "$discovered_file" | ||
for item in "${discovered_branches[@]}"; do | ||
echo "$item" | IFS='|' read -r name branch path url | ||
echo " - name: $name" >> "$DISCOVERED_FILE" | ||
echo " branch: $branch" >> "$DISCOVERED_FILE" | ||
echo " path: $path" >> "$DISCOVERED_FILE" | ||
echo " url: $url" >> "$DISCOVERED_FILE" | ||
echo " - name: $name" >> "$discovered_file" | ||
echo " branch: $branch" >> "$discovered_file" | ||
echo " path: $path" >> "$discovered_file" | ||
echo " url: $url" >> "$discovered_file" | ||
done | ||
fi | ||
|
||
if [ ! -s "ci.yml" ]; then | ||
echo | ||
echo "Info: generating ci.yml..." | ||
cat "$DISCOVERED_FILE" > ci.yml | ||
cat "$discovered_file" > ci.yml | ||
fi | ||
|
||
rm -f gitmodules.yml |