Files
macos-tools/network/setup-github-mirror.sh
2025-12-24 13:42:39 +08:00

259 lines
7.1 KiB
Bash
Executable File

#!/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."[email protected]:".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."[email protected]:".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" == "[email protected]:" ]]; then
test_repo="[email protected]: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 <<EOF
GitHub 镜像加速配置脚本
用法: $0 [选项]
选项:
1, ghproxy 使用 ghproxy.com 镜像(推荐)
2, ghproxynet 使用 ghproxy.net 镜像
3, ghps 使用 ghps.cc 镜像
4, direct 移除镜像,使用直连
test 测试所有镜像速度
status 显示当前配置
backup 仅备份当前配置
restore [file] 从备份恢复配置
list 列出所有备份
help 显示此帮助信息
示例:
$0 ghproxy # 配置 ghproxy.com 镜像
$0 test # 测试所有镜像速度
$0 direct # 恢复直连模式
$0 restore ~/.git-config-backups/gitconfig_20251221_120000.bak
注意:
- 每次修改配置前都会自动备份
- 备份文件保存在: $BACKUP_DIR
- SSH 方式([email protected]:)不受镜像影响
EOF
}
# 主函数
main() {
local command=${1:-help}
echo ""
print_info "=== GitHub 镜像加速配置工具 ==="
echo ""
case "$command" in
1|ghproxy)
setup_mirror "https://mirror.ghproxy.com/https://github.com/" "ghproxy.com"
show_current_config
;;
2|ghproxynet)
setup_mirror "https://ghproxy.net/https://github.com/" "ghproxy.net"
show_current_config
;;
3|ghps)
setup_mirror "https://ghps.cc/https://github.com/" "ghps.cc"
show_current_config
;;
4|direct)
remove_mirror
show_current_config
;;
test)
print_info "开始测试各镜像速度(每个测试最多 30 秒)..."
echo ""
test_mirror_speed "https://github.com/" "GitHub 直连"
echo ""
test_mirror_speed "https://mirror.ghproxy.com/https://github.com/" "ghproxy.com"
echo ""
test_mirror_speed "https://ghproxy.net/https://github.com/" "ghproxy.net"
echo ""
test_mirror_speed "https://ghps.cc/https://github.com/" "ghps.cc"
;;
status)
show_current_config
;;
backup)
backup_git_config
;;
restore)
if [ -z "$2" ]; then
list_backups
echo ""
print_warning "请指定要恢复的备份文件"
echo "用法: $0 restore <backup_file>"
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 "$@"