更新: 1 个文件 - 2026-03-17 23:14:38

这个提交包含在:
hao
2026-03-17 23:14:38 -07:00
父节点 ab159308b5
当前提交 99908f3e05

查看文件

@@ -20,6 +20,8 @@ GIT_USER="${GIT_USER:-hao}"
GIT_EMAIL="${GIT_EMAIL:-hao@users.noreply.git.hk.hao.work}"
AUTO_PUSH_MAIN="${AUTO_PUSH_MAIN:-1}"
LOCK_DIR="${REPO_DIR}/.git/.sync-gitea.lock"
RETRY_COUNT="${RETRY_COUNT:-3}"
RETRY_DELAY_SECONDS="${RETRY_DELAY_SECONDS:-3}"
cd "$REPO_DIR"
@@ -46,6 +48,21 @@ log_error() {
echo -e "${RED}[ERROR]${END} $1"
}
run_with_retries() {
local attempt=1
while true; do
if "$@"; then
return 0
fi
if [ "$attempt" -ge "$RETRY_COUNT" ]; then
return 1
fi
log_warning "命令失败,${RETRY_DELAY_SECONDS}s 后重试 (${attempt}/${RETRY_COUNT})"
sleep "$RETRY_DELAY_SECONDS"
attempt=$((attempt + 1))
done
}
repo_api_url() {
echo "${GITEA_API}/repos/${GIT_USER}/${REPO_NAME}"
}
@@ -55,7 +72,7 @@ repo_git_url() {
}
remote_repo_reachable() {
git ls-remote --exit-code origin HEAD >/dev/null 2>&1
run_with_retries git ls-remote --exit-code origin HEAD >/dev/null 2>&1
}
current_branch_name() {
@@ -94,7 +111,7 @@ ensure_remote_repo() {
return 0
fi
if curl --connect-timeout 5 --max-time 15 -fsS ${GITEA_TOKEN:+-H} ${GITEA_TOKEN:+"Authorization: token ${GITEA_TOKEN}"} "$(repo_api_url)" >/dev/null 2>&1; then
if run_with_retries curl --connect-timeout 5 --max-time 15 -fsS ${GITEA_TOKEN:+-H} ${GITEA_TOKEN:+"Authorization: token ${GITEA_TOKEN}"} "$(repo_api_url)" >/dev/null 2>&1; then
log_info "远程仓库已存在: ${GIT_USER}/${REPO_NAME}"
return 0
fi
@@ -221,9 +238,9 @@ push_changes() {
# 推送
if [ -n "$GITEA_TOKEN" ]; then
git -c http.extraHeader="Authorization: token ${GITEA_TOKEN}" push -u origin "$branch"
run_with_retries git -c http.extraHeader="Authorization: token ${GITEA_TOKEN}" push -u origin "$branch"
else
git push -u origin "$branch"
run_with_retries git push -u origin "$branch"
fi
if [ $? -eq 0 ]; then
log_success "推送完成: $branch"
@@ -233,13 +250,13 @@ push_changes() {
fi
if [ "$AUTO_PUSH_MAIN" = "1" ]; then
if git ls-remote --exit-code --heads origin main >/dev/null 2>&1; then
if git fetch origin main:refs/remotes/origin/main >/dev/null 2>&1; then
if run_with_retries git ls-remote --exit-code --heads origin main >/dev/null 2>&1; then
if run_with_retries git fetch origin main:refs/remotes/origin/main >/dev/null 2>&1; then
if git merge-base --is-ancestor refs/remotes/origin/main HEAD; then
if [ -n "$GITEA_TOKEN" ]; then
git -c http.extraHeader="Authorization: token ${GITEA_TOKEN}" push origin HEAD:main
run_with_retries git -c http.extraHeader="Authorization: token ${GITEA_TOKEN}" push origin HEAD:main
else
git push origin HEAD:main
run_with_retries git push origin HEAD:main
fi
git branch -f main HEAD >/dev/null 2>&1 || true
log_success "main 已快进到当前提交"
@@ -251,9 +268,9 @@ push_changes() {
fi
else
if [ -n "$GITEA_TOKEN" ]; then
git -c http.extraHeader="Authorization: token ${GITEA_TOKEN}" push origin HEAD:main
run_with_retries git -c http.extraHeader="Authorization: token ${GITEA_TOKEN}" push origin HEAD:main
else
git push origin HEAD:main
run_with_retries git push origin HEAD:main
fi
git branch -f main HEAD >/dev/null 2>&1 || true
log_success "main 已创建并指向当前提交"