Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Agent 笔记:远程工作区里打开和新建 Git 工作树

状态:已实现

## 先说结论

受信任的远程工作区里,Git 工具窗口“工作树”页签的打开和新建不再被拒绝。打开工作树就是打开主机上的另一个文件夹:Lithe 用打开这个工作区所用的连接(或 WSL 发行版)再连一次,在本窗口的新标签页里经一个新会话打开它;已有标签页通过活着的会话打开着它时,直接切过去,不再连接。新建对话框显示和接收主机路径,Core 的“放在仓库旁边”的建议也以主机路径显示,创建由主机上的 Git 完成。“在新窗口打开”和“在文件浏览器中显示”仍然拒绝:新客户端只打开本机文件夹(#2231 S10),文件夹也不在本机。

## 问题

工作树列表、锁定、解锁、删除在 #2283 已经走主机,但打开和新建被拒绝,原因有三:

- 工作树是主机上的另一个文件夹,打开它需要一个绑定到那个文件夹的会话;远程工作区的会话只绑定一个主机根目录(`RemoteWorkspace` 的 RemoteRoot),不能拿来打开别的文件夹。
- 新建对话框的建议路径由 Core 的 `git.worktreePreview` 按仓库路径推算。远程工作区的仓库路径是本机占位路径,建议出来的也是本机占位目录旁的路径,Core 再把它映射回主机时会落到错误的位置,Windows 上还会被当成本机路径。
- 用户在对话框里输入的是主机路径(POSIX),在 Windows 客户端上不是绝对路径,Core 会以“不是绝对路径”拒绝。

## 决策

### 打开工作树 = 用同一个连接打开另一个主机文件夹

- `openWorktree` 在远程工作区里改走 `openRemoteWorktree`:把列表里的占位路径换回主机路径(`remoteplaceholder.HostPath`),再交给 `openRemoteHostFolder`。换不回来说明列表过期,记为拒绝(`not_host_path`)并重新读取列表。
- `openRemoteHostFolder` 先按占位根 `Root(anchor, connectionID, folder)` 查找已有标签页,会话还活着就切过去。否则打开“远程开发”窗口并记下 `remoteReopen`(连接 ID 或 WSL 发行版与用户、主机文件夹);窗口读完已保存的连接后,`connectRemoteReopen` 选中同一个连接,像用户点“连接”一样连接,并直接打开这个文件夹。连接步骤、OpenSSH 的提问、从未决定过的文件夹的信任询问都和手动打开一样出现在窗口里。
- 连接已经在工作区打开之后被删除时,窗口说明原因(`desktop.remote.worktree.connectionRemoved`),记为拒绝(`connection_removed`),不连接。
- 新建对话框的“创建后打开”沿用同一条路径。

### 对话框里的主机路径都用“占位目录旁”的路径发给 Core

- `remoteplaceholder.HostNamedPath(root, host)` 把任何主机绝对路径映射为占位根旁 `.lithe-remote-host` 下的本机路径,Core 收到后按工作区绑定映射回主机。对话框发给 Core 的仓库路径、目标路径和上一次建议都用它(`worktreeHostNamed`),所以 Core 按“仓库路径”推出的建议也在 `.lithe-remote-host` 下,显示前再换回主机路径(`worktreeShown`)。Core 的建议规则和路径映射规则都不用改。
- 这样 Windows 和 WSL 上的主机路径也是本机绝对路径,不会被 Core 当作相对路径拒绝。
- 对话框在输入框下说明路径是主机上的文件夹(`desktop.remote.worktree.destination`)。

### 日志

只记 `git.worktree.open`、`remote.connect` 等动作和 `git_worktree_open` 的拒绝原因,不记主机、连接名、主机路径或用户输入的路径。

## 考虑过的备选方案

- **在当前会话里切换主机根目录**:一个窗口里同时会有两个文件夹的请求,绑定的 RemoteRoot 要随请求变化,监听、索引和信任都按根目录缓存,改动面大且容易串。没有采用;每个文件夹一个会话,和手动打开完全相同。
- **不经过连接窗口,后台直接连接**:OpenSSH 的提问、连接步骤和信任询问都在连接窗口里,后台连接要么另做一套界面,要么吞掉提问。没有采用。
- **让 Core 接收原始主机路径**:要给 `git.worktreePreview`、`git.createWorktree` 加一个“这是主机路径”的字段,且 Core 现有的路径映射只认本机路径。`HostNamedPath` 复用已有映射,不改协议。
- **在新窗口打开远程工作树**:新客户端只接受本机文件夹(S10),要支持就得把连接和会话交给另一个进程。保持拒绝。

## 后果

- 收益:远程工作树和本机工作树的操作基本一致;不改 Core 协议;Windows 和 WSL 的路径问题一并解决。
- 代价:每打开一个工作树多一个 SSH 会话(同一连接复用 OpenSSH 的设置,但不复用会话);打开时会出现连接窗口。
- 未完成:远程 Git 的 AskPass 转交(推送、拉取时的凭据询问);“在新窗口打开”仍拒绝。

## 验证

- `desktop/internal/workbench/remote_worktree_test.go`:工作树在新标签页经自己的会话打开,再次打开时切到已有标签页;不是主机路径的条目和已删除的连接都被拒绝并说明;新建对话框建议并接收主机路径,创建请求在工作区会话上发送 `HostNamedPath` 路径并选中新工作树;日志不含主机和路径。
- `desktop/internal/remoteplaceholder/remoteplaceholder_test.go` 的 `TestHostNamedPathNamesEveryHostPathBelowTheSibling`。
- `desktop/internal/workbench/remote_worktree_visual_test.go` 生成 `docs/assets/remote-worktrees/` 下的截图。

```bash
cd desktop && ../scripts/desktop/desktop-go.sh test ./internal/workbench ./internal/remoteplaceholder -run 'TestRemoteWorktree|TestHostNamed' -count=1
```

## 适用范围

`desktop/internal/workbench/remote_worktree.go`、`gitworktree.go` 中远程分支、`remote.go` 的 `reopen`、`desktop/internal/remoteplaceholder/`。以后其他“打开主机上另一个文件夹”的入口(例如 Dev Container 里的文件夹)都走 `openRemoteHostFolder`,不要另起连接流程。
4 changes: 4 additions & 0 deletions desktop/internal/i18n/shared.generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 19 additions & 2 deletions desktop/internal/remoteplaceholder/remoteplaceholder.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,27 @@ func LocalPath(root, remoteRoot, host string) (string, bool) {
if host == remote {
return root, true
}
base, suffix := root+HostSuffix, strings.TrimPrefix(host, "/")
if rest, ok := strings.CutPrefix(host, strings.TrimSuffix(remote, "/")+"/"); ok {
base, suffix = root, rest
return joinLocal(root, rest)
}
return HostNamedPath(root, host)
}

// HostNamedPath is the path below root's sibling (HostSuffix) that names host, an absolute host
// path, whether or not host lies in the workspace's folder. Core maps it to host as it maps every
// path of the sibling, and so maps the paths Core derives beside it, such as the folder it suggests
// for a new worktree next to the repository, to the host paths beside host. ok is false as for
// LocalPath.
func HostNamedPath(root, host string) (string, bool) {
if !path.IsAbs(host) {
return "", false
}
return joinLocal(root+HostSuffix, strings.TrimPrefix(path.Clean(host), "/"))
}

// joinLocal is base with the slash-separated suffix below it as a local path; ok is false on
// Windows for a name of suffix holding "\", which would name another path there.
func joinLocal(base, suffix string) (string, bool) {
if suffix == "" {
return base, true
}
Expand Down
28 changes: 28 additions & 0 deletions desktop/internal/remoteplaceholder/remoteplaceholder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package remoteplaceholder
import (
"errors"
"os"
"path"
"path/filepath"
"runtime"
"strings"
Expand Down Expand Up @@ -146,3 +147,30 @@ func TestHostAndLocalPathsMapBothWays(t *testing.T) {
}
}
}

// HostNamedPath names every absolute host path below the root's sibling, the workspace's own
// folder too, so that a path beside it is a host path again, and HostPath undoes it.
func TestHostNamedPathNamesEveryHostPathBelowTheSibling(t *testing.T) {
anchor := filepath.Join(string(filepath.Separator)+"cache", AnchorName)
root := Root(anchor, "c1", "/srv/repo")
sibling := root + HostSuffix
for _, tc := range []struct{ host, local string }{
{"/srv/repo", filepath.Join(sibling, "srv", "repo")},
{"/srv/repo/sub/", filepath.Join(sibling, "srv", "repo", "sub")},
{"/srv/repo-feature", filepath.Join(sibling, "srv", "repo-feature")},
{"/", sibling},
} {
local, ok := HostNamedPath(root, tc.host)
if !ok || local != tc.local {
t.Errorf("HostNamedPath(%q) = %q, %v; want %q", tc.host, local, ok, tc.local)
}
if host, ok := HostPath(root, "/srv/repo", local); !ok || host != path.Clean(tc.host) {
t.Errorf("HostPath(HostNamedPath(%q)) = %q, %v", tc.host, host, ok)
}
}
for _, host := range []string{"", "relative/path"} {
if local, ok := HostNamedPath(root, host); ok {
t.Errorf("HostNamedPath(%q) = %q; want no local path", host, local)
}
}
}
56 changes: 39 additions & 17 deletions desktop/internal/workbench/gitworktree.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package workbench
import (
"context"
"errors"
"path"
"path/filepath"
"strings"

"github.com/1lck/Lithe-IDEA/desktop/internal/core"
"github.com/1lck/Lithe-IDEA/desktop/internal/i18n"
"github.com/1lck/Lithe-IDEA/desktop/internal/remoteplaceholder"
"github.com/vivaui/viva/app"
"github.com/vivaui/viva/ui"
)
Expand Down Expand Up @@ -820,25 +822,22 @@ func (a *App) showWorktrees() {
}
}

// openWorktree opens the checkout at path as the window's project.
// openWorktree opens the checkout at path as the window's project. A remote workspace's checkout
// is a host folder, which opens over a session of its own in a tab of this window
// (openRemoteWorktree).
func (a *App) openWorktree(path string) {
// A remote workspace's checkout opens over a session of its own, which this window does not
// start yet; its placeholder would open as an empty local folder.
if a.refuseRemote(a.project, "git_worktree_open") {
a.performed("git.worktree.open", surfaceButton)
if p := a.project; p != nil && p.remote != nil {
a.openRemoteWorktree(p, path)
return
}
a.performed("git.worktree.open", surfaceButton)
// The worktree menu offers This Window and New Window itself.
a.openProjectThisWindow(path)
}

// beginCreateWorktree opens the dialog that creates a worktree and reads the references it
// can start from.
// can start from. In a remote workspace the dialog chooses a folder on the host.
func (a *App) beginCreateWorktree(p *project) {
// The dialog chooses the new checkout's folder on this computer.
if a.refuseRemote(p, "git_worktree_create") {
return
}
a.performed("git.worktree.create", surfaceButton)
w := &p.worktrees
cr := &w.create
Expand Down Expand Up @@ -877,16 +876,19 @@ func (a *App) syncWorktreeCreation(p *project) {

// previewWorktreeCreation has Core derive the dialog's values from its inputs and applies
// them; require makes an empty branch name an error, as a submit does. It reports whether Core
// answered; without an answer the dialog cannot create the worktree.
// answered; without an answer the dialog cannot create the worktree. Core derives them from the
// paths alone, so a remote workspace sends its paths as host-named ones (worktreeHostNamed) and
// shows the answer's as host paths.
func (a *App) previewWorktreeCreation(p *project, require bool) bool {
w := &p.worktrees
cr := &w.create
ctx, cancel := context.WithTimeout(a.operationContext(context.Background()), kernelCallTimeout)
defer cancel()
destination := p.worktreeHostNamed(cr.destination.Text())
result, err := a.kernel.GitWorktreeCreationPreview(ctx, core.GitWorktreeCreationInput{
Root: p.git.repositoryRoot(p.root), Mode: cr.mode, Kind: cr.kind, Reference: cr.reference,
Root: p.worktreeHostNamed(p.remoteHostPath(p.git.repositoryRoot(p.root))), Mode: cr.mode, Kind: cr.kind, Reference: cr.reference,
Name: cr.name.Text(), RequireName: require, Revision: cr.revision.Text(),
Destination: cr.destination.Text(), LastSuggestion: cr.suggested,
Destination: destination, LastSuggestion: p.worktreeHostNamed(cr.suggested),
References: cr.refs.References, CheckedOutBranches: checkedOutBranches(w.list),
})
if err != nil {
Expand All @@ -905,10 +907,11 @@ func (a *App) previewWorktreeCreation(p *project, require bool) bool {
if b := result.Branch; b != nil && b.TypingCleanedName != cr.name.Text() {
cr.name.SetText(b.TypingCleanedName)
}
if result.Destination != cr.destination.Text() {
cr.destination.SetText(result.Destination)
// A destination the user typed comes back as it was sent and stays as typed.
if result.Destination != destination {
cr.destination.SetText(p.worktreeShown(result.Destination))
}
cr.suggested = result.SuggestedDestination
cr.suggested = p.worktreeShown(result.SuggestedDestination)
cr.key = cr.inputs(w.gen)
return true
}
Expand All @@ -920,12 +923,20 @@ func (a *App) submitCreateWorktree(p *project) {
if p.worktrees.busy || !cr.result.Ready || !a.previewWorktreeCreation(p, true) || !cr.result.Ready {
return
}
typed := strings.TrimSpace(cr.destination.Text())
creation := core.GitWorktreeCreation{
Mode: cr.mode,
Destination: filepath.Clean(strings.TrimSpace(cr.destination.Text())),
Destination: filepath.Clean(typed),
NoCheckout: cr.noCheckout,
}
name := filepath.Base(creation.Destination)
// host is the new checkout's host folder in a remote workspace, which Core is sent as the
// path that names it.
var host string
if p.remote != nil {
host = path.Clean(typed)
creation.Destination, name = p.worktreeHostNamed(host), path.Base(host)
}
if b := cr.result.Branch; cr.mode == core.GitWorktreeNewBranch && b != nil {
creation.Name, name = b.CleanedName, b.CleanedName
}
Expand All @@ -944,6 +955,17 @@ func (a *App) submitCreateWorktree(p *project) {
return kernel.GitCreateWorktree(ctx, root, creation)
}, nil, func(a *App) {
a.notifySuccess(a.text.Worktrees.Created(name))
if p.remote != nil {
// The list names a host checkout by the path Core maps it to: below the root for one
// in the workspace's folder, below the root's sibling otherwise.
if local, ok := remoteplaceholder.LocalPath(p.root, p.remote.binding.RemoteRoot(), host); ok {
p.worktrees.selected = local
}
if openAfter {
a.openRemoteHostFolder(p, host)
}
return
}
// Git lists the checkout by its canonical path, which differs from the typed one under
// a symbolic link.
selected := creation.Destination
Expand Down
Loading
Loading