-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathworktree.sh
More file actions
99 lines (84 loc) · 2.87 KB
/
worktree.sh
File metadata and controls
99 lines (84 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/bin/bash
######################################################
# This script should work for any repository which uses Git version control.
#
# How to use this bash script?
# It is recommend to use a specific directory structure for this script to work BEST.
#
# Whenever you clone/download the Git repository for the very first time,
# following this directory structure
#
# Assume you do coding in this directory `/home/<user>/code`.
# This is how the directory structure should look like:
#
# /code/
# /<organisation-name>
# /<repository-name>
#
# After creating the directory structure in above format.
#
# ```
# cd /home/<user>/code/<organisation-name>/<repository-name>
# git clone <remote-repository-url> <default-branch-name>
# ```
#
# Usage
# ./worktree-setup.sh feature/new-feature
######################################################
main_directory_name=$(basename "$PWD")
# Function to create and setup a new worktree
setup_worktree() {
local branch_name="$1"
# Remove any special characters
local clean_branch_name=$(echo "${branch_name}" | sed 's/[^a-zA-Z0-9]/-/g')
# Create worktree
if git branch --list "${branch_name}" | grep -q "${branch_name}"; then
echo "Branch '$branch_name' exists."
git worktree add "../${clean_branch_name}" "${branch_name}"
else
echo "Creating worktree branch name: '${branch_name}'"
git worktree add -b "${branch_name}" "../${clean_branch_name}"
fi
# Move back a directory
cd ..
if [ -d "${clean_branch_name}" ]; then
# Change to new worktree directory
cd "./${clean_branch_name}"
else
echo "Directory '${clean_branch_name}' does not exists"
exit 1;
fi
# Add absolute path of the file you like to copy.
# Copy .env from `main|master` project directory
copy_to_relative_directory "packages/server/.env"
copy_to_relative_directory "packages/theme/.env"
################################
# From here onward you can customise this script
################################
# Install packages
if [ -x "$(command -v pnpm)" ]; then
pnpm install
else
pkgx +nodejs.org@22 +pnpm.io pnpm install
fi
# Set dynamic project name for Docker Compose
export DOCKER_COMPOSE_PROJECT_NAME="${clean_branch_name}"
export DOCKER_BUILDKIT=1
export COMPOSE_BAKE=true
# Spin up dev environment
docker compose -f docker-compose.dev.yml up -d
}
copy_to_relative_directory() {
local source_path="../${main_directory_name}"
local target_path="."
local file="${1#/}" # Remove leading slash if present
# Copy the file
if [ -f "${source_path}/${file}" ]; then
cp "${source_path}/${file}" "${target_path}/${file}"
echo "Copied '${target_path}/${file}' successfully"
else
echo "Error: File '${target_path}/${file}' not found"
return 1
fi
}
setup_worktree "$1"