From 99908f3e056f6aec248be0d4f231f089ea5d66d4 Mon Sep 17 00:00:00 2001 From: hao Date: Tue, 17 Mar 2026 23:14:38 -0700 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0:=201=20=E4=B8=AA=E6=96=87?= =?UTF-8?q?=E4=BB=B6=20-=202026-03-17=2023:14:38?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/sync-gitea.sh | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/scripts/sync-gitea.sh b/scripts/sync-gitea.sh index 18059dd9..57bf530e 100755 --- a/scripts/sync-gitea.sh +++ b/scripts/sync-gitea.sh @@ -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 已创建并指向当前提交"