93 lines
2.2 KiB
Bash
93 lines
2.2 KiB
Bash
|
|
#!/usr/bin/env bash
|
||
|
|
|
||
|
|
set -euo pipefail
|
||
|
|
|
||
|
|
SCHEMAS_REPO_URL="${SCHEMAS_REPO_URL:-ssh://git@git.crop-logic.ir:2222/sajad-dev/Schemas.git}"
|
||
|
|
TARGET_BRANCH="${TARGET_BRANCH:-develop}"
|
||
|
|
FORCE_SUBMODULE_ADD=false
|
||
|
|
|
||
|
|
usage() {
|
||
|
|
cat <<'EOF'
|
||
|
|
Usage: ./add_schemas_submodule.sh [--force|-f] [repo...]
|
||
|
|
|
||
|
|
Options:
|
||
|
|
-f, --force Pass --force to `git submodule add`
|
||
|
|
EOF
|
||
|
|
}
|
||
|
|
|
||
|
|
TARGET_REPOS=()
|
||
|
|
|
||
|
|
while [[ "$#" -gt 0 ]]; do
|
||
|
|
case "$1" in
|
||
|
|
-f|--force)
|
||
|
|
FORCE_SUBMODULE_ADD=true
|
||
|
|
;;
|
||
|
|
-h|--help)
|
||
|
|
usage
|
||
|
|
exit 0
|
||
|
|
;;
|
||
|
|
--)
|
||
|
|
shift
|
||
|
|
while [[ "$#" -gt 0 ]]; do
|
||
|
|
TARGET_REPOS+=("$1")
|
||
|
|
shift
|
||
|
|
done
|
||
|
|
break
|
||
|
|
;;
|
||
|
|
*)
|
||
|
|
TARGET_REPOS+=("$1")
|
||
|
|
;;
|
||
|
|
esac
|
||
|
|
shift
|
||
|
|
done
|
||
|
|
|
||
|
|
if [[ "${#TARGET_REPOS[@]}" -eq 0 ]]; then
|
||
|
|
TARGET_REPOS=("Ai" "Backend" "SensorHub")
|
||
|
|
fi
|
||
|
|
|
||
|
|
add_schemas_submodule() {
|
||
|
|
local repo_dir="$1"
|
||
|
|
local submodule_path="Schemas"
|
||
|
|
local submodule_dir="${repo_dir}/${submodule_path}"
|
||
|
|
local add_args=(-b "$TARGET_BRANCH")
|
||
|
|
|
||
|
|
if [[ "$FORCE_SUBMODULE_ADD" == true ]]; then
|
||
|
|
add_args+=(--force)
|
||
|
|
fi
|
||
|
|
|
||
|
|
if ! git -C "$repo_dir" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
|
||
|
|
echo "Skipping ${repo_dir}: not a git repository."
|
||
|
|
return 0
|
||
|
|
fi
|
||
|
|
|
||
|
|
if git -C "$repo_dir" config --file .gitmodules --get-regexp "submodule\\..*\\.path" 2>/dev/null \
|
||
|
|
| awk '{print $2}' \
|
||
|
|
| grep -Fxq "$submodule_path"; then
|
||
|
|
echo "Skipping ${repo_dir}: ${submodule_path} submodule already exists."
|
||
|
|
return 0
|
||
|
|
fi
|
||
|
|
|
||
|
|
if [[ -e "${repo_dir}/${submodule_path}" ]]; then
|
||
|
|
echo "Skipping ${repo_dir}: ${repo_dir}/${submodule_path} already exists."
|
||
|
|
return 0
|
||
|
|
fi
|
||
|
|
|
||
|
|
echo "Adding ${submodule_path} to ${repo_dir} on branch ${TARGET_BRANCH}"
|
||
|
|
git -C "$repo_dir" submodule add "${add_args[@]}" "$SCHEMAS_REPO_URL"
|
||
|
|
|
||
|
|
git -C "$submodule_dir" fetch origin "$TARGET_BRANCH"
|
||
|
|
if git -C "$submodule_dir" show-ref --verify --quiet "refs/heads/${TARGET_BRANCH}"; then
|
||
|
|
git -C "$submodule_dir" checkout "$TARGET_BRANCH"
|
||
|
|
else
|
||
|
|
git -C "$submodule_dir" checkout -b "$TARGET_BRANCH" "origin/${TARGET_BRANCH}"
|
||
|
|
fi
|
||
|
|
|
||
|
|
echo "Checked out ${submodule_dir} on ${TARGET_BRANCH}"
|
||
|
|
}
|
||
|
|
|
||
|
|
for repo_dir in "${TARGET_REPOS[@]}"; do
|
||
|
|
add_schemas_submodule "$repo_dir"
|
||
|
|
done
|
||
|
|
|
||
|
|
echo "Done."
|