Created
June 9, 2025 14:04
-
-
Save dpeng817/8915fa6875080e2f2e01e59584333226 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Function to create a git worktree with UV environment setup | |
| create_worktree() { | |
| local branch_name="$1" | |
| local start_branch="$2" | |
| # Validate arguments | |
| if [[ -z "$branch_name" ]]; then | |
| echo "Usage: create_worktree <branch_name> [start_branch]" | |
| echo "Example: create_worktree my-feature-branch master" | |
| echo " create_worktree my-feature-branch # uses current branch" | |
| return 1 | |
| fi | |
| # Use current branch if start_branch not specified | |
| if [[ -z "$start_branch" ]]; then | |
| start_branch=$(git branch --show-current) | |
| echo "No start branch specified, using current branch: $start_branch" | |
| fi | |
| # Check if we're in a git repository | |
| if ! git rev-parse --git-dir > /dev/null 2>&1; then | |
| echo "Error: Not in a git repository" | |
| return 1 | |
| fi | |
| # Check if start_branch exists | |
| if ! git show-ref --verify --quiet refs/heads/"$start_branch" && \ | |
| ! git show-ref --verify --quiet refs/remotes/origin/"$start_branch"; then | |
| echo "Error: Branch '$start_branch' does not exist" | |
| return 1 | |
| fi | |
| # Get current directory to determine install command | |
| local current_dir=$(pwd) | |
| local worktree_dir="$HOME/$branch_name" | |
| # Check if worktree directory already exists | |
| if [[ -d "$worktree_dir" ]]; then | |
| echo "Error: Directory '$worktree_dir' already exists" | |
| return 1 | |
| fi | |
| echo "Creating git worktree for branch '$branch_name' from '$start_branch'..." | |
| # Create the worktree | |
| if ! git worktree add -b "$branch_name" "$worktree_dir" "$start_branch"; then | |
| echo "Error: Failed to create git worktree" | |
| return 1 | |
| fi | |
| # Navigate to the new worktree | |
| cd "$worktree_dir" || { | |
| echo "Error: Failed to navigate to worktree directory" | |
| return 1 | |
| } | |
| echo "Setting up UV virtual environment..." | |
| # Create UV virtual environment with Python 3.11.8 | |
| if ! uv venv --python 3.11.8; then | |
| echo "Error: Failed to create UV virtual environment" | |
| return 1 | |
| fi | |
| # Activate the virtual environment | |
| source .venv/bin/activate | |
| echo "Copying claude.md files from original worktree..." | |
| # Find and copy all claude.md files from the original directory | |
| if find "$current_dir" -name "claude.md" -type f | head -1 > /dev/null; then | |
| while IFS= read -r -d '' claude_file; do | |
| # Get the relative path from the original directory | |
| relative_path="${claude_file#$current_dir/}" | |
| # Create the directory structure in the new worktree if needed | |
| target_dir="$(dirname "$relative_path")" | |
| if [[ "$target_dir" != "." ]]; then | |
| mkdir -p "$target_dir" | |
| fi | |
| # Copy the file | |
| cp "$claude_file" "$relative_path" | |
| echo " Copied: $relative_path" | |
| done < <(find "$current_dir" -name "claude.md" -type f -print0) | |
| else | |
| echo " No claude.md files found in original worktree" | |
| fi | |
| echo "Installing dependencies..." | |
| # Determine which make command to run based on original directory | |
| if [[ "$current_dir" == *"$DAGSTER_GIT_REPO_DIR"* ]]; then | |
| echo "Running 'make dev_install' in main directory..." | |
| make dev_install | |
| else | |
| echo "Running 'make dev_install' in dagster-cloud subdirectory..." | |
| if [[ -d "dagster-cloud" ]]; then | |
| (cd dagster-cloud && make dev_install) | |
| else | |
| echo "Warning: dagster-cloud subdirectory not found" | |
| fi | |
| fi | |
| echo "β Worktree setup complete!" | |
| echo "π Location: $worktree_dir" | |
| echo "π Python environment: activated" | |
| echo "π¦ Dependencies: installed" | |
| echo "π Claude.md files: copied" | |
| echo "" | |
| echo "You are now in the new worktree directory with the virtual environment activated." | |
| } | |
| # Usage examples: | |
| # create_worktree my-feature-branch master | |
| # create_worktree my-feature-branch # uses current branch | |
| # create_worktree bugfix-123 develop | |
| # Function to delete a git worktree and clean up | |
| delete_worktree() { | |
| local branch_name="$1" | |
| # Validate arguments | |
| if [[ -z "$branch_name" ]]; then | |
| echo "Usage: delete_worktree <branch_name>" | |
| echo "Example: delete_worktree my-feature-branch" | |
| return 1 | |
| fi | |
| local worktree_dir="$HOME/$branch_name" | |
| # Check if worktree directory exists | |
| if [[ ! -d "$worktree_dir" ]]; then | |
| echo "Error: Worktree directory '$worktree_dir' does not exist" | |
| return 1 | |
| fi | |
| # Check if it's actually a git worktree | |
| if [[ ! -f "$worktree_dir/.git" ]]; then | |
| echo "Error: '$worktree_dir' does not appear to be a git worktree" | |
| return 1 | |
| fi | |
| echo "Deleting git worktree '$branch_name'..." | |
| echo "π Location: $worktree_dir" | |
| # Confirm deletion | |
| read -p "Are you sure you want to delete this worktree? (y/N): " -n 1 -r | |
| echo | |
| if [[ ! $REPLY =~ ^[Yy]$ ]]; then | |
| echo "Cancelled." | |
| return 0 | |
| fi | |
| # Remove the worktree using git | |
| if git worktree remove "$worktree_dir" --force; then | |
| echo "β Worktree '$branch_name' deleted successfully" | |
| echo "ποΈ Directory: $worktree_dir" | |
| echo "π Virtual environment: removed" | |
| echo "π All files (including claude.md): removed" | |
| else | |
| echo "β Failed to remove worktree with git command" | |
| echo "You may need to manually remove the directory:" | |
| echo " rm -rf '$worktree_dir'" | |
| return 1 | |
| fi | |
| # Optional: Ask if they want to delete the branch too | |
| if git show-ref --verify --quiet refs/heads/"$branch_name"; then | |
| echo "" | |
| read -p "Do you also want to delete the branch '$branch_name'? (y/N): " -n 1 -r | |
| echo | |
| if [[ $REPLY =~ ^[Yy]$ ]]; then | |
| if git branch -D "$branch_name"; then | |
| echo "πΏ Branch '$branch_name' deleted" | |
| else | |
| echo "β Failed to delete branch '$branch_name'" | |
| fi | |
| fi | |
| fi | |
| } | |
| # delete_worktree my-feature-branch | |
| # delete_worktree bugfix-123 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment