Files
.profile/scripts/git_lib.sh
Søren Lund 11d0d131dd ci: add CODEBERG_TOKEN and support custom push URL
- Add CODEBERG_TOKEN secret to pipeline environment
- Update add_and_push to accept optional push URL and set remote
- Pass authenticated Codeberg URL to add_and_push in pipeline
2026-05-21 13:40:16 +02:00

66 lines
1.5 KiB
Bash

#!/bin/sh
set -eu
readonly BASE_URL="${BASE_URL:-https://codeberg.org/api/v1/users/}"
readonly LIMIT=30
get_clone_urls() {
user=$1
page=1
base_url="${BASE_URL}${user}/repos?limit=${LIMIT}"
while :; do
result="$(curl --silent "${base_url}&page=$page" | jq -r ".[].clone_url")"
[ -z "$result" ] && break
echo "$result"
lines="$(echo "$result" | wc -l)"
[ "$lines" -lt "$LIMIT" ] && break
page=$((page + 1))
done | sort
}
clone_repos() {
workdir=$1
if [ ! -d "$workdir" ]; then mkdir -p "$workdir"; fi
while read -r url; do
repo_name="$(basename "$url" ".git")"
if ! git clone --branch main --single-branch --depth 1 "$url" "${workdir}/${repo_name}"; then
git clone --branch master --single-branch --depth 1 "$url" "${workdir}/${repo_name}"
fi
done
}
get_branch() {
branch="$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo main)"
if [ "$branch" = "HEAD" ]; then
# Detached HEAD: assume main as default branch name; adjust if different
branch="main"
fi
echo "$branch"
}
add_and_push() {
file="$1"
msg="$2"
git_push_url="$3"
branch="$(get_branch)"
git add "$file"
git config user.email "${GIT_EMAIL:-ci@example.com}"
git config user.name "${GIT_NAME:-CI Bot}"
git commit --no-gpg-sign -m "$msg"
if [ -n "${git_push_url:-}" ]; then
git remote set-url origin "$git_push_url"
fi
git push origin "HEAD:${branch}"
}