Loading...
Loading...
Sync .env files from git root repository to worktrees. Use when asked to sync env, copy env, environment file, or when working in a git worktree that is missing a .env file. Automatically detects missing .env in worktrees.
npx skill4agent add qiaoshouqing/skills env-sync.env# Check if .git is a file (worktree) or directory (regular repo)
[ -f .git ] && echo "worktree" || echo "not-worktree"# Read gitdir and extract root path
GITDIR=$(cat .git | grep "^gitdir:" | sed 's/gitdir: //')
ROOT_REPO=$(echo "$GITDIR" | sed 's/\.git\/worktrees\/.*//')
# Validate: must be a real git repository
if [ -d "${ROOT_REPO}/.git" ]; then
echo "Valid root: $ROOT_REPO"
else
echo "Invalid root path - not a git repository"
exit 1
fi# IMPORTANT: Always check before copying
if [ -f ./.env ]; then
echo "WARNING: .env already exists in this worktree"
# ASK USER before proceeding
fiif [ -f "${ROOT_REPO}/.env" ]; then
cp "${ROOT_REPO}/.env" ./.env
echo "Synced .env from $ROOT_REPO"
else
echo "No .env found in root repository: $ROOT_REPO"
fi# Safe env sync with validation
if [ ! -f .git ]; then
echo "Not a git worktree"
exit 1
fi
GITDIR=$(cat .git | grep "^gitdir:" | sed 's/gitdir: //')
ROOT=$(echo "$GITDIR" | sed 's/\.git\/worktrees\/.*//')
# Validate root is a git repo
if [ ! -d "${ROOT}/.git" ]; then
echo "Invalid: $ROOT is not a git repository"
exit 1
fi
# Check if .env exists at root
if [ ! -f "${ROOT}/.env" ]; then
echo "No .env in root: $ROOT"
exit 1
fi
# Check if local .env exists - MUST ASK USER
if [ -f ./.env ]; then
echo "WARNING: .env already exists here. Ask user before overwriting."
exit 0
fi
# Safe to copy
cp "${ROOT}/.env" ./.env
echo "Synced .env from $ROOT"| Situation | Action |
|---|---|
| Not a worktree | Inform user, no action |
| Invalid root path | Warn user, no action |
| No root .env | Tell user path, suggest checking |
| .env exists | Ask user before overwrite |
| Permission denied | Suggest checking permissions |