Creates a git work-tree for repos that use git-crypt

#!/usr/bin/env bash
# git worktree add for a git-crypt repo: git-crypt-worktree
set -euo pipefail
if [ $# -ne 2 ]; then
echo 'usage: git-crypt-worktree ' >&2
exit 1
fi
branch=$1
wt=$2
common=$(git rev-parse --path-format=absolute --git-common-dir)
if [ ! -d "$common/git-crypt" ]; then
echo "git-crypt: no key in $common/git-crypt, run 'git-crypt unlock' in the main checkout first" >&2
exit 1
fi
# Existing local or remote branch: plain add (git sets up tracking for a remote one). Otherwise create it from HEAD.
if [ -n "$(git for-each-ref --count=1 "refs/heads/$branch" "refs/remotes/*/$branch")" ]; then
git worktree add --no-checkout "$wt" "$branch"
else
git worktree add --no-checkout -b "$branch" "$wt"
fi
# git-crypt reads its key from the worktree's own git dir, so point it at the main one.
ln -s "$common/git-crypt" "$(git -C "$wt" rev-parse --absolute-git-dir)/git-crypt"
git -C "$wt" reset --hard
参与讨论