M4.10: remotes: fetch, pull, push, clone and manage remotes
Owner decision D23: Lutra runs fetch, pull (merge or rebase per the new git.pull.mode setting), push (upstream set on the first push) and clone through the user's git. Any HTTPS or SSH server works; sign-in stays with git's credential helper (its own window is allowed for network calls only) and the ssh agent. Terminal prompts stay off, ext:: is refused, and URL userinfo is redacted in every message, progress line and remote listing. - vcs: Call::network with a 30 min timeout, a stderr progress sink and a Cancel flag that kills git's process group; remote.rs with clone_repo, fetch, pull, push (--porcelain rejections), remotes and add/set-url/ remove; explained refusals (rejected push, sign-in, host key, not found, unreachable, no upstream, local changes, conflicts). - engine: git.fetch/pull/push/clone/cancel/remotes/remoteAdd/remoteSetUrl/ remoteRemove, one network call at a time, gitProgress events, gitChanged after changes. - ui: status-bar progress with Cancel and ahead/behind, vcs.fetch/pull/ push/clone/manageRemotes (Git menu, no default shortcuts), Clone Repository and Manage Remotes dialogs; desktop pick_dir IPC. - Tests against local bare repositories only (vcs 8, engine 3, Vitest 11) plus unit and property tests; docs updated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
48b892a6d5
commit
d097558ba5
36 changed files with 4426 additions and 45 deletions
301
crates/engine/tests/git_remote.rs
Normal file
301
crates/engine/tests/git_remote.rs
Normal file
|
|
@ -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", "<?php // a\n");
|
||||
git(&seed, &["push", "-q", bare.to_str().unwrap(), "main"]);
|
||||
bare
|
||||
}
|
||||
|
||||
/// An engine behind a lock, like a host, recording every event.
|
||||
struct Host {
|
||||
/// The engine.
|
||||
engine: Mutex<Engine>,
|
||||
/// Events published so far.
|
||||
events: Mutex<Vec<Event>>,
|
||||
}
|
||||
|
||||
impl Host {
|
||||
/// An engine without a project.
|
||||
fn new() -> Arc<Self> {
|
||||
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<Value, EngineError> {
|
||||
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<Value> {
|
||||
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", "<?php // b\n");
|
||||
host.events.lock().unwrap().clear();
|
||||
let pushed = host.run("git.push", json!({})).unwrap();
|
||||
assert_eq!(
|
||||
pushed,
|
||||
json!({"remote": "origin", "branch": "feature", "upstreamSet": true})
|
||||
);
|
||||
let changed = host.seen("gitChanged");
|
||||
assert_eq!(
|
||||
changed.last().unwrap()["repo"]["upstream"],
|
||||
"origin/feature"
|
||||
);
|
||||
assert_eq!(
|
||||
git(&bare, &["rev-parse", "feature"]),
|
||||
git(&dir, &["rev-parse", "HEAD"])
|
||||
);
|
||||
|
||||
let other = tmp.path().join("other");
|
||||
git(
|
||||
tmp.path(),
|
||||
&["clone", "-q", bare.to_str().unwrap(), "other"],
|
||||
);
|
||||
configure(&other);
|
||||
git(&other, &["switch", "-q", "feature"]);
|
||||
commit(&other, "c.php", "<?php // c\n");
|
||||
git(&other, &["push", "-q"]);
|
||||
host.run("git.fetch", json!({"remote": "origin"})).unwrap();
|
||||
host.run("git.pull", json!({"mode": "rebase"})).unwrap();
|
||||
assert!(dir.join("c.php").exists());
|
||||
host.run("git.pull", json!({})).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cancel_stops_a_push_and_one_network_call_runs_at_a_time() {
|
||||
if !have_git() {
|
||||
return;
|
||||
}
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let bare = bare(tmp.path());
|
||||
std::fs::write(
|
||||
bare.join("hooks").join("pre-receive"),
|
||||
"#!/bin/sh\nsleep 30\n",
|
||||
)
|
||||
.unwrap();
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
std::fs::set_permissions(
|
||||
bare.join("hooks").join("pre-receive"),
|
||||
std::fs::Permissions::from_mode(0o755),
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
let dir = tmp.path().join("work");
|
||||
git(tmp.path(), &["clone", "-q", bare.to_str().unwrap(), "work"]);
|
||||
configure(&dir);
|
||||
commit(&dir, "b.php", "<?php // b\n");
|
||||
let host = Host::new();
|
||||
host.run("project.open", json!({"path": dir})).unwrap();
|
||||
let idle = host.run("git.cancel", json!({})).unwrap_err();
|
||||
assert_eq!(idle.code(), "disabled");
|
||||
|
||||
let pusher = Arc::clone(&host);
|
||||
let push = std::thread::spawn(move || pusher.run("git.push", json!({})));
|
||||
let deadline = Instant::now() + Duration::from_secs(20);
|
||||
while host.seen("gitProgress").is_empty() {
|
||||
assert!(Instant::now() < deadline, "no gitProgress");
|
||||
host.pump();
|
||||
std::thread::sleep(Duration::from_millis(20));
|
||||
}
|
||||
assert_eq!(host.seen("gitProgress")[0]["operation"], "push");
|
||||
let busy = host.run("git.fetch", json!({})).unwrap_err();
|
||||
assert!(busy.to_string().contains("another Git operation"), "{busy}");
|
||||
let started = Instant::now();
|
||||
assert_eq!(
|
||||
host.run("git.cancel", json!({})).unwrap()["cancelled"],
|
||||
true
|
||||
);
|
||||
let err = push.join().unwrap().unwrap_err();
|
||||
assert!(err.to_string().contains("cancelled"), "{err}");
|
||||
assert!(started.elapsed() < Duration::from_secs(10));
|
||||
host.pump();
|
||||
assert_eq!(host.seen("gitProgress").last().unwrap()["active"], false);
|
||||
host.run("git.fetch", json!({})).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bad_params_and_no_project() {
|
||||
if !have_git() {
|
||||
return;
|
||||
}
|
||||
let host = Host::new();
|
||||
let err = host.run("git.fetch", json!({})).unwrap_err();
|
||||
assert_eq!(err.code(), "disabled");
|
||||
let err = host
|
||||
.run("git.clone", json!({"url": "x", "dir": "relative"}))
|
||||
.unwrap_err();
|
||||
assert_eq!(err.code(), "invalid_params");
|
||||
let err = host
|
||||
.run(
|
||||
"git.clone",
|
||||
json!({"url": "--upload-pack=x", "dir": "/abs"}),
|
||||
)
|
||||
.unwrap_err();
|
||||
assert_eq!(err.code(), "invalid_params");
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
git(tmp.path(), &["init", "-q", "-b", "main"]);
|
||||
host.run("project.open", json!({"path": tmp.path()}))
|
||||
.unwrap();
|
||||
let err = host
|
||||
.run(
|
||||
"git.remoteAdd",
|
||||
json!({"name": "x", "url": "ext::sh -c id"}),
|
||||
)
|
||||
.unwrap_err();
|
||||
assert_eq!(err.code(), "invalid_params");
|
||||
let err = host
|
||||
.run("git.remoteRemove", json!({"name": "-f"}))
|
||||
.unwrap_err();
|
||||
assert_eq!(err.code(), "invalid_params");
|
||||
let err = host.run("git.pull", json!({"mode": "squash"})).unwrap_err();
|
||||
assert_eq!(err.code(), "invalid_params");
|
||||
let err = host.run("git.push", json!({})).unwrap_err();
|
||||
assert_eq!(err.code(), "failed");
|
||||
}
|
||||
|
|
@ -56,7 +56,8 @@ fn list_describes_the_c2_keys_with_catalog_keys_only() {
|
|||
"editor.completion.autoPopup",
|
||||
"editor.completion.delayMs",
|
||||
"editor.hover.delayMs",
|
||||
"git.executable"
|
||||
"git.executable",
|
||||
"git.pull.mode"
|
||||
]
|
||||
);
|
||||
for s in list.as_array().unwrap() {
|
||||
|
|
@ -126,7 +127,8 @@ fn get_set_reset_and_events() {
|
|||
"editor.completion.autoPopup": true,
|
||||
"editor.completion.delayMs": 0,
|
||||
"editor.hover.delayMs": 350,
|
||||
"git.executable": ""
|
||||
"git.executable": "",
|
||||
"git.pull.mode": "merge"
|
||||
})
|
||||
);
|
||||
let out = e
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue