From 29e753c1703207f3857aef1722fbbcd96df8cc3a Mon Sep 17 00:00:00 2001 From: hao Date: Tue, 17 Mar 2026 21:34:01 -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=2021:34:01?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/sync-gitea.sh | 44 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/scripts/sync-gitea.sh b/scripts/sync-gitea.sh index 121ca40c..33f3cf12 100755 --- a/scripts/sync-gitea.sh +++ b/scripts/sync-gitea.sh @@ -58,6 +58,36 @@ remote_repo_reachable() { git ls-remote --exit-code origin HEAD >/dev/null 2>&1 } +current_branch_name() { + local branch + branch=$(git branch --show-current 2>/dev/null || true) + if [ -z "$branch" ]; then + branch="main" + fi + echo "$branch" +} + +current_branch_needs_push() { + if ! git rev-parse --verify "@{upstream}" >/dev/null 2>&1; then + return 0 + fi + [ "$(git rev-list --count @{upstream}..HEAD)" -gt 0 ] +} + +main_ref_needs_push() { + if [ "$AUTO_PUSH_MAIN" != "1" ]; then + return 1 + fi + if ! git rev-parse --verify "refs/remotes/origin/main" >/dev/null 2>&1; then + return 0 + fi + ! git merge-base --is-ancestor "refs/remotes/origin/main" HEAD +} + +needs_push() { + current_branch_needs_push || main_ref_needs_push +} + ensure_remote_repo() { if remote_repo_reachable; then log_info "远程仓库可访问: ${GIT_USER}/${REPO_NAME}" @@ -186,12 +216,8 @@ push_changes() { log_info "推送到远程仓库..." # 获取当前分支 - local branch=$(git branch --show-current 2>/dev/null || echo "main") - - # 如果分支为空,使用 main - if [ -z "$branch" ]; then - branch="main" - fi + local branch + branch=$(current_branch_name) ensure_remote_repo @@ -238,7 +264,11 @@ push_changes() { full_sync() { init_repo commit_changes - push_changes + if needs_push; then + push_changes + else + log_info "没有需要推送的提交" + fi } auto_sync() {