|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -Eeou pipefail |
| 4 | +test "${MDB_BASH_DEBUG:-0}" -eq 1 && set -x |
| 5 | + |
| 6 | +usage() { |
| 7 | + echo "Initialize git worktree for the given branch." |
| 8 | + echo "It will create worktree and initialize it to be ready to use." |
| 9 | + echo "Usage: $0 [-f] <branch>" |
| 10 | + echo " -f - force creation/initialization even if worktree already exists or branch is checked out" |
| 11 | + echo "" |
| 12 | + echo "Example: $0 -f user/feature-branch" |
| 13 | + echo " This will create a worktree at ../user/feature-branch" |
| 14 | +} |
| 15 | + |
| 16 | +force=0 |
| 17 | +while getopts 'f' opt; do |
| 18 | + case ${opt} in |
| 19 | + (f) force=1 ;; |
| 20 | + (*) usage; exit 1;; |
| 21 | + esac |
| 22 | +done |
| 23 | +shift "$((OPTIND-1))" |
| 24 | + |
| 25 | +branch=$1 |
| 26 | + |
| 27 | +if [[ -z ${branch} ]]; then |
| 28 | + echo "Error: missing branch parameter" |
| 29 | + usage |
| 30 | + exit 1 |
| 31 | +fi |
| 32 | + |
| 33 | +# Convert slashes to underscores for directory name |
| 34 | +branch_dir="${branch//\//_}" |
| 35 | +worktree_path="${PROJECT_DIR}/../${branch_dir}" |
| 36 | + |
| 37 | +if ! git worktree list | grep "${worktree_path}" >/dev/null; then |
| 38 | + echo "Creating git worktree for branch '${branch}' at ${worktree_path}..." |
| 39 | + |
| 40 | + # Check if branch exists (locally or remotely) |
| 41 | + branch_exists=0 |
| 42 | + if git show-ref --verify --quiet "refs/heads/${branch}" || git show-ref --verify --quiet "refs/remotes/origin/${branch}"; then |
| 43 | + branch_exists=1 |
| 44 | + fi |
| 45 | + |
| 46 | + if [[ ${branch_exists} == 1 ]]; then |
| 47 | + # Branch exists, use it |
| 48 | + if [[ ${force} == 1 ]]; then |
| 49 | + git worktree add -f "${worktree_path}" "${branch}" || true |
| 50 | + else |
| 51 | + git worktree add "${worktree_path}" "${branch}" |
| 52 | + fi |
| 53 | + else |
| 54 | + # Branch doesn't exist, create it from current branch |
| 55 | + echo "Branch '${branch}' does not exist. Creating it from current branch..." |
| 56 | + if [[ ${force} == 1 ]]; then |
| 57 | + git worktree add -f -b "${branch}" "${worktree_path}" || true |
| 58 | + else |
| 59 | + git worktree add -b "${branch}" "${worktree_path}" |
| 60 | + fi |
| 61 | + fi |
| 62 | +fi |
| 63 | + |
| 64 | +if [[ ! -f "${worktree_path}/scripts/dev/contexts/private-context" || ${force} == 1 ]]; then |
| 65 | + echo "Initializing worktree in ${worktree_path}..." |
| 66 | + time ( |
| 67 | + mkdir -p "${worktree_path}/.generated" |
| 68 | + cp "${PROJECT_DIR}/.generated/.current_context" "${worktree_path}/.generated/" |
| 69 | + cp "${PROJECT_DIR}/scripts/dev/contexts/private-context" "${worktree_path}/scripts/dev/contexts/private-context" |
| 70 | + cp -r "${PROJECT_DIR}/bin" "${worktree_path}/bin" |
| 71 | + ( |
| 72 | + cd "${worktree_path}" |
| 73 | + make switch context="$(cat ".generated/.current_context")" |
| 74 | + scripts/dev/recreate_python_venv.sh |
| 75 | + ) |
| 76 | + ) |
| 77 | +fi |
| 78 | + |
| 79 | +( |
| 80 | + cd "${worktree_path}" |
| 81 | + make switch context="$(cat ".generated/.current_context")" |
| 82 | + source venv/bin/activate |
| 83 | + source .generated/context.export.env |
| 84 | +) |
| 85 | + |
| 86 | +# temporarily copying the files as those are not committed to master yet and won't be available in new git workspace |
| 87 | +cp -r "${PROJECT_DIR}/.run" "${worktree_path}/" |
| 88 | +cp -r "${PROJECT_DIR}/.idea" "${worktree_path}/" |
| 89 | +rm "${worktree_path}/.idea/workspace.xml" |
| 90 | +cp -r ${PROJECT_DIR}/scripts/aliases ${worktree_path}/scripts/aliases |
| 91 | + |
| 92 | +echo "Worktree $(realpath "${worktree_path}") has been prepared" |
0 commit comments