diff --git a/apps/lutra-desktop/src/main.rs b/apps/lutra-desktop/src/main.rs index a5f6e3c..324917c 100644 --- a/apps/lutra-desktop/src/main.rs +++ b/apps/lutra-desktop/src/main.rs @@ -95,7 +95,8 @@ fn run(args: &args::Args) -> Result<(), String> { engine_execute_waiting, engine_list_commands, engine_ready, - pick_file + pick_file, + pick_dir ]) .setup(move |app| { let setup_host = setup_slot.get().ok_or("the host is not ready")?; @@ -277,6 +278,22 @@ fn pick_file(app: tauri::AppHandle, title: String) -> Option { Some(path.to_string_lossy().into_owned()) } +/// IPC: shows the native folder picker for "Clone Repository…"'s parent +/// folder and returns the chosen folder, or `None` when the user cancels or +/// picks something that is not a local path. `async` keeps the blocking +/// picker off the main thread. +#[tauri::command(async)] +fn pick_dir(app: tauri::AppHandle, title: String) -> Option { + use tauri_plugin_dialog::DialogExt; + let picked = app + .dialog() + .file() + .set_title(title) + .blocking_pick_folder()?; + let path = picked.into_path().ok()?; + Some(path.to_string_lossy().into_owned()) +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/control/tests/events.rs b/crates/control/tests/events.rs index 77c9f0d..811445d 100644 --- a/crates/control/tests/events.rs +++ b/crates/control/tests/events.rs @@ -207,7 +207,8 @@ fn settings_changes_stream_and_persist() { "editor.completion.autoPopup": true, "editor.completion.delayMs": 0, "editor.hover.delayMs": 350, - "git.executable": "" + "git.executable": "", + "git.pull.mode": "merge" }), "{r}" ); diff --git a/crates/engine/src/cmd/git_remote.rs b/crates/engine/src/cmd/git_remote.rs new file mode 100644 index 0000000..827db5a --- /dev/null +++ b/crates/engine/src/cmd/git_remote.rs @@ -0,0 +1,701 @@ +//! Remote Git commands (M4.10, owner decision D23): fetch, pull, push, +//! clone, cancel, and the list of remotes with add, set-url and remove, +//! through the user's git (`lutra-vcs`). +//! +//! **Credentials stay with git.** HTTPS sign-in is git's credential +//! helper's (it may show its own window, such as Git Credential Manager's); +//! SSH uses the user's ssh with its agent and keys. Lutra never sees, +//! stores or logs a credential, terminal prompts are off, and URLs in +//! values, errors and progress have their userinfo redacted. +//! +//! **Off the lock.** Every command but `git.cancel` returns a pending +//! outcome: under the lock it parses its params and takes the setting; a +//! `lutra-git-remote` thread detects git, opens the project's repository +//! (`git.clone` needs none), runs the call and fills a reply slot. The +//! finish, under the lock again, emits `gitChanged` after a change. +//! +//! **One network call at a time.** fetch, pull, push and clone share one +//! slot; a second is refused while one runs. Its progress (git's +//! `--progress` lines) is kept by the thread and sent as `gitProgress` +//! from [`pump`] when it changed, so at most once per pump. `git.cancel` +//! sets its cancel flag: git and every process it started are killed and +//! the call fails with "cancelled". When the call ends, `gitProgress` +//! with `active: false` follows. + +use std::path::PathBuf; +use std::sync::mpsc; +use std::sync::{Arc, Mutex, PoisonError}; +use std::time::Duration; + +use lutra_vcs::{Cancel, Git, Network, ProgressSink, PullMode, Remote, Repo, VcsError}; +use serde::{Deserialize, Serialize}; +use serde_json::{Value, json}; + +use super::git::{EXECUTABLE_SETTING, GitChanged, RepoSummary}; +use super::params::parse; +use crate::workspace::path_string; +use crate::{Command, CommandSpec, EngineError, Event, Outcome, Pending, Registry, State}; + +/// The setting choosing how `git.pull` joins the fetched commits. +pub const PULL_MODE_SETTING: &str = "git.pull.mode"; + +/// Longest wait for a call: detection and opening (30 s each) and the +/// network timeout, plus slack. The runner kills git first. +const WAIT: Duration = Duration::from_secs(30 * 60 + 120); + +/// Registers the remote `git.*` commands. +/// +/// # Errors +/// [`EngineError::Internal`] if an id is taken. +pub fn register(registry: &mut Registry) -> Result<(), EngineError> { + registry.register(Box::new(Fetch))?; + registry.register(Box::new(Pull))?; + registry.register(Box::new(Push))?; + registry.register(Box::new(CloneRepo))?; + registry.register(Box::new(CancelCommand))?; + registry.register(Box::new(Remotes))?; + registry.register(Box::new(RemoteAdd))?; + registry.register(Box::new(RemoteSetUrl))?; + registry.register(Box::new(RemoteRemove))?; + Ok(()) +} + +/// Payload of [`Event::GitProgress`]: the running network call and its +/// latest progress line. +#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct GitProgress { + /// A network call runs. + pub active: bool, + /// Which: `fetch`, `pull`, `push` or `clone`. + #[serde(skip_serializing_if = "Option::is_none")] + pub operation: Option, + /// The phase of the latest line, e.g. `Receiving objects`, or the + /// whole line when it has no percentage. + #[serde(skip_serializing_if = "Option::is_none")] + pub phase: Option, + /// The phase's percentage, when git printed one. + #[serde(skip_serializing_if = "Option::is_none")] + pub percent: Option, +} + +/// The running network call. +#[derive(Debug)] +struct Active { + /// Which command runs. + operation: &'static str, + /// Stops it. + cancel: Cancel, + /// Its latest progress line, written by the reading thread. + latest: Arc>>, +} + +/// State of the remote commands. +#[derive(Debug, Default)] +pub struct GitRemote { + /// The running network call. + active: Option, + /// The `gitProgress` sent last, so an unchanged one is not sent again. + sent: Option, +} + +/// What one command does on its thread. +enum Op { + /// `git.fetch`. + Fetch(Option), + /// `git.pull`. + Pull(PullMode), + /// `git.push`. + Push(Option), + /// `git.clone`. + Clone { + /// The URL or path. + url: String, + /// The new work tree. + dir: PathBuf, + }, + /// `git.remotes`. + Remotes, + /// `git.remoteAdd`. + Add(String, String), + /// `git.remoteSetUrl`. + SetUrl(String, String), + /// `git.remoteRemove`. + Remove(String), +} + +impl Op { + /// The network operation's name; `None` for the local ones. + fn network(&self) -> Option<&'static str> { + match self { + Self::Fetch(_) => Some("fetch"), + Self::Pull(_) => Some("pull"), + Self::Push(_) => Some("push"), + Self::Clone { .. } => Some("clone"), + _ => None, + } + } +} + +/// What a thread hands back to the finish. +struct Reply { + /// The command's value or error. + value: Result, + /// After a successful change in the open project: its summary for + /// `gitChanged`. + changed: Option, +} + +/// Turns a backend error into the engine's: the caller's mistakes are +/// `invalid_params`, everything else `failed`. +fn engine_error(command: &str, error: VcsError) -> EngineError { + if error.is_invalid_input() { + EngineError::invalid_params(command, error.to_string()) + } else { + EngineError::Failed(error.to_string()) + } +} + +/// Runs `op` on a new thread and returns the pending outcome. +/// +/// # Errors +/// [`EngineError::Disabled`] without a project (except for clone), +/// [`EngineError::Failed`] while another network call runs. +fn start(state: &mut State, command: &'static str, op: Op) -> Result { + let project = state + .project + .as_ref() + .map(|w| w.project.root().to_path_buf()); + if project.is_none() && !matches!(op, Op::Clone { .. }) { + return Err(EngineError::Disabled(command.to_string())); + } + let setting = state + .settings + .get(EXECUTABLE_SETTING) + .and_then(|v| v.as_str().map(str::to_string)) + .unwrap_or_default(); + let mut net = Network::default(); + let mut slot_taken = false; + if let Some(operation) = op.network() { + if let Some(active) = &state.git_remote.active { + return Err(EngineError::Failed(format!( + "another Git operation is running ({}); wait for it or cancel it", + active.operation + ))); + } + let cancel = Cancel::new(); + let latest: Arc>> = Arc::new(Mutex::new(None)); + let writer = Arc::clone(&latest); + net = Network { + progress: Some(ProgressSink(Arc::new(move |line: &str| { + *writer.lock().unwrap_or_else(PoisonError::into_inner) = Some(line.to_string()); + }))), + cancel: Some(cancel.clone()), + }; + state.git_remote.active = Some(Active { + operation, + cancel, + latest, + }); + state.git_remote.sent = None; + slot_taken = true; + } + let slot: Arc>> = Arc::new(Mutex::new(None)); + let (tx, rx) = mpsc::channel(); + let filled = Arc::clone(&slot); + let thread_project = project.clone(); + let spawned = std::thread::Builder::new() + .name("lutra-git-remote".into()) + .spawn(move || { + let reply = work(command, &setting, thread_project, &op, &net); + *filled.lock().unwrap_or_else(PoisonError::into_inner) = Some(reply); + let _ = tx.send(Ok(Value::Null)); + }); + if let Err(e) = spawned { + if slot_taken { + state.git_remote.active = None; + } + return Err(EngineError::Failed(format!("cannot start git: {e}"))); + } + Ok(Outcome::pending(Pending::new(rx, WAIT, move |state, _| { + if slot_taken { + finish_network(state); + } + let reply = slot + .lock() + .unwrap_or_else(PoisonError::into_inner) + .take() + .ok_or_else(|| EngineError::Failed("git did not answer in time".into()))?; + let current = state + .project + .as_ref() + .map(|w| w.project.root().to_path_buf()); + if let Some(summary) = reply.changed + && current.is_some() + && current == project + { + state.emit(Event::GitChanged(GitChanged { + available: true, + repo: Some(summary), + files_changed: true, + })); + } + reply.value + }))) +} + +/// Frees the network slot and sends `gitProgress {active: false}`. +fn finish_network(state: &mut State) { + let Some(active) = state.git_remote.active.take() else { + return; + }; + let done = GitProgress { + active: false, + operation: Some(active.operation.to_string()), + phase: None, + percent: None, + }; + state.git_remote.sent = None; + state.emit(Event::GitProgress(done)); +} + +/// The thread's work: detect git, open the repository unless cloning, run +/// `op`. +fn work( + command: &'static str, + setting: &str, + project: Option, + op: &Op, + net: &Network, +) -> Reply { + let fail = |value: EngineError| Reply { + value: Err(value), + changed: None, + }; + let git = match Git::detect(setting) { + Ok(git) => git, + Err(e) => return fail(EngineError::Failed(e.to_string())), + }; + if let Op::Clone { url, dir } = op { + return Reply { + value: git + .clone_repo(url, dir, net) + .map(|()| json!({"root": path_string(dir)})) + .map_err(|e| engine_error(command, e)), + changed: None, + }; + } + let Some(project) = project else { + return fail(EngineError::Disabled(command.to_string())); + }; + let repo = match git.open(&project) { + Ok(Some(repo)) => repo, + Ok(None) => { + return fail(EngineError::Failed( + "the project is not inside a Git repository".into(), + )); + } + Err(e) => return fail(EngineError::Failed(e.to_string())), + }; + let value = run_op(&repo, op, net).map_err(|e| engine_error(command, e)); + let changed = (value.is_ok() && !matches!(op, Op::Remotes)).then(|| summary(&repo)); + Reply { value, changed } +} + +/// The repository summary for `gitChanged`; a failed query gives an empty +/// head (the event only asks clients to re-read). +fn summary(repo: &Repo) -> RepoSummary { + let head = repo.head().unwrap_or_default(); + RepoSummary { + root: path_string(repo.root()), + branch: head.branch, + detached: head.detached, + head: head.head, + upstream: head.upstream, + ahead: head.ahead, + behind: head.behind, + } +} + +/// One remote of `git.remotes`. +fn remote_json(remote: &Remote) -> Value { + let mut value = json!({"name": remote.name}); + if let Some(map) = value.as_object_mut() { + if let Some(url) = &remote.fetch_url { + map.insert("fetchUrl".into(), json!(url)); + } + if let Some(url) = &remote.push_url { + map.insert("pushUrl".into(), json!(url)); + } + } + value +} + +/// Runs one operation against the repository. +fn run_op(repo: &Repo, op: &Op, net: &Network) -> Result { + match op { + Op::Fetch(remote) => repo.fetch(remote.as_deref(), net).map(|()| json!({})), + Op::Pull(mode) => repo.pull(*mode, net).map(|()| json!({})), + Op::Push(remote) => { + let pushed = repo.push(remote.as_deref(), net)?; + Ok(json!({ + "remote": pushed.remote, + "branch": pushed.branch, + "upstreamSet": pushed.upstream_set, + })) + } + Op::Remotes => { + let remotes: Vec = repo.remotes()?.iter().map(remote_json).collect(); + Ok(json!({"remotes": remotes})) + } + Op::Add(name, url) => repo.remote_add(name, url).map(|()| json!({})), + Op::SetUrl(name, url) => repo.remote_set_url(name, url).map(|()| json!({})), + Op::Remove(name) => repo.remote_remove(name).map(|()| json!({})), + Op::Clone { .. } => Err(VcsError::Git("clone needs no repository".into())), + } +} + +/// Sends `gitProgress` when the running call's latest line changed. +/// Called from [`crate::Engine::pump_vfs`]. +pub fn pump(state: &mut State) { + let Some(active) = &state.git_remote.active else { + return; + }; + let line = active + .latest + .lock() + .unwrap_or_else(PoisonError::into_inner) + .clone(); + let (phase, percent) = match line { + Some(line) => { + let (phase, percent) = lutra_vcs::parse_progress(&line); + (Some(phase), percent) + } + None => (None, None), + }; + let now = GitProgress { + active: true, + operation: Some(active.operation.to_string()), + phase, + percent, + }; + if state.git_remote.sent.as_ref() != Some(&now) { + state.git_remote.sent = Some(now.clone()); + state.emit(Event::GitProgress(now)); + } +} + +/// Params of `git.fetch` and `git.push`. +#[derive(Debug, Deserialize)] +#[serde(deny_unknown_fields)] +struct RemoteParams { + /// A remote's name; absent means all remotes (fetch) or the upstream's + /// (push). + #[serde(default)] + remote: Option, +} + +/// `git.fetch`. +struct Fetch; + +impl Command for Fetch { + fn spec(&self) -> &CommandSpec { + &CommandSpec { + id: "git.fetch", + label: "Git: Fetch", + menu_path: None, + default_shortcut: None, + params: "{ remote?: remote name (all remotes when absent) }; returns {}; progress as gitProgress events, git.cancel stops it; sign-in through git's credential helper or ssh agent only", + } + } + + fn enabled(&self, state: &State) -> bool { + state.has_project() + } + + fn run(&self, state: &mut State, params: Value) -> Result { + let command = self.spec().id; + let p: RemoteParams = parse(command, params)?; + start(state, command, Op::Fetch(p.remote)) + } +} + +/// How `git.pull` joins. +#[derive(Debug, Clone, Copy, Deserialize)] +#[serde(rename_all = "lowercase")] +enum ModeParam { + /// A merge. + Merge, + /// A rebase. + Rebase, +} + +/// Params of `git.pull`. +#[derive(Debug, Deserialize)] +#[serde(deny_unknown_fields)] +struct PullParams { + /// Overrides the `git.pull.mode` setting. + #[serde(default)] + mode: Option, +} + +/// `git.pull`. +struct Pull; + +impl Command for Pull { + fn spec(&self) -> &CommandSpec { + &CommandSpec { + id: "git.pull", + label: "Git: Pull", + menu_path: None, + default_shortcut: None, + params: "{ mode?: \"merge\" | \"rebase\" (the git.pull.mode setting when absent) }; pulls the current branch's upstream; returns {}; refusals (no upstream, local changes, conflicts) explain themselves; progress as gitProgress events. Save dirty buffers first.", + } + } + + fn enabled(&self, state: &State) -> bool { + state.has_project() + } + + fn run(&self, state: &mut State, params: Value) -> Result { + let command = self.spec().id; + let p: PullParams = parse(command, params)?; + let rebase = match p.mode { + Some(ModeParam::Rebase) => true, + Some(ModeParam::Merge) => false, + None => state + .settings + .get(PULL_MODE_SETTING) + .is_some_and(|v| v.as_str() == Some("rebase")), + }; + let mode = if rebase { + PullMode::Rebase + } else { + PullMode::Merge + }; + start(state, command, Op::Pull(mode)) + } +} + +/// `git.push`. +struct Push; + +impl Command for Push { + fn spec(&self) -> &CommandSpec { + &CommandSpec { + id: "git.push", + label: "Git: Push", + menu_path: None, + default_shortcut: None, + params: "{ remote?: remote name for a branch without upstream (the only remote, else origin) }; pushes the current branch to its upstream, or sets one with the first push; returns { remote, branch, upstreamSet }; a rejected push explains itself; progress as gitProgress events", + } + } + + fn enabled(&self, state: &State) -> bool { + state.has_project() + } + + fn run(&self, state: &mut State, params: Value) -> Result { + let command = self.spec().id; + let p: RemoteParams = parse(command, params)?; + start(state, command, Op::Push(p.remote)) + } +} + +/// Params of `git.clone`. +#[derive(Debug, Deserialize)] +#[serde(deny_unknown_fields)] +struct CloneParams { + /// The URL (HTTPS, SSH, scp-like) or path. + url: String, + /// The new work tree: absolute, missing or empty. + dir: String, +} + +/// `git.clone`. +struct CloneRepo; + +impl Command for CloneRepo { + fn spec(&self) -> &CommandSpec { + &CommandSpec { + id: "git.clone", + label: "Git: Clone Repository", + menu_path: None, + default_shortcut: None, + params: "{ url: HTTPS, SSH or scp-like URL or a path, dir: absolute folder that is missing or empty (its parent must exist) }; returns { root }; works without a project; open it with project.open; progress as gitProgress events, git.cancel stops it and removes what it left", + } + } + + fn enabled(&self, _state: &State) -> bool { + true + } + + fn run(&self, state: &mut State, params: Value) -> Result { + let command = self.spec().id; + let p: CloneParams = parse(command, params)?; + lutra_vcs::check_url(&p.url).map_err(|e| engine_error(command, e))?; + let dir = PathBuf::from(p.dir.trim()); + if !dir.is_absolute() { + return Err(EngineError::invalid_params( + command, + "dir must be an absolute path", + )); + } + start(state, command, Op::Clone { url: p.url, dir }) + } +} + +/// `git.cancel`. +struct CancelCommand; + +impl Command for CancelCommand { + fn spec(&self) -> &CommandSpec { + &CommandSpec { + id: "git.cancel", + label: "Git: Cancel Fetch, Pull, Push or Clone", + menu_path: None, + default_shortcut: None, + params: "none; stops the running fetch, pull, push or clone (it then fails with \"cancelled\"); returns { cancelled: true }; disabled while none runs", + } + } + + fn enabled(&self, state: &State) -> bool { + state.git_remote.active.is_some() + } + + fn run(&self, state: &mut State, params: Value) -> Result { + let _: serde_json::Map = parse(self.spec().id, params)?; + let cancelled = state.git_remote.active.as_ref().is_some_and(|active| { + active.cancel.cancel(); + true + }); + Ok(Outcome::value(json!({"cancelled": cancelled}))) + } +} + +/// `git.remotes`. +struct Remotes; + +impl Command for Remotes { + fn spec(&self) -> &CommandSpec { + &CommandSpec { + id: "git.remotes", + label: "Git: Remotes", + menu_path: None, + default_shortcut: None, + params: "none; returns { remotes: [{ name, fetchUrl?, pushUrl? }] } in config order; URLs with userinfo are redacted (https://***@host/…); pushUrl only when it differs", + } + } + + fn enabled(&self, state: &State) -> bool { + state.has_project() + } + + fn run(&self, state: &mut State, params: Value) -> Result { + let command = self.spec().id; + let _: serde_json::Map = parse(command, params)?; + start(state, command, Op::Remotes) + } +} + +/// Params of `git.remoteAdd` and `git.remoteSetUrl`. +#[derive(Debug, Deserialize)] +#[serde(deny_unknown_fields)] +struct NameUrlParams { + /// The remote's name. + name: String, + /// Its URL or path. + url: String, +} + +/// Params of `git.remoteRemove`. +#[derive(Debug, Deserialize)] +#[serde(deny_unknown_fields)] +struct NameParams { + /// The remote's name. + name: String, +} + +/// Checks a name and URL under the lock, so bad input fails at once. +fn check_name_url(command: &str, p: &NameUrlParams) -> Result<(), EngineError> { + lutra_vcs::check_remote_name(&p.name).map_err(|e| engine_error(command, e))?; + lutra_vcs::check_url(&p.url).map_err(|e| engine_error(command, e)) +} + +/// `git.remoteAdd`. +struct RemoteAdd; + +impl Command for RemoteAdd { + fn spec(&self) -> &CommandSpec { + &CommandSpec { + id: "git.remoteAdd", + label: "Git: Add Remote", + menu_path: None, + default_shortcut: None, + params: "{ name, url }; adds a remote without fetching; returns {}", + } + } + + fn enabled(&self, state: &State) -> bool { + state.has_project() + } + + fn run(&self, state: &mut State, params: Value) -> Result { + let command = self.spec().id; + let p: NameUrlParams = parse(command, params)?; + check_name_url(command, &p)?; + start(state, command, Op::Add(p.name, p.url)) + } +} + +/// `git.remoteSetUrl`. +struct RemoteSetUrl; + +impl Command for RemoteSetUrl { + fn spec(&self) -> &CommandSpec { + &CommandSpec { + id: "git.remoteSetUrl", + label: "Git: Change Remote URL", + menu_path: None, + default_shortcut: None, + params: "{ name, url }; changes the remote's URL; returns {}", + } + } + + fn enabled(&self, state: &State) -> bool { + state.has_project() + } + + fn run(&self, state: &mut State, params: Value) -> Result { + let command = self.spec().id; + let p: NameUrlParams = parse(command, params)?; + check_name_url(command, &p)?; + start(state, command, Op::SetUrl(p.name, p.url)) + } +} + +/// `git.remoteRemove`. +struct RemoteRemove; + +impl Command for RemoteRemove { + fn spec(&self) -> &CommandSpec { + &CommandSpec { + id: "git.remoteRemove", + label: "Git: Remove Remote", + menu_path: None, + default_shortcut: None, + params: "{ name }; removes the remote with its remote-tracking branches; returns {}", + } + } + + fn enabled(&self, state: &State) -> bool { + state.has_project() + } + + fn run(&self, state: &mut State, params: Value) -> Result { + let command = self.spec().id; + let p: NameParams = parse(command, params)?; + lutra_vcs::check_remote_name(&p.name).map_err(|e| engine_error(command, e))?; + start(state, command, Op::Remove(p.name)) + } +} diff --git a/crates/engine/src/cmd/mod.rs b/crates/engine/src/cmd/mod.rs index 97fa077..50f47af 100644 --- a/crates/engine/src/cmd/mod.rs +++ b/crates/engine/src/cmd/mod.rs @@ -6,6 +6,7 @@ mod file; pub mod git; pub mod git_lines; pub mod git_log; +pub mod git_remote; pub mod goto; mod lsp_assist; mod lsp_diagnostics; @@ -70,5 +71,6 @@ pub fn register_builtins(registry: &mut Registry) -> Result<(), EngineError> { git::register(registry)?; git_lines::register(registry)?; git_log::register(registry)?; + git_remote::register(registry)?; Ok(()) } diff --git a/crates/engine/src/engine.rs b/crates/engine/src/engine.rs index 960e5a1..54fe8b0 100644 --- a/crates/engine/src/engine.rs +++ b/crates/engine/src/engine.rs @@ -153,6 +153,7 @@ impl Engine { cmd::terminal::pump(state); crate::lsp::pump(state); cmd::git::pump(state); + cmd::git_remote::pump(state); Ok(()) })?; Ok(state.take_events()) @@ -481,12 +482,21 @@ mod tests { "file.save", "file.saveAll", "git.branches", + "git.cancel", "git.checkout", + "git.clone", "git.commit", "git.createBranch", + "git.fetch", "git.lastMessage", "git.lineChanges", "git.log", + "git.pull", + "git.push", + "git.remoteAdd", + "git.remoteRemove", + "git.remoteSetUrl", + "git.remotes", "git.show", "git.showAt", "git.showCommit", diff --git a/crates/engine/src/event.rs b/crates/engine/src/event.rs index eedac10..804be6c 100644 --- a/crates/engine/src/event.rs +++ b/crates/engine/src/event.rs @@ -207,6 +207,8 @@ pub enum Event { GitChanged(crate::cmd::git::GitChanged), /// The language server's newest work-done progress task: `{active, title?, message?, percentage?}`; `active: false` when none runs. LspProgress(crate::lsp::LspProgress), + /// The running fetch, pull, push or clone: `{active, operation?, phase?, percent?}`; `active: false` once it ended (M4.10). + GitProgress(crate::cmd::git_remote::GitProgress), /// Terminal events (`terminalOutput`, `terminalExited`), see [`crate::cmd::terminal`]. #[serde(untagged)] Terminal(crate::cmd::terminal::TerminalEvent), diff --git a/crates/engine/src/settings.rs b/crates/engine/src/settings.rs index 0b60f69..1d317bd 100644 --- a/crates/engine/src/settings.rs +++ b/crates/engine/src/settings.rs @@ -283,6 +283,14 @@ pub const DEFS: &[Def] = &[ description_key: "settings.git.executable.description", section: "git", }, + Def { + key: "git.pull.mode", + kind: Kind::Enum(&["merge", "rebase"]), + default: DefaultValue::Str("merge"), + label_key: "settings.git.pull.mode.label", + description_key: "settings.git.pull.mode.description", + section: "git", + }, ]; /// Looks a known setting up by key. diff --git a/crates/engine/src/state.rs b/crates/engine/src/state.rs index 7a02175..f79b2b8 100644 --- a/crates/engine/src/state.rs +++ b/crates/engine/src/state.rs @@ -51,6 +51,8 @@ pub struct State { pub git: crate::cmd::git::GitState, /// Cached index texts for `git.lineChanges` (M4.4). pub git_lines: crate::cmd::git_lines::GitLines, + /// The running fetch, pull, push or clone (M4.10). + pub git_remote: crate::cmd::git_remote::GitRemote, /// Number of notifications emitted, for their ids. notifications: u64, /// Events queued for the host, oldest first. diff --git a/crates/engine/tests/git_remote.rs b/crates/engine/tests/git_remote.rs new file mode 100644 index 0000000..afe7063 --- /dev/null +++ b/crates/engine/tests/git_remote.rs @@ -0,0 +1,301 @@ +//! The remote `git.*` commands through the engine (M4.10), against local +//! bare repositories only: clone without a project, remotes with redacted +//! URLs, push with upstream, fetch and pull, `gitProgress` and +//! `gitChanged`, cancel of a running push, one network call at a time, +//! and bad params. Skips with a message when git is not installed. + +#![allow( + clippy::unwrap_used, + clippy::expect_used, + clippy::panic, + clippy::indexing_slicing +)] + +use std::path::Path; +use std::process::Command; +use std::sync::{Arc, Mutex}; +use std::time::{Duration, Instant}; + +use lutra_engine::{Engine, EngineError, Event, run_waiting}; +use serde_json::{Value, json}; + +/// Whether git is installed; prints why a test is skipped. +fn have_git() -> bool { + let ok = Command::new("git") + .arg("--version") + .output() + .is_ok_and(|o| o.status.success()); + if !ok { + eprintln!("skipped: git is not installed"); + } + ok +} + +/// Runs git for test setup. +fn git(dir: &Path, args: &[&str]) -> String { + let out = Command::new("git") + .args(args) + .current_dir(dir) + .output() + .unwrap(); + assert!( + out.status.success(), + "git {args:?}: {}", + String::from_utf8_lossy(&out.stderr) + ); + String::from_utf8_lossy(&out.stdout).trim().to_string() +} + +/// Gives a work tree a local identity and no line-ending conversion. +fn configure(dir: &Path) { + for (key, value) in [ + ("user.name", "Lutra Test"), + ("user.email", "test@lutra.invalid"), + ("commit.gpgsign", "false"), + ("core.autocrlf", "false"), + ] { + git(dir, &["config", key, value]); + } +} + +/// Writes `name` and commits it. +fn commit(dir: &Path, name: &str, text: &str) { + std::fs::write(dir.join(name), text).unwrap(); + git(dir, &["add", "--", name]); + git(dir, &["commit", "-q", "-m", &format!("change {name}")]); +} + +/// A bare repository `remote.git` under `root` with one commit on `main`. +fn bare(root: &Path) -> std::path::PathBuf { + let bare = root.join("remote.git"); + std::fs::create_dir(&bare).unwrap(); + git(&bare, &["init", "-q", "--bare", "-b", "main"]); + let seed = root.join("seed"); + std::fs::create_dir(&seed).unwrap(); + git(&seed, &["init", "-q", "-b", "main"]); + configure(&seed); + commit(&seed, "a.php", ", + /// Events published so far. + events: Mutex>, +} + +impl Host { + /// An engine without a project. + fn new() -> Arc { + Arc::new(Self { + engine: Mutex::new(Engine::new().unwrap()), + events: Mutex::new(Vec::new()), + }) + } + + /// Runs a command the way hosts do, waiting without the lock. + fn run(&self, command: &str, params: Value) -> Result { + run_waiting( + || self.engine.lock().unwrap(), + command, + params, + |events| self.events.lock().unwrap().extend_from_slice(events), + ) + .map(|o| o.value) + } + + /// Pumps once and keeps the events. + fn pump(&self) { + let events = self.engine.lock().unwrap().pump_vfs().unwrap(); + self.events.lock().unwrap().extend(events); + } + + /// The events of `kind` seen so far, as JSON. + fn seen(&self, kind: &str) -> Vec { + self.events + .lock() + .unwrap() + .iter() + .map(|e| serde_json::to_value(e).unwrap()) + .filter(|e| e["kind"] == kind) + .collect() + } +} + +#[test] +fn clone_open_remotes_push_fetch_and_pull() { + if !have_git() { + return; + } + let tmp = tempfile::tempdir().unwrap(); + let bare = bare(tmp.path()); + let host = Host::new(); + let dir = tmp.path().join("work"); + let cloned = host + .run( + "git.clone", + json!({"url": bare.to_str().unwrap(), "dir": dir.to_str().unwrap()}), + ) + .unwrap(); + assert!(cloned["root"].as_str().unwrap().ends_with("work")); + let progress = host.seen("gitProgress"); + assert_eq!(progress.last().unwrap()["active"], false); + assert_eq!(progress.last().unwrap()["operation"], "clone"); + configure(&dir); + git(&dir, &["checkout-index", "-f", "-u", "-a"]); + + host.run("project.open", json!({"path": dir})).unwrap(); + host.run( + "git.remoteAdd", + json!({"name": "fork", "url": "https://me:t0ken@example.invalid/x.git"}), + ) + .unwrap(); + let remotes = host.run("git.remotes", json!({})).unwrap(); + let list = remotes["remotes"].as_array().unwrap(); + assert_eq!(list[0]["name"], "origin"); + assert_eq!(list[1]["fetchUrl"], "https://***@example.invalid/x.git"); + assert!(!remotes.to_string().contains("t0ken")); + host.run( + "git.remoteSetUrl", + json!({"name": "fork", "url": "git@example.invalid:x.git"}), + ) + .unwrap(); + host.run("git.remoteRemove", json!({"name": "fork"})) + .unwrap(); + assert_eq!( + host.run("git.remotes", json!({})).unwrap()["remotes"] + .as_array() + .unwrap() + .len(), + 1 + ); + + git(&dir, &["switch", "-q", "-c", "feature"]); + commit(&dir, "b.php", " bool { matches!( self, - Self::InvalidPath(_) | Self::InvalidName(_) | Self::EmptyMessage + Self::InvalidPath(_) + | Self::InvalidName(_) + | Self::EmptyMessage + | Self::InvalidInput(_) ) } } diff --git a/crates/vcs/src/lib.rs b/crates/vcs/src/lib.rs index 987f361..c4fe45d 100644 --- a/crates/vcs/src/lib.rs +++ b/crates/vcs/src/lib.rs @@ -7,12 +7,16 @@ //! - [`Repo`] runs status, stage, unstage, commit, show, branches, //! checkout and branch creation. Calls that change the repository are //! serialised per repository. +//! - Remotes (M4.10, owner decision D23): fetch, pull, push, clone and +//! remote management, with progress and cancel; credentials stay with +//! git's credential helper and the ssh agent (see `remote.rs`). //! - [`RepoWatch`] notices changes inside `.git` (HEAD, index, refs). //! //! Every call goes through [`runner::run`]: arguments as an array, never a -//! shell; no terminal or credential prompts; a timeout; capped output; -//! machine-readable output only (porcelain v2, `-z`, NUL-separated -//! formats). No network operation exists here. +//! shell; no terminal prompts (network calls allow the credential helper's +//! own window); a timeout; capped output; machine-readable output only +//! (porcelain v2, `-z`, NUL-separated formats); URL userinfo redacted in +//! messages. //! //! Calls block for as long as git runs, so the engine runs them on their //! own threads, off its lock. @@ -20,6 +24,7 @@ mod error; pub mod log; pub mod porcelain; +mod remote; mod repo; pub mod runner; mod watch; @@ -27,8 +32,13 @@ mod watch; pub use error::VcsError; pub use log::{CommitDetails, CommitFile, LOG_LIMIT, LogCommit, LogQuery}; pub use porcelain::{FileState, FileStatus, Head, Status, Version}; +pub use remote::{ + MAX_URL_LEN, Network, PullMode, Pushed, Remote, check_remote_name, check_url, explain, + parse_progress, +}; pub use repo::{ Branch, Branches, CommitInfo, Contents, FILE_CAP, Git, MIN_VERSION, Repo, Rev, STATUS_CAP, decode, }; +pub use runner::{Cancel, ProgressSink, redact_urls}; pub use watch::{RepoWatch, fingerprint}; diff --git a/crates/vcs/src/remote.rs b/crates/vcs/src/remote.rs new file mode 100644 index 0000000..57d2f8e --- /dev/null +++ b/crates/vcs/src/remote.rs @@ -0,0 +1,719 @@ +//! Remotes (M4.10, owner decision D23): fetch, pull, push, clone and the +//! list of configured remotes, through the user's git. +//! +//! - **Any server.** HTTPS and SSH remotes on any host (GitHub, GitLab, +//! Forgejo, Bitbucket, a self-hosted server) and local paths work the +//! same way, because git does the transport. `ext::` URLs are refused +//! (`protocol.ext.allow=never`), so a URL never runs a command. +//! - **Credentials stay with git.** HTTPS sign-in goes through git's +//! credential helper, which may show its own window (Git Credential +//! Manager); SSH uses the user's ssh, its agent and keys. Lutra never +//! reads, stores or logs a credential or key, and never answers a +//! prompt: terminal prompts are off ([`runner::Call::network`]), so a +//! remote that needs a password nobody supplies fails with a message +//! instead of hanging. +//! - **Redaction.** Remote URLs ([`Remote`]) and every message have the +//! userinfo of URLs replaced ([`runner::redact_urls`]). +//! - **Clear messages.** A refusal keeps git's text and gets a short +//! explanation first when its cause is known ([`explain`]): a rejected +//! push, a sign-in that failed, an unknown SSH host key, an unreachable +//! or missing repository, no upstream, conflicts after a pull. +//! - **Progress and cancel.** Network calls run with `--progress`; each +//! stderr line reaches the [`Network`] sink, and its cancel flag kills +//! git and every process it started. + +use std::path::Path; +use std::time::Duration; + +use crate::VcsError; +use crate::repo::{Git, Repo}; +use crate::runner::{self, Call, Cancel, ProgressSink, redact_urls}; + +/// Longest URL accepted. +pub const MAX_URL_LEN: usize = 4096; + +/// Cap on config and push output read. +const SMALL_CAP: usize = 1024 * 1024; + +/// Global options of every network call: no `ext::` transport. +const NETWORK_OPTIONS: [&str; 2] = ["-c", "protocol.ext.allow=never"]; + +/// Progress and cancel of one network call. +#[derive(Debug, Clone, Default)] +pub struct Network { + /// Receives git's progress lines. + pub progress: Option, + /// Stops the call when set. + pub cancel: Option, +} + +/// A configured remote. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Remote { + /// Its name, e.g. `origin`. + pub name: String, + /// `remote..url`, userinfo redacted. + pub fetch_url: Option, + /// `remote..pushurl`, userinfo redacted; `None` when pushes go + /// to the fetch URL. + pub push_url: Option, +} + +/// How `git pull` joins the fetched commits. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum PullMode { + /// `git pull --no-rebase`: a merge (fast-forward when possible). + Merge, + /// `git pull --rebase`: local commits are replayed on top. + Rebase, +} + +/// What [`Repo::push`] pushed. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Pushed { + /// The remote pushed to. + pub remote: String, + /// The local branch pushed. + pub branch: String, + /// The branch had no upstream and got one with this push. + pub upstream_set: bool, +} + +/// Checks a remote URL or path from the caller. +/// +/// # Errors +/// [`VcsError::InvalidInput`] when it is blank, longer than +/// [`MAX_URL_LEN`], starts with `-` (an option), holds a control +/// character, or uses the `ext::` transport. +pub fn check_url(url: &str) -> Result<(), VcsError> { + let invalid = |why: &str| VcsError::InvalidInput(format!("invalid remote URL: {why}")); + if url.trim().is_empty() { + return Err(invalid("it is empty")); + } + if url.len() > MAX_URL_LEN { + return Err(invalid("it is too long")); + } + if url.starts_with('-') { + return Err(invalid("it starts with '-'")); + } + if url.chars().any(char::is_control) { + return Err(invalid("it holds a control character")); + } + if url.trim().to_ascii_lowercase().starts_with("ext::") { + return Err(invalid("the ext:: transport is not allowed")); + } + Ok(()) +} + +/// Checks a remote name from the caller; git checks the rest +/// (`valid_remote_name`). +/// +/// # Errors +/// [`VcsError::InvalidName`] when it is empty, starts with `-`, or holds +/// whitespace or a control character. +pub fn check_remote_name(name: &str) -> Result<(), VcsError> { + if name.is_empty() + || name.len() > 255 + || name.starts_with('-') + || name.chars().any(|c| c.is_control() || c.is_whitespace()) + { + return Err(VcsError::InvalidName(name.to_string())); + } + Ok(()) +} + +/// Known causes of a refusal, each with the text Lutra puts before git's +/// message. Checked in order; the first that matches wins. +const EXPLANATIONS: &[(&[&str], &str)] = &[ + ( + &[ + "[rejected]", + "non-fast-forward", + "fetch first", + "Updates were rejected", + ], + "The push was rejected: the remote branch has commits that your branch does not have. Pull first, then push again.", + ), + ( + &[ + "[remote rejected]", + "pre-receive hook declined", + "protected branch", + ], + "The server refused the push (for example a protected branch or a server-side hook).", + ), + ( + &["Host key verification failed"], + "The server's SSH host key is not known yet. Connect to it once from a terminal (for example ssh -T git@host) to check and accept the key, then try again.", + ), + ( + &[ + "Authentication failed", + "could not read Username", + "could not read Password", + "terminal prompts disabled", + "Permission denied (publickey", + "Invalid username or password", + "returned error: 401", + "returned error: 403", + "The requested URL returned error: 403", + ], + "Git could not sign in to the remote. Lutra leaves sign-in to git: set up its credential helper (for example sign in once with Git Credential Manager) or add your SSH key to the ssh agent, then try again.", + ), + ( + &[ + "Repository not found", + "does not appear to be a git repository", + "returned error: 404", + "not found", + ], + "The remote repository was not found. Check the URL and that you have access to it.", + ), + ( + &[ + "Could not resolve host", + "Failed to connect", + "Connection timed out", + "Connection refused", + "Network is unreachable", + "Could not read from remote repository", + ], + "The remote could not be reached. Check the URL and your network connection.", + ), + ( + &[ + "There is no tracking information", + "no upstream", + "has no upstream branch", + ], + "The current branch has no upstream branch. Push it once to set one.", + ), + ( + &[ + "would be overwritten", + "You have unstaged changes", + "Please commit or stash them", + "cannot pull with rebase", + ], + "Local changes are in the way. Commit or stash them, then pull again.", + ), + ( + &[ + "CONFLICT", + "Automatic merge failed", + "could not apply", + "Resolve all conflicts", + ], + "The pull stopped with conflicts. Resolve them, then commit the merge or continue the rebase.", + ), + ( + &[ + "Not possible to fast-forward", + "Need to specify how to reconcile divergent branches", + ], + "Your branch and the remote branch have diverged. Choose merge or rebase for pulls in Settings › Git, then pull again.", + ), + ( + &["already exists and is not an empty directory"], + "The target folder exists and is not empty. Choose an empty or new folder.", + ), +]; + +/// git's message with a short explanation first when its cause is known +/// (see [`EXPLANATIONS`]); URLs are redacted. +pub fn explain(message: &str) -> String { + let message = redact_urls(message.trim()); + let lower = message.to_lowercase(); + let found = EXPLANATIONS.iter().find(|(needles, _)| { + needles + .iter() + .any(|needle| lower.contains(&needle.to_lowercase())) + }); + match found { + Some((_, why)) if message.is_empty() => (*why).to_string(), + Some((_, why)) => format!("{why}\n\n{message}"), + None => message, + } +} + +/// A progress line split into its phase and percentage: `Receiving +/// objects: 45% (450/1000), 1.2 MiB` → (`Receiving objects`, 45). A +/// `remote: ` prefix is dropped. Lines without a percentage give the +/// whole line and `None`. +pub fn parse_progress(line: &str) -> (String, Option) { + let line = line.trim(); + let line = line.strip_prefix("remote:").map_or(line, str::trim); + if let Some((phase, rest)) = line.split_once(':') + && let Some((number, _)) = rest.trim_start().split_once('%') + && let Ok(percent) = number.trim().parse::() + { + return (phase.trim().to_string(), Some(percent.min(100))); + } + (line.to_string(), None) +} + +/// Turns a failed network call's error into one with [`explain`]'s text. +fn explained(error: VcsError) -> VcsError { + match error { + VcsError::Git(message) => VcsError::Git(explain(&message)), + other => other, + } +} + +/// A network call in `cwd` with [`NETWORK_OPTIONS`] first and the +/// progress sink and cancel flag of `net`. +fn network_call<'a>(cwd: &'a Path, args: &[&str], net: &Network) -> Call<'a> { + let mut all: Vec<&str> = NETWORK_OPTIONS.to_vec(); + all.extend_from_slice(args); + Call::network(cwd, &all) + .progress(net.progress.clone()) + .cancel(net.cancel.clone()) +} + +/// Checks a network call's output, with explained errors. +fn checked(output: Result) -> Result { + output?.checked().map_err(explained) +} + +impl Git { + /// Clones `url` into `dir` (`git clone --progress`), with progress and + /// cancel. + /// + /// # Parameters + /// - `url`: any URL or path git can clone from; checked with + /// [`check_url`]. + /// - `dir`: the new work tree; absolute, its parent must exist, and it + /// must be missing or an empty folder. + /// + /// When the clone fails or is cancelled, what it left in `dir` is + /// removed again (the folder itself too, if the clone created it). + /// + /// # Errors + /// [`VcsError::InvalidInput`] for a bad URL or folder, + /// [`VcsError::Cancelled`], or git's refusal with [`explain`]'s text. + pub fn clone_repo(&self, url: &str, dir: &Path, net: &Network) -> Result<(), VcsError> { + check_url(url)?; + let invalid = |why: &str| VcsError::InvalidInput(why.to_string()); + if !dir.is_absolute() { + return Err(invalid("the target folder must be an absolute path")); + } + let Some(parent) = dir.parent().filter(|p| p.is_dir()) else { + return Err(invalid("the parent of the target folder does not exist")); + }; + let existed = dir.exists(); + if existed { + let empty = std::fs::read_dir(dir) + .map(|mut entries| entries.next().is_none()) + .unwrap_or(false); + if !dir.is_dir() || !empty { + return Err(invalid( + "the target folder exists and is not empty; choose an empty or new folder", + )); + } + } + let Some(target) = dir.to_str() else { + return Err(invalid("the target folder's path is not valid Unicode")); + }; + let result = checked(runner::run( + self.executable(), + network_call( + parent, + &["clone", "--progress", "--end-of-options", url, target], + net, + ), + )); + if result.is_err() { + clean_up(dir, existed); + } + result.map(|_| ()) + } +} + +/// Removes what a failed clone left: the folder, or only its contents when +/// it existed (empty) before. Failures are ignored after a few tries: the +/// clone's error is what the caller reports, and a leftover folder is +/// visible to the user. +fn clean_up(dir: &Path, existed: bool) { + for _ in 0..5 { + let removed = if existed { + std::fs::read_dir(dir).is_ok_and(|entries| { + entries.flatten().all(|entry| { + let path = entry.path(); + if path.is_dir() { + std::fs::remove_dir_all(&path).is_ok() + } else { + std::fs::remove_file(&path).is_ok() + } + }) + }) + } else { + std::fs::remove_dir_all(dir).is_ok() || !dir.exists() + }; + if removed { + return; + } + std::thread::sleep(Duration::from_millis(100)); + } +} + +/// Parses `git config -z --get-regexp '^remote\..*\.(url|pushurl)$'` +/// output into remotes, in the order they are configured; the last value +/// of a key wins, as in git. URLs are redacted. +pub fn parse_remotes(bytes: &[u8]) -> Vec { + let mut remotes: Vec = Vec::new(); + for entry in bytes.split(|b| *b == 0) { + let text = String::from_utf8_lossy(entry); + let Some((key, value)) = text.split_once('\n') else { + continue; + }; + let Some(rest) = key.strip_prefix("remote.") else { + continue; + }; + let (name, push) = if let Some(name) = rest.strip_suffix(".pushurl") { + (name, true) + } else if let Some(name) = rest.strip_suffix(".url") { + (name, false) + } else { + continue; + }; + if name.is_empty() || value.is_empty() { + continue; + } + let index = match remotes.iter().position(|r| r.name == name) { + Some(index) => index, + None => { + remotes.push(Remote { + name: name.to_string(), + fetch_url: None, + push_url: None, + }); + remotes.len() - 1 + } + }; + let Some(remote) = remotes.get_mut(index) else { + continue; + }; + let value = Some(redact_urls(value)); + if push { + remote.push_url = value; + } else { + remote.fetch_url = value; + } + } + remotes +} + +/// The summaries of the refs `git push --porcelain` reports as rejected +/// (lines starting with `!`), e.g. `[rejected] (fetch first)`. +pub fn push_rejections(stdout: &[u8]) -> Vec { + String::from_utf8_lossy(stdout) + .lines() + .filter_map(|line| { + let rest = line.strip_prefix('!')?; + let summary = rest.rsplit('\t').next().unwrap_or(rest).trim(); + Some(summary.to_string()) + }) + .collect() +} + +impl Repo { + /// Runs a read in the work tree root. + fn read(&self, args: &[&str]) -> Result { + self.run(Call::read(self.root(), args, SMALL_CAP)) + } + + /// One config value; `None` when it is not set. + fn config(&self, key: &str) -> Result, VcsError> { + let output = self.read(&["config", "--get", "--end-of-options", key])?; + if !output.success() { + return Ok(None); + } + let value = String::from_utf8_lossy(&output.stdout).trim().to_string(); + Ok((!value.is_empty()).then_some(value)) + } + + /// The configured remotes, in config order, URLs redacted. + /// + /// # Errors + /// The runner's errors; no remote is an empty list. + pub fn remotes(&self) -> Result, VcsError> { + let output = self.read(&[ + "config", + "-z", + "--get-regexp", + r"^remote\..*\.(url|pushurl)$", + ])?; + if !output.success() { + return Ok(Vec::new()); + } + Ok(parse_remotes(&output.stdout)) + } + + /// Runs a remote-management call (`git remote …`), serialised with + /// the other changes. + fn manage(&self, args: &[&str]) -> Result<(), VcsError> { + let _guard = self.mutation_guard(); + self.run(Call::write(self.root(), args))?.checked()?; + Ok(()) + } + + /// Adds a remote (`git remote add`), without fetching. + /// + /// # Errors + /// [`VcsError::InvalidName`], [`VcsError::InvalidInput`], or git's + /// refusal (the remote exists, the name is not valid). + pub fn remote_add(&self, name: &str, url: &str) -> Result<(), VcsError> { + check_remote_name(name)?; + check_url(url)?; + self.manage(&["remote", "add", "--end-of-options", name, url]) + } + + /// Changes a remote's URL (`git remote set-url`). + /// + /// # Errors + /// As [`Repo::remote_add`]; an unknown remote is git's refusal. + pub fn remote_set_url(&self, name: &str, url: &str) -> Result<(), VcsError> { + check_remote_name(name)?; + check_url(url)?; + self.manage(&["remote", "set-url", "--end-of-options", name, url]) + } + + /// Removes a remote with its remote-tracking branches and the + /// upstream settings that named it (`git remote remove`). + /// + /// # Errors + /// [`VcsError::InvalidName`], or git's refusal (no such remote). + pub fn remote_remove(&self, name: &str) -> Result<(), VcsError> { + check_remote_name(name)?; + self.manage(&["remote", "remove", "--end-of-options", name]) + } + + /// Fetches one remote, or every remote when `remote` is `None` + /// (`git fetch --all`). + /// + /// # Errors + /// [`VcsError::InvalidName`], [`VcsError::Cancelled`], or git's + /// refusal with [`explain`]'s text. + pub fn fetch(&self, remote: Option<&str>, net: &Network) -> Result<(), VcsError> { + let args: Vec<&str> = match remote { + Some(name) => { + check_remote_name(name)?; + vec!["fetch", "--progress", "--end-of-options", name] + } + None => vec!["fetch", "--progress", "--all"], + }; + let _guard = self.mutation_guard(); + checked(self.run(network_call(self.root(), &args, net))).map(|_| ()) + } + + /// Pulls the current branch's upstream. A merge takes git's default + /// message without an editor (`--no-edit`). + /// + /// # Errors + /// [`VcsError::Cancelled`], or git's refusal with [`explain`]'s text + /// (no upstream, local changes in the way, conflicts). + pub fn pull(&self, mode: PullMode, net: &Network) -> Result<(), VcsError> { + let args: &[&str] = match mode { + PullMode::Merge => &["pull", "--progress", "--no-rebase", "--no-edit"], + PullMode::Rebase => &["pull", "--progress", "--rebase"], + }; + let _guard = self.mutation_guard(); + checked(self.run(network_call(self.root(), args, net))).map(|_| ()) + } + + /// Pushes the current branch. With an upstream it goes there + /// (`branch..remote` and `.merge`); without one it goes to a + /// branch of the same name on `remote` and becomes the upstream + /// (`--set-upstream`). Without `remote` that is the only remote, else + /// `origin`. Hooks (`pre-push`) run. + /// + /// # Errors + /// [`VcsError::InvalidName`]; [`VcsError::Git`] when HEAD is detached, + /// no remote fits, or git or the server refuses (with [`explain`]'s + /// text, also when git's exit status hides a rejected ref); + /// [`VcsError::Cancelled`]. + pub fn push(&self, remote: Option<&str>, net: &Network) -> Result { + if let Some(name) = remote { + check_remote_name(name)?; + } + let head = self.head()?; + let Some(branch) = head.branch.filter(|_| !head.detached) else { + return Err(VcsError::Git( + "HEAD is detached; check out a branch to push it.".into(), + )); + }; + let upstream_remote = self + .config(&format!("branch.{branch}.remote"))? + .filter(|r| r != "."); + let merge = self.config(&format!("branch.{branch}.merge"))?; + let local_ref = format!("refs/heads/{branch}"); + let (target, refspec, set_upstream) = match (upstream_remote, merge) { + (Some(up), Some(merge)) if remote.is_none_or(|r| r == up) => { + (up, format!("{local_ref}:{merge}"), false) + } + _ => { + let target = match remote { + Some(name) => name.to_string(), + None => self.default_remote()?, + }; + (target, format!("{local_ref}:{local_ref}"), true) + } + }; + let mut args = vec!["push", "--progress", "--porcelain"]; + if set_upstream { + args.push("--set-upstream"); + } + args.extend(["--end-of-options", &target, &refspec]); + let _guard = self.mutation_guard(); + let output = self.run(network_call(self.root(), &args, net))?; + let rejected = push_rejections(&output.stdout); + if !output.success() || !rejected.is_empty() { + let mut message = output.message(); + for line in rejected { + if !message.contains(&line) { + message.push('\n'); + message.push_str(&line); + } + } + return Err(VcsError::Git(explain(&message))); + } + Ok(Pushed { + remote: target, + branch, + upstream_set: set_upstream, + }) + } + + /// The remote a branch without upstream is pushed to: the only remote, + /// else `origin`. + fn default_remote(&self) -> Result { + let remotes = self.remotes()?; + match remotes.as_slice() { + [only] => Ok(only.name.clone()), + [] => Err(VcsError::Git( + "This repository has no remote to push to. Add one in Git › Manage Remotes…".into(), + )), + many if many.iter().any(|r| r.name == "origin") => Ok("origin".into()), + _ => Err(VcsError::Git( + "This repository has several remotes and none is named origin; choose the remote to push to.".into(), + )), + } + } +} + +#[cfg(test)] +mod tests { + #![allow(clippy::unwrap_used)] + + use super::*; + + #[test] + fn urls_are_checked() { + assert!(check_url("https://github.com/a/b.git").is_ok()); + assert!(check_url("git@github.com:a/b.git").is_ok()); + assert!(check_url("C:\\repos\\x.git").is_ok()); + assert!(check_url("").is_err()); + assert!(check_url(" ").is_err()); + assert!(check_url("--upload-pack=evil").is_err()); + assert!(check_url("https://a\nb").is_err()); + assert!(check_url("ext::sh -c touch% /tmp/x").is_err()); + assert!(check_url(&"a".repeat(MAX_URL_LEN + 1)).is_err()); + } + + #[test] + fn remote_names_are_checked() { + assert!(check_remote_name("origin").is_ok()); + assert!(check_remote_name("my.fork/x").is_ok()); + assert!(check_remote_name("").is_err()); + assert!(check_remote_name("-x").is_err()); + assert!(check_remote_name("a b").is_err()); + } + + #[test] + fn explanations_come_before_git_message() { + let text = explain( + " ! [rejected] main -> main (fetch first)\nerror: failed to push some refs to 'https://u:p@h/r'", + ); + assert!(text.starts_with("The push was rejected")); + assert!(text.contains("https://***@h/r")); + assert!(!text.contains("u:p")); + assert!( + explain( + "fatal: could not read Username for 'https://github.com': terminal prompts disabled" + ) + .starts_with("Git could not sign in") + ); + assert!( + explain("Host key verification failed.\nfatal: Could not read from remote repository.") + .starts_with("The server's SSH host key") + ); + assert!( + explain("fatal: '/x' does not appear to be a git repository") + .starts_with("The remote repository was not found") + ); + assert!( + explain("There is no tracking information for the current branch.") + .starts_with("The current branch has no upstream") + ); + assert_eq!(explain("something else"), "something else"); + } + + #[test] + fn progress_lines_give_phase_and_percent() { + assert_eq!( + parse_progress("Receiving objects: 45% (450/1000), 1.20 MiB | 2.00 MiB/s"), + ("Receiving objects".into(), Some(45)) + ); + assert_eq!( + parse_progress("remote: Counting objects: 100% (3/3), done."), + ("Counting objects".into(), Some(100)) + ); + assert_eq!( + parse_progress("Cloning into 'x'..."), + ("Cloning into 'x'...".into(), None) + ); + assert_eq!(parse_progress("a: 999% b"), ("a: 999% b".into(), None)); + } + + #[test] + fn remotes_parse_in_order_with_redacted_urls() { + let bytes = b"remote.origin.url\nhttps://me:tok@github.com/a/b.git\0remote.my.fork.url\ngit@h:x.git\0remote.origin.pushurl\nssh://git@h/b\0remote.bad.fetch\nx\0junk\0"; + let remotes = parse_remotes(bytes); + assert_eq!( + remotes, + [ + Remote { + name: "origin".into(), + fetch_url: Some("https://***@github.com/a/b.git".into()), + push_url: Some("ssh://***@h/b".into()), + }, + Remote { + name: "my.fork".into(), + fetch_url: Some("git@h:x.git".into()), + push_url: None, + }, + ] + ); + } + + #[test] + fn push_rejections_are_read_from_porcelain() { + let out = + b"To /tmp/r.git\n!\trefs/heads/main:refs/heads/main\t[rejected] (fetch first)\nDone\n"; + assert_eq!(push_rejections(out), ["[rejected] (fetch first)"]); + assert!( + push_rejections(b"To x\n=\trefs/heads/a:refs/heads/a\t[up to date]\nDone\n").is_empty() + ); + } + + proptest::proptest! { + #[test] + fn parsers_take_any_bytes(bytes in proptest::collection::vec(proptest::num::u8::ANY, 0..300)) { + let _ = parse_remotes(&bytes); + let _ = push_rejections(&bytes); + let text = String::from_utf8_lossy(&bytes); + let _ = parse_progress(&text); + let _ = explain(&text); + } + } +} diff --git a/crates/vcs/src/repo.rs b/crates/vcs/src/repo.rs index 1e055cf..211d098 100644 --- a/crates/vcs/src/repo.rs +++ b/crates/vcs/src/repo.rs @@ -82,6 +82,11 @@ impl Git { self.version } + /// The program that runs (for the remote calls in `remote.rs`). + pub(crate) fn executable(&self) -> &Path { + &self.executable + } + /// Opens the repository that contains `dir` (a nested repository wins /// over a parent one, as git decides). /// @@ -231,6 +236,12 @@ impl Repo { &self.prefix } + /// Serialises a change made outside this file (`remote.rs`) with the + /// changes made here. + pub(crate) fn mutation_guard(&self) -> MutexGuard<'_, ()> { + self.lock() + } + /// Serialises changes: the guard of [`Repo::mutations`]. A poisoned /// lock is taken over: the lock guards no data. fn lock(&self) -> MutexGuard<'_, ()> { diff --git a/crates/vcs/src/runner.rs b/crates/vcs/src/runner.rs index 5e2347e..397e69b 100644 --- a/crates/vcs/src/runner.rs +++ b/crates/vcs/src/runner.rs @@ -4,11 +4,21 @@ //! Every call goes through [`run`], and every process starts in [`spawn`], //! which puts git (and the hooks it runs) into a kill-on-close group //! (`lutra-proc`), so no git outlives Lutra. +//! +//! Network calls (M4.10, owner decision D23) are built with +//! [`Call::network`]: terminal prompts stay off, but the credential +//! helper may show its own window (Git Credential Manager), the timeout is +//! [`NETWORK_TIMEOUT`], stderr progress lines reach a [`ProgressSink`], +//! and a [`Cancel`] flag stops git. Every message that leaves the runner +//! has the userinfo of URLs replaced ([`redact_urls`]), so a token in a +//! remote URL never reaches an error or a log. use std::ffi::OsString; use std::io::{Read, Write}; use std::path::Path; use std::process::{Command, ExitStatus, Stdio}; +use std::sync::Arc; +use std::sync::atomic::{AtomicBool, Ordering}; use lutra_proc::Child; use std::sync::mpsc; @@ -23,9 +33,54 @@ pub const TIMEOUT: Duration = Duration::from_secs(30); /// Timeout of calls that run the user's hooks (commit, checkout). pub const HOOK_TIMEOUT: Duration = Duration::from_secs(120); +/// Timeout of network calls (fetch, pull, push, clone); [`Cancel`] stops +/// them sooner. +pub const NETWORK_TIMEOUT: Duration = Duration::from_secs(30 * 60); + /// Cap on stderr kept for error messages; the rest is read and dropped. pub const STDERR_CAP: usize = 16 * 1024; +/// Longest progress line passed to a [`ProgressSink`]; longer ones are cut. +const PROGRESS_LINE_CAP: usize = 1024; + +/// How often a waiting call checks its [`Cancel`] flag. +const CANCEL_POLL: Duration = Duration::from_millis(50); + +/// Receives git's progress lines (stderr split at `\r` and `\n`, blank +/// lines dropped, URLs redacted, at most 1 KiB each), on the thread that +/// reads stderr. +#[derive(Clone)] +pub struct ProgressSink(pub Arc); + +impl std::fmt::Debug for ProgressSink { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str("ProgressSink") + } +} + +/// A flag that stops a running call: once it is set, git (with every +/// process it started) is killed within [`CANCEL_POLL`] and the call ends +/// with [`VcsError::Cancelled`]. +#[derive(Debug, Clone, Default)] +pub struct Cancel(Arc); + +impl Cancel { + /// A flag that is not set. + pub fn new() -> Self { + Self::default() + } + + /// Sets the flag. + pub fn cancel(&self) { + self.0.store(true, Ordering::SeqCst); + } + + /// Whether the flag is set. + pub fn is_cancelled(&self) -> bool { + self.0.load(Ordering::SeqCst) + } +} + /// Environment variables that would point git at another repository than /// the one Lutra asked for, removed from every call. const SCRUBBED_ENV: &[&str] = &[ @@ -57,6 +112,14 @@ pub struct Call<'a> { /// A read that must not take optional locks (`GIT_OPTIONAL_LOCKS=0`), /// so it never competes with the user's own git for `index.lock`. pub read_only: bool, + /// A network call: the credential helper may show its own window + /// (`GCM_INTERACTIVE` is left as the user has it); terminal prompts + /// stay off. + pub network: bool, + /// Receives stderr progress lines while git runs. + pub progress: Option, + /// Stops the call when set. + pub cancel: Option, } impl<'a> Call<'a> { @@ -69,6 +132,9 @@ impl<'a> Call<'a> { timeout: TIMEOUT, stdout_cap, read_only: true, + network: false, + progress: None, + cancel: None, } } @@ -81,6 +147,30 @@ impl<'a> Call<'a> { } } + /// A network call (fetch, pull, push, clone; D23): it may change the + /// repository, has [`NETWORK_TIMEOUT`] and a 1 MiB output cap, and its + /// credential helper may show its own window. Terminal prompts stay + /// off (`GIT_TERMINAL_PROMPT=0`, no console, stdin closed). + pub fn network(cwd: &'a Path, args: &[&str]) -> Self { + Self { + network: true, + timeout: NETWORK_TIMEOUT, + ..Self::write(cwd, args) + } + } + + /// Sets the progress sink. + pub fn progress(mut self, sink: Option) -> Self { + self.progress = sink; + self + } + + /// Sets the cancel flag. + pub fn cancel(mut self, cancel: Option) -> Self { + self.cancel = cancel; + self + } + /// Sets stdin. pub fn stdin(mut self, bytes: Vec) -> Self { self.stdin = Some(bytes); @@ -112,15 +202,16 @@ impl Output { } /// git's message for a failure: stderr, or stdout when stderr is - /// empty (hooks often print to stdout), trimmed and capped at - /// [`STDERR_CAP`] bytes on a character boundary. + /// empty (hooks often print to stdout), trimmed, with URL userinfo + /// redacted ([`redact_urls`]) and capped at [`STDERR_CAP`] bytes on a + /// character boundary. pub fn message(&self) -> String { let source = if self.stderr.iter().all(u8::is_ascii_whitespace) { &self.stdout } else { &self.stderr }; - let text = String::from_utf8_lossy(source); + let text = redact_urls(&String::from_utf8_lossy(source)); let text = text.trim(); let mut end = text.len().min(STDERR_CAP); while !text.is_char_boundary(end) { @@ -164,12 +255,12 @@ pub fn spawn(command: Command) -> std::io::Result { struct Reaper(Option); impl Reaper { - /// Waits until the child exits or `deadline` passes. + /// Waits until the child exits, `deadline` passes or `cancel` is set. /// /// # Returns - /// The exit status, or `None` at the deadline (the child is killed - /// when the reaper drops). - fn wait_until(&mut self, deadline: Instant) -> Option { + /// The exit status, or `None` at the deadline or on cancel (the child + /// is killed when the reaper drops). + fn wait_until(&mut self, deadline: Instant, cancel: Option<&Cancel>) -> Option { let child = self.0.as_mut()?; let mut pause = Duration::from_millis(1); loop { @@ -182,7 +273,7 @@ impl Reaper { Err(_) => return None, } let now = Instant::now(); - if now >= deadline { + if now >= deadline || cancel.is_some_and(Cancel::is_cancelled) { return None; } thread::sleep(pause.min(deadline - now)); @@ -203,25 +294,70 @@ impl Drop for Reaper { } } +/// Splits progress output into lines at `\r` and `\n` and passes each +/// non-blank one to the sink; the unfinished tail waits for the next +/// chunk (at most [`PROGRESS_LINE_CAP`] bytes of it are kept). +struct LineSplitter { + /// Receives the lines. + sink: ProgressSink, + /// The start of the current line. + pending: Vec, +} + +impl LineSplitter { + /// Feeds bytes; `end` passes the tail on as a last line. + fn feed(&mut self, bytes: &[u8], end: bool) { + for &byte in bytes { + if byte == b'\r' || byte == b'\n' { + self.flush(); + } else if self.pending.len() < PROGRESS_LINE_CAP { + self.pending.push(byte); + } + } + if end { + self.flush(); + } + } + + /// Passes the current line on, unless it is blank. + fn flush(&mut self) { + let line = redact_urls(&String::from_utf8_lossy(&self.pending)); + self.pending.clear(); + let line = line.trim(); + if !line.is_empty() { + (self.sink.0)(line); + } + } +} + /// Reads `reader` to its end on a new thread, keeping at most `cap` bytes. /// /// # Parameters /// - `fail_over_cap`: stop at once and report `Err(())` when more than /// `cap` bytes arrive (stdout), instead of reading the rest and /// dropping it (stderr). +/// - `sink`: also receives every line read (stderr progress). fn collect( mut reader: impl Read + Send + 'static, cap: usize, fail_over_cap: bool, + sink: Option, ) -> mpsc::Receiver, ()>> { let (tx, rx) = mpsc::channel(); thread::spawn(move || { let mut kept = Vec::new(); let mut chunk = [0u8; 64 * 1024]; + let mut lines = sink.map(|sink| LineSplitter { + sink, + pending: Vec::new(), + }); let result = loop { match reader.read(&mut chunk) { Ok(0) => break Ok(kept), Ok(n) => { + if let Some(lines) = lines.as_mut() { + lines.feed(chunk.get(..n).unwrap_or_default(), false); + } let room = cap.saturating_sub(kept.len()); if n > room && fail_over_cap { break Err(()); @@ -232,6 +368,9 @@ fn collect( Err(_) => break Ok(kept), } }; + if let Some(lines) = lines.as_mut() { + lines.feed(&[], true); + } let _ = tx.send(result); }); rx @@ -246,13 +385,15 @@ fn collect( /// Global options on every call: `--no-pager`, `--literal-pathspecs` /// (paths are never patterns), `-c core.quotepath=false`, /// `-c color.ui=false`. Environment: `GIT_TERMINAL_PROMPT=0`, -/// `GCM_INTERACTIVE=never`, `GIT_OPTIONAL_LOCKS=0` for reads, and the -/// variables that redirect git to another repository removed. +/// `GCM_INTERACTIVE=never` (except for network calls, whose credential +/// helper may show its own window), `GIT_OPTIONAL_LOCKS=0` for reads, and +/// the variables that redirect git to another repository removed. /// /// # Errors /// [`VcsError::Spawn`], [`VcsError::Timeout`] (git is killed), -/// [`VcsError::TooLarge`] (git is killed). A non-zero exit status is not an -/// error here; see [`Output::checked`]. +/// [`VcsError::TooLarge`] (git is killed), [`VcsError::Cancelled`] (git +/// is killed). A non-zero exit status is not an error here; see +/// [`Output::checked`]. pub fn run(executable: &Path, call: Call<'_>) -> Result { let mut command = Command::new(executable); command @@ -267,7 +408,6 @@ pub fn run(executable: &Path, call: Call<'_>) -> Result { .args(&call.args) .current_dir(call.cwd) .env("GIT_TERMINAL_PROMPT", "0") - .env("GCM_INTERACTIVE", "never") .stdin(if call.stdin.is_some() { Stdio::piped() } else { @@ -278,6 +418,9 @@ pub fn run(executable: &Path, call: Call<'_>) -> Result { for name in SCRUBBED_ENV { command.env_remove(name); } + if !call.network { + command.env("GCM_INTERACTIVE", "never"); + } if call.read_only { command.env("GIT_OPTIONAL_LOCKS", "0"); } @@ -296,22 +439,41 @@ pub fn run(executable: &Path, call: Call<'_>) -> Result { let (Some(stdout), Some(stderr)) = (stdout, stderr) else { return Err(VcsError::Spawn("no pipes".into())); }; - let out_rx = collect(stdout, call.stdout_cap, true); - let err_rx = collect(stderr, STDERR_CAP, false); + let out_rx = collect(stdout, call.stdout_cap, true, None); + let err_rx = collect(stderr, STDERR_CAP, false, call.progress); + let cancel = call.cancel; + let cancelled = || cancel.as_ref().is_some_and(Cancel::is_cancelled); let timeout = || VcsError::Timeout { seconds: call.timeout.as_secs(), }; let remaining = || deadline.saturating_duration_since(Instant::now()); - let stdout = match out_rx.recv_timeout(remaining()) { - Ok(Ok(bytes)) => bytes, - Ok(Err(())) => { - return Err(VcsError::TooLarge { - limit: call.stdout_cap, - }); + let stdout = loop { + if cancelled() { + return Err(VcsError::Cancelled); + } + let slice = if cancel.is_some() { + remaining().min(CANCEL_POLL) + } else { + remaining() + }; + match out_rx.recv_timeout(slice) { + Ok(Ok(bytes)) => break bytes, + Ok(Err(())) => { + return Err(VcsError::TooLarge { + limit: call.stdout_cap, + }); + } + Err(mpsc::RecvTimeoutError::Timeout) if !remaining().is_zero() => {} + Err(_) => return Err(timeout()), } - Err(_) => return Err(timeout()), }; - let status = reaper.wait_until(deadline).ok_or_else(timeout)?; + let Some(status) = reaper.wait_until(deadline, cancel.as_ref()) else { + return Err(if cancelled() { + VcsError::Cancelled + } else { + timeout() + }); + }; let stderr = err_rx .recv_timeout(remaining().max(Duration::from_millis(200))) .ok() @@ -324,6 +486,42 @@ pub fn run(executable: &Path, call: Call<'_>) -> Result { }) } +/// Replaces the userinfo of every URL in `text` (`https://user:token@host` +/// becomes `https://***@host`), so credentials in a remote URL never reach +/// an error message, a progress line or a log. A URL is `://` after a +/// scheme character (letter, digit, `+`, `-`, `.`); its authority ends at +/// `/`, `?`, `#`, whitespace, a quote or an angle bracket, and its +/// userinfo is everything before the last `@` in it. scp-like addresses +/// (`git@host:path`) carry a user name only and stay as they are. +pub fn redact_urls(text: &str) -> String { + let mut out = String::with_capacity(text.len()); + let mut rest = text; + while let Some(at) = rest.find("://") { + let (before, after) = rest.split_at(at + 3); + let scheme_ok = before + .get(..at) + .and_then(|s| s.chars().next_back()) + .is_some_and(|c| c.is_ascii_alphanumeric() || matches!(c, '+' | '-' | '.')); + out.push_str(before); + let end = after + .find(|c: char| { + matches!(c, '/' | '?' | '#' | '"' | '\'' | '<' | '>' | '`') || c.is_whitespace() + }) + .unwrap_or(after.len()); + let (authority, tail) = after.split_at(end); + match authority.rfind('@') { + Some(user_end) if scheme_ok => { + out.push_str("***"); + out.push_str(authority.get(user_end..).unwrap_or_default()); + } + _ => out.push_str(authority), + } + rest = tail; + } + out.push_str(rest); + out +} + /// Keeps git from opening a console window on Windows. #[cfg(windows)] fn hide_console(command: &mut Command) { @@ -364,6 +562,69 @@ mod tests { assert!(output(b"", b"").message().starts_with("git failed")); } + #[test] + fn urls_lose_their_userinfo() { + assert_eq!( + redact_urls("fatal: unable to access 'https://me:ghp_secret@github.com/a/b.git/': 403"), + "fatal: unable to access 'https://***@github.com/a/b.git/': 403" + ); + assert_eq!(redact_urls("http://tok@h:8080"), "http://***@h:8080"); + assert_eq!( + redact_urls("ssh://git@host/x and https://a:b@c/d"), + "ssh://***@host/x and https://***@c/d" + ); + assert_eq!( + redact_urls("git@github.com:a/b.git"), + "git@github.com:a/b.git" + ); + assert_eq!( + redact_urls("https://github.com/a@b"), + "https://github.com/a@b" + ); + assert_eq!(redact_urls("file:///tmp/x"), "file:///tmp/x"); + assert_eq!(redact_urls("://@ é ://x@y"), "://@ é ://x@y"); + assert_eq!( + output(b"", b"error: https://u:p@h/r").message(), + "error: https://***@h/r" + ); + } + + #[test] + fn progress_lines_split_at_carriage_returns() { + let seen = Arc::new(std::sync::Mutex::new(Vec::::new())); + let copy = Arc::clone(&seen); + let mut lines = LineSplitter { + sink: ProgressSink(Arc::new(move |l: &str| { + copy.lock().unwrap().push(l.to_string()) + })), + pending: Vec::new(), + }; + lines.feed(b"Receiving objects: 10% (1/10)\rReceiving obj", false); + lines.feed( + b"ects: 100% (10/10), done.\n\nremote: https://a:b@h/x", + false, + ); + lines.feed(b"", true); + assert_eq!( + *seen.lock().unwrap(), + [ + "Receiving objects: 10% (1/10)", + "Receiving objects: 100% (10/10), done.", + "remote: https://***@h/x", + ] + ); + } + + proptest::proptest! { + #[test] + fn redaction_never_panics_and_keeps_no_userinfo(text in ".{0,200}") { + let out = redact_urls(&text); + if !text.contains("://") { + proptest::prop_assert_eq!(out, text); + } + } + } + #[test] fn message_is_capped_on_a_char_boundary() { let long = "é".repeat(STDERR_CAP); diff --git a/crates/vcs/tests/remote.rs b/crates/vcs/tests/remote.rs new file mode 100644 index 0000000..e3f30e0 --- /dev/null +++ b/crates/vcs/tests/remote.rs @@ -0,0 +1,377 @@ +//! Remotes (M4.10) against local bare repositories, never the network: +//! clone with progress, push with and without upstream, a rejected push, +//! fetch, pull by merge and by rebase, pull without upstream, remote +//! management with redacted URLs, cancel before and during a call, and +//! the clean-up after a failed clone. Every test skips with a message +//! when git is not installed. + +#![allow( + clippy::unwrap_used, + clippy::expect_used, + clippy::panic, + clippy::indexing_slicing +)] + +use std::path::Path; +use std::process::Command; +use std::sync::{Arc, Mutex}; +use std::time::{Duration, Instant}; + +use lutra_vcs::{Cancel, Git, Network, ProgressSink, PullMode, Repo, VcsError}; + +/// The installed git, or `None` (with a message) when there is none. +fn installed() -> Option { + match Git::detect("") { + Ok(git) => Some(git), + Err(e) => { + eprintln!("skipped: no usable git installed ({e})"); + None + } + } +} + +/// Runs git in `dir` for test setup and asserts it succeeded. +fn git(dir: &Path, args: &[&str]) -> String { + let out = Command::new("git") + .args(args) + .current_dir(dir) + .env_remove("GIT_DIR") + .env_remove("GIT_WORK_TREE") + .env_remove("GIT_INDEX_FILE") + .output() + .unwrap(); + assert!( + out.status.success(), + "git {args:?}: {}", + String::from_utf8_lossy(&out.stderr) + ); + String::from_utf8_lossy(&out.stdout).trim().to_string() +} + +/// Gives a work tree a local identity, no signing and no line-ending +/// conversion. +fn configure(dir: &Path) { + for (key, value) in [ + ("user.name", "Lutra Test"), + ("user.email", "test@lutra.invalid"), + ("commit.gpgsign", "false"), + ("core.autocrlf", "false"), + ] { + git(dir, &["config", key, value]); + } +} + +/// Writes `name` with `text` and commits it. +fn commit(dir: &Path, name: &str, text: &str) { + std::fs::write(dir.join(name), text).unwrap(); + git(dir, &["add", "--", name]); + git(dir, &["commit", "-q", "-m", &format!("change {name}")]); +} + +/// A bare repository `remote.git` with one commit on `main`, and its path. +fn bare_with_commit(root: &Path) -> std::path::PathBuf { + let bare = root.join("remote.git"); + std::fs::create_dir(&bare).unwrap(); + git(&bare, &["init", "-q", "--bare", "-b", "main"]); + let seed = root.join("seed"); + std::fs::create_dir(&seed).unwrap(); + git(&seed, &["init", "-q", "-b", "main"]); + configure(&seed); + commit(&seed, "a.txt", "one\n"); + git(&seed, &["push", "-q", bare.to_str().unwrap(), "main"]); + bare +} + +/// Clones `bare` into `root/` through Lutra, configures it, writes +/// the files again without line-ending conversion, and opens it. +fn clone_open(git_: &Git, bare: &Path, root: &Path, name: &str) -> Repo { + let dir = root.join(name); + git_.clone_repo(bare.to_str().unwrap(), &dir, &Network::default()) + .unwrap(); + configure(&dir); + git(&dir, &["checkout-index", "-f", "-u", "-a"]); + git_.open(&dir).unwrap().unwrap() +} + +/// A file's text with `\r\n` as `\n` (a global `core.autocrlf` applies to +/// clones). +fn read(path: &Path) -> String { + std::fs::read_to_string(path).unwrap().replace("\r\n", "\n") +} + +/// A network setting that records every progress line. +fn recording() -> (Network, Arc>>) { + let lines = Arc::new(Mutex::new(Vec::new())); + let copy = Arc::clone(&lines); + let net = Network { + progress: Some(ProgressSink(Arc::new(move |line: &str| { + copy.lock().unwrap().push(line.to_string()); + }))), + cancel: None, + }; + (net, lines) +} + +#[test] +fn clone_reports_progress_and_lists_origin() { + let Some(g) = installed() else { return }; + let tmp = tempfile::tempdir().unwrap(); + let bare = bare_with_commit(tmp.path()); + let dir = tmp.path().join("work"); + let (net, lines) = recording(); + g.clone_repo(bare.to_str().unwrap(), &dir, &net).unwrap(); + assert_eq!(read(&dir.join("a.txt")), "one\n"); + assert!( + lines + .lock() + .unwrap() + .iter() + .any(|l| l.contains("Cloning into")), + "progress: {:?}", + lines.lock().unwrap() + ); + let repo = g.open(&dir).unwrap().unwrap(); + let remotes = repo.remotes().unwrap(); + assert_eq!(remotes.len(), 1); + assert_eq!(remotes[0].name, "origin"); + assert!( + remotes[0] + .fetch_url + .as_deref() + .unwrap() + .contains("remote.git") + ); + assert_eq!( + repo.head().unwrap().upstream.as_deref(), + Some("origin/main") + ); +} + +#[test] +fn clone_refuses_a_full_folder_and_cleans_up_after_failure() { + let Some(g) = installed() else { return }; + let tmp = tempfile::tempdir().unwrap(); + let full = tmp.path().join("full"); + std::fs::create_dir(&full).unwrap(); + std::fs::write(full.join("x"), "x").unwrap(); + let bare = bare_with_commit(tmp.path()); + let err = g + .clone_repo(bare.to_str().unwrap(), &full, &Network::default()) + .unwrap_err(); + assert!(err.is_invalid_input(), "{err}"); + assert!(full.join("x").exists()); + + let missing = tmp.path().join("missing.git"); + let target = tmp.path().join("new"); + let err = g + .clone_repo(missing.to_str().unwrap(), &target, &Network::default()) + .unwrap_err(); + let VcsError::Git(message) = err else { + panic!("{err:?}") + }; + assert!( + message.starts_with("The remote repository was not found"), + "{message}" + ); + assert!(!target.exists()); + + let relative = g.clone_repo( + bare.to_str().unwrap(), + Path::new("rel"), + &Network::default(), + ); + assert!(relative.unwrap_err().is_invalid_input()); + let option = g.clone_repo( + "--upload-pack=x", + &tmp.path().join("o"), + &Network::default(), + ); + assert!(option.unwrap_err().is_invalid_input()); +} + +#[test] +fn a_cancelled_clone_stops_and_leaves_nothing() { + let Some(g) = installed() else { return }; + let tmp = tempfile::tempdir().unwrap(); + let bare = bare_with_commit(tmp.path()); + let target = tmp.path().join("cancelled"); + std::fs::create_dir(&target).unwrap(); + let cancel = Cancel::new(); + cancel.cancel(); + let net = Network { + progress: None, + cancel: Some(cancel), + }; + let err = g + .clone_repo(bare.to_str().unwrap(), &target, &net) + .unwrap_err(); + assert_eq!(err, VcsError::Cancelled); + assert!(target.is_dir(), "an empty folder given by the user stays"); + assert_eq!(std::fs::read_dir(&target).unwrap().count(), 0); +} + +#[test] +fn push_sets_the_upstream_once_and_a_diverged_push_is_rejected() { + let Some(g) = installed() else { return }; + let tmp = tempfile::tempdir().unwrap(); + let bare = bare_with_commit(tmp.path()); + let first = clone_open(&g, &bare, tmp.path(), "first"); + let second = clone_open(&g, &bare, tmp.path(), "second"); + + git(first.root(), &["switch", "-q", "-c", "feature"]); + commit(first.root(), "f.txt", "feature\n"); + let (net, lines) = recording(); + let pushed = first.push(None, &net).unwrap(); + assert_eq!(pushed.remote, "origin"); + assert_eq!(pushed.branch, "feature"); + assert!(pushed.upstream_set); + assert_eq!( + first.head().unwrap().upstream.as_deref(), + Some("origin/feature") + ); + assert!(!lines.lock().unwrap().is_empty()); + commit(first.root(), "f.txt", "feature 2\n"); + let again = first.push(None, &Network::default()).unwrap(); + assert!(!again.upstream_set); + assert_eq!( + git(&bare, &["rev-parse", "feature"]), + git(first.root(), &["rev-parse", "HEAD"]) + ); + + git(first.root(), &["switch", "-q", "main"]); + commit(first.root(), "a.txt", "from first\n"); + first.push(None, &Network::default()).unwrap(); + commit(second.root(), "b.txt", "from second\n"); + let err = second.push(None, &Network::default()).unwrap_err(); + let VcsError::Git(message) = err else { + panic!("{err:?}") + }; + assert!(message.starts_with("The push was rejected"), "{message}"); + assert!(message.contains("rejected")); +} + +#[test] +fn fetch_then_pull_by_merge_and_by_rebase() { + let Some(g) = installed() else { return }; + let tmp = tempfile::tempdir().unwrap(); + let bare = bare_with_commit(tmp.path()); + let first = clone_open(&g, &bare, tmp.path(), "first"); + let second = clone_open(&g, &bare, tmp.path(), "second"); + + commit(first.root(), "a.txt", "two\n"); + first.push(None, &Network::default()).unwrap(); + second.fetch(None, &Network::default()).unwrap(); + assert_eq!(second.head().unwrap().behind, Some(1)); + second.fetch(Some("origin"), &Network::default()).unwrap(); + second.pull(PullMode::Merge, &Network::default()).unwrap(); + assert_eq!(read(&second.root().join("a.txt")), "two\n"); + + commit(first.root(), "a.txt", "three\n"); + first.push(None, &Network::default()).unwrap(); + commit(second.root(), "local.txt", "mine\n"); + second.pull(PullMode::Rebase, &Network::default()).unwrap(); + assert_eq!(read(&second.root().join("a.txt")), "three\n"); + let parents = git(second.root(), &["rev-list", "--parents", "-n", "1", "HEAD"]); + assert_eq!(parents.split(' ').count(), 2, "rebased, no merge commit"); + let head = second.head().unwrap(); + assert_eq!((head.ahead, head.behind), (Some(1), Some(0))); + + let err = second.fetch(Some("-x"), &Network::default()).unwrap_err(); + assert!(err.is_invalid_input()); +} + +#[test] +fn pull_without_upstream_explains_itself() { + let Some(g) = installed() else { return }; + let tmp = tempfile::tempdir().unwrap(); + let bare = bare_with_commit(tmp.path()); + let repo = clone_open(&g, &bare, tmp.path(), "work"); + git(repo.root(), &["switch", "-q", "-c", "lonely"]); + let err = repo.pull(PullMode::Merge, &Network::default()).unwrap_err(); + let VcsError::Git(message) = err else { + panic!("{err:?}") + }; + assert!( + message.starts_with("The current branch has no upstream"), + "{message}" + ); +} + +#[test] +fn remotes_are_added_changed_and_removed_with_redacted_urls() { + let Some(g) = installed() else { return }; + let tmp = tempfile::tempdir().unwrap(); + let bare = bare_with_commit(tmp.path()); + let repo = clone_open(&g, &bare, tmp.path(), "work"); + repo.remote_add("fork", "https://me:s3cret@example.invalid/a/b.git") + .unwrap(); + let remotes = repo.remotes().unwrap(); + let fork = remotes.iter().find(|r| r.name == "fork").unwrap(); + assert_eq!( + fork.fetch_url.as_deref(), + Some("https://***@example.invalid/a/b.git") + ); + assert!(repo.remote_add("fork", "x").is_err(), "exists already"); + repo.remote_set_url("fork", "git@example.invalid:a/b.git") + .unwrap(); + let fork_url = repo + .remotes() + .unwrap() + .into_iter() + .find(|r| r.name == "fork") + .and_then(|r| r.fetch_url); + assert_eq!(fork_url.as_deref(), Some("git@example.invalid:a/b.git")); + repo.remote_remove("fork").unwrap(); + assert_eq!(repo.remotes().unwrap().len(), 1); + let err = repo.remote_remove("fork").unwrap_err(); + assert!(matches!(err, VcsError::Git(_)), "{err:?}"); + assert!( + repo.remote_add("bad name", "x") + .unwrap_err() + .is_invalid_input() + ); + assert!( + repo.remote_add("ok", "ext::sh -c x") + .unwrap_err() + .is_invalid_input() + ); + + repo.remote_remove("origin").unwrap(); + git(repo.root(), &["switch", "-q", "-c", "nowhere"]); + let err = repo.push(None, &Network::default()).unwrap_err(); + assert!(err.to_string().contains("no remote to push to"), "{err}"); +} + +#[test] +fn cancel_stops_a_running_push() { + let Some(g) = installed() else { return }; + let tmp = tempfile::tempdir().unwrap(); + let bare = bare_with_commit(tmp.path()); + let hook = bare.join("hooks").join("pre-receive"); + std::fs::write(&hook, "#!/bin/sh\nsleep 30\n").unwrap(); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + std::fs::set_permissions(&hook, std::fs::Permissions::from_mode(0o755)).unwrap(); + } + let repo = clone_open(&g, &bare, tmp.path(), "work"); + commit(repo.root(), "a.txt", "slow\n"); + let cancel = Cancel::new(); + let net = Network { + progress: None, + cancel: Some(cancel.clone()), + }; + let stopper = std::thread::spawn(move || { + std::thread::sleep(Duration::from_millis(500)); + cancel.cancel(); + }); + let started = Instant::now(); + let err = repo.push(None, &net).unwrap_err(); + stopper.join().unwrap(); + assert_eq!(err, VcsError::Cancelled); + assert!( + started.elapsed() < Duration::from_secs(10), + "took {:?}", + started.elapsed() + ); +} diff --git a/docs/architecture.md b/docs/architecture.md index 78f4940..f1ee049 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -1,6 +1,6 @@ # Architecture -> **Status:** M0 decisions recorded, M1 workspace, desktop bridge, tabs and editor, message catalog, engine construction, settings, themes, the recovery journal, notifications, UI commands, the palette, the settings dialog, the app icon and the syntax layer documented; M2 highlighting and `editor.reveal`, plus the search backend, the Find tool window, the symbol index, the Structure tool window and the go-to popup; M3 CLI and single instance, LSP client library, toolchain (PHP detection, Phpactor download), the app menu, MCP server, autosave and `flushDirty`, integrated terminal; engine ↔ Phpactor wiring and diagnostics (M3.3), pending outcomes (M3.5), completion, hover and signature help (M3.4); C5 child process groups (H1), settings dialog string and path controls and assist settings (H4), the banner-aligned themes and diagnostic/VCS tokens (H5), Phpactor UX: fast tick, messages, progress, `php -l` (H3); Git backend (M4.1), Changes tool window (M4.2); line changes, gutter markers and status colours (M4.4), diff viewer (M4.3), branches (M4.5) · **Last updated:** 2026-10-26 +> **Status:** M0 decisions recorded, M1 workspace, desktop bridge, tabs and editor, message catalog, engine construction, settings, themes, the recovery journal, notifications, UI commands, the palette, the settings dialog, the app icon and the syntax layer documented; M2 highlighting and `editor.reveal`, plus the search backend, the Find tool window, the symbol index, the Structure tool window and the go-to popup; M3 CLI and single instance, LSP client library, toolchain (PHP detection, Phpactor download), the app menu, MCP server, autosave and `flushDirty`, integrated terminal; engine ↔ Phpactor wiring and diagnostics (M3.3), pending outcomes (M3.5), completion, hover and signature help (M3.4); C5 child process groups (H1), settings dialog string and path controls and assist settings (H4), the banner-aligned themes and diagnostic/VCS tokens (H5), Phpactor UX: fast tick, messages, progress, `php -l` (H3); Git backend (M4.1), Changes tool window (M4.2); line changes, gutter markers and status colours (M4.4), diff viewer (M4.3), branches (M4.5); remotes: fetch, pull, push, clone and remote management (M4.10) · **Last updated:** 2026-10-28 ## Overview @@ -561,7 +561,7 @@ Watched directories: the VFS watcher is non-recursive, so `project.children` wat ### Git backend (M4.1) -- **Owner decision D20:** Git runs through the user's installed `git` (minimum 2.30), found through the setting `git.executable` (string, section `git`; `""` means `git` from `PATH`). No network operation exists (no fetch, pull, push), and no call can open a credential or terminal prompt. +- **Owner decision D20:** Git runs through the user's installed `git` (minimum 2.30), found through the setting `git.executable` (string, section `git`; `""` means `git` from `PATH`). No call can open a terminal prompt. Network calls (fetch, pull, push, clone) came with M4.10 under D23; see Remotes (M4.10). - **Crate `lutra-vcs` (L3, `crates/vcs`):** - `runner::run` is the only way git starts, and `runner::spawn` the only place a process starts, through H1's `lutra_proc::spawn`, so git and its hooks die with Lutra. Arguments are an array, never a shell. Every call gets `--no-pager --literal-pathspecs -c core.quotepath=false -c color.ui=false`, `GIT_TERMINAL_PROMPT=0`, `GCM_INTERACTIVE=never`, `GIT_OPTIONAL_LOCKS=0` for reads, no console window on Windows, and the variables that redirect git (`GIT_DIR`, `GIT_WORK_TREE`, `GIT_INDEX_FILE`, …) removed. - Timeouts: 30 s; commit, checkout and branch creation 120 s (hooks). A call past its timeout, or whose stdout passes its cap, is killed (a guard kills and reaps the child on every early return). @@ -660,6 +660,43 @@ Watched directories: the VFS watcher is non-recursive, so `project.children` wat - **Commands** (UI commands, menu group Git, no default shortcuts): `vcs.branches` (Git › Branches…), `vcs.newBranch {name?, checkout?}` (Git › New Branch…; with a `name` it creates the branch without the popup, checked out unless `checkout: false`, and toasts a refusal), `vcs.checkout {branch}` (without `branch`, as from the palette, it opens the popup; a `branch` that is no non-blank string toasts an error). All are enabled while `git.branches` is (an open project). - **Tests:** `ui/tests/vcs/Branches.test.ts` (16 Vitest tests against a fake engine: parsing, filter, head label, the item's states, popup order and marking, filtering, keyboard, checkout after `flushDirty` through `runWaiting`, a refusal toast, a cancelled checkout, creating with a blank name, a refusal and success, Escape back to the list, the commands' registration, params and enabled state). +### Remotes (M4.10) + +- **Owner decision D23 (2026-10-26):** Lutra may fetch, pull, push and clone through the user's git. Any HTTPS or SSH remote works (GitHub, GitLab, Forgejo, Bitbucket, a self-hosted server), and so does a local path, because git does the transport. +- **Credentials stay with git.** HTTPS sign-in goes through git's credential helper. It may show its own window, such as Git Credential Manager's: network calls do not set `GCM_INTERACTIVE=never`, every other call still does. SSH remotes use the user's `ssh` with its agent and keys, `~/.ssh/config` and `known_hosts`; Lutra sets no `GIT_SSH_COMMAND` and touches no key. Terminal prompts stay off (`GIT_TERMINAL_PROMPT=0`, stdin closed, no console). A remote that needs a password nobody supplies, a passphrase key without an agent, or an unknown host key therefore fails with a message instead of hanging. Lutra never reads, stores or logs a credential. +- **Redaction:** `runner::redact_urls` replaces the userinfo of every `scheme://user:secret@host` URL with `***` (`https://***@host/…`). It applies to every runner error message (`Output::message`), every progress line, and the URLs `git.remotes` returns. scp-like addresses (`git@host:path`) carry only a user name and stay as they are. Unit and property tests cover it. +- **Crate (`crates/vcs/src/remote.rs`, plus the runner):** + - `Call::network(cwd, args)`: a 30-minute timeout (`NETWORK_TIMEOUT`), a progress sink (stderr split at `\r`/`\n`, 1 KiB per line) and a `Cancel` flag. The runner polls the flag every 50 ms. When it is set, git and every process it started (`git-remote-https`, `ssh`, hooks) are killed through the `lutra_proc` group, and the call ends with `VcsError::Cancelled`. + - Every network call runs with `-c protocol.ext.allow=never`, and `check_url` refuses `ext::` URLs, blank URLs, URLs starting with `-`, control characters and anything over 4,096 bytes. Remote names are checked for `-`, whitespace and control characters; git checks the rest. Arguments follow `--end-of-options`. + - `Git::clone_repo(url, dir, net)` needs no repository. It runs `git clone --progress` in the parent folder, which must exist; `dir` must be absolute and missing or empty. A failed or cancelled clone removes what it left: the folder if the clone created it, else only its contents. + - `Repo::fetch(remote?)`: `fetch --progress `, or `--all`. `Repo::pull(mode)`: `pull --progress --no-rebase --no-edit` or `--rebase`. `Repo::push(remote?)`: the current branch with `--porcelain`. With an upstream (`branch..remote` and `.merge`) it goes there as `refs/heads/:`. Without one it goes to `` (the given one, else the only remote, else `origin`) as `refs/heads/:refs/heads/` with `--set-upstream`. Rejected refs (`!` lines) fail the push even if the exit status says otherwise. + - `Repo::remotes()` reads `config -z --get-regexp ^remote\..*\.(url|pushurl)$` in config order. `remote_add`, `remote_set_url` and `remote_remove` run `git remote …`. Changes hold the repository's mutation lock. + - **Clear messages:** `explain` puts one sentence before git's own text when the cause is known: a rejected push (pull first), a push the server refused (protected branch, hook), an unknown SSH host key, a failed sign-in (pointing at the credential helper and the ssh agent), a repository not found, a remote that cannot be reached, no upstream, local changes in the way, conflicts after a pull, diverged branches, a full clone target. +- **Engine (`crates/engine/src/cmd/git_remote.rs`):** pending outcomes on `lutra-git-remote` threads, which detect git and open the repository for each call. `git.clone` needs no project. + - `git.fetch {remote?}` → `{}`. + - `git.pull {mode?: merge|rebase}` → `{}`; the default comes from the setting `git.pull.mode` (enum `merge`|`rebase`, default `merge`, section Git). + - `git.push {remote?}` → `{remote, branch, upstreamSet}`. + - `git.clone {url, dir}` → `{root}`. + - `git.remotes` → `{remotes: [{name, fetchUrl?, pushUrl?}]}`. + - `git.remoteAdd {name, url}`, `git.remoteSetUrl {name, url}`, `git.remoteRemove {name}` → `{}`. + - `git.cancel` (immediate) → `{cancelled: true}`; disabled while nothing runs. + - Bad URLs, names, modes and relative clone folders are `invalid_params`; git's refusals are `failed` with the explained text. + - One network call runs at a time; a second is refused while one runs. + - **Event `gitProgress {active, operation?, phase?, percent?}`:** sent from the pump when the running call's latest line changed (at most once per pump). `active: false` follows when the call ends; when the call failed, it arrives with the next pump. `gitChanged` follows every successful change in the open project. +- **UI (`ui/src/components/vcs/remote/`):** + - `remote.ts` is the Pinia store. + - `RemoteStatusItem.vue` goes in the status bar, before the branch item. While a call runs it shows "Fetching: Receiving objects 45%" with a Cancel button (`git.cancel`). Otherwise it shows `↑ahead ↓behind` against the upstream from the shared status store, hidden when in step. The item supplies the UI commands and mounts the dialogs. + - `vcs.fetch`, `vcs.pull` and `vcs.push` (Git › Fetch / Pull / Push) end in a toast: done, cancelled, or git's explained refusal. Pull runs `flushDirty()` first and cancels when a save fails. Push says when it set the upstream. + - `vcs.clone` (Git › Clone Repository…, also without a project) opens a dialog with a URL field and a folder field. "Browse…" uses the new `pick_dir` IPC, the shell's native folder picker. The folder is the chosen parent plus the repository's name and follows URL edits until it is typed by hand. Clone runs `git.clone`, then `project.open`. git's refusal stays in the dialog. Cancel stops a running clone. + - `vcs.manageRemotes` (Git › Manage Remotes…) lists the remotes with redacted URLs and a separate push URL. Each row offers "Edit URL" (the field starts empty when the URL is redacted, so `***` is never saved) and "Remove" (asked in the row). A form adds a remote. Refusals show in the dialog. + - Both dialogs are exclusive modals (`clone`, `remotes`). + - No default shortcuts. Commands are enabled with a project (clone always) and while no network call runs. +- **Tests:** + - `crates/vcs/tests/remote.rs`: 8 tests against local bare repositories, never the network. Clone with progress; a full or missing target and clean-up; a cancelled clone; push with and without upstream; a rejected push; fetch and pull by merge and rebase; pull without upstream; remote management with redacted URLs; cancel of a running push held by a sleeping `pre-receive` hook, which stops in well under 10 s. + - Unit and property tests of the redaction, the progress lines, `explain`, the URL and name checks, and the parsers. + - `crates/engine/tests/git_remote.rs`: 3 tests. Clone without a project, remotes, push with upstream and `gitChanged`, fetch and pull; cancel and the one-call rule; bad params. + - `ui/tests/vcs/Remote.test.ts`: 11 Vitest tests. + ## Decisions (M0) | Question | Decision | Why | @@ -678,6 +715,7 @@ Watched directories: the VFS watcher is non-recursive, so `project.children` wat | Date | Change | Summary | |---|---|---| | 2026-10-28 | minor | M4.6: history backend (`git.log`, `git.showCommit`, `git.showAt`), the shared hook for new git commands, the Log tab, diffs at a commit | +| 2026-10-28 | minor | M4.10: remotes (`Call::network` with progress and cancel, URL redaction, `git.fetch/pull/push/clone/cancel/remotes/remoteAdd/remoteSetUrl/remoteRemove`, `gitProgress`, `git.pull.mode`, the clone and remotes dialogs) | | 2026-10-27 | minor | F3: `lutra_proc::spawn_suspended` (Phpactor) and `adopt` (ConPTY shell in a job), cancel superseded completions, syntax proptests compare leaf kinds | | 2026-10-27 | minor | C6 F1: remote branch checkout without a configured remote (`--no-track`), `git.lastMessage` and the Amend prefill, one shared git status store | | 2026-10-27 | minor | F2: `ui.screenshot` through WebView2 `CapturePreview` without focus; region fallback elsewhere; `--control` windows open unfocused | diff --git a/docs/gaps.md b/docs/gaps.md index c8f69fd..c4b7e8e 100644 --- a/docs/gaps.md +++ b/docs/gaps.md @@ -1,6 +1,6 @@ # Gaps -> **Status:** M0 closed G1; G2 under way (text buffer, engine buffers, tabs and editor in the app, recovery journal, autosave); G3 M1 part done (project tree, palette, settings dialog with string and path controls, themes) plus the app menu (M3.9); G4 under way (tree-sitter outline and folding library, Lezer highlighting, Structure tool window); G5 under way (search backend, Find tool window, symbol index, go-to popup); G6 under way (LSP client library, PHP detection, Phpactor download, engine wiring and diagnostics, completion and hover, definition, references and rename, Phpactor dies with Lutra, faster indexing, progress and messages, `php -l` on Windows); G7 under way (`lutra` CLI and single instance, MCP server, integrated terminal); G8 under way (Git backend, M4.1; Changes window, M4.2; gutter markers and status colours, M4.4; diff viewer, M4.3; branch widget and popup, M4.5); G14 English UI catalog in place · **Last updated:** 2026-10-26 +> **Status:** M0 closed G1; G2 under way (text buffer, engine buffers, tabs and editor in the app, recovery journal, autosave); G3 M1 part done (project tree, palette, settings dialog with string and path controls, themes) plus the app menu (M3.9); G4 under way (tree-sitter outline and folding library, Lezer highlighting, Structure tool window); G5 under way (search backend, Find tool window, symbol index, go-to popup); G6 under way (LSP client library, PHP detection, Phpactor download, engine wiring and diagnostics, completion and hover, definition, references and rename, Phpactor dies with Lutra, faster indexing, progress and messages, `php -l` on Windows); G7 under way (`lutra` CLI and single instance, MCP server, integrated terminal); G8 under way (Git backend, M4.1; Changes window, M4.2; gutter markers and status colours, M4.4; diff viewer, M4.3; branch widget and popup, M4.5; remotes, M4.10); G14 English UI catalog in place · **Last updated:** 2026-10-26 Every known shortfall, ranked. This is the work list: unless the owner gives a task, take the highest open gap. Hours are agent wall-clock hours for one agent, all estimated. @@ -13,7 +13,7 @@ Every known shortfall, ranked. This is the work list: unless the owner gives a t | G5 | Search and navigation: find in files, go to file/class/symbol, first index | M2 | ~35 h | | G6 | Language server: completion, definition, hover, rename via Phpactor (~~PHP detection and Phpactor download~~, M3.1; ~~engine wiring and diagnostics~~, M3.3; ~~completion, hover, signature help~~, M3.4; ~~definition, references, rename~~, M3.5) | M3 | ~15 h | | G7 | ~~Terminal~~ (M3.6: PTY sessions, xterm.js sub-tabs in the bottom panel; settings, persistence, links and search still open), ~~`lutra` CLI and single instance~~ (M3.7: open folders and files at a line, `--wait`, forwarding a second start, token-protected rendezvous), ~~MCP server~~ (M3.8: `lutra mcp` with list/execute every command, screenshot, read buffer, recent events, open buffers as resources; still missing: resource and tool change notifications, progress for long commands), files outside the open project (the CLI refuses them; needs out-of-project buffers or a second window), `lutra diff` | M3 | ~16 h | -| G8 | Git integration: ~~backend through the installed git~~ (M4.1: status, stage/unstage, commit and amend, file contents at HEAD/index, branches, checkout, branch creation, `gitChanged`), ~~Changes window~~ (M4.2: staged/changes/untracked groups, stage and unstage per file and group, commit and amend after saving, amend prefilled with the last message (C6 F1), no chunk staging), ~~gutter markers and status colours~~ (M4.4: added/modified/deleted against the index, tree and tab colours; no chunk actions), ~~diff viewer~~ (M4.3: side by side against the index or HEAD, editable working copy, next/previous change; no unified view, chunk actions or other revisions), ~~branch popup~~ (M4.5: status-bar branch, popup with local and remote branches and a filter, checkout after saving, new branch from HEAD; no merge, rebase, rename or delete), ~~log~~ (M4.6: Log tab with graph lanes, filters, paging, commit details, diffs at a commit, file history; no date or branch filters, no actions on commits), blame, conflicts, stash, remotes | M4 | ~122 h | +| G8 | Git integration: ~~backend through the installed git~~ (M4.1: status, stage/unstage, commit and amend, file contents at HEAD/index, branches, checkout, branch creation, `gitChanged`), ~~Changes window~~ (M4.2: staged/changes/untracked groups, stage and unstage per file and group, commit and amend after saving, amend prefilled with the last message (C6 F1), no chunk staging), ~~gutter markers and status colours~~ (M4.4: added/modified/deleted against the index, tree and tab colours; no chunk actions), ~~diff viewer~~ (M4.3: side by side against the index or HEAD, editable working copy, next/previous change; no unified view, chunk actions or other revisions), ~~branch popup~~ (M4.5: status-bar branch, popup with local and remote branches and a filter, checkout after saving, new branch from HEAD; no merge, rebase, rename or delete), ~~log~~ (M4.6: Log tab with graph lanes, filters, paging, commit details, diffs at a commit, file history; no date or branch filters, no actions on commits), ~~remotes~~ (M4.10: fetch, pull by merge or rebase, push with the upstream set on the first push, clone, manage remotes, progress and cancel, explained refusals, any HTTPS or SSH server through git's credential helper and the ssh agent; no force push, tags, per-branch push targets or hosting features such as pull requests), blame, conflicts, stash | M4 | ~122 h | | G9 | Debugger, test runner, Composer, run configurations | M5 | ~300 h | | G10 | Own indexer, type inference, inspections, refactorings | M6 | ~800 h | | G11 | Stability: crash-free operation on real projects and machines (~~recovery journal~~, M1.7) | ongoing | ~95 h | @@ -55,6 +55,7 @@ G6 progress: M3.2 landed the LSP client library in `crates/lsp`: JSON-RPC framin | Date | Change | Summary | |---|---|---| | 2026-10-28 | patch | G8: log and file history (M4.6) | +| 2026-10-28 | minor | G8 shrunk: remotes (M4.10) | | 2026-10-27 | minor | G11 note: suspended Phpactor start, terminal shell in a job, completion cancel, syntax proptest (F3) | | 2026-10-27 | patch | G8: amend prefill, remote checkout fix, shared status store (C6 F1) | | 2026-10-27 | minor | Screenshot item shrunk: webview capture without focus on Windows (F2) | diff --git a/docs/roadmap.md b/docs/roadmap.md index 50258ec..71ca717 100644 --- a/docs/roadmap.md +++ b/docs/roadmap.md @@ -1,6 +1,6 @@ # Milestones -> **Status:** M0–M3 done, M4 in progress (C5: Git backend, Changes window, gutter markers and status colours, diff viewer, branches; C6: log) · **Last updated:** 2026-10-28 +> **Status:** M0–M3 done, M4 in progress (C5: Git backend, Changes window, gutter markers and status colours, diff viewer, branches; C6: log, remotes) · **Last updated:** 2026-10-28 Hours are agent wall-clock hours for one agent; M0 and M1 so far are measured, the rest estimated. Since cycle C1 (factor 0.07) M1–M3 were scaled ×0.1 to ×0.15 (measured factors: C1 0.07, C2 0.10, C3 0.14, C4 0.17; next cycle ×0.18); M4–M6 keep their first raw estimates (multiply by ~0.1 for the calibrated value). @@ -10,7 +10,7 @@ Hours are agent wall-clock hours for one agent; M0 and M1 so far are measured, t | M1 | Editor and project: open folder, tree, tabs, editor, save, recovery, palette, settings, themes | done 2026-10-16 (M1.1 text, M1.2 VFS, M1.5 tree UI, M1.3 engine workspace, M1.4 desktop bridge, M1.6 tabs and editor, M1.11 message catalog, M1.9a settings backend, M1.10 themes, M1.7 recovery journal, M1.12 notifications, M1.13 C1 follow-ups, M1.8 command palette and keymap, M1.14 app icon, M1.9b settings dialog; C2 live checks pending, gaming mode) | ~5.6 (measured: C1 2.5, C2 3.1) | | M2 | Syntax and search: highlighting, structure, find in files, go to file/class/symbol | done 2026-10-17 (C3: M2.1 tree-sitter syntax, M2.2 highlighting + `editor.reveal`, M2.3 search backend, M2.4 Find tool window, M2.5 symbol index + go-to commands, M2.6 go-to popup, M2.7 Structure tool window) | ~6.1 (measured) | | M3 | LSP, terminal, CLI: completion, diagnostics, go to definition, hover, rename; terminal; `lutra` CLI and MCP — **MVP** | done 2026-10-22 (C4: M3.1 PHP detection and Phpactor download, M3.2 LSP client, M3.3 engine wiring and diagnostics, M3.4 completion, hover and signature help, M3.5 definition, references and rename, M3.6 integrated terminal, M3.7 CLI and single instance, M3.8 MCP server, M3.9 app menu, M3.10 autosave; live check with real key presses pending) | ~14 (measured) | -| M4 | Git: status, diff, commit, branches, log, blame, conflicts | in progress (C5: M4.1 backend, M4.2 Changes window, M4.4 gutter markers and status colours, M4.3 diff viewer, M4.5 branches done; C6: M4.6 log done) | ~122 | +| M4 | Git: status, diff, commit, branches, log, blame, conflicts | in progress (C5: M4.1 backend, M4.2 Changes window, M4.4 gutter markers and status colours, M4.3 diff viewer, M4.5 branches done; C6: M4.6 log, M4.10 remotes done) | ~122 | | M5 | Debugger (Xdebug via DAP), PHPUnit/Pest runner, Composer, run configurations | not started | ~300 | | M6 | Own indexer and inspections; Symfony, Laravel, Shopware, WordPress support | not started | ~1,100 | | M7 | Workbench and tools: Deployment (FTP/SFTP/local mappings, upload/download, compare with server, save before deploy, optional upload on save per server), Local History, diff tool (files/folders), scratch files, macros, keymap editor, user themes, plug-in system, Database tools and SQL, HTTP Client, SSH/Docker/Vagrant/WSL, File Watchers, external tools, TODO view, tasks and contexts | not started | ~900 | @@ -35,6 +35,7 @@ By the raw estimates the alpha gate needs M1–M4 plus cross-platform verificati | Date | Change | Summary | |---|---|---| | 2026-10-28 | minor | M4.6 log and file history | +| 2026-10-28 | minor | M4.10 remotes (fetch, pull, push, clone, manage remotes) | | 2026-10-26 | minor | M4.5 branch widget and popup | | 2026-10-26 | minor | M4.3 diff viewer | | 2026-10-25 | minor | M4.4 gutter markers and status colours | diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index c87abfe..e44f040 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -1,6 +1,6 @@ # Target-app parity -> **Status:** M1 done (text buffer, VFS, engine workspace, project tree, tabs and a plain-text editor, recovery, palette, settings and themes in the app); M2 started: PHP syntax highlighting, find in files with its tool window, the symbol index and the go-to popup, the Structure tool window; M3 started: the integrated terminal, go to definition, find references and rename through Phpactor; M4 started: Git backend (M4.1), Changes tool window (M4.2), diff viewer (M4.3), branches (M4.5) · **Last updated:** 2026-10-26 · **Target:** PhpStorm 2026.x +> **Status:** M1 done (text buffer, VFS, engine workspace, project tree, tabs and a plain-text editor, recovery, palette, settings and themes in the app); M2 started: PHP syntax highlighting, find in files with its tool window, the symbol index and the go-to popup, the Structure tool window; M3 started: the integrated terminal, go to definition, find references and rename through Phpactor; M4 started: Git backend (M4.1), Changes tool window (M4.2), diff viewer (M4.3), branches (M4.5), remotes (M4.10) · **Last updated:** 2026-10-28 · **Target:** PhpStorm 2026.x ## How this is measured @@ -22,7 +22,7 @@ Hours are the estimated agent wall-clock hours for one agent to reach behaviour | Inspections and quick-fixes | 8 | 5% (est.) | Lutra's own inspections and their settings, severities per inspection, quick-fixes (LSP code actions), suppression, inspection profiles, running inspections on a scope, the editor's problem counter and scrollbar marks, next/previous problem, a current-file Problems view and severity filters, external tools (PHPStan, Psalm, PHP_CodeSniffer) | ~287 (est.) | H3: PHP syntax errors on Windows from Lutra's own `php -l` (on open and save, off the engine lock, killed with Lutra), merged into the editor underlines and the Problems tab with source `php -l`; the language server's messages as notifications and its log as `lsp.log`. Tests: `crates/engine/tests/lsp_ux.rs`, unit tests incl. a parser proptest. M3.3: diagnostics from Phpactor (worse reflection: missing members, unknown classes, …) as wavy underlines, gutter markers and hover messages in the editor (`ui/src/components/editor/lint.ts`, tokens only), and a Problems tab in the bottom panel grouped by file with click to reveal (`ui/src/components/problems/`, `problems.open` `Mod+Alt+P`); engine `diagnosticsChanged`, `diagnostics.list`, `lspStatus` (`crates/engine/src/lsp/`). Tests: 10 engine tests against a fake server incl. a sync proptest, a TCP test, 16 Vitest tests, an ignored test against the real PHAR. Checked in the app on Windows 11 with the real Phpactor (port 7933): five errors underlined and listed; first diagnostics 2.7 s after `file.open`, 2.7–2.9 s after an edit (debug build, measured, incl. Phpactor's 1 s debounce). Graded against `plan/phpstorm/11-problems.md`: the depth comes from Phpactor, Lutra has no inspections of its own | | Refactoring | 7 | 3% (est.) | rename: preview of all changes, comments/strings and text occurrences, scope, renaming files with classes, one undo across files (closed files written on disk are not undone by the editor); extract variable/method/constant, inline, move, change signature, safe delete, pull up/push down, introduce parameter | ~244 (est.) | M3.5: Rename (`lsp.prepareRename` then `lsp.rename`, F2) through Phpactor with an inline field over the symbol that shows how many uses in how many files change. The workspace edit is checked completely first (inside the project, ranges fit, open buffers unchanged since the request, no file operations); then closed files are written atomically through the VFS (written ones restored if a later write fails), open buffers change through `buffer.edit` (dirty, one undo step each in the editor), Phpactor reindexes the written files (sent as `didOpen`/`didSave`/`didClose`; its index followed in 140 ms, measured) and a summary notification follows. Live: renaming a method used in 3 files took 28 ms (open buffer + 2 closed files, CRLF kept). Tests: `crates/engine/tests/lsp_nav.rs` (multi-file rename incl. a closed CRLF file, an edit outside the project refused, a bad range refused, a buffer edited meanwhile refused; ignored real-Phpactor rename and rename back), Vitest `ui/tests/nav/`. | | Code style and formatting | 4 | 0% | everything | ~120 (est.) | — | -| Git | 8 | 9% (est.) | Changes window: chunk and line staging, changelists, recent messages and a message template, pre-commit checks, skipping hooks, commit-and-push, a tree view and grouping, a context menu (rollback, delete, ignore); gutter markers: actions (show the old text, revert or stage a chunk, next/previous change), markers against HEAD; directory status colours; diff viewer: a unified view, three-way merge, revert or stage a chunk from the diff, comparing with any revision or branch, ignore-whitespace options, folders; branches: merge, rebase, rename, delete, compare, favourites, incoming/outgoing markers, a branch from another start point; log: date and branch filters, several selected commits, cherry-pick, revert, reset, checkout or new branch from a commit, compare two commits, history of a selection; blame, conflicts, stash, remotes (C6); ignored files in the status | ~122 (est.) | C6 M4.6: a Log tab in the bottom panel (`vcs.showLog`, menu Git › Log) lists all branches' commits in topological order with graph lanes, ref badges, author and date, 100 per page loaded as the list scrolls; message and author filters; the selected commit's message and changed files, each opening a read-only "Diff: ()" at that commit against its parent (renames followed); history of a file or folder (`vcs.showHistory`: menu, the tree's context menu, or the active editor tab) follows renames. Backend `git.log`, `git.showCommit`, `git.showAt`. Tests: `crates/vcs/tests/log.rs`, `crates/engine/tests/git_log.rs`, Vitest `ui/tests/vcs/Log.test.ts` (21). C6 F1: Amend fills an empty message box with the last commit message (`git.lastMessage`); checking out a remote branch whose remote is not configured gives an untracked local branch and a clean tree instead of a half-done checkout; one shared status store, so a `gitChanged` costs one `git.status` for the Changes window, the colours and the branch item. M4.5: a status-bar item shows the current branch, or the short hash when detached (`ui/src/components/vcs/branches/`); clicking it or `vcs.branches` (menu Git › Branches…) opens a popup with a filter field, "New branch…", the local branches (current marked, upstream shown) and the remote branches, keyboard-driven; choosing one runs `vcs.checkout {branch}`, which saves dirty buffers through `flushDirty()` first, cancels when a save fails and toasts git's refusal with the branch name; a remote branch checks out its tracking branch. "New branch…" / `vcs.newBranch {name?, checkout?}` creates one from HEAD (checked out by default) and keeps git's refusal next to the name. Tests: 16 Vitest tests (`ui/tests/vcs/Branches.test.ts`). M4.3: `vcs.showDiff {path, staged?}` (`Mod+Alt+Shift+D`, menu Git › Show Diff, double-click in Changes, the project tree's context menu, the palette) opens a "Diff: " tab in the editor area: `@codemirror/merge` side by side, the staged version (or the last commit for a staged row) read-only on the left, the working copy editable on the right through its normal engine buffer (saves, autosave, the journal), changed lines and words tinted with `--lt-vcs-*`, unchanged stretches collapsed, the left side re-read on `gitChanged`; next/previous change (`Alt+Shift+]` / `Alt+Shift+[`); notices for binary, too large, failed and deleted files (`ui/src/components/vcs/diff/`, 21 Vitest tests). M4.2: Changes tab in the bottom panel (`ui/src/components/vcs/changes/`): Staged / Changes / Untracked groups from `git.status` (re-read on `gitChanged`), stage and unstage per file and per group with checkboxes (a staged rename passes both paths), keyboard navigation with Space to stage, double-click or Enter opens through `vcs.showDiff` when the diff viewer provides it, else the file; a commit message box with Amend and Commit (also `Mod+Enter`), which runs `flushDirty()` first, cancels when a save fails and toasts the short hash; hints for a missing git and a folder without a repository; `vcs.openChanges` `Mod+Alt+Shift+G`, `vcs.commit` `Mod+Alt+Shift+M`, `vcs.refresh`. Tests: 19 Vitest tests (`ui/tests/vcs/ChangesToolWindow.test.ts`). M4.4: change markers in the editor gutter (added, modified, deleted against the index, 150 ms after typing and on every `gitChanged`, CRLF-safe) through `git.lineChanges` (line diff in Rust off the lock, index text cached and dropped on `gitChanged`); file names in the project tree and the editor tabs coloured by Git status (`--lt-vcs-*`), ignored entries in the ignored colour inside a repository. Tests: `crates/engine/tests/git_lines.rs` (real temp repositories), unit and property tests, Vitest (`ui/tests/editor/gitGutter.test.ts`, `ui/tests/vcs/vcsStatus.test.ts`). M4.1 (backend, reachable through the palette, control channel and MCP): `git.status`, `git.stage`, `git.unstage` (also before the first commit), `git.commit {message, amend}` with hooks, `git.show` at HEAD or the index, `git.branches`, `git.checkout` (remote → tracking branch), `git.createBranch`; `gitChanged` after changes and from a `.git` poll plus the project watcher. Runs the installed git ≥ 2.30 (`git.executable`), porcelain v2 / `-z` only, no prompts, timeouts and caps, off the engine lock. Tests against real temp repositories (`crates/vcs/tests/repo.rs`, `crates/engine/tests/git.rs`). Graded against `plan/phpstorm/16-vcs-basics.md`: committing, staging and switching or creating branches work from the UI, and changed lines and files are marked (read-only markers, no chunk actions); diff, log, blame and remotes do not yet. | +| Git | 8 | 11% (est.) | Changes window: chunk and line staging, changelists, recent messages and a message template, pre-commit checks, skipping hooks, commit-and-push, a tree view and grouping, a context menu (rollback, delete, ignore); gutter markers: actions (show the old text, revert or stage a chunk, next/previous change), markers against HEAD; directory status colours; diff viewer: a unified view, three-way merge, revert or stage a chunk from the diff, comparing with any revision or branch, ignore-whitespace options, folders; branches: merge, rebase, rename, delete, compare, favourites, incoming/outgoing markers, a branch from another start point; log: date and branch filters, several selected commits, cherry-pick, revert, reset, checkout or new branch from a commit, compare two commits, history of a selection; blame, conflicts, stash (C6); remotes: force push, tags, a push dialog with the outgoing commits and a target branch, hosting features (pull requests, issues), a check against a real hosted server; ignored files in the status | ~122 (est.) | M4.10: fetch, pull (merge or rebase per `git.pull.mode`, open files saved first) and push (upstream set on the first push) as `git.fetch/pull/push` and Git › Fetch / Pull / Push, with `gitProgress` in the status bar, Cancel (`git.cancel`, git and its children killed) and explained refusals (rejected push, sign-in, unknown host key, not found, unreachable, no upstream, conflicts); Git › Clone Repository… (`git.clone`, then `project.open`) and Git › Manage Remotes… (`git.remotes/remoteAdd/remoteSetUrl/remoteRemove`). Any HTTPS or SSH server through git's credential helper (its window allowed) and the ssh agent; no terminal prompts, `ext::` refused, URL userinfo redacted everywhere (D23). Tests against local bare repositories only (`crates/vcs/tests/remote.rs` 8, `crates/engine/tests/git_remote.rs` 3, Vitest `ui/tests/vcs/Remote.test.ts` 11); not checked against a hosted server. C6 M4.6: a Log tab in the bottom panel (`vcs.showLog`, menu Git › Log) lists all branches' commits in topological order with graph lanes, ref badges, author and date, 100 per page loaded as the list scrolls; message and author filters; the selected commit's message and changed files, each opening a read-only "Diff: ()" at that commit against its parent (renames followed); history of a file or folder (`vcs.showHistory`: menu, the tree's context menu, or the active editor tab) follows renames. Backend `git.log`, `git.showCommit`, `git.showAt`. Tests: `crates/vcs/tests/log.rs`, `crates/engine/tests/git_log.rs`, Vitest `ui/tests/vcs/Log.test.ts` (21). C6 F1: Amend fills an empty message box with the last commit message (`git.lastMessage`); checking out a remote branch whose remote is not configured gives an untracked local branch and a clean tree instead of a half-done checkout; one shared status store, so a `gitChanged` costs one `git.status` for the Changes window, the colours and the branch item. M4.5: a status-bar item shows the current branch, or the short hash when detached (`ui/src/components/vcs/branches/`); clicking it or `vcs.branches` (menu Git › Branches…) opens a popup with a filter field, "New branch…", the local branches (current marked, upstream shown) and the remote branches, keyboard-driven; choosing one runs `vcs.checkout {branch}`, which saves dirty buffers through `flushDirty()` first, cancels when a save fails and toasts git's refusal with the branch name; a remote branch checks out its tracking branch. "New branch…" / `vcs.newBranch {name?, checkout?}` creates one from HEAD (checked out by default) and keeps git's refusal next to the name. Tests: 16 Vitest tests (`ui/tests/vcs/Branches.test.ts`). M4.3: `vcs.showDiff {path, staged?}` (`Mod+Alt+Shift+D`, menu Git › Show Diff, double-click in Changes, the project tree's context menu, the palette) opens a "Diff: " tab in the editor area: `@codemirror/merge` side by side, the staged version (or the last commit for a staged row) read-only on the left, the working copy editable on the right through its normal engine buffer (saves, autosave, the journal), changed lines and words tinted with `--lt-vcs-*`, unchanged stretches collapsed, the left side re-read on `gitChanged`; next/previous change (`Alt+Shift+]` / `Alt+Shift+[`); notices for binary, too large, failed and deleted files (`ui/src/components/vcs/diff/`, 21 Vitest tests). M4.2: Changes tab in the bottom panel (`ui/src/components/vcs/changes/`): Staged / Changes / Untracked groups from `git.status` (re-read on `gitChanged`), stage and unstage per file and per group with checkboxes (a staged rename passes both paths), keyboard navigation with Space to stage, double-click or Enter opens through `vcs.showDiff` when the diff viewer provides it, else the file; a commit message box with Amend and Commit (also `Mod+Enter`), which runs `flushDirty()` first, cancels when a save fails and toasts the short hash; hints for a missing git and a folder without a repository; `vcs.openChanges` `Mod+Alt+Shift+G`, `vcs.commit` `Mod+Alt+Shift+M`, `vcs.refresh`. Tests: 19 Vitest tests (`ui/tests/vcs/ChangesToolWindow.test.ts`). M4.4: change markers in the editor gutter (added, modified, deleted against the index, 150 ms after typing and on every `gitChanged`, CRLF-safe) through `git.lineChanges` (line diff in Rust off the lock, index text cached and dropped on `gitChanged`); file names in the project tree and the editor tabs coloured by Git status (`--lt-vcs-*`), ignored entries in the ignored colour inside a repository. Tests: `crates/engine/tests/git_lines.rs` (real temp repositories), unit and property tests, Vitest (`ui/tests/editor/gitGutter.test.ts`, `ui/tests/vcs/vcsStatus.test.ts`). M4.1 (backend, reachable through the palette, control channel and MCP): `git.status`, `git.stage`, `git.unstage` (also before the first commit), `git.commit {message, amend}` with hooks, `git.show` at HEAD or the index, `git.branches`, `git.checkout` (remote → tracking branch), `git.createBranch`; `gitChanged` after changes and from a `.git` poll plus the project watcher. Runs the installed git ≥ 2.30 (`git.executable`), porcelain v2 / `-z` only, no prompts, timeouts and caps, off the engine lock. Tests against real temp repositories (`crates/vcs/tests/repo.rs`, `crates/engine/tests/git.rs`). Graded against `plan/phpstorm/16-vcs-basics.md`: committing, staging and switching or creating branches work from the UI, and changed lines and files are marked (read-only markers, no chunk actions); diff, log, blame and remotes do not yet. | | Terminal | 3 | 15% (est.) | renaming and splitting tabs, sessions kept across restarts, search in the output, clickable paths and links, drag and drop, command completion, terminal settings (shell path, start directory, environment, font, cursor shape, bell, copy on selection, middle-click paste), a key back to the editor, the PHP interpreter in `PATH`, a terminal in a file's directory, confirmation before killing a running process, moving a terminal into the editor | ~26 (est.) | M3.6: `crates/terminal` runs the shell in a pseudo terminal (`portable-pty`: ConPTY on Windows, Unix pty elsewhere) on its own reader, writer and waiter threads: UTF-8 split across reads joined, invalid bytes U+FFFD, a 1 MiB output buffer per session with backpressure (the shell waits, nothing is dropped while it runs), a non-blocking input queue, resize, kill, exit codes. Engine `terminal.create {cwd?, shell?, cols, rows}` / `terminal.input` / `terminal.resize` / `terminal.close`, events `terminalOutput` (≤ 64 KiB per terminal and 256 KiB per pump, round-robin) and `terminalExited {code}`; terminals close with the project and on `app.quit`. UI: a Terminal tab in the bottom panel (tabs next to Find), xterm.js with the fit addon, several terminals as sub-tabs (Local, Local (2)…) with close and new buttons, output replayed after collapsing the panel, copy (`Mod+Shift+C`, `Ctrl+C` with a selection) and paste (`Mod+Shift+V`, `Ctrl+V`), a focus rule that leaves plain Ctrl keys to the shell, Lutra's own ANSI colours (`--lt-terminal-*`, all text colours ≥ 4.5:1), `terminal.open` `Mod+Shift+T`. Tests: 8 session tests with real `cmd.exe` (echo, exit code, input, 6,000 multibyte characters without U+FFFD, 30,000 lines through a full buffer without losing a line, resize, kill, spawn errors) and UTF-8 proptests (`crates/terminal`), 3 engine tests (`crates/engine/tests/terminal.rs`), a TCP control test create → output → input → exit (`crates/control/tests/terminal.rs`), 20 Vitest tests with a mocked xterm (`ui/tests/terminal/`). Live check (Windows 11, debug build with bundled UI, control port 7936, temp project): `terminal.open` showed a pwsh prompt in the project root, first `terminalOutput` 0.2–0.5 s after the command; `php -v` printed PHP 8.4.24 within 0.5–1.5 s; `dir` listed the project; resizing the window changed the shell's width 144 → 190 → 105 → 142 columns (pwsh `WindowSize`); a second terminal as sub-tab "Local (2)"; Find and Terminal as bottom tabs; dark and light screenshots read; `exit 3` gave `terminalExited {code: 3}`; `app.quit` ended the app and its shells (measured). The live check found two bugs, fixed before the commit: xterm's own black viewport background showed as a bar under the last row, and programs printing ANSI white (PSReadLine) were unreadable on the light theme, so xterm now enforces a 4.5:1 minimum contrast. Typing, clicking and copy/paste were not driven in the app (the control channel has no input method); Vitest covers them. Graded against `plan/phpstorm/10-terminal.md` | | Run configurations | 2 | 0% | everything | ~50 (est.) | — | | Debugger | 5 | 0% | everything | ~120 (est.) | — | @@ -66,6 +66,7 @@ The project tree is the first user-visible feature (Project view 8%, estimated); | Date | Change | Summary | |---|---|---| | 2026-10-28 | patch | M4.6 evidence in the Git row (Log tab, commit details, diff at a commit, file history); score left to the PM | +| 2026-10-28 | minor | Git row: remotes (M4.10), 9% → 11% (est.) | | 2026-10-27 | patch | C6 F1 evidence in the Git row (Amend prefill, remote checkout fix, shared status store) | | 2026-10-26 | minor | C5 review: breadth 35/155 | | 2026-10-26 | minor | M4.5: Git 8% → 9% (est.), branch widget and popup; hours 128 → 122 | diff --git a/docs/tooling-parity.md b/docs/tooling-parity.md index 6c5cb11..f2cbbc5 100644 --- a/docs/tooling-parity.md +++ b/docs/tooling-parity.md @@ -1,6 +1,6 @@ # Tooling checklist -> **Status:** integrated terminal partial (M3.6), Git backend, Changes window, diff viewer and branches, log partial (M4.1, M4.2, M4.3, M4.5, M4.6) · **Last updated:** 2026-10-28 +> **Status:** integrated terminal partial (M3.6), Git backend, Changes window, diff viewer, branches, log and remotes partial (M4.1, M4.2, M4.3, M4.5, M4.6, M4.10) · **Last updated:** 2026-10-28 - [ ] Integrated terminal with tabs - M3.6 (2026-10-18): PTY sessions (pwsh → powershell → cmd on Windows, `$SHELL` or `/bin/sh` on Unix; cwd = project root) in the bottom panel's Terminal tab, several terminals as sub-tabs, xterm.js rendering with Lutra's colours, copy/paste, resize, exit codes; closed with the project. Missing: settings, persistence across restarts, split tabs, search, links, the PHP interpreter in `PATH`. @@ -9,6 +9,7 @@ - M4.3 (2026-10-26): a side-by-side diff of the working copy against the staged version or the last commit, editable on the right, with next/previous change. Missing: comparing with other revisions or branches, a unified view, three-way merge for conflicts. - M4.5 (2026-10-26): current branch in the status bar, switch to a local or remote branch (saving open files first, git's refusal shown) and create a branch from HEAD in the branches popup. Missing: merge, rebase, rename, delete, push/pull, log, blame, conflicts, stash. - M4.6 (2026-10-28): Log tab with graph lanes, message and author filters and lazy paging; commit details with each file's diff at that commit; history of a file from the tree, the editor or the menu. Missing: date and branch filters, actions on commits (cherry-pick, revert, reset, checkout), comparing two commits, blame, conflicts, stash, push/pull. + - M4.10 (2026-10-28): fetch, pull (merge or rebase, setting `git.pull.mode`; open files saved first) and push (upstream set on the first push), each with progress and Cancel in the status bar and explained refusals; Clone Repository… from any HTTPS or SSH URL or a path, then the project opens; Manage Remotes… (list with redacted URLs, add, change URL, remove). Sign-in stays with git's credential helper (its own window is allowed) and the ssh agent; terminal prompts are off, so a missing credential fails with a message. Tested against local bare repositories only. Missing: force push, tags, pushing to another branch name, fetch/pull of one branch, prune options, hosting features (pull requests, issues), checked against a real GitHub/GitLab/Forgejo server. - [ ] Composer: install, update, require, scripts - [ ] PHP interpreters: local, Docker - [ ] Xdebug via DAP: breakpoints, stepping, variables, watches @@ -23,6 +24,7 @@ | Date | Change | Summary | |---|---|---| | 2026-10-28 | minor | Git: log and file history (M4.6) | +| 2026-10-28 | minor | Git: fetch, pull, push, clone and remote management (M4.10) | | 2026-10-26 | minor | Git: branch widget, checkout and new branch (M4.5) | | 2026-10-26 | minor | Git: diff viewer (M4.3) | | 2026-10-25 | minor | Git: status, staging and commit from the Changes tab (M4.2) | diff --git a/docs/ui-parity.md b/docs/ui-parity.md index 1362a99..bf0a646 100644 --- a/docs/ui-parity.md +++ b/docs/ui-parity.md @@ -1,6 +1,6 @@ # UI parity checklist -> **Status:** main window layout (M1.5), project tree running against the engine (M1.4), editor tabs with a CodeMirror editor (M1.6), light and dark themes (M1.10), restore dialog for unsaved changes (M1.7), notification toasts (M1.12), command palette and global keymap (M1.8), settings dialog with search (M1.9b), syntax highlighting and `editor.reveal` (M2.2), Find tool window in the bottom panel (M2.4), go-to popup (M2.6), left stripe and Structure tool window (M2.7), app menu (M3.9), Terminal tool window and bottom panel tabs (M3.6), Problems tool window and editor diagnostics (M3.3), completion, quick documentation and parameter info (M3.4), string and path settings with completion and tooltip settings (H4), banner-aligned themes with diagnostic and VCS colours (H5), Changes tool window (M4.2), Git change markers and status colours (M4.4), diff viewer and the project tree's context menu (M4.3), branch widget and popup (M4.5) · **Last updated:** 2026-10-26 +> **Status:** main window layout (M1.5), project tree running against the engine (M1.4), editor tabs with a CodeMirror editor (M1.6), light and dark themes (M1.10), restore dialog for unsaved changes (M1.7), notification toasts (M1.12), command palette and global keymap (M1.8), settings dialog with search (M1.9b), syntax highlighting and `editor.reveal` (M2.2), Find tool window in the bottom panel (M2.4), go-to popup (M2.6), left stripe and Structure tool window (M2.7), app menu (M3.9), Terminal tool window and bottom panel tabs (M3.6), Problems tool window and editor diagnostics (M3.3), completion, quick documentation and parameter info (M3.4), string and path settings with completion and tooltip settings (H4), banner-aligned themes with diagnostic and VCS colours (H5), Changes tool window (M4.2), Git change markers and status colours (M4.4), diff viewer and the project tree's context menu (M4.3), branch widget and popup (M4.5), fetch/pull/push progress, the clone and remotes dialogs (M4.10) · **Last updated:** 2026-10-28 - [ ] Main window: tool window stripes, editor area, status bar, navigation bar - M2.7 (2026-10-17): a left stripe with one tab per left tool window (Project, Structure); the shown one is marked, clicking it again hides the window. Still missing: right docking, several bottom tool windows, drag to rearrange, navigation bar, layout persistence. @@ -45,11 +45,13 @@ - M4.4 (2026-10-25): the editor gutter shows a bar beside lines added or changed since the staged version and a notch where lines were deleted (tooltips from the catalog), updated 150 ms after typing and whenever Git changes; file names in the project tree and the tabs take the Git status colour (added, modified, deleted, untracked, conflicted; ignored entries inside a repository). Vitest. Missing: clicking a marker to see the old text, revert or stage the chunk, next/previous change, markers against HEAD, coloured directories. - M4.3 (2026-10-26): "Diff: " tabs in the editor area after the file tabs (`vcs.showDiff` `Mod+Alt+Shift+D`, menu Git › Show Diff; double-click or Enter in Changes; right click or Shift+F10 on a file in the project tree → Show Diff): the staged version or the last commit on the left, read-only, and the working copy on the right, editable like its own tab; a caption says what is compared; changed lines and words tinted with the VCS tokens, a change bar in each gutter, unchanged stretches collapsed to a clickable row; next/previous change `Alt+Shift+]` / `Alt+Shift+[` (wrapping); notices for binary, too large and deleted files and for git's errors. The project tree got its first context menu (Open, Show Diff; WAI-ARIA menu, keyboard). Vitest. Missing: unified view, revert/stage arrows between the sides, synchronised horizontal scrolling, whitespace and highlighting options, comparing with any revision, a diff window of its own, more tree context menu items. - M4.5 (2026-10-26): the status bar shows the current branch (Lucide `git-branch`), or the short hash when detached, hidden outside a repository. A click or `vcs.branches` (menu Git › Branches…) opens a popup above it: a filter field, "New branch…", Local (current one checked, upstream beside it) and Remote headings; Up/Down/Page keys, Enter or a click checks out, Escape or a click outside closes and returns the focus. "New branch…" switches to a name field with a "Check out" checkbox; git's refusal shows under the field. An exclusive modal. Vitest (16 tests). Missing: merge, rebase, rename, delete, compare with current, favourites, recent branches first, incoming/outgoing markers, a submenu per branch. + - M4.10 (2026-10-28): Git › Fetch / Pull / Push (`vcs.fetch`, `vcs.pull`, `vcs.push`, no default shortcuts) with a status-bar item left of the branch: "Fetching: Receiving objects 45%" and a Cancel button while one runs, otherwise `↑ahead ↓behind` against the upstream (hidden when in step); toasts for done, cancelled and git's explained refusal. Git › Clone Repository… (`vcs.clone`, also without a project): URL and folder fields, Browse… (native folder picker; the folder follows the repository name in the URL), git's refusal in the dialog, then the cloned project opens. Git › Manage Remotes… (`vcs.manageRemotes`): remotes with redacted URLs and push URLs, Edit URL, Remove (asked in the row), an add form. Both dialogs are exclusive modals. Vitest (11 tests). Missing: push dialog with the commits to push and a target branch, force push, tags, pull options per call, a clone dialog listing the user's repositories on a hosting service. ## Revision history | Date | Change | Summary | |---|---|---| +| 2026-10-28 | minor | Fetch/pull/push with progress, clone and remotes dialogs (M4.10) | | 2026-10-26 | minor | Branch widget and popup (M4.5) | | 2026-10-26 | minor | Diff viewer and the project tree's context menu (M4.3) | | 2026-10-25 | minor | Git change markers in the gutter, status colours in the tree and tabs (M4.4) | diff --git a/ui/src/commands/modals.ts b/ui/src/commands/modals.ts index 0194f99..24b55a6 100644 --- a/ui/src/commands/modals.ts +++ b/ui/src/commands/modals.ts @@ -18,14 +18,25 @@ * but the go-to open commands closes it. So is the app menu (M3.9): any * command but `menu.open` closes it. * - **Navigation (M3.5):** the definition picker and the inline rename field - * are exclusive modals too. So is the branches popup (M4.5). + * are exclusive modals too. So is the branches popup (M4.5), and so are + * "Clone Repository…" and "Manage Remotes…" (M4.10). */ import { defineStore } from "pinia"; import { ref, watch } from "vue"; import { useEditorStore } from "../stores/editor"; /** The exclusive modals. */ -export type ModalId = "palette" | "goto" | "menu" | "settings" | "closePrompt" | "navPicker" | "rename" | "branches"; +export type ModalId = + | "palette" + | "goto" + | "menu" + | "settings" + | "closePrompt" + | "navPicker" + | "rename" + | "branches" + | "clone" + | "remotes"; /** Store of the open exclusive modal. */ export const useModalsStore = defineStore("modals", () => { diff --git a/ui/src/commands/uiCommands.ts b/ui/src/commands/uiCommands.ts index c95a827..7a7e80a 100644 --- a/ui/src/commands/uiCommands.ts +++ b/ui/src/commands/uiCommands.ts @@ -37,9 +37,14 @@ export const UI_COMMANDS: readonly UiCommandSpec[] = [ { id: "view.toggleProjectTree", labelKey: "command.view.toggleProjectTree", menuPath: "View/Project", defaultShortcut: "Mod+1" }, { id: "vcs.branches", labelKey: "command.vcs.branches", menuPath: "Git/Branches…" }, { id: "vcs.checkout", labelKey: "command.vcs.checkout" }, + { id: "vcs.clone", labelKey: "command.vcs.clone", menuPath: "Git/Clone Repository…" }, { id: "vcs.commit", labelKey: "command.vcs.commit", menuPath: "Git/Commit…", defaultShortcut: "Mod+Alt+Shift+M" }, + { id: "vcs.fetch", labelKey: "command.vcs.fetch", menuPath: "Git/Fetch" }, + { id: "vcs.manageRemotes", labelKey: "command.vcs.manageRemotes", menuPath: "Git/Manage Remotes…" }, { id: "vcs.newBranch", labelKey: "command.vcs.newBranch", menuPath: "Git/New Branch…" }, { id: "vcs.openChanges", labelKey: "command.vcs.openChanges", menuPath: "Git/Changes", defaultShortcut: "Mod+Alt+Shift+G" }, + { id: "vcs.pull", labelKey: "command.vcs.pull", menuPath: "Git/Pull" }, + { id: "vcs.push", labelKey: "command.vcs.push", menuPath: "Git/Push" }, { id: "vcs.refresh", labelKey: "command.vcs.refresh", menuPath: "Git/Refresh Changes" }, { id: "vcs.showLog", labelKey: "command.vcs.showLog", menuPath: "Git/Log" }, { id: "vcs.showHistory", labelKey: "command.vcs.showHistory", menuPath: "Git/Show History" }, diff --git a/ui/src/components/StatusBar.vue b/ui/src/components/StatusBar.vue index dbfc4f4..6e9471f 100644 --- a/ui/src/components/StatusBar.vue +++ b/ui/src/components/StatusBar.vue @@ -2,13 +2,15 @@ /** * Status bar along the bottom of the main window. Shows the product name and * version, or the last engine error, and on the right the language server's - * progress (H3) and the Git branch (M4.5). + * progress (H3), a running fetch, pull, push or clone or the commits + * ahead and behind (M4.10), and the Git branch (M4.5). */ import { computed } from "vue"; import { t } from "../i18n"; import { useAppStore } from "../stores/app"; import LspProgressItem from "./problems/LspProgressItem.vue"; import BranchStatusItem from "./vcs/branches/BranchStatusItem.vue"; +import RemoteStatusItem from "./vcs/remote/RemoteStatusItem.vue"; const app = useAppStore(); @@ -29,6 +31,7 @@ const label = computed(() => { > {{ label }} + diff --git a/ui/src/components/vcs/remote/CloneDialog.vue b/ui/src/components/vcs/remote/CloneDialog.vue new file mode 100644 index 0000000..3dd7f5b --- /dev/null +++ b/ui/src/components/vcs/remote/CloneDialog.vue @@ -0,0 +1,223 @@ + + + + + diff --git a/ui/src/components/vcs/remote/RemoteStatusItem.vue b/ui/src/components/vcs/remote/RemoteStatusItem.vue new file mode 100644 index 0000000..905c9d8 --- /dev/null +++ b/ui/src/components/vcs/remote/RemoteStatusItem.vue @@ -0,0 +1,226 @@ + + + + + diff --git a/ui/src/components/vcs/remote/RemotesDialog.vue b/ui/src/components/vcs/remote/RemotesDialog.vue new file mode 100644 index 0000000..564315c --- /dev/null +++ b/ui/src/components/vcs/remote/RemotesDialog.vue @@ -0,0 +1,324 @@ + + + + + diff --git a/ui/src/components/vcs/remote/remote.ts b/ui/src/components/vcs/remote/remote.ts new file mode 100644 index 0000000..41501d0 --- /dev/null +++ b/ui/src/components/vcs/remote/remote.ts @@ -0,0 +1,486 @@ +/** + * Remotes (M4.10): fetch, pull, push, clone and the list of remotes, + * through the engine's `git.*` commands (owner decision D23). + * + * - **Credentials stay with git.** Lutra never asks for a password or a + * key: HTTPS sign-in is git's credential helper's (it may open its own + * window), SSH uses the ssh agent. A failed sign-in comes back as git's + * explained message, shown in a toast. + * - **Progress:** the running call's `gitProgress` events ({@link progress}) + * for the status-bar item, which offers Cancel (`git.cancel`). One + * network call runs at a time; the engine refuses a second. + * - **Fetch / pull / push:** each is a pending outcome (`runWaiting`) and + * ends in a toast: done, cancelled, or git's explained refusal. Pull + * saves every dirty buffer first, so git sees what the editor shows; a + * failed save cancels it. Push names the remote and branch, and says + * when it set the upstream. + * - **Clone:** the dialog's URL and folder go to `git.clone`; the folder + * is proposed as the chosen parent plus the repository's name from the + * URL. After the clone the project opens (`project.open`). + * - **Manage remotes:** `git.remotes` (URLs come redacted), add, change + * URL and remove; refusals stay in the dialog. + */ +import { defineStore } from "pinia"; +import { computed, ref, shallowRef } from "vue"; +import { onEngineEvent, run, runWaiting, type EngineEventPayload, type JsonValue } from "../../../ipc"; +import { useAutosaveStore } from "../../../stores/autosave"; +import { useNotificationsStore } from "../../../stores/notifications"; + +/** The engine's text for a cancelled call (`VcsError::Cancelled`). */ +export const CANCELLED = "the Git operation was cancelled"; + +/** Most remotes kept from one answer. */ +export const MAX_REMOTES = 1000; + +/** A network operation. */ +export type Operation = "fetch" | "pull" | "push" | "clone"; + +/** The running network call, as `gitProgress` reports it. */ +export interface Progress { + /** Which call runs. */ + operation: Operation; + /** The phase of git's latest line (`Receiving objects`), or "". */ + phase: string; + /** Its percentage, or null. */ + percent: number | null; +} + +/** A configured remote, as `git.remotes` reports it. */ +export interface Remote { + /** Its name. */ + name: string; + /** Its URL (userinfo redacted), or "". */ + fetchUrl: string; + /** Its push URL when it differs, else null. */ + pushUrl: string | null; +} + +/** + * Reads a `gitProgress` event; null when nothing runs or it is malformed. + * + * @param event - the untrusted event. + */ +export function toProgress(event: EngineEventPayload): Progress | null { + if (event.kind !== "gitProgress" || event.active !== true) { + return null; + } + const operation = event.operation; + if (operation !== "fetch" && operation !== "pull" && operation !== "push" && operation !== "clone") { + return null; + } + const percent = + typeof event.percent === "number" && Number.isFinite(event.percent) + ? Math.min(100, Math.max(0, Math.round(event.percent))) + : null; + return { + operation, + phase: typeof event.phase === "string" ? event.phase.slice(0, 200) : "", + percent, + }; +} + +/** + * Reads the result of `git.remotes`; malformed entries are dropped. + * + * @param raw - untrusted. + */ +export function toRemotes(raw: unknown): Remote[] { + if (typeof raw !== "object" || raw === null) { + return []; + } + const list = (raw as Record).remotes; + if (!Array.isArray(list)) { + return []; + } + const out: Remote[] = []; + for (const entry of list as unknown[]) { + if (out.length >= MAX_REMOTES) { + break; + } + if (typeof entry !== "object" || entry === null) { + continue; + } + const r = entry as Record; + if (typeof r.name !== "string" || r.name === "") { + continue; + } + out.push({ + name: r.name, + fetchUrl: typeof r.fetchUrl === "string" ? r.fetchUrl : "", + pushUrl: typeof r.pushUrl === "string" && r.pushUrl !== "" ? r.pushUrl : null, + }); + } + return out; +} + +/** + * The repository's name from a clone URL or path: the last segment + * without `.git`, or "" when there is none. + * + * @param url - `https://host/a/b.git`, `git@host:a/b.git`, `C:\repos\b`. + */ +export function repoName(url: string): string { + const trimmed = url.trim().replace(/[\\/]+$/, ""); + const last = trimmed.split(/[\\/:]/).pop() ?? ""; + return last.replace(/\.git$/i, ""); +} + +/** + * Joins a folder and a name with the folder's own separator. + * + * @param parent - an absolute folder. + * @param name - the new folder's name. + */ +export function joinPath(parent: string, name: string): string { + const sep = parent.includes("\\") && !parent.includes("/") ? "\\" : "/"; + return parent.endsWith(sep) ? `${parent}${name}` : `${parent}${sep}${name}`; +} + +/** + * The text of an engine error for people: the message as it is. + * + * @param message - the error message. + */ +function reasonOf(message: string): string { + return message.trim() === "" ? "?" : message.trim(); +} + +/** The Remotes store. */ +export const useRemoteStore = defineStore("remote", () => { + const notifications = useNotificationsStore(); + + /** The running network call, or null. */ + const progress = shallowRef(null); + /** A fetch, pull, push or clone was started here and has not answered. */ + const starting = ref(false); + /** Whether a network call runs. */ + const busy = computed(() => starting.value || progress.value !== null); + + /** Whether the clone dialog is open. */ + const cloneOpen = ref(false); + /** The clone dialog's URL. */ + const cloneUrl = ref(""); + /** The clone dialog's target folder. */ + const cloneDir = ref(""); + /** Why the last clone failed, shown in the dialog. */ + const cloneError = ref(null); + /** Whether a clone runs. */ + const cloning = ref(false); + + /** Whether the Manage Remotes dialog is open. */ + const remotesOpen = ref(false); + /** The remotes last read. */ + const remotes = shallowRef([]); + /** Whether `git.remotes` runs. */ + const loading = ref(false); + /** Why the last remote change or read failed, shown in the dialog. */ + const remotesError = ref(null); + /** Whether a remote change runs. */ + const changing = ref(false); + + /** Stops listening; set while started. */ + let unsubscribe: (() => void) | null = null; + /** How many holders called {@link start}. */ + let holders = 0; + + /** + * Follows `gitProgress`. Counted: every `start` needs a `stop`. + */ + function start(): void { + holders += 1; + if (unsubscribe === null) { + unsubscribe = onEngineEvent((event) => { + if (event.kind === "gitProgress") { + progress.value = toProgress(event); + } else if (event.kind === "projectClosed") { + remotes.value = []; + } + }); + } + } + + /** Stops following `gitProgress` when the last holder stops. */ + function stop(): void { + holders = Math.max(0, holders - 1); + if (holders === 0) { + unsubscribe?.(); + unsubscribe = null; + } + } + + /** + * Runs one network command and reports its end. + * + * @param command - `git.fetch`, `git.pull`, `git.push` or `git.clone`. + * @param params - its params. + * @param failedKey - the catalog key of the failure toast. + * @returns the value, or null when it failed or was cancelled. + */ + async function network(command: string, params: JsonValue, failedKey: string): Promise { + starting.value = true; + try { + const result = await runWaiting(command, params); + if (result.ok) { + return result.value; + } + const reason = reasonOf(result.error.message); + if (reason.toLowerCase().includes(CANCELLED.toLowerCase())) { + notifications.notify({ level: "info", messageKey: "notification.vcs.remoteCancelled" }); + } else { + notifications.notify({ level: "error", messageKey: failedKey, message: reason, params: { reason } }); + } + return null; + } finally { + starting.value = false; + } + } + + /** + * Fetches every remote. + * + * @returns whether it succeeded. + */ + async function fetch(): Promise { + const value = await network("git.fetch", {}, "notification.vcs.fetchFailed"); + if (value === null) { + return false; + } + notifications.notify({ level: "info", messageKey: "notification.vcs.fetched" }); + return true; + } + + /** + * Saves every dirty buffer, then pulls the current branch's upstream + * (merge or rebase per the `git.pull.mode` setting). + * + * @returns whether it succeeded. + */ + async function pull(): Promise { + const flushed = await useAutosaveStore().flushDirty(); + if (flushed.error !== null || flushed.failed.length > 0) { + notifications.notify({ level: "warning", messageKey: "notification.vcs.pullNotSaved" }); + return false; + } + const value = await network("git.pull", {}, "notification.vcs.pullFailed"); + if (value === null) { + return false; + } + notifications.notify({ level: "info", messageKey: "notification.vcs.pulled" }); + return true; + } + + /** + * Pushes the current branch (setting its upstream on the first push). + * + * @returns whether it succeeded. + */ + async function push(): Promise { + const value = await network("git.push", {}, "notification.vcs.pushFailed"); + if (value === null) { + return false; + } + const v = typeof value === "object" && !Array.isArray(value) ? value : {}; + const branch = typeof v.branch === "string" ? v.branch : ""; + const remote = typeof v.remote === "string" ? v.remote : ""; + notifications.notify({ + level: "info", + messageKey: v.upstreamSet === true ? "notification.vcs.pushedUpstream" : "notification.vcs.pushed", + params: { branch, remote }, + }); + return true; + } + + /** + * Stops the running network call. + * + * @returns whether one was stopped. + */ + async function cancel(): Promise { + const result = await run("git.cancel"); + return result.ok; + } + + /** Opens the clone dialog with empty fields. */ + function showClone(): void { + cloneUrl.value = ""; + cloneDir.value = ""; + cloneError.value = null; + cloneOpen.value = true; + } + + /** Closes the clone dialog (a running clone goes on). */ + function hideClone(): void { + cloneOpen.value = false; + } + + /** + * Proposes the target folder: `parent` plus the repository's name. + * + * @param parent - the chosen parent folder. + */ + function chooseParent(parent: string): void { + const name = repoName(cloneUrl.value); + cloneDir.value = name === "" ? parent : joinPath(parent, name); + } + + /** + * Clones {@link cloneUrl} into {@link cloneDir} and opens the project. + * + * @returns whether the clone succeeded. + */ + async function clone(): Promise { + const url = cloneUrl.value.trim(); + const dir = cloneDir.value.trim(); + if (url === "" || dir === "") { + cloneError.value = "\u0000empty"; + return false; + } + if (cloning.value || busy.value) { + return false; + } + cloning.value = true; + cloneError.value = null; + starting.value = true; + try { + const result = await runWaiting("git.clone", { url, dir }); + if (!result.ok) { + const reason = reasonOf(result.error.message); + if (reason.toLowerCase().includes(CANCELLED.toLowerCase())) { + notifications.notify({ level: "info", messageKey: "notification.vcs.remoteCancelled" }); + } else { + cloneError.value = reason; + } + return false; + } + const value = result.value; + const root = + typeof value === "object" && value !== null && !Array.isArray(value) && typeof value.root === "string" + ? value.root + : dir; + cloneOpen.value = false; + notifications.notify({ level: "info", messageKey: "notification.vcs.cloned", params: { dir: root } }); + const opened = await run("project.open", { path: root }); + if (!opened.ok) { + notifications.notifyError("project.open", opened.error); + } + return true; + } finally { + cloning.value = false; + starting.value = false; + } + } + + /** Reads the remotes. */ + async function loadRemotes(): Promise { + loading.value = true; + const result = await runWaiting("git.remotes"); + loading.value = false; + if (!result.ok) { + remotesError.value = reasonOf(result.error.message); + return; + } + remotes.value = toRemotes(result.value); + } + + /** + * Opens the Manage Remotes dialog and reads the remotes. + * + * @returns when the list arrived. + */ + function showRemotes(): Promise { + remotesError.value = null; + remotesOpen.value = true; + return loadRemotes(); + } + + /** Closes the Manage Remotes dialog. */ + function hideRemotes(): void { + remotesOpen.value = false; + } + + /** + * Runs one remote change and reads the list again. + * + * @param command - `git.remoteAdd`, `git.remoteSetUrl` or `git.remoteRemove`. + * @param params - its params. + * @returns null when done, else why not (also kept in {@link remotesError}). + */ + async function change(command: string, params: JsonValue): Promise { + if (changing.value) { + return "busy"; + } + changing.value = true; + try { + const result = await runWaiting(command, params); + if (!result.ok) { + remotesError.value = reasonOf(result.error.message); + return remotesError.value; + } + remotesError.value = null; + await loadRemotes(); + return null; + } finally { + changing.value = false; + } + } + + /** + * Adds a remote. + * + * @param name - its name; surrounding blanks are dropped. + * @param url - its URL; surrounding blanks are dropped. + */ + function addRemote(name: string, url: string): Promise { + return change("git.remoteAdd", { name: name.trim(), url: url.trim() }); + } + + /** + * Changes a remote's URL. + * + * @param name - the remote. + * @param url - the new URL; surrounding blanks are dropped. + */ + function setRemoteUrl(name: string, url: string): Promise { + return change("git.remoteSetUrl", { name, url: url.trim() }); + } + + /** + * Removes a remote. + * + * @param name - the remote. + */ + function removeRemote(name: string): Promise { + return change("git.remoteRemove", { name }); + } + + return { + progress, + busy, + cloneOpen, + cloneUrl, + cloneDir, + cloneError, + cloning, + remotesOpen, + remotes, + loading, + remotesError, + changing, + start, + stop, + fetch, + pull, + push, + cancel, + showClone, + hideClone, + chooseParent, + clone, + loadRemotes, + showRemotes, + hideRemotes, + addRemote, + setRemoteUrl, + removeRemote, + }; +}); diff --git a/ui/src/components/vcs/remote/remoteDialog.css b/ui/src/components/vcs/remote/remoteDialog.css new file mode 100644 index 0000000..647cfce --- /dev/null +++ b/ui/src/components/vcs/remote/remoteDialog.css @@ -0,0 +1,149 @@ +/* Shared look of the Clone Repository and Manage Remotes dialogs (M4.10). */ + +.remote-dialog { + position: fixed; + inset: 0; + display: flex; + align-items: flex-start; + justify-content: center; + padding-top: 10%; + background: color-mix(in srgb, var(--lt-bg) 50%, transparent); + z-index: 20; +} + +.remote-dialog__box { + width: min(560px, 90vw); + display: flex; + flex-direction: column; + gap: 10px; + padding: 16px; + background: var(--lt-bg); + color: var(--lt-fg); + border: 1px solid var(--lt-border); + border-radius: 6px; +} + +.remote-dialog__title { + margin: 0; + font-size: var(--lt-font-size-ui); + font-weight: 600; +} + +.remote-dialog__field { + display: flex; + flex-direction: column; + gap: 4px; +} + +.remote-dialog__row { + display: flex; + gap: 8px; +} + +.remote-dialog__input { + flex: 1; + min-width: 0; + padding: 4px 6px; + font: inherit; + color: var(--lt-fg); + background: var(--lt-editor-bg); + border: 1px solid var(--lt-border); + border-radius: 4px; +} + +.remote-dialog__hint { + margin: 0; + color: var(--lt-fg-muted); + font-size: var(--lt-font-size-small); +} + +.remote-dialog__error { + margin: 0; + color: var(--lt-error); + white-space: pre-wrap; + overflow-wrap: anywhere; + max-height: 30vh; + overflow-y: auto; +} + +.remote-dialog__buttons { + display: flex; + justify-content: flex-end; + gap: 8px; +} + +.remote-dialog__button { + padding: 4px 12px; + font: inherit; + color: var(--lt-fg); + background: var(--lt-editor-bg); + border: 1px solid var(--lt-border); + border-radius: 4px; +} + +.remote-dialog__button--primary { + border-color: var(--lt-accent); +} + +.remote-dialog__button:disabled { + color: var(--lt-fg-muted); +} + +.remote-dialog__input:focus-visible, +.remote-dialog__button:focus-visible { + outline: 1px solid var(--lt-accent); +} + +.remote-dialog__list { + margin: 0; + padding: 0; + list-style: none; + max-height: 40vh; + overflow-y: auto; +} + +.remote-dialog__remote { + display: flex; + flex-wrap: wrap; + align-items: center; + gap: 4px 12px; + padding: 6px 0; + border-bottom: 1px solid var(--lt-border); +} + +.remote-dialog__name { + font-weight: 600; + min-width: 8ch; +} + +.remote-dialog__urls { + display: flex; + flex: 1; + flex-direction: column; + min-width: 0; +} + +.remote-dialog__url { + overflow-wrap: anywhere; +} + +.remote-dialog__url--push { + color: var(--lt-fg-muted); + font-size: var(--lt-font-size-small); +} + +.remote-dialog__actions { + display: inline-flex; + align-items: center; + gap: 6px; + margin-left: auto; +} + +.remote-dialog__form { + display: flex; + gap: 8px; +} + +.remote-dialog__input--name { + flex: 0 0 12ch; +} diff --git a/ui/src/i18n/en.json b/ui/src/i18n/en.json index 7d4cde4..c6e0ac5 100644 --- a/ui/src/i18n/en.json +++ b/ui/src/i18n/en.json @@ -21,11 +21,16 @@ "command.unavailable": "{command} cannot run right now", "command.vcs.branches": "Branches…", "command.vcs.checkout": "Check Out Branch", + "command.vcs.clone": "Clone Repository…", "command.vcs.commit": "Commit…", + "command.vcs.fetch": "Fetch", + "command.vcs.manageRemotes": "Manage Remotes…", "command.vcs.newBranch": "New Branch…", "command.vcs.nextChange": "Next Change", "command.vcs.openChanges": "Changes", "command.vcs.previousChange": "Previous Change", + "command.vcs.pull": "Pull", + "command.vcs.push": "Push", "command.vcs.refresh": "Refresh Changes", "command.vcs.showDiff": "Show Diff", "command.vcs.showHistory": "Show History", @@ -161,10 +166,20 @@ "notification.vcs.checkedOut": "Checked out {branch}", "notification.vcs.checkoutFailed": "Git did not check out {branch}: {reason}", "notification.vcs.checkoutNotSaved": "Checkout of {branch} cancelled: not every file could be saved first.", + "notification.vcs.cloned": "Cloned into {dir}", "notification.vcs.commitNotSaved": "Commit cancelled: not every file could be saved first.", "notification.vcs.committed": "Committed {hash}: {summary}", + "notification.vcs.fetchFailed": "Fetch failed: {reason}", + "notification.vcs.fetched": "Fetched from every remote", "notification.vcs.historyNoFile": "Open a file or pass a path to show its history", "notification.vcs.newBranchFailed": "Branch {branch} was not created: {reason}", + "notification.vcs.pullFailed": "Pull failed: {reason}", + "notification.vcs.pullNotSaved": "Pull cancelled: not every file could be saved first.", + "notification.vcs.pulled": "Pulled the current branch", + "notification.vcs.pushFailed": "Push failed: {reason}", + "notification.vcs.pushed": "Pushed {branch} to {remote}", + "notification.vcs.pushedUpstream": "Pushed {branch} to {remote} and set it as the upstream", + "notification.vcs.remoteCancelled": "Git operation cancelled", "notification.vfs.pumpFailed": "Applying changes on disk failed: {reason}", "notification.watch.failed": "Lutra stopped watching the project for changes on disk: {reason}", "palette.label": "Command palette", @@ -242,6 +257,10 @@ "settings.error.stringTooLong": "Enter at most {max} bytes", "settings.git.executable.description": "Path to the git program (2.30 or newer) that Lutra runs for version control. Leave empty to use git from PATH.", "settings.git.executable.label": "Git executable", + "settings.git.pull.mode.description": "How Pull joins the commits it fetched with your local commits: a merge (fast-forward when possible) or a rebase of your local commits on top.", + "settings.git.pull.mode.label": "Pull mode", + "settings.git.pull.mode.value.merge": "Merge", + "settings.git.pull.mode.value.rebase": "Rebase", "settings.hint.pathAuto": "Detected automatically", "settings.php.executable.description": "Path to the PHP executable (8.2 or newer) that runs Phpactor. Leave empty to use php from PATH.", "settings.php.executable.label": "PHP executable", @@ -320,6 +339,16 @@ "vcs.changes.unstageAll": "Unstage all", "vcs.changes.unstageFile": "Unstage {path}", "vcs.checkout.invalidParams": "vcs.checkout needs a branch name (params: branch).", + "vcs.clone.browse": "Browse…", + "vcs.clone.dir": "Folder", + "vcs.clone.fieldsEmpty": "Enter a URL and a folder.", + "vcs.clone.hint": "Any HTTPS or SSH URL, or a local path. Sign-in goes through Git: its credential helper (which may open its own window) or your SSH agent. Lutra never sees your password or keys.", + "vcs.clone.pickTitle": "Choose the folder to clone into", + "vcs.clone.run": "Clone", + "vcs.clone.running": "Cloning…", + "vcs.clone.title": "Clone Repository", + "vcs.clone.url": "Repository URL", + "vcs.clone.urlPlaceholder": "https://host/owner/repo.git or git@host:owner/repo.git", "vcs.commit.amend": "Amend", "vcs.commit.amendButton": "Amend Commit", "vcs.commit.button": "Commit", @@ -359,6 +388,28 @@ "vcs.log.refresh": "Reload the history", "vcs.log.toolWindow.title": "Log", "vcs.log.truncated": "More files changed than are listed here.", + "vcs.remote.cancel": "Cancel the Git operation", + "vcs.remote.operation.clone": "Cloning", + "vcs.remote.operation.fetch": "Fetching", + "vcs.remote.operation.pull": "Pulling", + "vcs.remote.operation.push": "Pushing", + "vcs.remote.progress": "{operation}: {phase}", + "vcs.remote.progressPercent": "{operation}: {phase} {percent}%", + "vcs.remote.sync": "{ahead} commits to push and {behind} to pull ({upstream})", + "vcs.remotes.add": "Add", + "vcs.remotes.editUrl": "Edit URL", + "vcs.remotes.fieldsEmpty": "Enter a name and a URL for the new remote.", + "vcs.remotes.loading": "Reading remotes…", + "vcs.remotes.name": "Name of the new remote", + "vcs.remotes.namePlaceholder": "Name", + "vcs.remotes.none": "This repository has no remotes yet.", + "vcs.remotes.pushUrl": "push: {url}", + "vcs.remotes.remove": "Remove", + "vcs.remotes.removeQuestion": "Remove {name}?", + "vcs.remotes.title": "Manage Remotes", + "vcs.remotes.url": "URL of the new remote", + "vcs.remotes.urlEmpty": "Enter the new URL.", + "vcs.remotes.urlOf": "URL of {name}", "vcs.state.added": "added", "vcs.state.conflicted": "conflicted", "vcs.state.copied": "copied", diff --git a/ui/src/ipc.ts b/ui/src/ipc.ts index b94956f..2c2692a 100644 --- a/ui/src/ipc.ts +++ b/ui/src/ipc.ts @@ -154,6 +154,17 @@ export type EngineEventPayload = repo: unknown; /** Always true: re-query `git.status`. */ filesChanged: unknown; + } + | { + kind: "gitProgress"; + /** Whether a fetch, pull, push or clone runs; unchecked. */ + active: unknown; + /** `fetch`, `pull`, `push` or `clone`; unchecked. */ + operation?: unknown; + /** The phase of git's latest progress line, or the line; unchecked. */ + phase?: unknown; + /** 0–100; unchecked. */ + percent?: unknown; }; /** Callback receiving engine events. */ @@ -355,3 +366,20 @@ export async function pickFile(title: string): Promise> return { ok: false, error: toEngineError(reason) }; } } + +/** + * Shows the native folder picker of the desktop shell (the "Browse…" + * button of "Clone Repository…"). + * + * @param title - the picker's title, from the catalog. + * @returns the chosen folder's absolute path, null when the user cancelled, + * or the error the call failed with (for example outside the shell). + */ +export async function pickDir(title: string): Promise> { + try { + const value = await invoke("pick_dir", { title }); + return { ok: true, value: typeof value === "string" ? value : null }; + } catch (reason: unknown) { + return { ok: false, error: toEngineError(reason) }; + } +} diff --git a/ui/tests/i18n/catalog.test.ts b/ui/tests/i18n/catalog.test.ts index 5c705ea..1177847 100644 --- a/ui/tests/i18n/catalog.test.ts +++ b/ui/tests/i18n/catalog.test.ts @@ -45,7 +45,8 @@ const usedKeys = sourceFiles(SRC).flatMap((file) => * `settings.list`, the `messageKey` of * `notification` events (engine and desktop host), the keys the * notifications store passes through a constant, and the Changes - * window's commit toasts (M4.2) and the branch toasts (M4.5), passed as + * window's commit toasts (M4.2), the branch toasts (M4.5) and the remote + * toasts (M4.10), passed as * `messageKey`. */ const RUNTIME_KEYS = new Set([ @@ -68,6 +69,16 @@ const RUNTIME_KEYS = new Set([ "notification.vcs.commitNotSaved", "notification.vcs.committed", "notification.vcs.newBranchFailed", + "notification.vcs.cloned", + "notification.vcs.fetchFailed", + "notification.vcs.fetched", + "notification.vcs.pullFailed", + "notification.vcs.pullNotSaved", + "notification.vcs.pulled", + "notification.vcs.pushFailed", + "notification.vcs.pushed", + "notification.vcs.pushedUpstream", + "notification.vcs.remoteCancelled", "notification.settings.corrupt", "notification.settings.corruptNotMoved", "notification.settings.unavailable", @@ -98,6 +109,10 @@ const RUNTIME_KEYS = new Set([ "settings.editor.hover.delayMs.label", "settings.git.executable.description", "settings.git.executable.label", + "settings.git.pull.mode.description", + "settings.git.pull.mode.label", + "settings.git.pull.mode.value.merge", + "settings.git.pull.mode.value.rebase", "settings.php.executable.description", "settings.php.executable.label", "settings.phpactor.autoDownload.description", diff --git a/ui/tests/menu/AppMenu.test.ts b/ui/tests/menu/AppMenu.test.ts index b051445..95ce7df 100644 --- a/ui/tests/menu/AppMenu.test.ts +++ b/ui/tests/menu/AppMenu.test.ts @@ -277,11 +277,16 @@ describe("AppMenu", () => { "goto.symbolOpen", "vcs.branches", "vcs.openChanges", + "vcs.clone", "vcs.commit", + "vcs.fetch", "vcs.showLog", + "vcs.manageRemotes", "vcs.newBranch", "vcs.nextChange", "vcs.previousChange", + "vcs.pull", + "vcs.push", "vcs.refresh", "vcs.showDiff", "vcs.showHistory", diff --git a/ui/tests/palette/CommandPalette.test.ts b/ui/tests/palette/CommandPalette.test.ts index 92e4d44..520e79a 100644 --- a/ui/tests/palette/CommandPalette.test.ts +++ b/ui/tests/palette/CommandPalette.test.ts @@ -261,15 +261,18 @@ describe("CommandPalette", () => { "vcs.branches", "vcs.openChanges", "vcs.checkout", + "vcs.clone", "project.close", "palette.open", "vcs.commit", "editor.complete", + "vcs.fetch", "find.open", "goto.classOpen", "goto.fileOpen", "goto.symbolOpen", "vcs.showLog", + "vcs.manageRemotes", "vcs.newBranch", "vcs.nextChange", "project.openDialog", @@ -277,6 +280,8 @@ describe("CommandPalette", () => { "editor.parameterInfo", "vcs.previousChange", "problems.open", + "vcs.pull", + "vcs.push", "editor.quickDocumentation", "app.quit", "vcs.refresh", diff --git a/ui/tests/vcs/Remote.test.ts b/ui/tests/vcs/Remote.test.ts new file mode 100644 index 0000000..0f6bd30 --- /dev/null +++ b/ui/tests/vcs/Remote.test.ts @@ -0,0 +1,372 @@ +/** + * Tests of the remotes feature (M4.10): reading untrusted `gitProgress` + * and `git.remotes`, the repository name and folder of a clone, the + * status-bar item (progress with Cancel, ahead/behind, hidden when in + * step), fetch / pull / push through the UI commands with their toasts + * (pull saves first, a failed save cancels it, git's explained refusal, + * a cancelled call), the Clone dialog (Browse…, the folder following the + * URL, the clone then `project.open`, git's refusal in the dialog), the + * Manage Remotes dialog (list, add, edit without the redacted URL, remove + * after asking), and the registration of the UI commands. The engine is a + * fake behind the mocked Tauri `invoke` and `listen`. + */ +import { flushPromises, mount, type VueWrapper } from "@vue/test-utils"; +import { createPinia, setActivePinia, type Pinia } from "pinia"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; + +const invoke = vi.hoisted(() => vi.fn()); +const listen = vi.hoisted(() => vi.fn()); +vi.mock("@tauri-apps/api/core", () => ({ invoke })); +vi.mock("@tauri-apps/api/event", () => ({ listen })); + +import { useCommandsStore } from "../../src/commands/store"; +import { UI_COMMANDS } from "../../src/commands/uiCommands"; +import RemoteStatusItem from "../../src/components/vcs/remote/RemoteStatusItem.vue"; +import { joinPath, repoName, toProgress, toRemotes, useRemoteStore } from "../../src/components/vcs/remote/remote"; +import { useNotificationsStore } from "../../src/stores/notifications"; +import { useVcsStatusStore } from "../../src/stores/vcsStatus"; + +/** A recorded engine call. */ +interface Call { + /** `engine_execute` or `engine_execute_waiting`. */ + via: string; + /** Command id. */ + command: string; + /** Its params. */ + params: Record; +} + +/** Every engine call, in order. */ +let calls: Call[]; +/** What `git.status` answers. */ +let gitStatus: unknown; +/** What `git.remotes` answers. */ +let gitRemotes: unknown; +/** Errors to throw, by command id. */ +let failures: Record; +/** What `file.saveAll` answers. */ +let saveAll: unknown; +/** What `pick_dir` answers. */ +let picked: string | null; +/** Event listeners registered through `listen`. */ +let listeners: ((event: { payload: unknown }) => void)[]; +/** Pinia of the current test. */ +let pinia: Pinia; +/** Mounted components, unmounted after each test. */ +let mounted: VueWrapper[]; + +/** + * The calls of a command. + * + * @param command - the command id. + */ +function callsOf(command: string): Call[] { + return calls.filter((c) => c.command === command); +} + +/** + * Sends an engine event to every listener. + * + * @param payload - the event. + */ +async function emit(payload: unknown): Promise { + for (const listener of listeners) { + listener({ payload }); + } + await flushPromises(); +} + +/** The texts of the toasts shown. */ +function toasts(): string[] { + return useNotificationsStore(pinia).toasts.map((toast) => toast.text); +} + +/** Mounts the status-bar item with started stores. */ +async function mountItem(): Promise { + useVcsStatusStore(pinia).start(); + const wrapper = mount(RemoteStatusItem, { attachTo: document.body, global: { plugins: [pinia] } }); + mounted.push(wrapper); + await useCommandsStore(pinia).start(); + await flushPromises(); + return wrapper; +} + +beforeEach(() => { + pinia = createPinia(); + setActivePinia(pinia); + calls = []; + mounted = []; + failures = {}; + listeners = []; + picked = null; + gitStatus = { + available: true, + repo: { root: "C:/p", branch: "main", detached: false, head: "0123", upstream: "origin/main", ahead: 0, behind: 0 }, + files: [], + }; + gitRemotes = { + remotes: [ + { name: "origin", fetchUrl: "https://***@github.com/a/b.git", pushUrl: "git@github.com:a/b.git" }, + { name: "fork", fetchUrl: "git@forgejo.example:me/b.git" }, + ], + }; + saveAll = { saved: [], failed: [], skipped: [] }; + invoke.mockReset(); + listen.mockReset(); + listen.mockImplementation((_name: string, callback: (event: { payload: unknown }) => void) => { + listeners.push(callback); + return Promise.resolve(() => { + listeners = listeners.filter((l) => l !== callback); + }); + }); + invoke.mockImplementation((cmd: string, args?: { command: string; params: Record }) => { + if (cmd === "engine_list_commands") { + return Promise.resolve( + ["git.status", "git.fetch", "git.pull", "git.push", "git.remotes", "git.clone"].map((id) => ({ + id, + label: id, + enabled: true, + })), + ); + } + if (cmd === "pick_dir") { + return Promise.resolve(picked); + } + if ((cmd !== "engine_execute" && cmd !== "engine_execute_waiting") || !args) { + return Promise.resolve(null); + } + calls.push({ via: cmd, command: args.command, params: args.params }); + const failure = failures[args.command]; + if (failure) { + return Promise.reject(Object.assign(new Error(failure.message), failure)); + } + switch (args.command) { + case "git.status": + return Promise.resolve(gitStatus); + case "git.remotes": + return Promise.resolve(gitRemotes); + case "git.push": + return Promise.resolve({ remote: "origin", branch: "feature", upstreamSet: true }); + case "git.clone": + return Promise.resolve({ root: String(args.params.dir) }); + case "git.cancel": + return Promise.resolve({ cancelled: true }); + case "file.saveAll": + return Promise.resolve(saveAll); + default: + return Promise.resolve({}); + } + }); +}); + +afterEach(() => { + for (const wrapper of mounted) { + wrapper.unmount(); + } + useVcsStatusStore(pinia).stop(); + document.body.innerHTML = ""; +}); + +describe("reading the engine's answers", () => { + it("reads gitProgress and drops what is not running or malformed", () => { + expect( + toProgress({ kind: "gitProgress", active: true, operation: "push", phase: "Writing objects", percent: 42.4 }), + ).toEqual({ operation: "push", phase: "Writing objects", percent: 42 }); + expect(toProgress({ kind: "gitProgress", active: true, operation: "clone", percent: 300 })).toEqual({ + operation: "clone", + phase: "", + percent: 100, + }); + expect(toProgress({ kind: "gitProgress", active: false, operation: "fetch" })).toBeNull(); + expect(toProgress({ kind: "gitProgress", active: true, operation: "rm -rf" })).toBeNull(); + expect(toProgress({ kind: "projectClosed" })).toBeNull(); + }); + + it("reads git.remotes and drops malformed entries", () => { + expect( + toRemotes({ remotes: [{ name: "o", fetchUrl: "u", pushUrl: "" }, { name: "" }, null, "x", { name: "p", fetchUrl: 3 }] }), + ).toEqual([ + { name: "o", fetchUrl: "u", pushUrl: null }, + { name: "p", fetchUrl: "", pushUrl: null }, + ]); + expect(toRemotes(null)).toEqual([]); + expect(toRemotes({ remotes: "no" })).toEqual([]); + }); + + it("names the clone folder after the repository", () => { + expect(repoName("https://github.com/owner/lutra.git")).toBe("lutra"); + expect(repoName("git@forgejo.example:me/site.GIT/")).toBe("site"); + expect(repoName("C:\\repos\\tool")).toBe("tool"); + expect(repoName("")).toBe(""); + expect(joinPath("C:\\src", "lutra")).toBe("C:\\src\\lutra"); + expect(joinPath("/home/me/", "lutra")).toBe("/home/me/lutra"); + expect(joinPath("C:/src", "lutra")).toBe("C:/src/lutra"); + }); +}); + +describe("the status-bar item", () => { + it("shows the running call with Cancel, then ahead and behind", async () => { + const wrapper = await mountItem(); + expect(wrapper.find("[data-test=remote-progress]").exists()).toBe(false); + expect(wrapper.find("[data-test=remote-sync]").exists()).toBe(false); + await emit({ kind: "gitProgress", active: true, operation: "fetch", phase: "Receiving objects", percent: 45 }); + expect(wrapper.get("[data-test=remote-progress]").text()).toBe("Fetching: Receiving objects 45%"); + expect(useRemoteStore(pinia).busy).toBe(true); + expect(useCommandsStore(pinia).isEnabled("vcs.push")).toBe(false); + await wrapper.get("[data-test=remote-cancel]").trigger("click"); + await flushPromises(); + expect(callsOf("git.cancel")).toHaveLength(1); + expect(callsOf("git.cancel")[0]?.via).toBe("engine_execute"); + await emit({ kind: "gitProgress", active: false, operation: "fetch" }); + expect(wrapper.find("[data-test=remote-progress]").exists()).toBe(false); + expect(useCommandsStore(pinia).isEnabled("vcs.push")).toBe(true); + + gitStatus = { + available: true, + repo: { root: "C:/p", branch: "main", detached: false, upstream: "origin/main", ahead: 2, behind: 1 }, + files: [], + }; + await useVcsStatusStore(pinia).refresh(); + await flushPromises(); + expect(wrapper.get("[data-test=remote-sync]").text().replace(/\s+/g, "")).toBe("21"); + expect(wrapper.get("[data-test=remote-sync]").attributes("aria-label")).toBe( + "2 commits to push and 1 to pull (origin/main)", + ); + }); +}); + +describe("fetch, pull and push", () => { + it("pushes and says when the upstream was set", async () => { + await mountItem(); + await useCommandsStore(pinia).execute("vcs.push"); + await flushPromises(); + expect(callsOf("git.push")).toEqual([{ via: "engine_execute_waiting", command: "git.push", params: {} }]); + expect(toasts()).toContain("Pushed feature to origin and set it as the upstream"); + }); + + it("saves before a pull and cancels it when a save fails", async () => { + await mountItem(); + await useCommandsStore(pinia).execute("vcs.pull"); + await flushPromises(); + const order = calls.map((c) => c.command).filter((c) => c === "file.saveAll" || c === "git.pull"); + expect(order).toEqual(["file.saveAll", "git.pull"]); + expect(toasts()).toContain("Pulled the current branch"); + + saveAll = { saved: [], failed: [{ id: 3, path: "a.php", message: "denied" }], skipped: [] }; + calls = []; + await useCommandsStore(pinia).execute("vcs.pull"); + await flushPromises(); + expect(callsOf("git.pull")).toHaveLength(0); + expect(toasts()).toContain("Pull cancelled: not every file could be saved first."); + }); + + it("shows git's explained refusal, and a cancelled call as information", async () => { + await mountItem(); + failures["git.push"] = { + code: "failed", + message: "The push was rejected: the remote branch has commits that your branch does not have.", + }; + await useCommandsStore(pinia).execute("vcs.push"); + await flushPromises(); + expect(toasts().at(-1)).toBe( + "Push failed: The push was rejected: the remote branch has commits that your branch does not have.", + ); + expect(useNotificationsStore(pinia).toasts.at(-1)?.level).toBe("error"); + failures["git.fetch"] = { code: "failed", message: "the Git operation was cancelled" }; + await useCommandsStore(pinia).execute("vcs.fetch"); + await flushPromises(); + expect(toasts().at(-1)).toBe("Git operation cancelled"); + expect(useNotificationsStore(pinia).toasts.at(-1)?.level).toBe("info"); + }); +}); + +describe("Clone Repository", () => { + it("proposes the folder from Browse… and the URL, clones and opens the project", async () => { + const wrapper = await mountItem(); + await useCommandsStore(pinia).execute("vcs.clone"); + await flushPromises(); + const dialog = wrapper.get("[data-test=clone-dialog]"); + expect(document.activeElement).toBe(dialog.get("[data-test=clone-url]").element); + await dialog.get("[data-test=clone-url]").setValue("https://github.com/owner/lutra.git"); + picked = "C:\\src"; + await dialog.get("[data-test=clone-browse]").trigger("click"); + await flushPromises(); + expect((dialog.get("[data-test=clone-dir]").element as HTMLInputElement).value).toBe("C:\\src\\lutra"); + await dialog.get("[data-test=clone-url]").setValue("git@forgejo.example:me/site.git"); + await flushPromises(); + expect((dialog.get("[data-test=clone-dir]").element as HTMLInputElement).value).toBe("C:\\src\\site"); + await dialog.get("[data-test=clone-run]").trigger("click"); + await flushPromises(); + expect(callsOf("git.clone")[0]?.params).toEqual({ url: "git@forgejo.example:me/site.git", dir: "C:\\src\\site" }); + expect(callsOf("project.open")[0]?.params).toEqual({ path: "C:\\src\\site" }); + expect(wrapper.find("[data-test=clone-dialog]").exists()).toBe(false); + expect(toasts()).toContain("Cloned into C:\\src\\site"); + }); + + it("keeps the dialog with git's refusal and asks for both fields", async () => { + const wrapper = await mountItem(); + await useCommandsStore(pinia).execute("vcs.clone"); + await flushPromises(); + await wrapper.get("[data-test=clone-run]").trigger("click"); + await flushPromises(); + expect(wrapper.get("[data-test=clone-error]").text()).toBe("Enter a URL and a folder."); + expect(callsOf("git.clone")).toHaveLength(0); + failures["git.clone"] = { + code: "failed", + message: "The remote repository was not found. Check the URL and that you have access to it.", + }; + await wrapper.get("[data-test=clone-url]").setValue("https://github.com/nope/nope.git"); + await wrapper.get("[data-test=clone-dir]").setValue("C:/src/nope"); + await wrapper.get("[data-test=clone-url]").trigger("keydown", { key: "Enter" }); + await flushPromises(); + expect(wrapper.get("[data-test=clone-error]").text()).toContain("was not found"); + expect(callsOf("project.open")).toHaveLength(0); + await wrapper.get("[data-test=clone-url]").trigger("keydown", { key: "Escape" }); + expect(wrapper.find("[data-test=clone-dialog]").exists()).toBe(false); + }); +}); + +describe("Manage Remotes", () => { + it("lists, adds, edits and removes remotes", async () => { + const wrapper = await mountItem(); + await useCommandsStore(pinia).execute("vcs.manageRemotes"); + await flushPromises(); + const origin = wrapper.get("[data-test=remote-origin]"); + expect(origin.text()).toContain("https://***@github.com/a/b.git"); + expect(origin.text()).toContain("push: git@github.com:a/b.git"); + expect(wrapper.get("[data-test=remote-fork]").text()).toContain("git@forgejo.example:me/b.git"); + + await wrapper.get("[data-test=remote-new-name]").setValue("upstream"); + await wrapper.get("[data-test=remote-new-url]").setValue(" https://gitlab.com/x/b.git "); + await wrapper.get("[data-test=remote-add]").trigger("submit"); + await flushPromises(); + expect(callsOf("git.remoteAdd")[0]?.params).toEqual({ name: "upstream", url: "https://gitlab.com/x/b.git" }); + expect(callsOf("git.remotes")).toHaveLength(2); + + await origin.get("[data-test=remote-edit]").trigger("click"); + const field = wrapper.get("[data-test=remote-edit-url]"); + expect((field.element as HTMLInputElement).value).toBe(""); + await field.setValue("https://bitbucket.org/a/b.git"); + await wrapper.get("[data-test=remote-save]").trigger("click"); + await flushPromises(); + expect(callsOf("git.remoteSetUrl")[0]?.params).toEqual({ name: "origin", url: "https://bitbucket.org/a/b.git" }); + + failures["git.remoteRemove"] = { code: "failed", message: "error: No such remote: 'fork'" }; + await wrapper.get("[data-test=remote-fork]").get("[data-test=remote-remove]").trigger("click"); + await wrapper.get("[data-test=remote-remove-yes]").trigger("click"); + await flushPromises(); + expect(callsOf("git.remoteRemove")[0]?.params).toEqual({ name: "fork" }); + expect(wrapper.get("[data-test=remotes-error]").text()).toBe("error: No such remote: 'fork'"); + }); +}); + +describe("registration", () => { + it("adds the remote commands to the Git menu without default shortcuts", () => { + const ids = ["vcs.clone", "vcs.fetch", "vcs.manageRemotes", "vcs.pull", "vcs.push"]; + for (const id of ids) { + const spec = UI_COMMANDS.find((c) => c.id === id); + expect(spec?.menuPath?.startsWith("Git/")).toBe(true); + expect(spec?.defaultShortcut).toBeUndefined(); + } + }); +});