#![allow(dead_code)]
use std::path::{Path, PathBuf};
use std::process::Command;
use tempfile::TempDir;
pub fn create_bare_repo() -> (TempDir, PathBuf, PathBuf) {
create_bare_repo_with_prefix("test")
}
pub fn create_bare_repo_with_prefix(prefix: &str) -> (TempDir, PathBuf, PathBuf) {
let tmp = TempDir::new().expect("failed to create temp dir");
let source = tmp.path().join("source");
std::fs::create_dir_all(&source).unwrap();
run(&source, &["git", "init", "-b", "main"]);
run(&source, &["git", "config", "user.email", "test@test.com"]);
run(&source, &["git", "config", "user.name", "Test"]);
let file = source.join("README.md");
std::fs::write(&file, "# test\n").unwrap();
run(&source, &["git", "add", "."]);
run(&source, &["git", "commit", "-m", "initial"]);
let project_dir = tmp.path().join("project");
std::fs::create_dir_all(&project_dir).unwrap();
let bare = project_dir.join("repo.git");
run(
tmp.path(),
&[
"git",
"clone",
"--bare",
source.to_str().unwrap(),
bare.to_str().unwrap(),
],
);
run_with_env(
&bare,
&[
"git",
"config",
"remote.origin.fetch",
"+refs/heads/*:refs/remotes/origin/*",
],
);
run_with_env(
&bare,
&[
"git",
"symbolic-ref",
"refs/remotes/origin/HEAD",
"refs/remotes/origin/main",
],
);
let config_content = format!("[worktree]\nprefix = \"{prefix}\"\n");
std::fs::write(bare.join(".grov.toml"), config_content).unwrap();
(tmp, bare, project_dir)
}
fn run(dir: &Path, args: &[&str]) {
let status = Command::new(args[0])
.args(&args[1..])
.current_dir(dir)
.output()
.expect("failed to run command");
assert!(
status.status.success(),
"command failed: {:?}\nstderr: {}",
args,
String::from_utf8_lossy(&status.stderr)
);
}
fn run_with_env(bare_path: &Path, args: &[&str]) {
let status = Command::new(args[0])
.args(&args[1..])
.env("GIT_DIR", bare_path)
.output()
.expect("failed to run command");
assert!(
status.status.success(),
"command failed: {:?}\nstderr: {}",
args,
String::from_utf8_lossy(&status.stderr)
);
}