#!/bin/bash # Gitea 仓库管理脚本 set -e # 配置 GITEA_HOST="gitea" GITEA_USER="ben" GITEA_HTTP_URL="http://${GITEA_HOST}:3000" GITEA_SSH_URL="git@${GITEA_HOST}" REPO_NAME="mgmt" # 颜色定义 RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # 打印带颜色的消息 print_message() { local color=$1 local message=$2 echo -e "${color}${message}${NC}" } # 检查 SSH 连接 check_ssh_connection() { print_message $BLUE "🔍 检查 Gitea SSH 连接..." if ssh -o ConnectTimeout=5 -o BatchMode=yes "${GITEA_SSH_URL}" 2>&1 | grep -q "successfully authenticated"; then print_message $GREEN "✅ SSH 连接正常" return 0 else print_message $RED "❌ SSH 连接失败" return 1 fi } # 检查仓库状态 check_repo_status() { print_message $BLUE "📊 检查仓库状态..." if [ -d ".git" ]; then print_message $GREEN "✅ Git 仓库已初始化" if git remote get-url origin >/dev/null 2>&1; then local origin_url=$(git remote get-url origin) print_message $GREEN "✅ 远程仓库: $origin_url" else print_message $YELLOW "⚠️ 未配置远程仓库" fi local branch=$(git branch --show-current) print_message $BLUE "📍 当前分支: $branch" local status=$(git status --porcelain) if [ -z "$status" ]; then print_message $GREEN "✅ 工作目录干净" else print_message $YELLOW "⚠️ 有未提交的变更" fi else print_message $RED "❌ 不是 Git 仓库" fi } # 初始化仓库 init_repo() { print_message $BLUE "📦 初始化 Git 仓库..." if [ ! -d ".git" ]; then git init git config user.name "${GITEA_USER}" git config user.email "${GITEA_USER}@example.com" print_message $GREEN "✅ Git 仓库初始化完成" fi # 配置远程仓库 if ! git remote get-url origin >/dev/null 2>&1; then git remote add origin "${GITEA_SSH_URL}:${GITEA_USER}/${REPO_NAME}.git" print_message $GREEN "✅ 远程仓库配置完成" fi } # 同步代码 sync_code() { print_message $BLUE "🔄 同步代码..." # 检查是否有未提交的变更 if ! git diff --quiet || ! git diff --staged --quiet; then print_message $YELLOW "⚠️ 发现未提交的变更" git status --short read -p "是否提交这些变更? (y/N): " -n 1 -r echo if [[ $REPLY =~ ^[Yy]$ ]]; then git add . read -p "请输入提交消息: " commit_message git commit -m "$commit_message" print_message $GREEN "✅ 变更已提交" else print_message $YELLOW "⚠️ 跳过提交" return 1 fi fi # 推送到远程仓库 if git push origin main; then print_message $GREEN "✅ 代码推送成功" else print_message $RED "❌ 代码推送失败" return 1 fi } # 拉取最新代码 pull_code() { print_message $BLUE "⬇️ 拉取最新代码..." if git pull origin main; then print_message $GREEN "✅ 代码拉取成功" else print_message $RED "❌ 代码拉取失败" return 1 fi } # 查看提交历史 show_history() { print_message $BLUE "📜 提交历史:" git log --oneline --graph --decorate -10 } # 查看分支状态 show_branches() { print_message $BLUE "🌿 分支状态:" git branch -a } # 创建新分支 create_branch() { local branch_name=$1 if [ -z "$branch_name" ]; then read -p "请输入分支名称: " branch_name fi if [ -n "$branch_name" ]; then git checkout -b "$branch_name" print_message $GREEN "✅ 分支 '$branch_name' 创建成功" else print_message $RED "❌ 分支名称不能为空" fi } # 切换分支 switch_branch() { local branch_name=$1 if [ -z "$branch_name" ]; then print_message $BLUE "可用分支:" git branch -a read -p "请输入要切换的分支名称: " branch_name fi if [ -n "$branch_name" ]; then git checkout "$branch_name" print_message $GREEN "✅ 已切换到分支 '$branch_name'" else print_message $RED "❌ 分支名称不能为空" fi } # 显示帮助 show_help() { echo "Gitea 仓库管理脚本" echo "" echo "用法: $0 [命令]" echo "" echo "命令:" echo " check 检查连接和仓库状态" echo " init 初始化仓库" echo " sync 同步代码到远程仓库" echo " pull 拉取最新代码" echo " history 查看提交历史" echo " branches 查看分支状态" echo " create-branch [name] 创建新分支" echo " switch-branch [name] 切换分支" echo " status 查看仓库状态" echo " help 显示帮助信息" echo "" echo "示例:" echo " $0 check # 检查状态" echo " $0 sync # 同步代码" echo " $0 create-branch feature-x # 创建功能分支" } # 主函数 main() { local command=${1:-help} case $command in check) check_ssh_connection check_repo_status ;; init) init_repo ;; sync) sync_code ;; pull) pull_code ;; history) show_history ;; branches) show_branches ;; create-branch) create_branch "$2" ;; switch-branch) switch_branch "$2" ;; status) check_repo_status ;; help|--help|-h) show_help ;; *) print_message $RED "❌ 未知命令: $command" show_help exit 1 ;; esac } # 执行主函数 main "$@"