Files
ClawSkills/git-hangman-lab/scripts/git/repo-config

133 lines
2.9 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
usage() {
echo "Usage: $0 --repo-path <path> --email <email> [--recursive]"
echo " --repo-path: Path to the git repository"
echo " --email: Email address to configure"
echo " --recursive: Also configure all submodules (recursive)"
exit 2
}
REPO_PATH=""
EMAIL=""
RECURSIVE=false
while [[ $# -gt 0 ]]; do
case "$1" in
--repo-path)
REPO_PATH="${2:-}"
shift 2
;;
--email)
EMAIL="${2:-}"
shift 2
;;
--recursive)
RECURSIVE=true
shift
;;
*)
usage
;;
esac
done
if [[ -z "$REPO_PATH" || -z "$EMAIL" ]]; then
usage
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="$(pass_mgr get-username --key git)"
PASS="$(pass_mgr get-secret --key git)"
if [[ -z "$USER" || -z "$PASS" ]]; then
echo "Missing credentials from pass_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"