Files
gitea-cli/scripts/rename.sh
timmy faee15bc78 Initial commit
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-21 13:04:09 +08:00

48 lines
1.2 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=lib/common.sh
source "${SCRIPT_DIR}/lib/common.sh"
usage() {
cat >&2 <<'EOF'
Usage: rename.sh <old-name> <new-name>
EOF
exit 1
}
[[ $# -eq 2 ]] || usage
OLD="$1"
NEW="$2"
load_config
log_info "改名 ${GITEA_USER}/${OLD}${GITEA_USER}/${NEW}"
gitea_api PATCH "/repos/${GITEA_USER}/${OLD}" "{\"name\":\"${NEW}\"}"
case "$HTTP_CODE" in
200|201) log_ok "改名完成" ;;
404) log_err "找不到 repo${GITEA_USER}/${OLD}"; exit 1 ;;
422) log_err "新名稱可能已被使用或無效"; cat "$GITEA_API_RESP_FILE" >&2; echo "" >&2; exit 1 ;;
*)
log_err "改名失敗 (HTTP ${HTTP_CODE})"
{ cat "$GITEA_API_RESP_FILE"; echo ""; } >&2
exit 1
;;
esac
# 若 cwd 的 origin 指到舊 repo自動更新
if CURR="$(infer_repo_from_cwd 2>/dev/null)"; then
if [[ "$CURR" == "${GITEA_USER}/${OLD}" ]]; then
NEW_CLEAN_URL="${GITEA_URL}/${GITEA_USER}/${NEW}.git"
git remote set-url origin "$NEW_CLEAN_URL"
log_ok "cwd 的 origin 已更新為 ${NEW_CLEAN_URL}"
fi
fi
NEW_WEB_URL="${GITEA_URL}/${GITEA_USER}/${NEW}"
log_done "完成"
echo "$NEW_WEB_URL"