#!/bin/bash # GitHub 镜像加速配置脚本 # 支持多种镜像源和快速切换 set -e BACKUP_DIR="$HOME/.git-config-backups" TIMESTAMP=$(date +%Y%m%d_%H%M%S) # 颜色定义 RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # 打印带颜色的消息 print_info() { echo -e "${BLUE}[INFO]${NC} $1" } print_success() { echo -e "${GREEN}[SUCCESS]${NC} $1" } print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } print_error() { echo -e "${RED}[ERROR]${NC} $1" } # 备份当前 Git 配置 backup_git_config() { mkdir -p "$BACKUP_DIR" if [ -f "$HOME/.gitconfig" ]; then cp "$HOME/.gitconfig" "$BACKUP_DIR/gitconfig_$TIMESTAMP.bak" print_success "Git 配置已备份到: $BACKUP_DIR/gitconfig_$TIMESTAMP.bak" fi } # 显示当前配置 show_current_config() { print_info "当前 GitHub 相关的 Git 配置:" echo "" git config --global --list | grep -E "url\.|github" || echo " (无相关配置)" echo "" } # 配置镜像加速 setup_mirror() { local mirror_url=$1 local mirror_name=$2 print_info "正在配置 GitHub 镜像: $mirror_name" # 备份配置 backup_git_config # 移除现有的 GitHub URL 重写规则(避免冲突) git config --global --unset-all url."git@github.com:".insteadOf 2>/dev/null || true git config --global --unset-all url."https://mirror.ghproxy.com/https://github.com".insteadOf 2>/dev/null || true git config --global --unset-all url."https://ghproxy.net/https://github.com".insteadOf 2>/dev/null || true git config --global --unset-all url."https://ghps.cc/https://github.com".insteadOf 2>/dev/null || true git config --global --unset-all url."https://gh-proxy.com/https://github.com".insteadOf 2>/dev/null || true # 设置新的镜像 git config --global url."${mirror_url}".insteadOf "https://github.com/" print_success "GitHub 镜像已配置为: $mirror_name" print_info "镜像地址: $mirror_url" } # 移除镜像配置 remove_mirror() { print_info "正在移除 GitHub 镜像配置..." # 备份配置 backup_git_config # 移除所有镜像配置 git config --global --unset-all url."git@github.com:".insteadOf 2>/dev/null || true git config --global --unset-all url."https://mirror.ghproxy.com/https://github.com".insteadOf 2>/dev/null || true git config --global --unset-all url."https://ghproxy.net/https://github.com".insteadOf 2>/dev/null || true git config --global --unset-all url."https://ghps.cc/https://github.com".insteadOf 2>/dev/null || true git config --global --unset-all url."https://gh-proxy.com/https://github.com".insteadOf 2>/dev/null || true print_success "GitHub 镜像配置已移除,恢复直连模式" } # 测试镜像速度 test_mirror_speed() { local mirror_url=$1 local mirror_name=$2 print_info "测试 $mirror_name 速度..." # 创建临时目录 local temp_dir=$(mktemp -d) cd "$temp_dir" # 测试小仓库克隆速度 local test_repo="https://github.com/octocat/Hello-World" if [[ "$mirror_url" == "git@github.com:" ]]; then test_repo="git@github.com:octocat/Hello-World.git" else test_repo="${mirror_url}octocat/Hello-World" fi echo " 测试仓库: $test_repo" local start_time=$(date +%s) if timeout 30 git clone --depth 1 "$test_repo" test-repo 2>&1 | tail -5; then local end_time=$(date +%s) local duration=$((end_time - start_time)) print_success "$mirror_name 克隆成功,耗时: ${duration}秒" else print_error "$mirror_name 克隆失败或超时" fi # 清理 cd - > /dev/null rm -rf "$temp_dir" } # 列出所有备份 list_backups() { print_info "可用的配置备份:" if [ -d "$BACKUP_DIR" ]; then ls -lht "$BACKUP_DIR" | tail -n +2 || echo " (无备份)" else echo " (无备份)" fi } # 从备份恢复 restore_from_backup() { local backup_file=$1 if [ ! -f "$backup_file" ]; then print_error "备份文件不存在: $backup_file" exit 1 fi # 备份当前配置 backup_git_config # 恢复 cp "$backup_file" "$HOME/.gitconfig" print_success "已从备份恢复: $backup_file" } # 显示帮助信息 show_help() { cat <" else restore_from_backup "$2" show_current_config fi ;; list) list_backups ;; help|--help|-h) show_help ;; *) print_error "未知命令: $command" echo "" show_help exit 1 ;; esac echo "" } # 执行主函数 main "$@"