forge
Clone
https://git.tahinli.com/Tahinli/forge.git
0 watching·0 forks·Rust·AGPL-3.0·9.7 MiB·37e985d·Tahinli·2 hours ago·363 commits
| 1 | use std::path::Path; |
| 2 | |
| 3 | use std::process::Stdio; |
| 4 | |
| 5 | use crate::command::{GitCommand, GitEnv}; |
| 6 | use crate::error::Result; |
| 7 | use crate::history::read_pipe_head; |
| 8 | |
| 9 | const MAX_OUTPUT_BYTES: usize = 64 * 1024; |
| 10 | |
| 11 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 12 | pub struct FsckReport { |
| 13 | pub ok: bool, |
| 14 | pub output: String, |
| 15 | } |
| 16 | |
| 17 | pub async fn fsck(env: &GitEnv, repo: &Path, full: bool) -> Result<FsckReport> { |
| 18 | let mut cmd = GitCommand::new(env, "fsck") |
| 19 | .git_dir(repo) |
| 20 | .arg("--no-dangling"); |
| 21 | if full { |
| 22 | cmd = cmd.arg("--full"); |
| 23 | } |
| 24 | let mut command = cmd.into_tokio(); |
| 25 | command |
| 26 | .stdin(Stdio::null()) |
| 27 | .stdout(Stdio::piped()) |
| 28 | .stderr(Stdio::piped()); |
| 29 | let mut child = command.spawn()?; |
| 30 | let stdout = child.stdout.take().map(|p| tokio::spawn(read_pipe_head(p))); |
| 31 | let stderr = child.stderr.take().map(|p| tokio::spawn(read_pipe_head(p))); |
| 32 | let status = child.wait().await?; |
| 33 | let mut text = String::new(); |
| 34 | for task in [stdout, stderr].into_iter().flatten() { |
| 35 | text.push_str(&String::from_utf8_lossy(&task.await.unwrap_or_default())); |
| 36 | } |
| 37 | let mut end = text.len().min(MAX_OUTPUT_BYTES); |
| 38 | while !text.is_char_boundary(end) { |
| 39 | end -= 1; |
| 40 | } |
| 41 | text.truncate(end); |
| 42 | Ok(FsckReport { |
| 43 | ok: status.success(), |
| 44 | output: text.trim().to_owned(), |
| 45 | }) |
| 46 | } |
| 47 | |
| 48 | #[cfg(test)] |
| 49 | mod tests { |
| 50 | use super::*; |
| 51 | use std::process::Command; |
| 52 | |
| 53 | fn env(dir: &Path) -> GitEnv { |
| 54 | GitEnv { |
| 55 | git_bin: "git".into(), |
| 56 | data_dir: dir.to_path_buf(), |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | #[tokio::test] |
| 61 | async fn clean_repo_passes_and_damaged_repo_fails() { |
| 62 | let dir = tempfile::tempdir().expect("tempdir"); |
| 63 | let work = dir.path().join("work"); |
| 64 | std::fs::create_dir_all(&work).expect("mkdir"); |
| 65 | let run = |args: &[&str]| { |
| 66 | let out = Command::new("git") |
| 67 | .current_dir(&work) |
| 68 | .env("GIT_AUTHOR_NAME", "t") |
| 69 | .env("GIT_AUTHOR_EMAIL", "t@example.invalid") |
| 70 | .env("GIT_COMMITTER_NAME", "t") |
| 71 | .env("GIT_COMMITTER_EMAIL", "t@example.invalid") |
| 72 | .args(args) |
| 73 | .output() |
| 74 | .expect("git"); |
| 75 | assert!(out.status.success(), "{args:?}: {out:?}"); |
| 76 | }; |
| 77 | run(&["init", "-q", "-b", "main"]); |
| 78 | run(&["commit", "-q", "--allow-empty", "-m", "one"]); |
| 79 | let git_dir = work.join(".git"); |
| 80 | |
| 81 | let clean = fsck(&env(dir.path()), &git_dir, true).await.expect("fsck"); |
| 82 | assert!(clean.ok, "{}", clean.output); |
| 83 | |
| 84 | let head = Command::new("git") |
| 85 | .current_dir(&work) |
| 86 | .args(["rev-parse", "HEAD"]) |
| 87 | .output() |
| 88 | .expect("rev-parse"); |
| 89 | let oid = String::from_utf8_lossy(&head.stdout).trim().to_owned(); |
| 90 | let object = git_dir.join("objects").join(&oid[..2]).join(&oid[2..]); |
| 91 | std::fs::remove_file(&object).expect("remove object"); |
| 92 | std::fs::write(&object, b"not zlib").expect("corrupt"); |
| 93 | let damaged = fsck(&env(dir.path()), &git_dir, false).await.expect("fsck"); |
| 94 | assert!(!damaged.ok); |
| 95 | assert!(!damaged.output.is_empty()); |
| 96 | |
| 97 | let missing = fsck(&env(dir.path()), &dir.path().join("nope.git"), false) |
| 98 | .await |
| 99 | .expect("fsck"); |
| 100 | assert!(!missing.ok); |
| 101 | } |
| 102 | } |