env-sync

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Env Sync - Git Worktree Environment File Synchronization

Env Sync - Git工作树环境文件同步

This skill syncs
.env
files from a git root repository to git worktrees.
该技能可将Git根仓库中的.env文件同步到Git工作树。

When to Use

适用场景

  • User asks to sync env or copy env
  • User mentions missing environment variables or .env file
  • Working in a git worktree that lacks a .env file
  • User asks to set up environment for a worktree
  • 用户要求同步env或复制env
  • 用户提到缺少环境变量或.env文件
  • 在缺少.env文件的Git工作树中工作
  • 用户要求为工作树配置环境

Security Rules

安全规则

  1. NEVER display .env file contents - they contain secrets
  2. ALWAYS ask before overwriting - if .env already exists, confirm with user first
  3. Validate paths - ensure the root path exists and is a valid git repository
  4. No arbitrary paths - only sync from the actual git root repository
  1. 绝对不要显示.env文件内容 - 它们包含敏感信息
  2. 覆盖前务必询问用户 - 如果.env已存在,先与用户确认
  3. 验证路径 - 确保根路径存在且是有效的Git仓库
  4. 禁止任意路径 - 仅从实际的Git根仓库同步

Instructions for Agent

Agent操作说明

Step 1: Check if in a Git Worktree

步骤1:检查是否处于Git工作树中

bash
undefined
bash
undefined

Check if .git is a file (worktree) or directory (regular repo)

Check if .git is a file (worktree) or directory (regular repo)

[ -f .git ] && echo "worktree" || echo "not-worktree"

If not a worktree, inform user this skill is for git worktrees only.
[ -f .git ] && echo "worktree" || echo "not-worktree"

如果不是工作树,告知用户该技能仅适用于Git工作树。

Step 2: Find and Validate Root Repository

步骤2:查找并验证根仓库

bash
undefined
bash
undefined

Read gitdir and extract root path

Read gitdir and extract root path

GITDIR=$(cat .git | grep "^gitdir:" | sed 's/gitdir: //') ROOT_REPO=$(echo "$GITDIR" | sed 's/.git/worktrees/.*//')
GITDIR=$(cat .git | grep "^gitdir:" | sed 's/gitdir: //') ROOT_REPO=$(echo "$GITDIR" | sed 's/.git/worktrees/.*//')

Validate: must be a real git repository

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
undefined
if [ -d "${ROOT_REPO}/.git" ]; then echo "Valid root: $ROOT_REPO" else echo "Invalid root path - not a git repository" exit 1 fi
undefined

Step 3: Check for Existing .env

步骤3:检查是否存在现有.env文件

bash
undefined
bash
undefined

IMPORTANT: Always check before copying

IMPORTANT: Always check before copying

if [ -f ./.env ]; then echo "WARNING: .env already exists in this worktree"

ASK USER before proceeding

fi

**If .env exists: STOP and ask user "Do you want to overwrite the existing .env file?"**
if [ -f ./.env ]; then echo "WARNING: .env already exists in this worktree"

ASK USER before proceeding

fi

**如果.env已存在:停止操作并询问用户“是否要覆盖现有的.env文件?”**

Step 4: Copy .env (only after user confirmation if needed)

步骤4:复制.env文件(仅在需要时获得用户确认后执行)

bash
if [ -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
bash
if [ -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

Complete Safe Script

完整安全脚本

bash
undefined
bash
undefined

Safe env sync with validation

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/.*//')
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

Validate root is a git repo

if [ ! -d "${ROOT}/.git" ]; then echo "Invalid: $ROOT is not a git repository" exit 1 fi
if [ ! -d "${ROOT}/.git" ]; then echo "Invalid: $ROOT is not a git repository" exit 1 fi

Check if .env exists at root

Check if .env exists at root

if [ ! -f "${ROOT}/.env" ]; then echo "No .env in root: $ROOT" exit 1 fi
if [ ! -f "${ROOT}/.env" ]; then echo "No .env in root: $ROOT" exit 1 fi

Check if local .env exists - MUST ASK USER

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
if [ -f ./.env ]; then echo "WARNING: .env already exists here. Ask user before overwriting." exit 0 fi

Safe to copy

Safe to copy

cp "${ROOT}/.env" ./.env echo "Synced .env from $ROOT"
undefined
cp "${ROOT}/.env" ./.env echo "Synced .env from $ROOT"
undefined

Response Guidelines

回复指南

  • Always inform user what was done
  • If .env already exists: MUST ask user before overwriting
  • If root .env doesn't exist: tell user the path checked
  • Never display .env file contents
  • Report only: file exists/copied, line count, file size
  • 始终告知用户执行的操作
  • 如果.env已存在:覆盖前务必询问用户
  • 如果根仓库中没有.env:告知用户检查的路径
  • 绝对不要显示.env文件内容
  • 仅报告:文件存在/已复制、行数、文件大小

Error Handling

错误处理

SituationAction
Not a worktreeInform user, no action
Invalid root pathWarn user, no action
No root .envTell user path, suggest checking
.env existsAsk user before overwrite
Permission deniedSuggest checking permissions
情况操作
非工作树告知用户,不执行任何操作
无效根路径警告用户,不执行任何操作
根仓库无.env告知用户路径,建议检查
.env已存在覆盖前询问用户
权限不足建议检查权限