Skip to content

Instantly share code, notes, and snippets.

@gcslaoli
Last active November 10, 2022 08:09
Show Gist options
  • Save gcslaoli/71d0ee1d5ab1d034c2b59c6647e5fb88 to your computer and use it in GitHub Desktop.
Save gcslaoli/71d0ee1d5ab1d034c2b59c6647e5fb88 to your computer and use it in GitHub Desktop.
#!/bin/bash
# 使用镜像加速github
# Author: LiDong ([email protected])
# Date: 2022-11-10
PERFIX="https://gh.hjmcloud.cn/"
# 获取环境变量 CGITPERFIX, 如果没有设置则使用默认值
if [ -n "$CGITPERFIX" ]; then
PERFIX=$CGITPERFIX
fi
# 如果没有参数,则显示帮助
if [ $# -eq 0 ]; then
# 获取脚本所在目录
DIR=$(
cd $(dirname $0)
pwd
)
echo "------------------------------------------------------------"
echo "Cgit installed in $DIR"
echo "Current mirror: $PERFIX"
echo "Usage: cgit install|clone|wget"
echo " install: install cgit"
echo " clone: clone github repo"
echo " wget: wget github repo or file"
echo "Example:"
echo " ./cgit install"
echo " cgit clone https://github.com/cool-team-official/cool-admin-vue.git"
echo " cgit wget -O cool-admin-codespace.zip https://github.com/cool-team-official/cool-admin-codespace/archive/refs/heads/local.zip"
echo "------------------------------------------------------------"
exit 0
fi
# 如果第一个参数为 install 则安装
if [ "$1" == "install" ]; then
echo "安装中..."
cp $0 /usr/local/bin/cgit
chmod +x /usr/local/bin/cgit
echo "安装完成!"
exit 0
fi
# 如果第一个参数为 clone 则 clone
if [ "$1" == "clone" ]; then
# 获取所有参数
args=("$@")
# 获取参数个数
length=$#
# 遍历参数
for ((i = 0; i < $length; i++)); do
# 获取参数
arg=${args[$i]}
# 判断参数是否是github仓库地址
if [[ $arg == *"github.com/"* ]]; then
# 替换参数, 使用镜像地址
args[$i]=${PERFIX}$arg
fi
done
# Replace URL and call git
echo "Using mirror: $PERFIX ..."
echo "git ${args[@]}"
git ${args[@]}
fi
# 如果第一个参数为 wget 则 wget
if [ "$1" == "wget" ]; then
# 获取所有参数
args=("$@")
# 获取参数个数
length=$#
# 遍历参数
for ((i = 0; i < $length; i++)); do
# 获取参数
arg=${args[$i]}
# 判断参数是否是github仓库地址 或者是文件地址
if [[ $arg == *"github.com/"* ]] || [[ $arg == *"raw.githubusercontent.com/"* ]]; then
# 替换参数, 使用镜像地址
args[$i]=${PERFIX}$arg
fi
done
# 移除第一个参数
unset args[0]
# Replace URL and call git
echo "Using mirror: $PERFIX ..."
echo "wget ${args[@]}"
wget ${args[@]}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment