- git.lineChanges {id} -> {ranges: [{kind, startLine, endLine}], version}:
the buffer against its index blob, line diff with similar (Myers +
Replace, no Compact) off the lock, CRLF-safe; index texts cached per
path and dropped on gitChanged (State::emit -> git_lines::observe)
- editor gutter markers for added, modified and deleted lines, queried
150 ms after edits, on show and on gitChanged, mapped through edits
- vcsStatus store (git.status on projectOpened and gitChanged, no
polling) colouring file names in the project tree and editor tabs with
the --lt-vcs-* tokens; contrast also checked on the active tab
- tests: unit and property tests, real-repo engine tests, Vitest
- docs: architecture, parity, gaps, roadmap, ui-parity, ROADMAP
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
222 lines
6.8 KiB
Rust
222 lines
6.8 KiB
Rust
//! `git.lineChanges` through the engine against real temp repositories:
|
|
//! added, modified and deleted lines of an edited buffer, a CRLF buffer
|
|
//! against an LF index blob, an untracked file, the cache dropped by
|
|
//! `gitChanged` after staging, a project outside any repository, and an
|
|
//! unknown buffer. 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::Mutex;
|
|
|
|
use lutra_engine::{Engine, EngineError, 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]) {
|
|
let out = Command::new("git")
|
|
.args(args)
|
|
.current_dir(dir)
|
|
.output()
|
|
.unwrap();
|
|
assert!(
|
|
out.status.success(),
|
|
"git {args:?}: {}",
|
|
String::from_utf8_lossy(&out.stderr)
|
|
);
|
|
}
|
|
|
|
/// A repository with `files` committed; `autocrlf` sets `core.autocrlf`.
|
|
fn repo(dir: &Path, autocrlf: &str, files: &[(&str, &str)]) {
|
|
git(dir, &["init", "-q", "-b", "main"]);
|
|
for (key, value) in [
|
|
("user.name", "Lutra Test"),
|
|
("user.email", "test@lutra.invalid"),
|
|
("commit.gpgsign", "false"),
|
|
("core.autocrlf", autocrlf),
|
|
("core.hooksPath", ".git/hooks"),
|
|
] {
|
|
git(dir, &["config", key, value]);
|
|
}
|
|
for (name, text) in files {
|
|
std::fs::write(dir.join(name), text).unwrap();
|
|
}
|
|
git(dir, &["add", "-A"]);
|
|
git(dir, &["commit", "-q", "-m", "base"]);
|
|
}
|
|
|
|
/// An engine behind a lock, like a host.
|
|
struct Host {
|
|
/// The engine.
|
|
engine: Mutex<Engine>,
|
|
}
|
|
|
|
impl Host {
|
|
/// An engine with `root` open as the project.
|
|
fn open(root: &Path) -> Self {
|
|
let host = Self {
|
|
engine: Mutex::new(Engine::new().unwrap()),
|
|
};
|
|
host.run("project.open", json!({"path": root})).unwrap();
|
|
host
|
|
}
|
|
|
|
/// 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, |_| {}).map(|o| o.value)
|
|
}
|
|
|
|
/// Opens `path` and returns its buffer id.
|
|
fn open_file(&self, path: &str) -> u64 {
|
|
self.run("file.open", json!({"path": path})).unwrap()["id"]
|
|
.as_u64()
|
|
.unwrap()
|
|
}
|
|
|
|
/// Replaces the whole text of buffer `id`.
|
|
fn set_text(&self, id: u64, text: &str) {
|
|
let current = self.run("buffer.get", json!({"id": id})).unwrap();
|
|
let len = current["text"].as_str().unwrap().encode_utf16().count();
|
|
self.run(
|
|
"buffer.edit",
|
|
json!({
|
|
"id": id,
|
|
"baseVersion": current["version"],
|
|
"edits": [{"from": 0, "to": len, "insert": text}],
|
|
}),
|
|
)
|
|
.unwrap();
|
|
}
|
|
|
|
/// The ranges of `git.lineChanges` for buffer `id`.
|
|
fn ranges(&self, id: u64) -> Value {
|
|
self.run("git.lineChanges", json!({"id": id})).unwrap()["ranges"].clone()
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn reports_added_modified_and_deleted_lines() {
|
|
if !have_git() {
|
|
return;
|
|
}
|
|
let dir = tempfile::tempdir().unwrap();
|
|
repo(
|
|
dir.path(),
|
|
"false",
|
|
&[("a.php", "<?php\none();\ntwo();\nthree();\nfour();\n")],
|
|
);
|
|
let host = Host::open(dir.path());
|
|
let id = host.open_file("a.php");
|
|
let version = host.run("buffer.get", json!({"id": id})).unwrap()["version"]
|
|
.as_u64()
|
|
.unwrap();
|
|
let first = host.run("git.lineChanges", json!({"id": id})).unwrap();
|
|
assert_eq!(first, json!({"ranges": [], "version": version}));
|
|
|
|
host.set_text(id, "<?php\nONE();\ntwo();\nnew();\nfour();\n");
|
|
let changes = host.run("git.lineChanges", json!({"id": id})).unwrap();
|
|
assert_eq!(changes["version"], version + 1);
|
|
assert_eq!(
|
|
changes["ranges"],
|
|
json!([
|
|
{"kind": "modified", "startLine": 1, "endLine": 2},
|
|
{"kind": "modified", "startLine": 3, "endLine": 4},
|
|
])
|
|
);
|
|
|
|
host.set_text(id, "<?php\ntwo();\nthree();\nfour();\nfive();\nsix();\n");
|
|
assert_eq!(
|
|
host.ranges(id),
|
|
json!([
|
|
{"kind": "deleted", "startLine": 1, "endLine": 1},
|
|
{"kind": "added", "startLine": 4, "endLine": 6},
|
|
])
|
|
);
|
|
|
|
let err = host.run("git.lineChanges", json!({"id": 999})).unwrap_err();
|
|
assert_eq!(err.code(), "failed");
|
|
let err = host.run("git.lineChanges", json!({"id": "x"})).unwrap_err();
|
|
assert_eq!(err.code(), "invalid_params");
|
|
}
|
|
|
|
#[test]
|
|
fn a_crlf_buffer_matches_its_lf_index_blob() {
|
|
if !have_git() {
|
|
return;
|
|
}
|
|
let dir = tempfile::tempdir().unwrap();
|
|
repo(
|
|
dir.path(),
|
|
"true",
|
|
&[("w.php", "<?php\r\na();\r\nb();\r\n")],
|
|
);
|
|
let host = Host::open(dir.path());
|
|
let show = host
|
|
.run("git.show", json!({"path": "w.php", "ref": "index"}))
|
|
.unwrap();
|
|
assert_eq!(show["text"], "<?php\na();\nb();\n");
|
|
let id = host.open_file("w.php");
|
|
assert_eq!(host.ranges(id), json!([]));
|
|
host.set_text(id, "<?php\r\na();\r\nb();\r\nc();\r\n");
|
|
assert_eq!(
|
|
host.ranges(id),
|
|
json!([{"kind": "added", "startLine": 3, "endLine": 4}])
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn staging_clears_the_cached_index_text() {
|
|
if !have_git() {
|
|
return;
|
|
}
|
|
let dir = tempfile::tempdir().unwrap();
|
|
repo(dir.path(), "false", &[("a.php", "<?php\nold();\n")]);
|
|
let host = Host::open(dir.path());
|
|
let id = host.open_file("a.php");
|
|
host.set_text(id, "<?php\nnew();\n");
|
|
assert_eq!(
|
|
host.ranges(id),
|
|
json!([{"kind": "modified", "startLine": 1, "endLine": 2}])
|
|
);
|
|
host.run("file.save", json!({"id": id})).unwrap();
|
|
host.run("git.stage", json!({"paths": ["a.php"]})).unwrap();
|
|
assert_eq!(host.ranges(id), json!([]));
|
|
}
|
|
|
|
#[test]
|
|
fn untracked_files_and_projects_without_a_repository_have_no_ranges() {
|
|
if !have_git() {
|
|
return;
|
|
}
|
|
let dir = tempfile::tempdir().unwrap();
|
|
repo(dir.path(), "false", &[("a.php", "<?php\n")]);
|
|
std::fs::write(dir.path().join("new.php"), "<?php\nnew();\n").unwrap();
|
|
let host = Host::open(dir.path());
|
|
let id = host.open_file("new.php");
|
|
assert_eq!(host.ranges(id), json!([]));
|
|
|
|
let plain = tempfile::tempdir().unwrap();
|
|
std::fs::write(plain.path().join("b.php"), "<?php\n").unwrap();
|
|
let host = Host::open(plain.path());
|
|
let id = host.open_file("b.php");
|
|
host.set_text(id, "<?php\nmore();\n");
|
|
assert_eq!(host.ranges(id), json!([]));
|
|
}
|