Skip to content

feat: worktree extension apis - #517

Merged
sudo-tee merged 4 commits into
mainfrom
feat/worktree-extension-apis
Oct 10, 2026
Merged

sudo-tee merged 4 commits into
mainfrom
feat/worktree-extension-apis

Conversation

@sudo-tee

@sudo-tee sudo-tee commented Oct 9, 2026 •

Copy link
Copy Markdown
Owner

This should address #513

Summary

Adds public APIs for worktree workflows without modifying internals or changing Neovim’s cwd. Addresses #513 and #511.

New APIs

Directory-bound sessions

require('opencode.api').open_session({
  directory = '/path/to/repo/.worktrees/feature',
  new = true,
  title = 'Feature work',
})

Opens a session in a logical panel tab bound to the requested directory.

Live custom slash commands

local opencode = require('opencode')

opencode.register_slash_command({
  name = 'worktree',
  desc = 'Open a worktree session',
  args = true,
  fn = function(args)
    return require('opencode.api').open_session({
      directory = args[1],
      new = true,
    })
  end,
})

-- Remove when no longer needed:
opencode.unregister_slash_command('worktree')

Per-directory session lock policies

lock_session_to_directory now accepts a function alongside the existing boolean:

require('opencode').setup({
  lock_session_to_directory = function(change)
    local function common_dir(dir)
      local result = vim.system({
        'git', '-C', dir, 'rev-parse',
        '--path-format=absolute', '--git-common-dir',
      }, { text = true }):wait()

      return result.code == 0 and vim.trim(result.stdout) or nil
    end

    local repo = common_dir(change.from)
    return repo ~= nil and repo == common_dir(change.to)
  end,
})

Behavior

Directory-bound tabs preserve their directory across tab switches. Completion and new sessions use that directory. Locked tabs retain their binding on cwd changes; unlocked tabs resume following cwd.

Includes documentation and tests for all three APIs.

Allow lock_session_to_directory to be a function called on each directory
change with { from, to, session }. Manual lock still overrides the policy,
and errors are reported and fall back to following the cwd.

Track bound_editor_cwd on session tabs so a bound directory is
distinguished from an actual editor directory change in check_cwd and
handle_directory_change.
@sudo-tee sudo-tee changed the title Feat/worktree extension apis feat: worktree extension apis Oct 10, 2026
@sudo-tee
sudo-tee merged commit ee684b1 into main Oct 10, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant