C3: chunk staging, unstaging and revert from the gutter and the diff viewer
- crates/vcs chunk.rs: chunks of a file (buffer text vs index, HEAD vs
index), a minimal patch applied with git apply --cached, revert as a
byte edit; token refusal when buffer, index or HEAD changed; CRLF
(autocrlf and stored), no final newline, BOM, staged renames, first
commit; binary and untracked refused. Real-git tests + property test.
- engine: git.chunks, git.stageChunk, git.unstageChunk, git.revertChunk
(revert edits the buffer under the lock, undoable in the editor);
cmd:
:start_then; git.lineChanges shares the vcs line diff.
- UI: chunk popup on a gutter marker click (Stage / Unstage / Revert /
Previous / Next), hollow staged markers, vcs.showChunk, diff viewer
hint, "partly staged" badge in the Changes window.
- Docs: G8 row, Git parity row evidence, ui-parity, architecture.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
d8e66a4652
commit
301443db7d
28 changed files with 3159 additions and 101 deletions
402
crates/vcs/tests/chunk.rs
Normal file
402
crates/vcs/tests/chunk.rs
Normal file
|
|
@ -0,0 +1,402 @@
|
|||
//! Chunk staging (C8 C3) against real repositories in temp folders:
|
||||
//! staging, unstaging and reverting single chunks; CRLF files (stored as
|
||||
//! LF through `core.autocrlf`, and stored as CRLF), files without a final
|
||||
//! line break, a byte order mark, a staged rename, the first commit, and
|
||||
//! every refusal (binary, untracked, stale token). 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 lutra_vcs::{FileChunks, Git, Repo, VcsError};
|
||||
|
||||
/// The installed git, or `None` (with a message) when there is none.
|
||||
fn installed() -> Option<Git> {
|
||||
match Git::detect("") {
|
||||
Ok(git) => Some(git),
|
||||
Err(e) => {
|
||||
eprintln!("skipped: no usable git installed ({e})");
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Runs git in `dir` for test setup, asserts it succeeded and returns its
|
||||
/// raw output.
|
||||
fn git_raw(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(out.stdout).unwrap()
|
||||
}
|
||||
|
||||
/// Runs git for setup.
|
||||
fn git(dir: &Path, args: &[&str]) {
|
||||
git_raw(dir, args);
|
||||
}
|
||||
|
||||
/// A new repository on `main` with a local identity and `autocrlf`.
|
||||
fn init(dir: &Path, autocrlf: &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.safecrlf", "false"),
|
||||
("core.hooksPath", ".git/hooks"),
|
||||
] {
|
||||
git(dir, &["config", key, value]);
|
||||
}
|
||||
}
|
||||
|
||||
/// Writes a file.
|
||||
fn write(dir: &Path, path: &str, text: &[u8]) {
|
||||
std::fs::write(dir.join(path), text).unwrap();
|
||||
}
|
||||
|
||||
/// The index blob of `path`, as stored.
|
||||
fn index(dir: &Path, path: &str) -> String {
|
||||
git_raw(dir, &["cat-file", "blob", &format!(":0:{path}")])
|
||||
}
|
||||
|
||||
/// A repository with `path` committed as `text`, opened.
|
||||
fn committed(g: &Git, dir: &Path, autocrlf: &str, path: &str, text: &str) -> Repo {
|
||||
init(dir, autocrlf);
|
||||
write(dir, path, text.as_bytes());
|
||||
git(dir, &["add", "-A"]);
|
||||
git(dir, &["commit", "-q", "-m", "init"]);
|
||||
g.open(dir).unwrap().expect("a repository")
|
||||
}
|
||||
|
||||
/// Ten numbered lines.
|
||||
const TEN: &str = "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n";
|
||||
|
||||
/// The chunks of `path` for `text`.
|
||||
fn chunks(repo: &Repo, path: &str, text: &str) -> FileChunks {
|
||||
repo.chunks(path, text).unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stages_and_unstages_one_of_two_chunks() {
|
||||
let Some(g) = installed() else { return };
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let dir = tmp.path();
|
||||
let repo = committed(&g, dir, "false", "a.php", TEN);
|
||||
let work = "1\ntwo\n3\n4\n5\n6\n7\n8\nnine\n10\n";
|
||||
write(dir, "a.php", work.as_bytes());
|
||||
|
||||
let before = chunks(&repo, "a.php", work);
|
||||
assert_eq!(before.unstaged.len(), 2);
|
||||
assert!(before.staged.is_empty());
|
||||
assert_eq!(before.unstaged[1].lines, 8..9);
|
||||
assert_eq!(before.unstaged[1].removed, vec!["9".to_string()]);
|
||||
assert_eq!(before.unstaged[1].added, vec!["nine".to_string()]);
|
||||
|
||||
repo.stage_chunk("a.php", work, 1, &before.token).unwrap();
|
||||
assert_eq!(index(dir, "a.php"), "1\n2\n3\n4\n5\n6\n7\n8\nnine\n10\n");
|
||||
let after = chunks(&repo, "a.php", work);
|
||||
assert_eq!(after.unstaged.len(), 1);
|
||||
assert_eq!(after.unstaged[0].lines, 1..2);
|
||||
assert_eq!(after.staged.len(), 1);
|
||||
assert_eq!(after.staged[0].lines, 8..9);
|
||||
|
||||
repo.unstage_chunk("a.php", work, 0, &after.token).unwrap();
|
||||
assert_eq!(index(dir, "a.php"), TEN);
|
||||
assert_eq!(chunks(&repo, "a.php", work).unstaged.len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_stale_token_or_text_is_refused() {
|
||||
let Some(g) = installed() else { return };
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let dir = tmp.path();
|
||||
let repo = committed(&g, dir, "false", "a.php", TEN);
|
||||
let work = "1\ntwo\n3\n4\n5\n6\n7\n8\n9\n10\n";
|
||||
let shown = chunks(&repo, "a.php", work);
|
||||
|
||||
let edited = "1\ntwo\n3\n4\n5\n6\n7\n8\n9\nten\n";
|
||||
assert_eq!(
|
||||
repo.stage_chunk("a.php", edited, 0, &shown.token),
|
||||
Err(VcsError::Stale)
|
||||
);
|
||||
assert_eq!(
|
||||
repo.stage_chunk("a.php", work, 5, &shown.token),
|
||||
Err(VcsError::Stale)
|
||||
);
|
||||
|
||||
write(dir, "a.php", b"1\n2\n3\n4\n5\n6\n7\n8\n9\nX\n");
|
||||
git(dir, &["add", "a.php"]);
|
||||
assert_eq!(
|
||||
repo.stage_chunk("a.php", work, 0, &shown.token),
|
||||
Err(VcsError::Stale)
|
||||
);
|
||||
assert_eq!(
|
||||
repo.revert_chunk("a.php", work, 0, &shown.token),
|
||||
Err(VcsError::Stale)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn crlf_working_copy_with_an_lf_index_stays_lf() {
|
||||
let Some(g) = installed() else { return };
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let dir = tmp.path();
|
||||
let repo = committed(&g, dir, "true", "a.php", "a\r\nb\r\nc\r\n");
|
||||
assert_eq!(index(dir, "a.php"), "a\nb\nc\n");
|
||||
let work = "a\r\nB\r\nc\r\nd\r\n";
|
||||
let shown = chunks(&repo, "a.php", work);
|
||||
assert_eq!(shown.unstaged.len(), 2);
|
||||
|
||||
repo.stage_chunk("a.php", work, 0, &shown.token).unwrap();
|
||||
assert_eq!(index(dir, "a.php"), "a\nB\nc\n");
|
||||
let next = chunks(&repo, "a.php", work);
|
||||
repo.stage_chunk("a.php", work, 0, &next.token).unwrap();
|
||||
assert_eq!(index(dir, "a.php"), "a\nB\nc\nd\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn crlf_stored_in_the_index_keeps_crlf() {
|
||||
let Some(g) = installed() else { return };
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let dir = tmp.path();
|
||||
let repo = committed(&g, dir, "false", "a.php", "a\r\nb\r\nc\r\n");
|
||||
let work = "a\nB\nc\n";
|
||||
let shown = chunks(&repo, "a.php", work);
|
||||
assert_eq!(shown.unstaged.len(), 1);
|
||||
repo.stage_chunk("a.php", work, 0, &shown.token).unwrap();
|
||||
assert_eq!(index(dir, "a.php"), "a\r\nB\r\nc\r\n");
|
||||
|
||||
let crlf = "a\r\nB\r\nc\r\n";
|
||||
let after = chunks(&repo, "a.php", crlf);
|
||||
assert!(after.unstaged.is_empty());
|
||||
assert_eq!(after.staged.len(), 1);
|
||||
repo.unstage_chunk("a.php", crlf, 0, &after.token).unwrap();
|
||||
assert_eq!(index(dir, "a.php"), "a\r\nb\r\nc\r\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn files_without_a_final_line_break() {
|
||||
let Some(g) = installed() else { return };
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let dir = tmp.path();
|
||||
let repo = committed(&g, dir, "false", "a.php", "a\nb");
|
||||
|
||||
let work = "a\nb\nc\n";
|
||||
let shown = chunks(&repo, "a.php", work);
|
||||
assert_eq!(shown.unstaged.len(), 1);
|
||||
repo.stage_chunk("a.php", work, 0, &shown.token).unwrap();
|
||||
assert_eq!(index(dir, "a.php"), "a\nb\nc\n");
|
||||
|
||||
let work = "a\nX";
|
||||
let shown = chunks(&repo, "a.php", work);
|
||||
assert_eq!(shown.unstaged.len(), 1);
|
||||
repo.stage_chunk("a.php", work, 0, &shown.token).unwrap();
|
||||
assert_eq!(index(dir, "a.php"), "a\nX");
|
||||
|
||||
let after = chunks(&repo, "a.php", work);
|
||||
let edit = repo.revert_chunk("a.php", "a\nY", 0, &chunks(&repo, "a.php", "a\nY").token);
|
||||
assert_eq!(edit.unwrap().insert, "X");
|
||||
repo.unstage_chunk("a.php", work, 0, &after.token).unwrap();
|
||||
assert_eq!(index(dir, "a.php"), "a\nb");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_byte_order_mark_survives_staging() {
|
||||
let Some(g) = installed() else { return };
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let dir = tmp.path();
|
||||
let repo = committed(&g, dir, "false", "a.php", "\u{feff}a\nb\nc\n");
|
||||
let work = "b\nc\nd\n";
|
||||
let shown = chunks(&repo, "a.php", work);
|
||||
assert_eq!(shown.unstaged.len(), 2);
|
||||
repo.stage_chunk("a.php", work, 0, &shown.token).unwrap();
|
||||
assert_eq!(index(dir, "a.php"), "\u{feff}b\nc\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reverting_returns_the_index_lines_for_the_buffer() {
|
||||
let Some(g) = installed() else { return };
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let dir = tmp.path();
|
||||
let repo = committed(&g, dir, "true", "a.php", "a\r\nb\r\nc\r\nd\r\n");
|
||||
let work = "a\r\nB\r\nB2\r\nc\r\n";
|
||||
let shown = chunks(&repo, "a.php", work);
|
||||
assert_eq!(shown.unstaged.len(), 2);
|
||||
|
||||
let edit = repo.revert_chunk("a.php", work, 0, &shown.token).unwrap();
|
||||
let mut text = work.to_string();
|
||||
text.replace_range(edit.range.clone(), &edit.insert);
|
||||
assert_eq!(text, "a\r\nb\r\nc\r\n");
|
||||
|
||||
let edit = repo.revert_chunk("a.php", work, 1, &shown.token).unwrap();
|
||||
let mut text = work.to_string();
|
||||
text.replace_range(edit.range, &edit.insert);
|
||||
assert_eq!(text, "a\r\nB\r\nB2\r\nc\r\nd\r\n");
|
||||
assert_eq!(index(dir, "a.php"), "a\nb\nc\nd\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reverting_lines_deleted_after_an_open_last_line() {
|
||||
let Some(g) = installed() else { return };
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let dir = tmp.path();
|
||||
let repo = committed(&g, dir, "false", "a.php", "a\nb\nc\n");
|
||||
let work = "a\nb";
|
||||
let shown = chunks(&repo, "a.php", work);
|
||||
assert_eq!(shown.unstaged.len(), 1);
|
||||
let edit = repo.revert_chunk("a.php", work, 0, &shown.token).unwrap();
|
||||
let mut text = work.to_string();
|
||||
text.replace_range(edit.range, &edit.insert);
|
||||
assert_eq!(text, "a\nb\nc\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_staged_rename_unstages_against_the_old_path() {
|
||||
let Some(g) = installed() else { return };
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let dir = tmp.path();
|
||||
let repo = committed(&g, dir, "false", "old.php", TEN);
|
||||
git(dir, &["mv", "old.php", "new.php"]);
|
||||
let work = "1\n2\n3\n4\n5\n6\n7\n8\n9\nten\n";
|
||||
write(dir, "new.php", work.as_bytes());
|
||||
git(dir, &["add", "new.php"]);
|
||||
|
||||
let shown = chunks(&repo, "new.php", work);
|
||||
assert_eq!(shown.head_path.as_deref(), Some("old.php"));
|
||||
assert!(shown.unstaged.is_empty());
|
||||
assert_eq!(shown.staged.len(), 1);
|
||||
assert_eq!(shown.staged[0].lines, 9..10);
|
||||
|
||||
repo.unstage_chunk("new.php", work, 0, &shown.token)
|
||||
.unwrap();
|
||||
assert_eq!(index(dir, "new.php"), TEN);
|
||||
let status = git_raw(dir, &["status", "--porcelain"]);
|
||||
assert!(status.contains("RM old.php -> new.php"), "{status}");
|
||||
|
||||
let after = chunks(&repo, "new.php", work);
|
||||
repo.stage_chunk("new.php", work, 0, &after.token).unwrap();
|
||||
assert_eq!(index(dir, "new.php"), work);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn before_the_first_commit_everything_staged_is_one_chunk() {
|
||||
let Some(g) = installed() else { return };
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let dir = tmp.path();
|
||||
init(dir, "false");
|
||||
write(dir, "a.php", b"a\nb\n");
|
||||
git(dir, &["add", "a.php"]);
|
||||
let repo = g.open(dir).unwrap().unwrap();
|
||||
let shown = chunks(&repo, "a.php", "a\nb\n");
|
||||
assert_eq!(shown.staged.len(), 1);
|
||||
assert_eq!(shown.staged[0].lines, 0..2);
|
||||
repo.unstage_chunk("a.php", "a\nb\n", 0, &shown.token)
|
||||
.unwrap();
|
||||
assert_eq!(index(dir, "a.php"), "");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn binary_and_untracked_files_are_refused() {
|
||||
let Some(g) = installed() else { return };
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let dir = tmp.path();
|
||||
init(dir, "false");
|
||||
write(dir, "bin.dat", b"a\0b\n");
|
||||
git(dir, &["add", "-A"]);
|
||||
git(dir, &["commit", "-q", "-m", "init"]);
|
||||
write(dir, "new.php", b"x\n");
|
||||
let repo = g.open(dir).unwrap().unwrap();
|
||||
assert!(matches!(
|
||||
repo.chunks("bin.dat", "a\n"),
|
||||
Err(VcsError::ChunkRefused(m)) if m.contains("binary")
|
||||
));
|
||||
assert!(matches!(
|
||||
repo.chunks("new.php", "x\n"),
|
||||
Err(VcsError::ChunkRefused(m)) if m.contains("no staged version")
|
||||
));
|
||||
assert!(matches!(
|
||||
repo.chunks("../x", ""),
|
||||
Err(VcsError::InvalidPath(_))
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_path_with_spaces_and_quotes_stages() {
|
||||
let Some(g) = installed() else { return };
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let dir = tmp.path();
|
||||
let name = if cfg!(windows) {
|
||||
"a b.php"
|
||||
} else {
|
||||
"a \"b\".php"
|
||||
};
|
||||
let repo = committed(&g, dir, "false", name, TEN);
|
||||
let work = "1\n2\n3\nfour\n5\n6\n7\n8\n9\n10\n";
|
||||
let shown = chunks(&repo, name, work);
|
||||
repo.stage_chunk(name, work, 0, &shown.token).unwrap();
|
||||
assert_eq!(index(dir, name), work);
|
||||
}
|
||||
|
||||
proptest::proptest! {
|
||||
#![proptest_config(proptest::prelude::ProptestConfig::with_cases(6))]
|
||||
|
||||
/// Staging every chunk one by one makes the index equal the working
|
||||
/// text (up to the final break, which no chunk carries alone).
|
||||
#[test]
|
||||
fn staging_every_chunk_reaches_the_working_text(
|
||||
old in proptest::collection::vec("[abc]", 0..8),
|
||||
new in proptest::collection::vec("[abc]", 0..8),
|
||||
old_open in proptest::bool::ANY,
|
||||
new_open in proptest::bool::ANY,
|
||||
) {
|
||||
let Some(g) = installed() else { return Ok(()) };
|
||||
let join = |lines: &[String], open: bool| {
|
||||
let mut text = lines.join("\n");
|
||||
if !open && !lines.is_empty() {
|
||||
text.push('\n');
|
||||
}
|
||||
text
|
||||
};
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let dir = tmp.path();
|
||||
let repo = committed(&g, dir, "false", "a.txt", &join(&old, old_open));
|
||||
let work = join(&new, new_open);
|
||||
for _ in 0..30 {
|
||||
let shown = chunks(&repo, "a.txt", &work);
|
||||
if shown.unstaged.is_empty() {
|
||||
break;
|
||||
}
|
||||
repo.stage_chunk("a.txt", &work, 0, &shown.token).unwrap();
|
||||
}
|
||||
let staged = index(dir, "a.txt");
|
||||
proptest::prop_assert_eq!(staged.trim_end_matches('\n'), work.trim_end_matches('\n'));
|
||||
for _ in 0..30 {
|
||||
let shown = chunks(&repo, "a.txt", &work);
|
||||
if shown.staged.is_empty() {
|
||||
break;
|
||||
}
|
||||
repo.unstage_chunk("a.txt", &work, 0, &shown.token).unwrap();
|
||||
}
|
||||
let back = index(dir, "a.txt");
|
||||
let original = join(&old, old_open);
|
||||
proptest::prop_assert_eq!(back.trim_end_matches('\n'), original.trim_end_matches('\n'));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue