| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (c) 2026 UtopiaIA |
| 3 | |
| 4 | //! Differential tests of the history queries, with the git command line as |
| 5 | //! the oracle: on fixture repositories built here, on random histories, and |
| 6 | //! on the history of this repository, imported into braid and mapped back |
| 7 | //! through the git map. |
| 8 | //! |
| 9 | //! - `log -- <path>` against `git log --first-parent --format=%H -- <path>`, |
| 10 | //! for every path a history ever had, its directories, and paths it never |
| 11 | //! had; |
| 12 | //! - merge bases against `git merge-base --all`; |
| 13 | //! - blame against `git blame --first-parent --line-porcelain` (no `-w`, |
| 14 | //! `-M` or `-C`), line by line, for every file at several revisions. |
| 15 | //! |
| 16 | //! Blame differs from git's in two documented ways, and the tests check that |
| 17 | //! it differs in no other: |
| 18 | //! |
| 19 | //! - Git's blame follows a file across whole-file renames (it has no switch |
| 20 | //! to turn that off), and braid's does not. A line that git traces to a |
| 21 | //! changeset older than the one where the path last appeared on the |
| 22 | //! first-parent chain is, for braid, that changeset's, and git must have |
| 23 | //! reached it under another file name. |
| 24 | //! - Git's diff and braid's can differ: when a change has several shortest |
| 25 | //! line diffs, they may keep different copies of lines that repeat, such |
| 26 | //! as closing braces; and neither always finds a shortest diff, so on some |
| 27 | //! changes one inserts a few more lines than the other. Where the two |
| 28 | //! blames differ, the tests walk the file's versions along the first-parent |
| 29 | //! chain: replaying git's own diffs (`git diff -U0`) must give git's blame |
| 30 | //! line for line; braid's blame at each version must be braid's own diff |
| 31 | //! applied to its blame at the version before, a diff that keeps only |
| 32 | //! equal lines; and that diff must never insert fewer lines than a |
| 33 | //! shortest diff does, which would be a bug. That bound is computed here |
| 34 | //! exactly, from a longest common subsequence: `git diff --minimal` is not |
| 35 | //! one, since git treats lines that repeat very often specially even then. |
| 36 | //! Files |
| 37 | //! where the two diffs insert as many lines at every version count as |
| 38 | //! realigned, the others as heuristic divergences. |
| 39 | |
| 40 | use std::collections::{BTreeMap, BTreeSet, HashMap}; |
| 41 | use std::ops::Range; |
| 42 | use std::path::{Path, PathBuf}; |
| 43 | use std::process::{Command, Stdio}; |
| 44 | |
| 45 | use braid_engine::{LineChange, read_changes, read_changeset}; |
| 46 | use braid_format::{EntryKind, ObjectId}; |
| 47 | use braid_git::{GitMap, GitOid, ImportOptions}; |
| 48 | use braid_repo::{Repo, RepoHistory}; |
| 49 | use tempfile::TempDir; |
| 50 | |
| 51 | /// Variables that would point git at another repository or change what it |
| 52 | /// reads. |
| 53 | const LEAKY_VARIABLES: [&str; 12] = [ |
| 54 | "GIT_DIR", |
| 55 | "GIT_WORK_TREE", |
| 56 | "GIT_INDEX_FILE", |
| 57 | "GIT_OBJECT_DIRECTORY", |
| 58 | "GIT_ALTERNATE_OBJECT_DIRECTORIES", |
| 59 | "GIT_COMMON_DIR", |
| 60 | "GIT_NAMESPACE", |
| 61 | "GIT_CEILING_DIRECTORIES", |
| 62 | "GIT_REPLACE_REF_BASE", |
| 63 | "GIT_NO_REPLACE_OBJECTS", |
| 64 | "GIT_GRAFT_FILE", |
| 65 | "GIT_SHALLOW_FILE", |
| 66 | ]; |
| 67 | |
| 68 | /// A git command in `dir`, without the user's or the system's |
| 69 | /// configuration, with fixed identities, the clock at `seconds`, and |
| 70 | /// literal pathspecs. |
| 71 | fn git_command(dir: &Path, seconds: i64) -> Command { |
| 72 | let mut command = Command::new("git"); |
| 73 | command.current_dir(dir); |
| 74 | for variable in LEAKY_VARIABLES { |
| 75 | command.env_remove(variable); |
| 76 | } |
| 77 | let date = format!("{seconds} +0200"); |
| 78 | command |
| 79 | .env("GIT_CONFIG_GLOBAL", "/dev/null") |
| 80 | .env("GIT_CONFIG_NOSYSTEM", "1") |
| 81 | .env("GIT_LITERAL_PATHSPECS", "1") |
| 82 | .env("GIT_AUTHOR_NAME", "Ada Lovelace") |
| 83 | .env("GIT_AUTHOR_EMAIL", "ada@example.org") |
| 84 | .env("GIT_AUTHOR_DATE", &date) |
| 85 | .env("GIT_COMMITTER_NAME", "Charles Babbage") |
| 86 | .env("GIT_COMMITTER_EMAIL", "charles@example.org") |
| 87 | .env("GIT_COMMITTER_DATE", &date) |
| 88 | .args([ |
| 89 | "-c", |
| 90 | "init.defaultBranch=main", |
| 91 | "-c", |
| 92 | "commit.gpgSign=false", |
| 93 | "-c", |
| 94 | "core.autocrlf=false", |
| 95 | "-c", |
| 96 | "core.quotePath=false", |
| 97 | "-c", |
| 98 | "gc.auto=0", |
| 99 | ]); |
| 100 | command |
| 101 | } |
| 102 | |
| 103 | /// Runs git and returns its standard output; `None` when it exits with 1, |
| 104 | /// as `git merge-base` does when there is no answer. |
| 105 | fn run(mut command: Command) -> Option<Vec<u8>> { |
| 106 | let output = command |
| 107 | .stdin(Stdio::null()) |
| 108 | .output() |
| 109 | .expect("git must be installed to run these tests"); |
| 110 | match output.status.code() { |
| 111 | Some(0) => Some(output.stdout), |
| 112 | Some(1) if output.stderr.is_empty() => None, |
| 113 | _ => panic!( |
| 114 | "{command:?} failed: {}", |
| 115 | String::from_utf8_lossy(&output.stderr) |
| 116 | ), |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | fn text(bytes: Vec<u8>) -> String { |
| 121 | String::from_utf8(bytes).unwrap() |
| 122 | } |
| 123 | |
| 124 | /// A git repository with a work tree, in a temporary directory. |
| 125 | struct Fixture { |
| 126 | _dir: TempDir, |
| 127 | work: PathBuf, |
| 128 | clock: i64, |
| 129 | } |
| 130 | |
| 131 | impl Fixture { |
| 132 | fn new() -> Self { |
| 133 | let dir = tempfile::tempdir().unwrap(); |
| 134 | let work = dir.path().join("work"); |
| 135 | std::fs::create_dir(&work).unwrap(); |
| 136 | let mut fixture = Fixture { |
| 137 | _dir: dir, |
| 138 | work, |
| 139 | clock: 1_700_000_000, |
| 140 | }; |
| 141 | fixture.git(&["init", "-q"]); |
| 142 | fixture |
| 143 | } |
| 144 | |
| 145 | fn git(&mut self, args: &[&str]) -> String { |
| 146 | let mut command = git_command(&self.work, self.clock); |
| 147 | command.args(args); |
| 148 | let out = run(command).unwrap_or_else(|| panic!("git {args:?} exited with 1")); |
| 149 | text(out).trim().to_owned() |
| 150 | } |
| 151 | |
| 152 | fn write(&self, path: &str, content: &str) { |
| 153 | let path = self.work.join(path); |
| 154 | std::fs::create_dir_all(path.parent().unwrap()).unwrap(); |
| 155 | std::fs::write(path, content).unwrap(); |
| 156 | } |
| 157 | |
| 158 | fn remove(&self, path: &str) { |
| 159 | std::fs::remove_file(self.work.join(path)).unwrap(); |
| 160 | } |
| 161 | |
| 162 | /// Makes a file executable, in the work tree where the file system can |
| 163 | /// say so, and in the index. |
| 164 | fn make_executable(&mut self, path: &str) { |
| 165 | #[cfg(unix)] |
| 166 | { |
| 167 | use std::os::unix::fs::PermissionsExt; |
| 168 | let file = self.work.join(path); |
| 169 | std::fs::set_permissions(file, std::fs::Permissions::from_mode(0o755)).unwrap(); |
| 170 | } |
| 171 | self.git(&["update-index", "--chmod=+x", path]); |
| 172 | } |
| 173 | |
| 174 | /// Commits everything in the work tree, even nothing. |
| 175 | fn commit(&mut self, message: &str) -> String { |
| 176 | self.clock += 60; |
| 177 | self.git(&["add", "-A"]); |
| 178 | self.git(&["commit", "-q", "--allow-empty", "-m", message]); |
| 179 | self.git(&["rev-parse", "HEAD"]) |
| 180 | } |
| 181 | |
| 182 | /// Merges `others` into the current branch with a merge commit. |
| 183 | fn merge(&mut self, others: &[&str], message: &str) -> String { |
| 184 | self.clock += 60; |
| 185 | let mut args = vec!["merge", "-q", "--no-ff", "--no-edit", "-m", message]; |
| 186 | args.extend(others); |
| 187 | self.git(&args); |
| 188 | self.git(&["rev-parse", "HEAD"]) |
| 189 | } |
| 190 | |
| 191 | fn checkout(&mut self, branch: &str) { |
| 192 | self.git(&["checkout", "-q", branch]); |
| 193 | } |
| 194 | |
| 195 | fn branch(&mut self, branch: &str) { |
| 196 | self.git(&["checkout", "-q", "-b", branch]); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | /// Lines `1..=n` of a file, each naming `tag`. |
| 201 | fn lines(tag: &str, n: usize) -> String { |
| 202 | (1..=n).map(|i| format!("{tag} {i}\n")).collect() |
| 203 | } |
| 204 | |
| 205 | /// Builds a history with everything blame and path history meet: edits, |
| 206 | /// renames, deletions and re-additions, a file that becomes a directory, |
| 207 | /// mode changes, an empty commit, merges whose side branches change files, |
| 208 | /// an octopus merge, criss-cross merges, and blocks of lines that move and |
| 209 | /// repeat. |
| 210 | fn build_fixture() -> Fixture { |
| 211 | let mut f = Fixture::new(); |
| 212 | f.write("README.md", &lines("readme", 6)); |
| 213 | f.write("src/lib.rs", &lines("lib", 12)); |
| 214 | f.write("src/util.rs", &lines("util", 10)); |
| 215 | f.write("docs/a.txt", &lines("a", 5)); |
| 216 | f.write("docs/read me.txt", "spaces\n"); |
| 217 | f.write("notes", "a file for now\n"); |
| 218 | f.write("caf\u{e9}.txt", &lines("caf\u{e9}", 3)); |
| 219 | f.commit("First"); |
| 220 | |
| 221 | let lib = lines("lib", 12).replace("lib 3\n", "LIB 3\n") + "lib 13\n"; |
| 222 | f.write("src/lib.rs", &lib); |
| 223 | f.commit("Edit lib"); |
| 224 | |
| 225 | f.remove("docs/a.txt"); |
| 226 | f.commit("Remove a"); |
| 227 | |
| 228 | // A side branch edits util and adds a file; main edits the README. |
| 229 | f.branch("side"); |
| 230 | f.write( |
| 231 | "src/util.rs", |
| 232 | &lines("util", 10).replace("util 5\n", "side 5\n"), |
| 233 | ); |
| 234 | f.write("side.txt", &lines("side", 3)); |
| 235 | f.commit("Side work"); |
| 236 | f.checkout("main"); |
| 237 | f.write("README.md", &(lines("readme", 6) + "readme 7\n")); |
| 238 | f.commit("Readme grows"); |
| 239 | f.merge(&["side"], "Merge side"); |
| 240 | |
| 241 | // A rename with an edit, and a rename of a whole directory. |
| 242 | f.git(&["mv", "src/util.rs", "src/helpers.rs"]); |
| 243 | let helpers = lines("util", 10).replace("util 5\n", "side 5\n") + "helpers 11\n"; |
| 244 | f.write("src/helpers.rs", &helpers); |
| 245 | f.commit("Rename util to helpers"); |
| 246 | f.git(&["mv", "docs", "manual"]); |
| 247 | f.commit("Rename docs to manual"); |
| 248 | |
| 249 | // Deleted earlier, added again with some of the old lines. |
| 250 | f.write("docs/a.txt", &(lines("a", 3) + "new a 4\n")); |
| 251 | f.commit("Add a again"); |
| 252 | |
| 253 | // A file that becomes a directory. |
| 254 | f.remove("notes"); |
| 255 | f.write("notes/today.txt", &lines("today", 2)); |
| 256 | f.commit("Notes become a directory"); |
| 257 | |
| 258 | // A mode change only, then an empty commit. |
| 259 | f.make_executable("src/lib.rs"); |
| 260 | f.commit("Make lib executable"); |
| 261 | f.commit("Nothing"); |
| 262 | |
| 263 | // An octopus merge of three branches. |
| 264 | for (branch, file) in [ |
| 265 | ("o1", "o1.txt"), |
| 266 | ("o2", "src/o2.rs"), |
| 267 | ("o3", "manual/o3.md"), |
| 268 | ] { |
| 269 | f.branch(branch); |
| 270 | f.write(file, &lines(branch, 4)); |
| 271 | f.commit(&format!("Work on {branch}")); |
| 272 | f.checkout("main"); |
| 273 | } |
| 274 | f.write("src/lib.rs", &(lib.clone() + "lib 14\n")); |
| 275 | f.commit("Lib grows"); |
| 276 | f.merge(&["o1", "o2", "o3"], "Octopus"); |
| 277 | |
| 278 | // Criss-cross: x and y merge each other. |
| 279 | f.branch("x"); |
| 280 | f.write("x.txt", &lines("x", 3)); |
| 281 | let x1 = f.commit("x 1"); |
| 282 | f.checkout("main"); |
| 283 | f.branch("y"); |
| 284 | f.write("y.txt", &lines("y", 3)); |
| 285 | let y1 = f.commit("y 1"); |
| 286 | f.merge(&[x1.as_str()], "y merges x"); |
| 287 | f.checkout("x"); |
| 288 | f.merge(&[y1.as_str()], "x merges y"); |
| 289 | f.write("x.txt", &(lines("x", 3) + "x 4\n")); |
| 290 | f.commit("x 2"); |
| 291 | f.checkout("y"); |
| 292 | f.write("y.txt", &(lines("y", 3) + "y 4\n")); |
| 293 | f.commit("y 2"); |
| 294 | f.checkout("main"); |
| 295 | f.merge(&["x"], "Merge x"); |
| 296 | f.merge(&["y"], "Merge y"); |
| 297 | |
| 298 | // Blocks that move, lines that repeat, a line that loses its newline. |
| 299 | let blocks = "fn one() {\n 1\n}\n\nfn two() {\n 2\n}\n\nfn three() {\n 3\n}\n"; |
| 300 | f.write("src/blocks.rs", blocks); |
| 301 | f.commit("Blocks"); |
| 302 | let swapped = "fn three() {\n 3\n}\n\nfn one() {\n 1\n}\n\nfn two() {\n 2\n}\n"; |
| 303 | f.write("src/blocks.rs", swapped); |
| 304 | f.commit("Move three up"); |
| 305 | f.write( |
| 306 | "src/blocks.rs", |
| 307 | &(swapped.to_owned() + "\nfn one() {\n 1\n}\n"), |
| 308 | ); |
| 309 | f.commit("Repeat one"); |
| 310 | f.write("README.md", &(lines("readme", 6) + "readme 7")); |
| 311 | f.commit("README without a final newline"); |
| 312 | f.write( |
| 313 | "src/lib.rs", |
| 314 | &lib.replace("lib 7\n", "").replace("lib 9\n", "LIB 9\n"), |
| 315 | ); |
| 316 | f.commit("Lib shrinks"); |
| 317 | f |
| 318 | } |
| 319 | |
| 320 | /// A braid repository with a git repository imported, and the map back. |
| 321 | struct Imported { |
| 322 | _dir: TempDir, |
| 323 | repo: Repo, |
| 324 | map: GitMap, |
| 325 | } |
| 326 | |
| 327 | impl Imported { |
| 328 | /// Imports every branch and tag. |
| 329 | fn all(git: &Path) -> Self { |
| 330 | let dir = tempfile::tempdir().unwrap(); |
| 331 | let repo = Repo::init(dir.path()).unwrap(); |
| 332 | repo.git_import(git, "test").unwrap(); |
| 333 | let map = repo.git_map().unwrap(); |
| 334 | Imported { |
| 335 | _dir: dir, |
| 336 | repo, |
| 337 | map, |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | /// Imports what `HEAD` reaches. |
| 342 | fn head(git: &Path) -> Self { |
| 343 | let dir = tempfile::tempdir().unwrap(); |
| 344 | let repo = Repo::init(dir.path()).unwrap(); |
| 345 | let mut map = GitMap::new(); |
| 346 | braid_git::import( |
| 347 | git, |
| 348 | repo.store(), |
| 349 | &mut map, |
| 350 | &ImportOptions { |
| 351 | refs: vec![b"HEAD".to_vec()], |
| 352 | }, |
| 353 | ) |
| 354 | .unwrap(); |
| 355 | Imported { |
| 356 | _dir: dir, |
| 357 | repo, |
| 358 | map, |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | fn changeset(&self, commit: &str) -> ObjectId { |
| 363 | let oid: GitOid = commit |
| 364 | .parse() |
| 365 | .unwrap_or_else(|_| panic!("{commit:?} is not a commit id")); |
| 366 | self.map |
| 367 | .changeset(&oid) |
| 368 | .unwrap_or_else(|| panic!("commit {commit} was not imported")) |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | /// What the comparisons found. |
| 373 | #[derive(Debug, Default)] |
| 374 | struct Tally { |
| 375 | paths: usize, |
| 376 | merge_bases: usize, |
| 377 | blamed_files: usize, |
| 378 | blamed_lines: usize, |
| 379 | /// Lines git traced across a rename, which braid gives to the changeset |
| 380 | /// where the path appeared. |
| 381 | renamed_lines: usize, |
| 382 | /// Files and lines where braid and git differ while their diffs insert |
| 383 | /// as many lines at every version: they kept different copies of equal |
| 384 | /// lines. |
| 385 | realigned_files: usize, |
| 386 | realigned_lines: usize, |
| 387 | /// Files and lines where braid and git differ and, at some version, |
| 388 | /// their diffs insert different numbers of lines, none fewer than the |
| 389 | /// shortest diff. |
| 390 | heuristic_files: usize, |
| 391 | heuristic_lines: usize, |
| 392 | /// Versions whose blame was checked against braid's own diff. |
| 393 | versions_checked: usize, |
| 394 | /// The files that differ, as `path at revision`, and why. |
| 395 | differences: Vec<String>, |
| 396 | } |
| 397 | |
| 398 | /// `log -- <path>` for each path, against git. |
| 399 | fn compare_path_logs( |
| 400 | git: &Path, |
| 401 | imported: &Imported, |
| 402 | history: &mut RepoHistory<'_>, |
| 403 | revision: &str, |
| 404 | paths: &BTreeSet<String>, |
| 405 | tally: &mut Tally, |
| 406 | ) { |
| 407 | let head = imported.changeset(&rev_parse(git, revision)); |
| 408 | for path in paths { |
| 409 | let mut command = git_command(git, 0); |
| 410 | command.args(["log", "--first-parent", "--format=%H", revision, "--", path]); |
| 411 | let expected: Vec<ObjectId> = text(run(command).unwrap()) |
| 412 | .lines() |
| 413 | .map(|commit| imported.changeset(commit)) |
| 414 | .collect(); |
| 415 | let braid_path = if path == "." { "" } else { path.as_str() }; |
| 416 | let found = history |
| 417 | .path_log(head, &[braid_path.as_bytes()], usize::MAX) |
| 418 | .unwrap(); |
| 419 | assert_eq!(found, expected, "log {revision} -- {path}"); |
| 420 | tally.paths += 1; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | /// Merge bases of each pair, against git. |
| 425 | fn compare_merge_bases( |
| 426 | git: &Path, |
| 427 | imported: &Imported, |
| 428 | history: &mut RepoHistory<'_>, |
| 429 | pairs: &[(String, String)], |
| 430 | tally: &mut Tally, |
| 431 | ) { |
| 432 | for (a, b) in pairs { |
| 433 | let mut command = git_command(git, 0); |
| 434 | command.args(["merge-base", "--all", a, b]); |
| 435 | let mut expected: Vec<ObjectId> = run(command) |
| 436 | .map(text) |
| 437 | .unwrap_or_default() |
| 438 | .lines() |
| 439 | .map(|commit| imported.changeset(commit)) |
| 440 | .collect(); |
| 441 | expected.sort(); |
| 442 | let (x, y) = ( |
| 443 | imported.changeset(&rev_parse(git, a)), |
| 444 | imported.changeset(&rev_parse(git, b)), |
| 445 | ); |
| 446 | assert_eq!( |
| 447 | history.merge_bases(x, y).unwrap(), |
| 448 | expected, |
| 449 | "merge bases of {a} and {b}" |
| 450 | ); |
| 451 | // The walking version agrees too. |
| 452 | assert_eq!( |
| 453 | braid_engine::merge_bases(imported.repo.store(), x, y).unwrap(), |
| 454 | expected |
| 455 | ); |
| 456 | tally.merge_bases += 1; |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | /// One line of `git blame --line-porcelain`: the commit and the file name |
| 461 | /// the line has there. |
| 462 | struct GitLine { |
| 463 | commit: String, |
| 464 | filename: String, |
| 465 | } |
| 466 | |
| 467 | fn git_blame(git: &Path, revision: &str, path: &str) -> Vec<GitLine> { |
| 468 | let mut command = git_command(git, 0); |
| 469 | command.args([ |
| 470 | "blame", |
| 471 | "--first-parent", |
| 472 | "--line-porcelain", |
| 473 | revision, |
| 474 | "--", |
| 475 | path, |
| 476 | ]); |
| 477 | let output = run(command).unwrap(); |
| 478 | let mut lines = Vec::new(); |
| 479 | let mut commit = None; |
| 480 | let mut filename = None; |
| 481 | for line in output.split(|byte| *byte == b'\n') { |
| 482 | if line.first() == Some(&b'\t') { |
| 483 | lines.push(GitLine { |
| 484 | commit: commit.take().unwrap(), |
| 485 | filename: filename.take().unwrap(), |
| 486 | }); |
| 487 | } else if commit.is_none() |
| 488 | && line.len() > 41 |
| 489 | && line[40] == b' ' |
| 490 | && line[..40].iter().all(u8::is_ascii_hexdigit) |
| 491 | { |
| 492 | commit = Some(text(line[..40].to_vec())); |
| 493 | } else if let Some(name) = line.strip_prefix(b"filename ") { |
| 494 | filename = Some(text(name.to_vec())); |
| 495 | } |
| 496 | } |
| 497 | lines |
| 498 | } |
| 499 | |
| 500 | /// The versions of a file along the first-parent chain of `at`, oldest |
| 501 | /// first: each changeset that set it since it last appeared there, where |
| 502 | /// its first parent had no file, with the blob it set. Found by reading |
| 503 | /// changesets, without derived data. |
| 504 | fn versions(repo: &Repo, at: ObjectId, path: &str) -> Vec<(ObjectId, ObjectId)> { |
| 505 | let objects = repo.store(); |
| 506 | let file = |kind: &EntryKind| { |
| 507 | matches!( |
| 508 | kind, |
| 509 | EntryKind::File { .. } | EntryKind::Executable { .. } | EntryKind::Symlink { .. } |
| 510 | ) |
| 511 | }; |
| 512 | let mut found = Vec::new(); |
| 513 | let mut current = at; |
| 514 | loop { |
| 515 | let changeset = read_changeset(objects, current).unwrap(); |
| 516 | let changes = read_changes(objects, current, &changeset).unwrap(); |
| 517 | let parent = changeset.parents.first().copied(); |
| 518 | let set = changes |
| 519 | .iter() |
| 520 | .find(|change| change.path == path.as_bytes()) |
| 521 | .and_then(|change| change.op.to_entry()) |
| 522 | .filter(file); |
| 523 | if let Some(kind) = set { |
| 524 | let (EntryKind::File { blob, .. } |
| 525 | | EntryKind::Executable { blob, .. } |
| 526 | | EntryKind::Symlink { blob, .. }) = kind |
| 527 | else { |
| 528 | unreachable!("only files are kept") |
| 529 | }; |
| 530 | found.push((current, blob)); |
| 531 | let before = parent.and_then(|parent| { |
| 532 | let root = read_changeset(objects, parent).unwrap().root; |
| 533 | braid_engine::entry_at(objects, root, path.as_bytes()).unwrap() |
| 534 | }); |
| 535 | if !before.as_ref().is_some_and(file) { |
| 536 | found.reverse(); |
| 537 | return found; |
| 538 | } |
| 539 | } |
| 540 | current = parent.unwrap_or_else(|| panic!("{path} never appeared")); |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | /// The hunks of `git diff -U0` between two blobs: for each, the lines it |
| 545 | /// replaces and the lines it puts there, counted from 0. |
| 546 | fn git_diff_hunks(git: &Path, old: &GitOid, new: &GitOid) -> Vec<(Range<usize>, Range<usize>)> { |
| 547 | if old == new { |
| 548 | return Vec::new(); |
| 549 | } |
| 550 | let mut command = git_command(git, 0); |
| 551 | command.args([ |
| 552 | "diff", |
| 553 | "-U0", |
| 554 | "--text", |
| 555 | "--no-color", |
| 556 | "--no-ext-diff", |
| 557 | "--indent-heuristic", |
| 558 | ]); |
| 559 | command.args([old.to_hex(), new.to_hex()]); |
| 560 | let range = |spec: &str| -> Range<usize> { |
| 561 | let (start, len) = match spec.split_once(',') { |
| 562 | Some((start, len)) => (start.parse::<usize>().unwrap(), len.parse().unwrap()), |
| 563 | None => (spec.parse().unwrap(), 1), |
| 564 | }; |
| 565 | if len == 0 { |
| 566 | start..start |
| 567 | } else { |
| 568 | start - 1..start - 1 + len |
| 569 | } |
| 570 | }; |
| 571 | text(run(command).unwrap()) |
| 572 | .lines() |
| 573 | .filter_map(|line| line.strip_prefix("@@ -")) |
| 574 | .map(|header| { |
| 575 | let (before, rest) = header.split_once(" +").unwrap(); |
| 576 | let after = rest.split_once(" @@").unwrap().0; |
| 577 | (range(before), range(after)) |
| 578 | }) |
| 579 | .collect() |
| 580 | } |
| 581 | |
| 582 | /// The origins of the lines of a new version, from those of the old one |
| 583 | /// and the hunks that make it, for the changeset `id` that made it. |
| 584 | fn replay( |
| 585 | old: &[ObjectId], |
| 586 | new_lines: usize, |
| 587 | hunks: &[(Range<usize>, Range<usize>)], |
| 588 | id: ObjectId, |
| 589 | ) -> Vec<ObjectId> { |
| 590 | let mut origins = Vec::with_capacity(new_lines); |
| 591 | let mut kept = 0; |
| 592 | for (before, after) in hunks { |
| 593 | origins.extend_from_slice(&old[kept..before.start]); |
| 594 | origins.extend(std::iter::repeat_n(id, after.len())); |
| 595 | kept = before.end; |
| 596 | } |
| 597 | origins.extend_from_slice(&old[kept..]); |
| 598 | assert_eq!(origins.len(), new_lines); |
| 599 | origins |
| 600 | } |
| 601 | |
| 602 | /// The lines of a blob, as blame counts them: each ends after a `\n`, and |
| 603 | /// a last line without one is a line too. |
| 604 | fn split_lines(content: &[u8]) -> Vec<&[u8]> { |
| 605 | let mut lines: Vec<&[u8]> = content.split_inclusive(|byte| *byte == b'\n').collect(); |
| 606 | if content.is_empty() { |
| 607 | lines.clear(); |
| 608 | } |
| 609 | lines |
| 610 | } |
| 611 | |
| 612 | /// The lines of the new version that braid's line diff keeps, each paired |
| 613 | /// with a line of the old version, must be equal to it. |
| 614 | fn check_kept_lines(old: &[u8], new: &[u8], changes: &[LineChange], what: &str) { |
| 615 | let (old, new) = (split_lines(old), split_lines(new)); |
| 616 | let (mut at_old, mut at_new) = (0, 0); |
| 617 | for change in changes { |
| 618 | assert_eq!( |
| 619 | change.old.start - at_old, |
| 620 | change.new.start - at_new, |
| 621 | "{what}: as many kept lines on both sides" |
| 622 | ); |
| 623 | while at_old < change.old.start { |
| 624 | assert_eq!(old[at_old], new[at_new], "{what}: a kept line is equal"); |
| 625 | at_old += 1; |
| 626 | at_new += 1; |
| 627 | } |
| 628 | (at_old, at_new) = (change.old.end, change.new.end); |
| 629 | } |
| 630 | assert_eq!( |
| 631 | &old[at_old..], |
| 632 | &new[at_new..], |
| 633 | "{what}: the kept end is equal" |
| 634 | ); |
| 635 | } |
| 636 | |
| 637 | /// Braid's blame of every version of a file, oldest first, checked against |
| 638 | /// braid's own line diff: the first version is all its own, and each other |
| 639 | /// one is the diff applied to the blame of the version before, a diff that |
| 640 | /// keeps only equal lines. Returns each version's content and blame. |
| 641 | fn check_versions( |
| 642 | imported: &Imported, |
| 643 | history: &mut RepoHistory<'_>, |
| 644 | path: &str, |
| 645 | versions: &[(ObjectId, ObjectId)], |
| 646 | tally: &mut Tally, |
| 647 | ) -> Vec<(Vec<u8>, Vec<ObjectId>)> { |
| 648 | let mut checked: Vec<(Vec<u8>, Vec<ObjectId>)> = Vec::new(); |
| 649 | for (changeset, blob) in versions { |
| 650 | let what = format!("{path} at {changeset}"); |
| 651 | let content = braid_engine::read_blob(imported.repo.store(), *blob).unwrap(); |
| 652 | let lines = split_lines(&content).len(); |
| 653 | let own = history |
| 654 | .blame(*changeset, path.as_bytes()) |
| 655 | .unwrap() |
| 656 | .unwrap() |
| 657 | .origins; |
| 658 | assert_eq!(own.len(), lines, "{what}: one origin per line"); |
| 659 | match checked.last() { |
| 660 | None => assert!( |
| 661 | own.iter().all(|origin| origin == changeset), |
| 662 | "{what}: a new file is all its own" |
| 663 | ), |
| 664 | Some((old_content, old_own)) => { |
| 665 | let changes = braid_engine::line_diff(old_content, &content); |
| 666 | check_kept_lines(old_content, &content, &changes, &what); |
| 667 | let hunks: Vec<(Range<usize>, Range<usize>)> = changes |
| 668 | .iter() |
| 669 | .map(|change| (change.old.clone(), change.new.clone())) |
| 670 | .collect(); |
| 671 | assert_eq!( |
| 672 | own, |
| 673 | replay(old_own, lines, &hunks, *changeset), |
| 674 | "{what}: braid's blame follows braid's diff" |
| 675 | ); |
| 676 | } |
| 677 | } |
| 678 | tally.versions_checked += 1; |
| 679 | checked.push((content, own)); |
| 680 | } |
| 681 | checked |
| 682 | } |
| 683 | |
| 684 | /// Lines a diff's hunks insert. |
| 685 | fn inserts(hunks: &[(Range<usize>, Range<usize>)]) -> usize { |
| 686 | hunks.iter().map(|(_, after)| after.len()).sum() |
| 687 | } |
| 688 | |
| 689 | /// Lines a shortest diff from `old` to `new` inserts: the new lines outside a |
| 690 | /// longest common subsequence. For a given pair, insertions minus deletions |
| 691 | /// is fixed, so a diff with the fewest edits also has the fewest insertions. |
| 692 | /// Exact, unlike any heuristic diff: the common prefix and suffix are cut, |
| 693 | /// then the middle is solved by dynamic programming. |
| 694 | fn fewest_inserts(old: &[u8], new: &[u8]) -> usize { |
| 695 | let (old, new) = (split_lines(old), split_lines(new)); |
| 696 | let prefix = old.iter().zip(&new).take_while(|(a, b)| a == b).count(); |
| 697 | let (old, new) = (&old[prefix..], &new[prefix..]); |
| 698 | let suffix = old |
| 699 | .iter() |
| 700 | .rev() |
| 701 | .zip(new.iter().rev()) |
| 702 | .take_while(|(a, b)| a == b) |
| 703 | .count(); |
| 704 | let (old, new) = (&old[..old.len() - suffix], &new[..new.len() - suffix]); |
| 705 | let mut row = vec![0u32; new.len() + 1]; |
| 706 | for a in old { |
| 707 | let mut diagonal = 0u32; |
| 708 | for (j, b) in new.iter().enumerate() { |
| 709 | let above = row[j + 1]; |
| 710 | row[j + 1] = if a == b { |
| 711 | diagonal + 1 |
| 712 | } else { |
| 713 | above.max(row[j]) |
| 714 | }; |
| 715 | diagonal = above; |
| 716 | } |
| 717 | } |
| 718 | new.len() - row[new.len()] as usize |
| 719 | } |
| 720 | |
| 721 | /// Blame of each file at `revision`, against git, line by line. |
| 722 | /// |
| 723 | /// Braid's blame of every version of the file is checked against braid's |
| 724 | /// own diff. Where braid's blame and git's differ, the documented rules must |
| 725 | /// explain it (see the module documentation): git's own diffs, replayed, |
| 726 | /// give git's blame, and braid's diff never inserts fewer lines than the |
| 727 | /// shortest one. |
| 728 | fn compare_blames( |
| 729 | git: &Path, |
| 730 | imported: &Imported, |
| 731 | history: &mut RepoHistory<'_>, |
| 732 | revision: &str, |
| 733 | files: &[String], |
| 734 | tally: &mut Tally, |
| 735 | ) { |
| 736 | let at = imported.changeset(revision); |
| 737 | let mut heights: HashMap<ObjectId, u64> = HashMap::new(); |
| 738 | for path in files { |
| 739 | let expected = git_blame(git, revision, path); |
| 740 | let blame = history |
| 741 | .blame(at, path.as_bytes()) |
| 742 | .unwrap() |
| 743 | .unwrap_or_else(|| panic!("no blame for {path} at {revision}")); |
| 744 | assert_eq!( |
| 745 | blame.origins.len(), |
| 746 | expected.len(), |
| 747 | "lines of {path} at {revision}" |
| 748 | ); |
| 749 | let versions = versions(&imported.repo, at, path); |
| 750 | let checked = check_versions(imported, history, path, &versions, tally); |
| 751 | assert_eq!( |
| 752 | checked.last().map(|(_, own)| own), |
| 753 | Some(&blame.origins), |
| 754 | "{path} at {revision}: the blame of its last version" |
| 755 | ); |
| 756 | let appeared = versions[0].0; |
| 757 | let mut height = |id: ObjectId| -> u64 { |
| 758 | *heights |
| 759 | .entry(id) |
| 760 | .or_insert_with(|| history.graph(id).unwrap().height) |
| 761 | }; |
| 762 | let floor = height(appeared); |
| 763 | let mut wanted = Vec::with_capacity(expected.len()); |
| 764 | for (n, line) in expected.iter().enumerate() { |
| 765 | let git_origin = imported.changeset(&line.commit); |
| 766 | wanted.push(if height(git_origin) >= floor { |
| 767 | git_origin |
| 768 | } else { |
| 769 | // Git followed a rename; braid starts where the path appeared. |
| 770 | assert_ne!( |
| 771 | &line.filename, |
| 772 | path, |
| 773 | "line {} of {path} at {revision}: git found it before the path appeared, under the same name", |
| 774 | n + 1 |
| 775 | ); |
| 776 | tally.renamed_lines += 1; |
| 777 | appeared |
| 778 | }); |
| 779 | } |
| 780 | tally.blamed_files += 1; |
| 781 | tally.blamed_lines += expected.len(); |
| 782 | let differing = blame |
| 783 | .origins |
| 784 | .iter() |
| 785 | .zip(&wanted) |
| 786 | .filter(|(origin, wanted)| origin != wanted) |
| 787 | .count(); |
| 788 | if differing == 0 { |
| 789 | continue; |
| 790 | } |
| 791 | |
| 792 | // Git's blame, replayed from git's own diffs, and the lengths of the |
| 793 | // two diffs at every version. |
| 794 | let mut replayed: Vec<ObjectId> = Vec::new(); |
| 795 | let mut same_lengths = true; |
| 796 | for (i, (changeset, blob)) in versions.iter().enumerate() { |
| 797 | let lines = split_lines(&checked[i].0).len(); |
| 798 | if i == 0 { |
| 799 | replayed = vec![*changeset; lines]; |
| 800 | continue; |
| 801 | } |
| 802 | let what = format!("{path} at {changeset}"); |
| 803 | let old = imported.map.git_blob(&versions[i - 1].1).unwrap(); |
| 804 | let new = imported.map.git_blob(blob).unwrap(); |
| 805 | let inserted = braid_engine::line_diff(&checked[i - 1].0, &checked[i].0) |
| 806 | .iter() |
| 807 | .map(|change| change.new.len()) |
| 808 | .sum::<usize>(); |
| 809 | let fewest = fewest_inserts(&checked[i - 1].0, &checked[i].0); |
| 810 | assert!( |
| 811 | inserted >= fewest, |
| 812 | "{what}: braid's diff inserts {inserted} lines, the shortest diff {fewest}" |
| 813 | ); |
| 814 | let git_hunks = git_diff_hunks(git, &old, &new); |
| 815 | same_lengths &= inserted == inserts(&git_hunks); |
| 816 | replayed = replay(&replayed, lines, &git_hunks, *changeset); |
| 817 | } |
| 818 | assert_eq!( |
| 819 | replayed, wanted, |
| 820 | "{path} at {revision}: git's own diffs, replayed, give git's blame" |
| 821 | ); |
| 822 | let why = if same_lengths { |
| 823 | tally.realigned_files += 1; |
| 824 | tally.realigned_lines += differing; |
| 825 | "realigned" |
| 826 | } else { |
| 827 | tally.heuristic_files += 1; |
| 828 | tally.heuristic_lines += differing; |
| 829 | "heuristic" |
| 830 | }; |
| 831 | let short = &revision[..revision.len().min(12)]; |
| 832 | tally |
| 833 | .differences |
| 834 | .push(format!("{path} at {short}: {differing} lines, {why}")); |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | /// The commit a revision names. |
| 839 | fn rev_parse(git: &Path, revision: &str) -> String { |
| 840 | let mut command = git_command(git, 0); |
| 841 | command.args(["rev-parse", "--verify", "--end-of-options"]); |
| 842 | command.arg(format!("{revision}^{{commit}}")); |
| 843 | text(run(command).unwrap()).trim().to_owned() |
| 844 | } |
| 845 | |
| 846 | /// Every path a history ever had, from `git log`, with their directories. |
| 847 | fn every_path(git: &Path, revision: &str) -> BTreeSet<String> { |
| 848 | let mut command = git_command(git, 0); |
| 849 | command.args(["log", "--format=", "--name-only", "--no-renames", revision]); |
| 850 | let mut paths = BTreeSet::new(); |
| 851 | for path in text(run(command).unwrap()) |
| 852 | .lines() |
| 853 | .filter(|l| !l.is_empty()) |
| 854 | { |
| 855 | let mut dir = path; |
| 856 | paths.insert(path.to_owned()); |
| 857 | while let Some(slash) = dir.rfind('/') { |
| 858 | dir = &dir[..slash]; |
| 859 | paths.insert(dir.to_owned()); |
| 860 | } |
| 861 | } |
| 862 | paths |
| 863 | } |
| 864 | |
| 865 | /// The files at a revision. |
| 866 | fn files_at(git: &Path, revision: &str) -> Vec<String> { |
| 867 | let mut command = git_command(git, 0); |
| 868 | command.args(["ls-tree", "-r", "--name-only", "-z", revision]); |
| 869 | text(run(command).unwrap()) |
| 870 | .split('\0') |
| 871 | .filter(|path| !path.is_empty()) |
| 872 | .map(str::to_owned) |
| 873 | .collect() |
| 874 | } |
| 875 | |
| 876 | /// The commits of the first-parent chain of a revision, newest first. |
| 877 | fn first_parents(git: &Path, revision: &str) -> Vec<String> { |
| 878 | let mut command = git_command(git, 0); |
| 879 | command.args(["rev-list", "--first-parent", revision]); |
| 880 | text(run(command).unwrap()) |
| 881 | .lines() |
| 882 | .map(str::to_owned) |
| 883 | .collect() |
| 884 | } |
| 885 | |
| 886 | /// Every commit, newest first. |
| 887 | fn all_commits(git: &Path, revisions: &[&str]) -> Vec<String> { |
| 888 | let mut command = git_command(git, 0); |
| 889 | command.arg("rev-list").args(revisions); |
| 890 | text(run(command).unwrap()) |
| 891 | .lines() |
| 892 | .map(str::to_owned) |
| 893 | .collect() |
| 894 | } |
| 895 | |
| 896 | /// About `target` pairs of commits, spread over all of them, each once. |
| 897 | fn spread_pairs(commits: &[String], target: usize) -> Vec<(String, String)> { |
| 898 | let n = commits.len(); |
| 899 | let total = n * (n + 1) / 2; |
| 900 | let every = total.div_ceil(target).max(1); |
| 901 | let mut pairs = Vec::new(); |
| 902 | let mut k = 0; |
| 903 | for i in 0..n { |
| 904 | for j in i..n { |
| 905 | if k % every == 0 { |
| 906 | pairs.push((commits[i].clone(), commits[j].clone())); |
| 907 | } |
| 908 | k += 1; |
| 909 | } |
| 910 | } |
| 911 | pairs |
| 912 | } |
| 913 | |
| 914 | #[test] |
| 915 | fn a_fixture_history_matches_git() { |
| 916 | let fixture = build_fixture(); |
| 917 | let git = fixture.work.as_path(); |
| 918 | let imported = Imported::all(git); |
| 919 | let mut history = imported.repo.history(); |
| 920 | let mut tally = Tally::default(); |
| 921 | |
| 922 | // Path history, for every path and directory, and paths never there. |
| 923 | let mut paths = every_path(git, "main"); |
| 924 | paths.extend([ |
| 925 | "nothing".to_owned(), |
| 926 | "src/nothing.rs".to_owned(), |
| 927 | ".".to_owned(), |
| 928 | ]); |
| 929 | for revision in ["main", "main~3", "x", "y"] { |
| 930 | compare_path_logs(git, &imported, &mut history, revision, &paths, &mut tally); |
| 931 | } |
| 932 | |
| 933 | // Merge bases of pairs of every commit, criss-cross ones included. |
| 934 | let commits = all_commits(git, &["--all"]); |
| 935 | let mut pairs = spread_pairs(&commits, 150); |
| 936 | let x = imported.repo.resolve("x").unwrap(); |
| 937 | let y = imported.repo.resolve("y").unwrap(); |
| 938 | assert_eq!(history.merge_bases(x, y).unwrap().len(), 2, "criss-cross"); |
| 939 | pairs.push(("x".to_owned(), "y".to_owned())); |
| 940 | compare_merge_bases(git, &imported, &mut history, &pairs, &mut tally); |
| 941 | |
| 942 | // Blame of every file at every revision of main's first-parent chain. |
| 943 | for revision in first_parents(git, "main") { |
| 944 | let files = files_at(git, &revision); |
| 945 | compare_blames(git, &imported, &mut history, &revision, &files, &mut tally); |
| 946 | } |
| 947 | history.flush().unwrap(); |
| 948 | eprintln!("{tally:?}"); |
| 949 | assert!(tally.renamed_lines > 0, "the fixture has renames"); |
| 950 | } |
| 951 | |
| 952 | /// Commits of a random history built with plumbing: `count` commits whose |
| 953 | /// parents are picked by a fixed generator, many of them merges, some of |
| 954 | /// them octopus merges, some criss-cross. |
| 955 | fn random_history(fixture: &mut Fixture, seed: u64, count: usize) -> Vec<String> { |
| 956 | let mut state = seed | 1; |
| 957 | let mut next = |below: usize| { |
| 958 | state ^= state << 13; |
| 959 | state ^= state >> 7; |
| 960 | state ^= state << 17; |
| 961 | (state % below as u64) as usize |
| 962 | }; |
| 963 | let tree = fixture.git(&["mktree"]); |
| 964 | let mut commits: Vec<String> = Vec::new(); |
| 965 | let mut has_child = BTreeSet::new(); |
| 966 | for n in 0..count { |
| 967 | let parents: Vec<usize> = match (commits.len(), next(10)) { |
| 968 | (0, _) | (_, 0) => Vec::new(), |
| 969 | (len, 1..=4) => vec![next(len)], |
| 970 | (len, 5..=7) => vec![next(len), next(len)], |
| 971 | (len, 8) => (0..2 + next(3)).map(|_| next(len)).collect(), |
| 972 | (len, _) if len >= 2 => { |
| 973 | // Two merges of the same pair, in both orders. |
| 974 | let (a, b) = (next(len), next(len)); |
| 975 | fixture.clock += 1; |
| 976 | let message = format!("{n} criss"); |
| 977 | let first = fixture.git(&[ |
| 978 | "commit-tree", |
| 979 | &tree, |
| 980 | "-p", |
| 981 | &commits[a], |
| 982 | "-p", |
| 983 | &commits[b], |
| 984 | "-m", |
| 985 | &message, |
| 986 | ]); |
| 987 | has_child.extend([a, b]); |
| 988 | commits.push(first); |
| 989 | vec![b, a] |
| 990 | } |
| 991 | _ => Vec::new(), |
| 992 | }; |
| 993 | fixture.clock += 1; |
| 994 | let mut args = vec!["commit-tree".to_owned(), tree.clone()]; |
| 995 | for parent in &parents { |
| 996 | args.extend(["-p".to_owned(), commits[*parent].clone()]); |
| 997 | has_child.insert(*parent); |
| 998 | } |
| 999 | args.extend(["-m".to_owned(), format!("{n}")]); |
| 1000 | let args: Vec<&str> = args.iter().map(String::as_str).collect(); |
| 1001 | let commit = fixture.git(&args); |
| 1002 | commits.push(commit); |
| 1003 | } |
| 1004 | // A branch for every commit nothing descends from, so that the import |
| 1005 | // brings them all. |
| 1006 | for (n, commit) in commits.iter().enumerate() { |
| 1007 | if !has_child.contains(&n) { |
| 1008 | fixture.git(&["update-ref", &format!("refs/heads/tip{n}"), commit]); |
| 1009 | } |
| 1010 | } |
| 1011 | commits |
| 1012 | } |
| 1013 | |
| 1014 | #[test] |
| 1015 | fn merge_bases_of_random_histories_match_git() { |
| 1016 | let mut tally = Tally::default(); |
| 1017 | for seed in [7, 1234, 98765] { |
| 1018 | let mut fixture = Fixture::new(); |
| 1019 | let commits = random_history(&mut fixture, seed, 40); |
| 1020 | let git = fixture.work.clone(); |
| 1021 | let imported = Imported::all(&git); |
| 1022 | let mut history = imported.repo.history(); |
| 1023 | let pairs = spread_pairs(&commits, 120); |
| 1024 | compare_merge_bases(&git, &imported, &mut history, &pairs, &mut tally); |
| 1025 | } |
| 1026 | eprintln!("{tally:?}"); |
| 1027 | } |
| 1028 | |
| 1029 | /// The repository these tests live in, when it is a full git checkout. |
| 1030 | fn this_repository() -> Option<PathBuf> { |
| 1031 | let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../.."); |
| 1032 | if !root.join(".git").exists() { |
| 1033 | eprintln!("skipped: the sources are not in a git checkout"); |
| 1034 | return None; |
| 1035 | } |
| 1036 | let mut command = git_command(&root, 0); |
| 1037 | command.args(["rev-parse", "--is-shallow-repository"]); |
| 1038 | if text(run(command).unwrap()).trim() != "false" { |
| 1039 | eprintln!("skipped: this checkout is shallow"); |
| 1040 | return None; |
| 1041 | } |
| 1042 | Some(root) |
| 1043 | } |
| 1044 | |
| 1045 | #[test] |
| 1046 | fn the_history_of_this_repository_matches_git() { |
| 1047 | let Some(root) = this_repository() else { |
| 1048 | return; |
| 1049 | }; |
| 1050 | let head = text( |
| 1051 | run({ |
| 1052 | let mut command = git_command(&root, 0); |
| 1053 | command.args(["rev-parse", "HEAD"]); |
| 1054 | command |
| 1055 | }) |
| 1056 | .unwrap(), |
| 1057 | ) |
| 1058 | .trim() |
| 1059 | .to_owned(); |
| 1060 | let imported = Imported::head(&root); |
| 1061 | let mut history = imported.repo.history(); |
| 1062 | let mut tally = Tally::default(); |
| 1063 | |
| 1064 | let mut paths = every_path(&root, &head); |
| 1065 | paths.extend(["nothing".to_owned(), ".".to_owned()]); |
| 1066 | compare_path_logs(&root, &imported, &mut history, &head, &paths, &mut tally); |
| 1067 | |
| 1068 | let commits = all_commits(&root, &[&head]); |
| 1069 | let pairs = spread_pairs(&commits, 150); |
| 1070 | compare_merge_bases(&root, &imported, &mut history, &pairs, &mut tally); |
| 1071 | |
| 1072 | // Every file at HEAD, and some of the files at older revisions along the |
| 1073 | // first-parent chain. |
| 1074 | let chain = first_parents(&root, &head); |
| 1075 | compare_blames( |
| 1076 | &root, |
| 1077 | &imported, |
| 1078 | &mut history, |
| 1079 | &head, |
| 1080 | &files_at(&root, &head), |
| 1081 | &mut tally, |
| 1082 | ); |
| 1083 | for revision in chain.iter().skip(1).step_by(6) { |
| 1084 | let files: Vec<String> = files_at(&root, revision).into_iter().step_by(4).collect(); |
| 1085 | compare_blames(&root, &imported, &mut history, revision, &files, &mut tally); |
| 1086 | } |
| 1087 | history.flush().unwrap(); |
| 1088 | eprintln!("{tally:?}"); |
| 1089 | assert!(tally.blamed_lines > 0); |
| 1090 | } |
| 1091 | |
| 1092 | /// The paths of a git history and their map to braid, for debugging a |
| 1093 | /// failure by hand: `cargo test -p elffuss-braid-repo --test |
| 1094 | /// history_vs_git -- --ignored --nocapture`. |
| 1095 | #[test] |
| 1096 | #[ignore = "prints the fixture's history"] |
| 1097 | fn print_the_fixture() { |
| 1098 | let fixture = build_fixture(); |
| 1099 | let mut command = git_command(&fixture.work, 0); |
| 1100 | command.args(["log", "--graph", "--all", "--format=%h %s", "--name-status"]); |
| 1101 | eprintln!("{}", text(run(command).unwrap())); |
| 1102 | let imported = Imported::all(&fixture.work); |
| 1103 | let mut by_commit = BTreeMap::new(); |
| 1104 | for (commit, changeset) in imported.map.commits() { |
| 1105 | by_commit.insert(commit.to_hex(), changeset.short()); |
| 1106 | } |
| 1107 | eprintln!("{by_commit:#?}"); |
| 1108 | } |