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
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ and listed with their assets on the

### Added

- **Open a stack's folder from the dashboard**
([ADR-0046](docs/adr/0046-the-hub-opens-a-worktree-in-the-users-programs.md)):
the path in the stack header is a menu. It copies the path, or opens the
shown tree (the main working tree or a lane's worktree) in Explorer or the
file manager, Windows Terminal, PowerShell or a terminal, VS Code, Cursor,
Windsurf, Zed, Claude Desktop, Claude Code or Orca: whichever of them the
hub finds on this machine (an editor or agent by the URL scheme it
registered). The hub opens only a registered stack's own trees, with a
program from that list (`GET /hub/openers`, `POST /hub/open`). A lane
shared with you offers the copy only.
- **winget**: `winget install nuetzliches.devstack` (once winget's catalogue
has the release). Each release submits its manifests
(`packaging/winget/`) to `microsoft/winget-pkgs`; the release workflow
Expand Down
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,13 @@ windows-sys = { version = "0.61", features = [
"Win32_System_ProcessStatus",
"Win32_System_Registry",
"Win32_System_SystemInformation",
"Win32_System_Com",
"Win32_System_Console",
"Win32_System_Diagnostics_ToolHelp",
"Win32_System_JobObjects",
"Win32_System_Threading",
"Win32_UI_Input_KeyboardAndMouse",
"Win32_UI_Shell",
"Win32_UI_WindowsAndMessaging",
] }

Expand Down
40 changes: 40 additions & 0 deletions crates/devstack-daemon/src/lanes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,20 @@ fn worktrees(repo: &Path) -> Option<(Vec<Worktree>, Vec<PathBuf>)> {
Some((here, missing.into_iter().map(|w| w.path).collect()))
}

/// The worktree of `repo` (the main working tree included) that `path`
/// names, as git lists it; `repo` itself also without git. `None` when
/// `path` is none of them. What the hub opens (ADR-0046) is checked against
/// it.
pub fn worktree_of(repo: &Path, path: &Path) -> Option<PathBuf> {
if same_tree(repo, path) {
return Some(repo.to_path_buf());
}
let (here, _) = worktrees(repo)?;
here.into_iter()
.map(|w| w.path)
.find(|w| same_tree(w, path))
}

/// What changes in `<git common dir>/worktrees` when a worktree is added or
/// removed or checks out another branch: its entries and their `HEAD`s.
fn fingerprint(dir: &Path) -> Vec<(std::ffi::OsString, Option<std::time::SystemTime>)> {
Expand Down Expand Up @@ -2408,6 +2422,32 @@ impl Lanes {
mod tests {
use super::*;

#[test]
fn only_the_repository_and_its_worktrees_are_its_trees() {
let d = tempfile::tempdir().unwrap();
let repo = d.path().join("repo");
std::fs::create_dir_all(&repo).unwrap();
let g = |args: &[&str]| git(&repo, args).unwrap();
g(&["init", "-q"]);
g(&[
"-c",
"user.name=t",
"-c",
"user.email=t@t",
"commit",
"-q",
"--allow-empty",
"-m",
"x",
]);
let wt = d.path().join("wt");
g(&["worktree", "add", "-q", &wt.to_string_lossy()]);
assert!(worktree_of(&repo, &repo).is_some());
assert!(worktree_of(&repo, &wt).is_some_and(|p| same_tree(&p, &wt)));
assert!(worktree_of(&repo, &repo.join(".git")).is_none());
assert!(worktree_of(&repo, d.path()).is_none());
}

#[test]
fn the_state_of_a_forgotten_lane_is_swept() {
let d = tempfile::tempdir().unwrap();
Expand Down
68 changes: 68 additions & 0 deletions crates/devstack-hub/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ static UI: Dir<'_> = include_dir!("$OUT_DIR/ui");
mod dialog;
mod guests;
mod notify;
mod open;
mod pool;
pub mod release;
mod share;
Expand Down Expand Up @@ -452,6 +453,8 @@ fn router(state: AppState) -> axum::Router {
.route("/hub/notify/test", post(notify::test))
.route("/hub/inspect", get(inspect))
.route("/hub/pick-folder", post(pick_folder))
.route("/hub/openers", get(openers))
.route("/hub/open", post(open_tree))
.route("/hub/stacks/add", post(stack_add))
.route("/hub/stacks/init", post(stack_init))
.route("/hub/stacks/scan", post(stack_scan))
Expand Down Expand Up @@ -1455,6 +1458,71 @@ async fn pick_folder() -> Response {
}
}

/// `GET /hub/openers`: what this machine can open a stack's tree with
/// (ADR-0046), `[{id, label, kind}]`; empty without a desktop.
async fn openers() -> Response {
match tokio::task::spawn_blocking(open::found).await {
Ok(found) => Json(found.into_iter().map(|f| f.opener).collect::<Vec<_>>()).into_response(),
Err(e) => err(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()),
}
}

#[derive(Deserialize)]
struct OpenQuery {
stack: String,
path: String,
with: String,
}

/// `POST /hub/open {stack, path, with}`: open one of the stack's trees (its
/// repository or a worktree of it) with a program `GET /hub/openers` lists.
/// Anything else is refused: the page names a tree and a program, never a
/// command (ADR-0046).
async fn open_tree(State(s): State<AppState>, Json(q): Json<OpenQuery>) -> Response {
let Some(e) = entry(&s, &q.stack) else {
return err(StatusCode::NOT_FOUND, "no such stack");
};
let opened = tokio::task::spawn_blocking(move || {
let raw = PathBuf::from(&q.path);
if !raw.is_absolute() || !local_path(&q.path, &raw) {
return Err((
StatusCode::BAD_REQUEST,
"a folder on this machine".to_string(),
));
}
let Some(tree) = devstack_daemon::lanes::worktree_of(&e.repo, &raw) else {
return Err((
StatusCode::BAD_REQUEST,
"not a worktree of this stack".to_string(),
));
};
let file = e
.file
.strip_prefix(&e.repo)
.map(|rel| tree.join(rel))
.unwrap_or_else(|_| e.file.clone());
open::open(&q.with, &tree, &e.repo, &file).map_err(|e| match e {
open::OpenError::Unknown => (
StatusCode::BAD_REQUEST,
format!(
"{} is not a program this machine opens folders with",
q.with
),
),
open::OpenError::Failed(why) => (StatusCode::BAD_GATEWAY, why),
})
})
.await;
match opened {
Ok(Ok(())) => StatusCode::NO_CONTENT.into_response(),
Ok(Err((status, msg))) => {
diag::warn("open_failed", serde_json::json!({ "error": msg }));
err(status, msg)
}
Err(e) => err(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()),
}
}

/// `GET /hub/inspect?path=`: is it a folder, does it have a stack file, is
/// it registered already.
async fn inspect(Query(q): Query<PathQuery>) -> Response {
Expand Down
Loading