@humanu/orchestra 0.5.2 โ†’ 0.5.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,227 @@
1
+ #!/bin/bash
2
+
3
+ ###############################################################################
4
+ # commands.sh โ€“ Pure Git-Worktree command-line operations
5
+ # ---------------------------------------------------------------------------
6
+ # This script provides pure git worktree management without any tmux functionality.
7
+ # It's sourced by gw.sh for command-line operations.
8
+ ###############################################################################
9
+
10
+ # Ensure we have access to core utilities from gw.sh
11
+ if ! declare -f git_repo_root >/dev/null 2>&1; then
12
+ echo "Error: commands.sh must be sourced from gw.sh (git.sh API required)" >&2
13
+ return 1 2>/dev/null || exit 1
14
+ fi
15
+
16
+ # --------------------------- Command Functions ------------------------------
17
+
18
+ # Copy .env files from source to target worktree
19
+ copy_env_files() {
20
+ local source_root="$1"
21
+ local target_root="$2"
22
+ echo "๐Ÿ”ง Copying .env files to new worktree..."
23
+ if [ -f "$source_root/.env" ]; then
24
+ cp "$source_root/.env" "$target_root/.env" && echo " โœ… Copied .env from root"
25
+ fi
26
+ if [ -d "$source_root/nodejs" ]; then
27
+ mkdir -p "$target_root/nodejs"
28
+ for env_file in "$source_root/nodejs"/.env*; do
29
+ [ -f "$env_file" ] || continue
30
+ cp "$env_file" "$target_root/nodejs/$(basename "$env_file")" && echo " โœ… Copied $(basename "$env_file") from nodejs/"
31
+ done
32
+ fi
33
+ if [ -d "$source_root/nextjs" ]; then
34
+ mkdir -p "$target_root/nextjs"
35
+ for env_file in "$source_root/nextjs"/.env*; do
36
+ [ -f "$env_file" ] || continue
37
+ cp "$env_file" "$target_root/nextjs/$(basename "$env_file")" && echo " โœ… Copied $(basename "$env_file") from nextjs/"
38
+ done
39
+ fi
40
+ if [ -d "$source_root/ingest_py" ]; then
41
+ mkdir -p "$target_root/ingest_py"
42
+ for env_file in "$source_root/ingest_py"/.env*; do
43
+ [ -f "$env_file" ] || continue
44
+ cp "$env_file" "$target_root/ingest_py/$(basename "$env_file")" && echo " โœ… Copied $(basename "$env_file") from ingest_py/"
45
+ done
46
+ fi
47
+ echo "๐Ÿ”ง Environment files copied successfully!"; echo ""
48
+ }
49
+
50
+ # Create branch + worktree
51
+ cmd_create_worktree() {
52
+ if [ -z "${1-}" ]; then
53
+ err "Branch name required"
54
+ echo "Usage: gw create <branch>"
55
+ return 1
56
+ fi
57
+ local branch_name="$1"
58
+ local root; root="$(git_require_repo_root)" || return 1
59
+
60
+ local dir; dir="$(git_build_worktree_path "$branch_name")"
61
+
62
+ info "๐ŸŒณ Creating branch '$branch_name' and worktree at '$dir'..."
63
+ dir="$(git_create_branch_and_worktree "$branch_name")"
64
+ copy_env_files "$root" "$dir"
65
+ info "โœ… Success! Worktree created at '$dir'."
66
+ echo "cd \"$dir\""
67
+ (cd "$dir" && git_status_short)
68
+ }
69
+
70
+ # Delete worktree + branch
71
+ cmd_delete_worktree() {
72
+ if [ -z "${1-}" ]; then
73
+ err "Branch name required"
74
+ echo "Usage: gw delete <branch>"
75
+ return 1
76
+ fi
77
+ local branch_name="$1"
78
+ git_require_repo || return 1
79
+
80
+ local worktree_path; worktree_path="$(git_branch_to_worktree_path "$branch_name")"
81
+
82
+ info "๐Ÿ—‘๏ธ Removing worktree and deleting branch '$branch_name'..."
83
+
84
+ # Remove worktree if it exists
85
+ if [[ -n "$worktree_path" ]] && [[ -d "$worktree_path" ]]; then
86
+ git_remove_worktree "$worktree_path"
87
+ fi
88
+
89
+ # Delete branch if it exists
90
+ if git_branch_exists "$branch_name"; then
91
+ git_delete_branch "$branch_name"
92
+ fi
93
+
94
+ info "๐Ÿงน Done."
95
+ }
96
+
97
+ # List worktrees (pure worktree listing, no tmux sessions)
98
+ cmd_list_worktrees() {
99
+ local root; root="$(git_require_repo_root)" || return 1
100
+
101
+ local here; here="$(git_current_path)"
102
+ info "๐ŸŒณ Listing Git Worktrees (repo: $root)"
103
+ echo ""
104
+ printf ' %-35s %-12s %s\n' "BRANCH" "HEAD" "PATH"
105
+ printf ' %s\n' "------------------------------------------------------------------------------------------"
106
+
107
+ while IFS=$'\t' read -r path branch sha; do
108
+ local rel="$path"
109
+ [[ "$path" == "$root" ]] && rel="."
110
+ [[ "$path" == "$root"/* ]] && rel="${path#$root/}"
111
+ local indicator=" "
112
+ [[ "$path" == "$here" ]] && indicator="โ—‰ "
113
+ printf '%s๐ŸŒฟ %-35s %-12s %s\n' "$indicator" "$branch" "$sha" "$rel"
114
+ done < <(git_list_worktrees)
115
+
116
+ printf ' %s\n' "------------------------------------------------------------------------------------------"
117
+ }
118
+
119
+ # Checkout/switch worktree (pure worktree switching, no tmux)
120
+ cmd_checkout_worktree() {
121
+ local create_new=false
122
+ local branch_name=""
123
+ local root; root="$(git_require_repo_root)" || return 1
124
+
125
+ if [[ "${1-}" == "-b" ]]; then
126
+ [[ -z "${2-}" ]] && { err "Branch name required for -b"; echo "Usage: gw checkout -b <branch>"; return 1; }
127
+ create_new=true
128
+ branch_name="$2"
129
+ [[ "$#" -gt 2 ]] && { err "Too many args"; return 1; }
130
+ else
131
+ [[ -z "${1-}" ]] && { err "Branch name required"; echo "Usage: gw checkout <branch>"; return 1; }
132
+ branch_name="$1"
133
+ [[ "$#" -gt 1 ]] && { err "Too many args"; return 1; }
134
+ fi
135
+
136
+ if $create_new; then
137
+ if git_branch_exists "$branch_name"; then
138
+ # Branch exists, ensure worktree exists
139
+ local wt; wt="$(git_branch_to_worktree_path "$branch_name")"
140
+ if [[ -n "$wt" ]]; then
141
+ info "โœ… Worktree exists at '$wt'"
142
+ echo "cd \"$wt\""
143
+ else
144
+ # Create worktree for existing branch
145
+ info "๐ŸŒณ Creating worktree for existing branch '$branch_name'..."
146
+ wt="$(git_ensure_worktree_for_branch "$branch_name")"
147
+ copy_env_files "$root" "$wt"
148
+ echo "cd \"$wt\""
149
+ fi
150
+ return 0
151
+ else
152
+ # Create new branch and worktree
153
+ cmd_create_worktree "$branch_name" || return $?
154
+ local wt; wt="$(git_branch_to_worktree_path "$branch_name")"
155
+ [[ -n "$wt" ]] && {
156
+ echo "cd \"$wt\""
157
+ return 0
158
+ }
159
+ err "Worktree created but not found"
160
+ return 1
161
+ fi
162
+ else
163
+ # Switch to existing branch
164
+ local wt; wt="$(git_branch_to_worktree_path "$branch_name")"
165
+ if [[ -n "$wt" ]]; then
166
+ echo "cd \"$wt\""
167
+ return 0
168
+ fi
169
+
170
+ # Try to create worktree for existing branch
171
+ if git_branch_exists "$branch_name"; then
172
+ info "๐ŸŒณ Creating worktree for existing branch '$branch_name'..."
173
+ wt="$(git_ensure_worktree_for_branch "$branch_name")"
174
+ copy_env_files "$root" "$wt"
175
+ echo "cd \"$wt\""
176
+ return 0
177
+ else
178
+ err "Branch '$branch_name' does not exist"
179
+ return 1
180
+ fi
181
+ fi
182
+ }
183
+
184
+ # Current worktree status
185
+ cmd_status() {
186
+ local root; root="$(git_require_repo_root)" || return 1
187
+
188
+ local here; here="$(git_current_path)"
189
+ local rel="$here"
190
+ [[ "$here" == "$root" ]] && rel="."
191
+ [[ "$here" == "$root"/* ]] && rel="${here#$root/}"
192
+
193
+ local branch; branch="$(git_current_branch)"
194
+ if [[ -z "$branch" ]]; then
195
+ branch="$(git_current_commit_short)"
196
+ [[ -z "$branch" ]] && { err "Cannot determine branch/commit"; return 1; }
197
+ info "๐ŸŒณ In worktree '$rel' (Detached HEAD: $branch)"
198
+ else
199
+ info "๐ŸŒณ In worktree '$rel' on branch '$branch'"
200
+ fi
201
+ echo ""
202
+ git_status "$@"
203
+ }
204
+
205
+ # Copy env files from current worktree to an existing target worktree
206
+ cmd_copy_env() {
207
+ local target_branch="${1-}"
208
+ if [[ -z "$target_branch" ]]; then
209
+ err "Target worktree name required"
210
+ echo "Usage: gw copy-env <worktreename>"
211
+ return 1
212
+ fi
213
+
214
+ git_require_repo || return 1
215
+
216
+ # Find absolute path of target worktree by branch name
217
+ local target_path; target_path="$(git_branch_to_worktree_path "$target_branch")"
218
+ if [[ -z "$target_path" ]]; then
219
+ err "Worktree for branch '$target_branch' not found"
220
+ return 1
221
+ fi
222
+
223
+ local source_root; source_root="$(git_current_path)"
224
+ info "๐Ÿ”ง Copying env files from '$source_root' -> '$target_path'"
225
+ copy_env_files "$source_root" "$target_path"
226
+ info "โœ… Env files synced to '$target_branch'"
227
+ }