156 lines
4.2 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# 设置错误时立即退出
set -e
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# 日志函数
log_info() {
echo -e "${GREEN}[INFO]${NC} $1"
}
log_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# 安装Git
install_git() {
log_info "安装Git..."
# 安装Git
sudo apt-get update
sudo apt-get install -y git
# 安装Git工具
sudo apt-get install -y git-extras
sudo apt-get install -y git-flow
sudo apt-get install -y git-lfs
log_info "Git安装完成"
}
# 配置Git
configure_git() {
log_info "配置Git..."
# 配置Git全局设置
git config --global core.autocrlf input
git config --global core.fileMode false
git config --global core.ignorecase false
git config --global core.quotepath false
git config --global core.safecrlf warn
git config --global core.editor "vim"
# 配置Git颜色
git config --global color.ui true
git config --global color.branch.current "yellow reverse"
git config --global color.branch.local "yellow"
git config --global color.branch.remote "green"
git config --global color.diff.meta "yellow bold"
git config --global color.diff.frag "magenta bold"
git config --global color.diff.old "red"
git config --global color.diff.new "green"
git config --global color.status.added "green"
git config --global color.status.changed "yellow"
git config --global color.status.untracked "cyan"
git config --global color.status.deleted "red"
# 配置Git别名
git config --global alias.st "status"
git config --global alias.co "checkout"
git config --global alias.br "branch"
git config --global alias.ci "commit"
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
git config --global alias.lga "log --color --graph --all --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
git config --global alias.df "diff"
git config --global alias.dc "diff --cached"
git config --global alias.unstage "reset HEAD --"
git config --global alias.last "log -1 HEAD"
git config --global alias.visual "!gitk"
# 配置Git提交模板
cat > ~/.gitmessage << EOF
# <type>(<scope>): <subject>
#
# <body>
#
# <footer>
#
# type: feat, fix, docs, style, refactor, test, chore
# scope: 影响范围例如user, auth, api
# subject: 简短描述不超过50个字符
# body: 详细描述,可以分多行
# footer: 不兼容变动、关闭Issue等
EOF
git config --global commit.template ~/.gitmessage
log_info "Git配置完成"
}
# 安装Git工具
install_git_tools() {
log_info "安装Git工具..."
# 安装tig
sudo apt-get install -y tig
# 安装lazygit
LAZYGIT_VERSION=$(curl -s "https://api.github.com/repos/jesseduffield/lazygit/releases/latest" | grep -Po '"tag_name": "v\K[^"]*')
curl -Lo lazygit.tar.gz "https://github.com/jesseduffield/lazygit/releases/latest/download/lazygit_${LAZYGIT_VERSION}_Linux_x86_64.tar.gz"
tar xf lazygit.tar.gz -C /usr/local/bin
rm lazygit.tar.gz
# 安装delta
curl -sS https://raw.githubusercontent.com/dandavison/delta/master/install.sh | bash
log_info "Git工具安装完成"
}
# 配置GitHub
configure_github() {
log_info "配置GitHub..."
# 生成SSH密钥
if [ ! -f ~/.ssh/id_ed25519 ]; then
ssh-keygen -t ed25519 -C "$(whoami)@$(hostname)"
fi
# 显示公钥
log_info "你的GitHub SSH公钥"
cat ~/.ssh/id_ed25519.pub
log_info "请将上述公钥添加到你的GitHub账号中。"
log_info "访问 https://github.com/settings/keys 添加SSH密钥。"
}
# 主函数
main() {
log_info "开始配置Git环境..."
# 安装Git
install_git
# 配置Git
configure_git
# 安装Git工具
install_git_tools
# 配置GitHub
configure_github
log_info "Git环境配置完成"
log_info "请运行 'source ~/.zshrc' 或重新打开终端以使环境变量生效。"
}
# 执行主函数
main