124 lines
3.1 KiB
Bash
Executable File
124 lines
3.1 KiB
Bash
Executable File
#!/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"
|
||
|
||
REPO_NAME=""
|
||
VISIBILITY="public"
|
||
COMMIT_MSG=""
|
||
|
||
while [[ $# -gt 0 ]]; do
|
||
case "$1" in
|
||
-m)
|
||
[[ $# -ge 2 ]] || { log_err "-m 需要一個訊息參數"; exit 1; }
|
||
COMMIT_MSG="$2"
|
||
shift 2
|
||
;;
|
||
public|private)
|
||
VISIBILITY="$1"
|
||
shift
|
||
;;
|
||
-h|--help)
|
||
cat >&2 <<'EOF'
|
||
Usage: publish.sh [repo] [public|private] [-m "commit message"]
|
||
EOF
|
||
exit 0
|
||
;;
|
||
-*)
|
||
log_err "未知參數:$1"
|
||
exit 1
|
||
;;
|
||
*)
|
||
if [[ -z "$REPO_NAME" ]]; then
|
||
REPO_NAME="$1"
|
||
elif [[ "$VISIBILITY" == "public" ]]; then
|
||
VISIBILITY="$1"
|
||
else
|
||
log_err "多餘參數:$1"
|
||
exit 1
|
||
fi
|
||
shift
|
||
;;
|
||
esac
|
||
done
|
||
|
||
REPO_NAME="${REPO_NAME:-$(basename "$PWD")}"
|
||
|
||
case "$VISIBILITY" in
|
||
public) PRIVATE=false ;;
|
||
private) PRIVATE=true ;;
|
||
*)
|
||
log_err "可見性只能是 public 或 private,收到:${VISIBILITY}"
|
||
exit 1
|
||
;;
|
||
esac
|
||
|
||
load_config
|
||
|
||
CLEAN_URL="${GITEA_URL}/${GITEA_USER}/${REPO_NAME}.git"
|
||
WEB_URL="${GITEA_URL}/${GITEA_USER}/${REPO_NAME}"
|
||
|
||
log_info "建立 Gitea repo:${GITEA_USER}/${REPO_NAME} (${VISIBILITY})"
|
||
|
||
gitea_api POST "/user/repos" \
|
||
"{\"name\":\"${REPO_NAME}\",\"private\":${PRIVATE},\"auto_init\":false,\"default_branch\":\"main\"}"
|
||
|
||
case "$HTTP_CODE" in
|
||
201) log_ok "repo 已建立" ;;
|
||
409) log_warn "repo 已存在,略過建立步驟" ;;
|
||
401|403)
|
||
log_err "Gitea 授權失敗 (HTTP ${HTTP_CODE})"
|
||
{
|
||
echo "請確認 ${CONFIG_FILE} 的 GITEA_TOKEN 是否正確、權限是否包含 write:repository"
|
||
cat "$GITEA_API_RESP_FILE"
|
||
echo ""
|
||
} >&2
|
||
exit 1
|
||
;;
|
||
*)
|
||
log_err "建立 repo 失敗 (HTTP ${HTTP_CODE})"
|
||
{ cat "$GITEA_API_RESP_FILE"; echo ""; } >&2
|
||
exit 1
|
||
;;
|
||
esac
|
||
|
||
if [[ ! -d .git ]]; then
|
||
log_info "git init"
|
||
git init -b main >/dev/null
|
||
fi
|
||
|
||
if ! git rev-parse --verify HEAD >/dev/null 2>&1; then
|
||
git add -A
|
||
if git diff --cached --quiet; then
|
||
log_info "工作目錄沒有檔案,建立空的 initial commit"
|
||
git commit --allow-empty -m "${COMMIT_MSG:-Initial commit}" >/dev/null
|
||
else
|
||
log_info "建立 initial commit"
|
||
git commit -m "${COMMIT_MSG:-Initial commit}" >/dev/null
|
||
fi
|
||
else
|
||
git add -A
|
||
if ! git diff --cached --quiet; then
|
||
log_info "提交未提交的變更"
|
||
git commit -m "${COMMIT_MSG:-Update}" >/dev/null
|
||
fi
|
||
fi
|
||
|
||
BRANCH="$(git rev-parse --abbrev-ref HEAD)"
|
||
|
||
if git remote get-url origin >/dev/null 2>&1; then
|
||
git remote set-url origin "$CLEAN_URL"
|
||
else
|
||
git remote add origin "$CLEAN_URL"
|
||
fi
|
||
|
||
log_info "推送到 origin (${CLEAN_URL}) branch=${BRANCH}"
|
||
git_push_with_token "$BRANCH"
|
||
|
||
log_done "完成"
|
||
echo " Branch: ${BRANCH}" >&2
|
||
# stdout:最終結構化輸出
|
||
echo "$WEB_URL"
|