Files
test_skill/gitea_push/scripts/smart_commit.sh
2026-01-19 21:30:14 +01:00

85 lines
1.5 KiB
Bash

---
info "Commit créé : ${COMMIT_HASH}"
# Push helpers
get_remote_url() {
git remote get-url "${GIT_REMOTE}" 2>/dev/null || true
}
# Build an authenticated URL for one-shot push (does not change remote config)
# Works for https remotes.
make_auth_url_from_remote() {
local remote_url="$1"
if [ -z "${GITEA_HOST:-}" ] || [ -z "${GITEA_USER:-}" ] || [ -z "${GITEA_TOKEN:-}" ]; then
echo ""
return
fi
# Only for http(s)
if ! echo "$remote_url" | grep -qE '^https?://'; then
echo ""
return
fi
# Extract path after host
# Accepts:
# https://gitea.host/user/repo.git
# https://gitea.host/user/repo
local path
path=$(echo "$remote_url" | sed -E 's#^https?://[^/]+/##')
# If remote points to a different host, skip
if ! echo "$remote_url" | grep -q "${GITEA_HOST}"; then
echo ""
return
fi
# Auth URL
echo "https://${GITEA_USER}:${GITEA_TOKEN}@${GITEA_HOST}/${path}"
}
# Push
info "Push vers ${GIT_REMOTE}/${CURRENT_BRANCH}"
set +e
if git push "${GIT_REMOTE}" "${CURRENT_BRANCH}"; then
set -e
info "Push OK"
echo "$DIFF_STAT"
exit 0
fi
set -e
warn "Push standard échoué. Tentative de fallback via token (si configuré)."
REMOTE_URL="$(get_remote_url)"
AUTH_URL="$(make_auth_url_from_remote "$REMOTE_URL")"
if [ -n "$AUTH_URL" ]; then
set +e
if git push "$AUTH_URL" "${CURRENT_BRANCH}"; then
set -e
info "Push OK via URL authentifiée (remote inchangé)"
echo "$DIFF_STAT"
exit 0
fi
set -e
fi
error "Push échoué. Vérifier : droits Gitea, token, remote ${GIT_REMOTE}, ou credentials git."
exit 1