From 9551225ee921635d6c6bac8d85292c0a912f5e3f Mon Sep 17 00:00:00 2001 From: laily Date: Fri, 26 Jun 2026 11:10:23 +0800 Subject: [PATCH] feat: update git credential --- AGENTS.md | 7 ++++-- git/.gitconfig | 6 ++++++ git/.gitconfig.darwin | 4 ++-- git/.gitconfig.linux | 5 ++--- git/git-credential-linux.sh | 26 ++-------------------- git/git-credential.sh | 43 +++++++++++++++++++++++++++++++++++++ script/git-init.sh | 8 +++++-- 7 files changed, 66 insertions(+), 33 deletions(-) create mode 100755 git/git-credential.sh diff --git a/AGENTS.md b/AGENTS.md index 120296d..594602a 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -60,8 +60,11 @@ ff reload # 重载 fish 配置 ## Git 配置 -- 公共配置在 `git/.gitconfig`;**按系统拆分**用 `includeIf ondarwin` / `onlinux` -- Linux 凭据:`git/git-credential-linux.sh`(libsecret 优先,否则 cache) +- 公共配置在 `git/.gitconfig` +- **凭据**(所有 Git 版本):`git/git-credential.sh` 写在主配置里,不依赖 `includeIf ondarwin/onlinux`(需 Git 2.36+) + - macOS:`osxkeychain` → 回退 `store`(`~/.git-credentials`) + - Linux:`libsecret` → 回退 `store` +- 平台扩展(可选):`git/.gitconfig.darwin` / `.gitconfig.linux` 仅在高版本 Git 下通过 includeIf 加载 ## 边界(不要做的事) diff --git a/git/.gitconfig b/git/.gitconfig index d88c7d2..548f670 100644 --- a/git/.gitconfig +++ b/git/.gitconfig @@ -123,6 +123,12 @@ [http] sslVerify = false +# 凭据:统一 wrapper,不依赖 includeIf ondarwin/onlinux(需 Git 2.36+) +# 低版本 Git 也会加载本节;回退为 git-credential-store(~/.git-credentials) +[credential] + helper = ~/.dotfile/git/git-credential.sh + +# 以下 includeIf 需 Git 2.36+;仅用于其它平台差异配置(若有) [includeIf "ondarwin"] path = ~/.dotfile/git/.gitconfig.darwin [includeIf "onlinux"] diff --git a/git/.gitconfig.darwin b/git/.gitconfig.darwin index 788266b..eaaf0f4 100644 --- a/git/.gitconfig.darwin +++ b/git/.gitconfig.darwin @@ -1,2 +1,2 @@ -[credential] - helper = osxkeychain +# macOS 专用 Git 配置(可选扩展;凭据见主配置 git-credential.sh) +# 需要 Git 2.36+ 的 includeIf ondarwin 才会加载本文件 diff --git a/git/.gitconfig.linux b/git/.gitconfig.linux index a7eddff..19b52f5 100644 --- a/git/.gitconfig.linux +++ b/git/.gitconfig.linux @@ -1,3 +1,2 @@ -# 凭据:优先 libsecret(见 git-credential-linux.sh),未安装则 cache + 提示 -[credential] - helper = ~/.dotfile/git/git-credential-linux.sh +# Linux 专用 Git 配置(可选扩展;凭据见主配置 git-credential.sh) +# 需要 Git 2.36+ 的 includeIf onlinux 才会加载本文件 diff --git a/git/git-credential-linux.sh b/git/git-credential-linux.sh index c065edb..fb58d58 100755 --- a/git/git-credential-linux.sh +++ b/git/git-credential-linux.sh @@ -1,25 +1,3 @@ #!/bin/bash -# Linux 凭据 helper:优先 libsecret,否则回退 cache 并提示安装 -set -euo pipefail - -warn_missing_libsecret() { - local marker="${XDG_CACHE_HOME:-$HOME/.cache}/git-credential-linux.warned" - [[ -f "$marker" ]] && return 0 - mkdir -p "$(dirname "$marker")" - touch "$marker" - cat >&2 <<'EOF' -提示: 未找到 git-credential-libsecret,当前使用内存缓存(默认 1 周过期或登出后失效)。 - -安装持久化凭据存储: - Debian/Ubuntu: sudo apt install libsecret-1-0 git-credential-libsecret - Fedora/RHEL: sudo dnf install git-credential-libsecret - Arch Linux: sudo pacman -S libsecret git -EOF -} - -if command -v git-credential-libsecret >/dev/null 2>&1; then - exec git-credential-libsecret "$@" -fi - -warn_missing_libsecret -exec git-credential-cache --timeout=604800 "$@" +# 已合并到 git-credential.sh;保留此文件仅为兼容旧配置引用 +exec "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/git-credential.sh" "$@" diff --git a/git/git-credential.sh b/git/git-credential.sh new file mode 100755 index 0000000..f1b9d2b --- /dev/null +++ b/git/git-credential.sh @@ -0,0 +1,43 @@ +#!/bin/bash +# 跨平台 Git 凭据 helper(不依赖 includeIf ondarwin/onlinux,兼容 Git < 2.36) +# macOS: osxkeychain → store +# Linux: libsecret → store +set -euo pipefail + +warn_store_fallback() { + local reason="$1" + local marker="${XDG_CACHE_HOME:-$HOME/.cache}/git-credential-store.warned" + [[ -f "$marker" ]] && return 0 + mkdir -p "$(dirname "$marker")" + touch "$marker" + cat >&2 </dev/null 2>&1; then + exec git-credential-osxkeychain "$@" + fi + warn_store_fallback "未找到 osxkeychain" + exec git-credential-store "$@" + ;; +Linux) + if command -v git-credential-libsecret >/dev/null 2>&1; then + exec git-credential-libsecret "$@" + fi + warn_store_fallback "未找到 git-credential-libsecret" + exec git-credential-store "$@" + ;; +*) + warn_store_fallback "未知系统 $(uname -s)" + exec git-credential-store "$@" + ;; +esac diff --git a/script/git-init.sh b/script/git-init.sh index d326d94..add9ca4 100755 --- a/script/git-init.sh +++ b/script/git-init.sh @@ -10,7 +10,9 @@ usage() { 用法: git-init 备份已有 ~/.gitconfig,软链接到 ~/.dotfile/git/.gitconfig。 -同时为 git-credential-linux.sh 添加可执行权限。 +同时为 git-credential.sh 添加可执行权限。 + +凭据由 ~/.dotfile/git/git-credential.sh 处理(兼容 Git < 2.36,低版本回退 store)。 选项: -h, --help 显示此帮助 @@ -28,4 +30,6 @@ if [ -f "$src" ]; then fi ln -s "$HOME/.dotfile/git/.gitconfig" "$src" -chmod +x "$HOME/.dotfile/git/git-credential-linux.sh" +chmod +x "$HOME/.dotfile/git/git-credential.sh" +# 兼容旧路径:若仍存在则保持可执行 +chmod +x "$HOME/.dotfile/git/git-credential-linux.sh" 2>/dev/null || true