Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

6.5 KB
Raw Blame
---
source: src/shell/mod.rs
expression: output
---
# worktrunk shell integration for bash

# Only initialize if wt is available (in PATH or via WORKTRUNK_BIN)
if command -v wt >/dev/null 2>&1 || [[ -n "${WORKTRUNK_BIN:-}" ]]; then

    # Override wt command so it can change the parent shell's directory.
    # WORKTRUNK_BIN can override the binary path (for testing dev builds).
    wt() {
        local use_source=false
        local args=()
        for arg in "$@"; do
            if [[ "$arg" == --source ]]; then use_source=true; else args+=("$arg"); fi
        done
        # Completion mode: call binary directly, no directive files needed.
        # This check MUST be here (not in the binary) because clap's completion
        # handler runs before argument parsing.
        if [[ -n "${COMPLETE:-}" ]]; then
            command "${WORKTRUNK_BIN:-wt}" "${args[@]}"
            return
        fi

        # Native SIGINT can unwind this body before ordinary cleanup. Capture one
        # cleanup function for both the next prompt and a noninteractive shell exit.
        local cd_file owner cleanup_body exit_trap owns_exit=false prompt_provider prompt_armed=true
        local prompt_value="${PROMPT_COMMAND-}" prompt_present="${PROMPT_COMMAND+x}" prompt_declared=false
        local prompt_declaration prompt_flags
        if builtin declare -p PROMPT_COMMAND >/dev/null 2>&1; then prompt_declared=true; fi
        # Readonly prompts cannot be replaced; allexport would export our callback.
        if [[ $- == *a* ]] || ! (PROMPT_COMMAND="$prompt_value") 2>/dev/null; then prompt_armed=false; fi
        cd_file="$(mktemp)" || return
        owner="_wt_cleanup_${cd_file##*/}"
        owner="${owner//[^a-zA-Z0-9_]/_}"
        exit_trap="$(builtin trap -p EXIT)"
        # trap -p may expose an inherited but inactive EXIT handler in a subshell.
        # Reinstalling it would activate the parent's handler there.
        if [[ ( $BASH_SUBSHELL -eq 0 || -z "$exit_trap" ) && ( $- != *i* || "$prompt_armed" == true ) ]]; then owns_exit=true; fi
        builtin printf -v prompt_provider 'if %s prompt "$?"; then builtin eval %q; else builtin eval %q; fi' "$owner" "$prompt_value" "$prompt_value"
        # %q captures the state after the caller's local variables have unwound.
        builtin printf -v cleanup_body '
            local cd_file=%q owner=%q exit_trap=%q owns_exit=%q
            local prompt_value=%q prompt_present=%q prompt_declared=%q prompt_provider=%q
        ' "$cd_file" "$owner" "$exit_trap" "$owns_exit" "$prompt_value" "$prompt_present" "$prompt_declared" "$prompt_provider"
        # `builtin cd` bypasses any user `cd` alias or function (e.g. zoxide's
        # `alias cd=__zoxide_z`) that would otherwise be substituted into the
        # function body when it's defined. See #2643.
        cleanup_body+='
            local event=$1 status=$2 cd_status=0
            if [[ "$owns_exit" == true ]]; then builtin trap - EXIT; fi
            if [[ -s "$cd_file" ]]; then
                builtin cd -- "$(<"$cd_file")" || cd_status=$?
            fi
            command rm -f "$cd_file"
            if [[ ${PROMPT_COMMAND-} == "$prompt_provider" ]]; then
                if [[ -n "$prompt_present" ]]; then
                    PROMPT_COMMAND="$prompt_value"
                else
                    local prompt_flags
                    prompt_flags="$(builtin declare -p PROMPT_COMMAND 2>/dev/null)" || prompt_flags=
                    prompt_flags=${prompt_flags#declare }
                    prompt_flags=${prompt_flags%% *}
                    if [[ "$prompt_declared" == true ]]; then
                        builtin unset "PROMPT_COMMAND[0]"
                    else
                        builtin unset PROMPT_COMMAND
                    fi
                    if [[ "$prompt_flags" == *x* ]]; then builtin export PROMPT_COMMAND; fi
                fi
            fi
            if [[ "$owns_exit" == true && -n "$exit_trap" ]]; then builtin eval "$exit_trap"; fi
            builtin unset -f "$owner"
            if [[ "$event" == normal && $status -eq 0 ]]; then
                return "$cd_status"
            else
                return "$status"
            fi
        '
        builtin eval "$owner() { $cleanup_body }"
        if [[ "$prompt_armed" == true ]]; then PROMPT_COMMAND="$prompt_provider"; fi
        # Bash 3.2 only reports an exported, unset variable after assignment.
        prompt_declaration="$(builtin declare -p PROMPT_COMMAND 2>/dev/null)" || prompt_declaration=
        prompt_flags=${prompt_declaration#declare }
        prompt_flags=${prompt_flags%% *}

        local exit_code=0
        local execution=("${WORKTRUNK_BIN:-wt}" "${args[@]}")
        if [[ "$use_source" == true ]]; then execution=(cargo run --bin wt --quiet -- "${args[@]}"); fi
        if [[ "$owns_exit" == true ]]; then
            local exit_provider="$owner exit \"\$?\""
            if [[ -n "$exit_trap" ]]; then
                local exit_callback=${exit_trap#trap -- }
                exit_callback=${exit_callback% EXIT}
                builtin printf -v exit_provider 'if %s exit "$?"; then builtin eval %s; else builtin eval %s; fi' "$owner" "$exit_callback" "$exit_callback"
            fi
            builtin trap "$exit_provider" EXIT
        fi
        case "$prompt_armed:$prompt_flags" in
            true:*x*) PROMPT_COMMAND="$prompt_value" WORKTRUNK_DIRECTIVE_CD_FILE="$cd_file" command "${execution[@]}" || exit_code=$? ;;
            *) WORKTRUNK_DIRECTIVE_CD_FILE="$cd_file" command "${execution[@]}" || exit_code=$? ;;
        esac
        if "$owner" normal "$exit_code"; then return 0; else return $?; fi
    }

    # Lazy completions - generate on first TAB, then delegate to clap's completer
    _wt_lazy_complete() {
        # Generate completions function once (check if clap's function exists)
        if ! declare -F _clap_complete_wt >/dev/null; then
            # Use `command` to bypass the shell function and call the binary directly.
            # Without this, `wt` would call the shell function which evals
            # the completion script internally but doesn't re-emit it.
            # WORKTRUNK_COMPLETE_NAME emits the registration under the name bound
            # below; clap would otherwise name everything after its own command
            # name and the call below would hit an undefined function (#3816).
            eval "$(WORKTRUNK_COMPLETE_NAME="wt" COMPLETE=bash command "${WORKTRUNK_BIN:-wt}" 2>/dev/null)" || return
        fi
        _clap_complete_wt "$@"
    }

    complete -o nospace -o bashdefault -F _wt_lazy_complete wt
fi