136 lines
2.9 KiB
Bash
Executable File
136 lines
2.9 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
usage() {
|
|
echo "Usage: $0 --repo-path <path> [--recursive]"
|
|
echo " --repo-path: Path to the git repository"
|
|
echo " --recursive: Also configure all submodules (recursive)"
|
|
exit 2
|
|
}
|
|
|
|
REPO_PATH=""
|
|
RECURSIVE=false
|
|
|
|
while [[ $# -gt 0 ]]; do
|
|
case "$1" in
|
|
--repo-path)
|
|
REPO_PATH="${2:-}"
|
|
shift 2
|
|
;;
|
|
--recursive)
|
|
RECURSIVE=true
|
|
shift
|
|
;;
|
|
*)
|
|
usage
|
|
;;
|
|
esac
|
|
done
|
|
|
|
if [[ -z "$REPO_PATH" ]]; then
|
|
usage
|
|
fi
|
|
|
|
# Get email from ego-mgr
|
|
EMAIL=$(ego-mgr get email)
|
|
|
|
# Check if email is provided
|
|
if [[ -z "$EMAIL" ]]; then
|
|
echo "Error: email not set in ego-mgr, please contact ard"
|
|
exit 1
|
|
fi
|
|
|
|
# Check if it's a git repo (either .git is a directory or a file with gitdir: reference)
|
|
is_git_repo() {
|
|
local repo="$1"
|
|
if [[ -d "$repo/.git" ]]; then
|
|
return 0
|
|
elif [[ -f "$repo/.git" ]]; then
|
|
local gitdir
|
|
gitdir=$(grep -m1 "gitdir:" "$repo/.git" | cut -d' ' -f2 | tr -d ' ')
|
|
if [[ -n "$gitdir" ]]; then
|
|
return 0
|
|
fi
|
|
fi
|
|
return 1
|
|
}
|
|
|
|
if ! is_git_repo "$REPO_PATH"; then
|
|
echo "Not a git repo: $REPO_PATH"
|
|
exit 1
|
|
fi
|
|
|
|
USER="$(secret-mgr get-username --key git)"
|
|
PASS="$(secret-mgr get-secret --key git)"
|
|
|
|
if [[ -z "$USER" || -z "$PASS" ]]; then
|
|
echo "Missing credentials from secret-mgr (key: git)"
|
|
exit 2
|
|
fi
|
|
|
|
# URL-encode username for credential URL
|
|
ENC_USER="$(U="$USER" python3 - <<'PY'
|
|
import os, urllib.parse
|
|
print(urllib.parse.quote(os.environ['U'], safe=''))
|
|
PY
|
|
)"
|
|
|
|
# Function to configure a single repo
|
|
configure_repo() {
|
|
local repo="$1"
|
|
local relative="${2:-}"
|
|
|
|
# Get relative path name for display
|
|
local name="${relative:-$repo}"
|
|
|
|
echo "Configuring: $name"
|
|
|
|
# Set local user.name / user.email
|
|
( cd "$repo" && git config user.name "$USER" )
|
|
( cd "$repo" && git config user.email "$EMAIL" )
|
|
|
|
# Resolve the real git dir (works for normal repos and submodules)
|
|
local git_dir
|
|
git_dir="$(cd "$repo" && git rev-parse --absolute-git-dir)"
|
|
local cred_file="${git_dir}/credentials"
|
|
|
|
( cd "$repo" && git config credential.helper "store --file ${cred_file}" )
|
|
( cd "$repo" && GIT_ASKPASS=true git credential-store --file "${cred_file}" store <<EOF
|
|
protocol=https
|
|
host=git.hangman-lab.top
|
|
username=${ENC_USER}
|
|
password=${PASS}
|
|
EOF
|
|
)
|
|
|
|
echo " - user.name: $USER"
|
|
echo " - user.email: $EMAIL"
|
|
echo " - credential.helper: configured"
|
|
}
|
|
|
|
# Configure main repo
|
|
configure_repo "$REPO_PATH"
|
|
|
|
# Configure submodules if --recursive is specified
|
|
if [[ "$RECURSIVE" == "true" ]]; then
|
|
echo ""
|
|
echo "Configuring submodules..."
|
|
|
|
# Get submodules list
|
|
submodules=$(cd "$REPO_PATH" && git submodule status --recursive 2>/dev/null | awk '{print $2}' || true)
|
|
|
|
if [[ -z "$submodules" ]]; then
|
|
echo "No submodules found"
|
|
else
|
|
for sm in $submodules; do
|
|
sm_path="$REPO_PATH/$sm"
|
|
if is_git_repo "$sm_path"; then
|
|
configure_repo "$sm_path" "$sm"
|
|
fi
|
|
done
|
|
fi
|
|
fi
|
|
|
|
echo ""
|
|
echo "OK"
|