#!/bin/bash # 将 ~/.gitconfig 链接到 dotfile,并设置 Linux 凭据脚本可执行 SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # shellcheck disable=SC1091 source "$SCRIPT_DIR/common/help.sh" usage() { cat <<'EOF' 用法: git-init 备份已有 ~/.gitconfig,软链接到 ~/.dotfile/git/.gitconfig。 同时为 git-credential.sh 添加可执行权限。 若本机还没有 ~/.gitconfig.work,则生成空模板(~/workrepo 公司身份,需自行填写)。 凭据由 ~/.dotfile/git/git-credential.sh 处理(兼容 Git < 2.36,低版本回退 store)。 选项: -h, --help 显示此帮助 EOF } dotfile_help_requested "${1:-}" && dotfile_show_help set -euo pipefail echo 'Init git config' src="$HOME/.gitconfig" target="$HOME/.dotfile/git/.gitconfig" if [ -L "$src" ] && [ "$(readlink "$src")" = "$target" ]; then echo " $src already links to dotfile config, skip" else if [ -e "$src" ] || [ -L "$src" ]; then backup="${src}.backup.$(date +%Y%m%d%H%M%S)" mv "$src" "$backup" echo " Backed up existing config: $backup" fi ln -s "$target" "$src" echo " Linked: $src -> $target" fi chmod +x "$HOME/.dotfile/git/git-credential.sh" # 兼容旧路径:若仍存在则保持可执行 chmod +x "$HOME/.dotfile/git/git-credential-linux.sh" 2>/dev/null || true work_cfg="$HOME/.gitconfig.work" if [ -e "$work_cfg" ]; then echo " $work_cfg already exists, skip" else cat > "$work_cfg" <<'EOF' # 公司 Git 身份(仅本机,不要提交到 dotfile) # ~/workrepo 下的仓库会加载本文件。取消注释并填写后再 commit: # [user] # name = Your Name # email = you@company.com EOF echo " Created $work_cfg — fill in company user.name / user.email" fi