| 1 | use std::path::Path; |
| 2 | use std::path::PathBuf; |
| 3 | use std::process::Command; |
| 4 | |
| 5 | use forge_git::GitEnv; |
| 6 | use forge_git::history::DiffLimits; |
| 7 | |
| 8 | pub const BIG: DiffLimits = DiffLimits { |
| 9 | max_files: 1000, |
| 10 | max_lines_per_file: 10_000, |
| 11 | max_total_lines: 100_000, |
| 12 | }; |
| 13 | |
| 14 | pub fn git_env(data_dir: &Path) -> GitEnv { |
| 15 | GitEnv { |
| 16 | git_bin: PathBuf::from("/usr/bin/git"), |
| 17 | data_dir: data_dir.to_path_buf(), |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | pub struct WorkRepo { |
| 22 | _dir: tempfile::TempDir, |
| 23 | pub work: PathBuf, |
| 24 | pub git_dir: PathBuf, |
| 25 | pub env: GitEnv, |
| 26 | } |
| 27 | |
| 28 | impl WorkRepo { |
| 29 | pub fn new() -> Self { |
| 30 | let dir = tempfile::tempdir().unwrap(); |
| 31 | let work = dir.path().join("work"); |
| 32 | std::fs::create_dir(&work).unwrap(); |
| 33 | let env = git_env(dir.path()); |
| 34 | let git_dir = work.join(".git"); |
| 35 | let repo = Self { |
| 36 | _dir: dir, |
| 37 | work, |
| 38 | git_dir, |
| 39 | env, |
| 40 | }; |
| 41 | repo.git(&["init", "-q", "-b", "main"]); |
| 42 | repo |
| 43 | } |
| 44 | |
| 45 | pub fn git(&self, args: &[&str]) -> String { |
| 46 | let out = Command::new("/usr/bin/git") |
| 47 | .arg("-C") |
| 48 | .arg(&self.work) |
| 49 | .args([ |
| 50 | "-c", |
| 51 | "user.name=tahinli", |
| 52 | "-c", |
| 53 | "user.email=tahinli@example.com", |
| 54 | "-c", |
| 55 | "commit.gpgsign=false", |
| 56 | ]) |
| 57 | .args(args) |
| 58 | .env_clear() |
| 59 | .env("PATH", "/usr/bin:/bin") |
| 60 | .env("HOME", self._dir.path()) |
| 61 | .env("GIT_CONFIG_NOSYSTEM", "1") |
| 62 | .env("GIT_CONFIG_GLOBAL", "/dev/null") |
| 63 | .output() |
| 64 | .unwrap(); |
| 65 | assert!( |
| 66 | out.status.success(), |
| 67 | "{args:?} failed: {}", |
| 68 | String::from_utf8_lossy(&out.stderr) |
| 69 | ); |
| 70 | String::from_utf8_lossy(&out.stdout).into_owned() |
| 71 | } |
| 72 | |
| 73 | pub fn write(&self, name: &str, content: impl AsRef<[u8]>) { |
| 74 | let p = self.work.join(name); |
| 75 | std::fs::create_dir_all(p.parent().unwrap()).unwrap(); |
| 76 | std::fs::write(p, content).unwrap(); |
| 77 | } |
| 78 | |
| 79 | pub fn commit(&self, msg: &str) -> String { |
| 80 | self.git(&["add", "-A"]); |
| 81 | self.git(&["commit", "-q", "--allow-empty", "-m", msg]); |
| 82 | self.git(&["rev-parse", "HEAD"]).trim().to_owned() |
| 83 | } |
| 84 | } |