Skip to content

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

5.6 KB
Raw Blame
---
source: tests/integration_tests/init.rs
info:
  program: wt
  args:
    - config
    - shell
    - init
    - zsh
  env:
    APPDATA: "[TEST_CONFIG_HOME]"
    CLAUDE_CONFIG_DIR: "[TEST_CLAUDE_CONFIG]"
    CLICOLOR_FORCE: "1"
    COLUMNS: "500"
    GIT_ALLOW_PROTOCOL: file
    GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
    GIT_AUTHOR_EMAIL: [email protected]
    GIT_AUTHOR_NAME: Test User
    GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
    GIT_COMMITTER_EMAIL: [email protected]
    GIT_COMMITTER_NAME: Test User
    GIT_CONFIG_COUNT: "2"
    GIT_CONFIG_GLOBAL: /nonexistent/wt/gitconfig
    GIT_CONFIG_KEY_0: user.useConfigOnly
    GIT_CONFIG_KEY_1: rerere.enabled
    GIT_CONFIG_SYSTEM: /nonexistent/wt/gitconfig
    GIT_CONFIG_VALUE_0: "true"
    GIT_CONFIG_VALUE_1: "false"
    GIT_TERMINAL_PROMPT: "0"
    HOME: "[TEST_HOME]"
    LANG: C
    LC_ALL: C
    LLVM_PROFILE_FILE: "[LLVM_PROFILE_FILE]"
    OPENCODE_CONFIG_DIR: "[TEST_OPENCODE_CONFIG]"
    PATH: "[PATH]"
    TERM: alacritty
    USERPROFILE: "[TEST_HOME]"
    WORKTRUNK_APPROVALS_PATH: "[TEST_APPROVALS]"
    WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
    WORKTRUNK_SYSTEM_CONFIG_PATH: "[TEST_SYSTEM_CONFIG]"
    WORKTRUNK_TEST_BASH_INSTALLED: "0"
    WORKTRUNK_TEST_CLAUDE_INSTALLED: "0"
    WORKTRUNK_TEST_CODEX_INSTALLED: "0"
    WORKTRUNK_TEST_DELAYED_STREAM_MS: "-1"
    WORKTRUNK_TEST_EPOCH: "1735776000"
    WORKTRUNK_TEST_FISH_INSTALLED: "0"
    WORKTRUNK_TEST_GEMINI_INSTALLED: "0"
    WORKTRUNK_TEST_MOCK_CONFIG_DIR: "[TEST_MOCK_CONFIG]"
    WORKTRUNK_TEST_NUSHELL_ENV: "0"
    WORKTRUNK_TEST_OPENCODE_INSTALLED: "0"
    WORKTRUNK_TEST_PARENT_SHELL: ""
    WORKTRUNK_TEST_POWERSHELL_ENV: "0"
    WORKTRUNK_TEST_POWERSHELL_INSTALLED: "0"
    WORKTRUNK_TEST_PROBE_TIMEOUT_MS: "60000"
    WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"
    WORKTRUNK_TEST_ZSH_INSTALLED: "0"
    XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"
---
success: true
exit_code: 0
----- stdout -----
# worktrunk shell integration for zsh
#
# Completions require zsh's completion system (compinit). If completions don't work:
#   autoload -Uz compinit && compinit  # add before this line in .zshrc

# 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 -a 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

        local cd_file exit_code=0
        cd_file="$(mktemp)"

        local -a execution=("${WORKTRUNK_BIN:-wt}" "${args[@]}")
        if [[ "$use_source" == true ]]; then
            execution=(cargo run --bin wt --quiet -- "${args[@]}")
        fi
        {
            WORKTRUNK_DIRECTIVE_CD_FILE="$cd_file" command "${execution[@]}"
        } always {
            exit_code=$?
            # cd file holds a raw path (no shell escaping needed).
            # `builtin cd` bypasses any user `cd` alias or function (e.g. zoxide's
            # `alias cd=__zoxide_z`) that would otherwise be substituted into this
            # function body when it's defined. See #2643.
            if [[ -s "$cd_file" ]]; then
                builtin cd -- "$(<"$cd_file")"
                local cd_exit=$?
                if [[ $exit_code -eq 0 ]]; then
                    exit_code=$cd_exit
                fi
            fi

            command rm -f "$cd_file"
        } || :
        return "$exit_code"
    }

    # 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 ! (( $+functions[_clap_dynamic_completer_wt] )); 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=zsh command "${WORKTRUNK_BIN:-wt}" 2>/dev/null)" || return
        fi
        _clap_dynamic_completer_wt "$@"
    }

    # Register completion (silently skip if compinit hasn't run yet).
    # We don't warn here because this script runs on every shell startup - users
    # shouldn't see warnings every time they open a terminal. Instead, `wt config
    # shell install` detects missing compinit and shows a one-time advisory.
    if (( $+functions[compdef] )); then
        compdef _wt_lazy_complete wt
        # Single-column display keeps descriptions visually associated with each branch.
        # Users can override: zstyle ':completion:*:wt:*' list-max ''
        zstyle ':completion:*:wt:*' list-max 1
        # Prevent grouping branches with identical descriptions (same timestamp) on one line.
        # Without this, "release  main  -- + 12m" instead of separate lines per branch.
        zstyle ':completion:*:*:wt:*' list-grouped false
    fi
fi

----- stderr -----