WIP git::create_orphan_branch()

This commit is contained in:
Sebastian Kuzminsky 2025-07-05 10:30:07 -06:00
parent f467e35ff2
commit 0afdcc4aa9
2 changed files with 64 additions and 10 deletions

View file

@ -6,6 +6,7 @@ edition = "2024"
[dependencies] [dependencies]
anyhow = "1.0.95" anyhow = "1.0.95"
clap = { version = "4.5.26", features = ["derive"] } clap = { version = "4.5.26", features = ["derive"] }
mktemp = "0.5.1"
serde = { version = "1.0.217", features = ["derive"] } serde = { version = "1.0.217", features = ["derive"] }
thiserror = "2.0.11" thiserror = "2.0.11"
toml = "0.8.19" toml = "0.8.19"

View file

@ -1,3 +1,5 @@
use std::io::Write;
#[derive(Debug, thiserror::Error)] #[derive(Debug, thiserror::Error)]
pub enum GitError { pub enum GitError {
#[error(transparent)] #[error(transparent)]
@ -34,16 +36,62 @@ pub fn remove_worktree(worktree_dir: &std::path::Path) -> Result<(), GitError> {
Ok(()) Ok(())
} }
pub fn create_orphan_branch(branch: &str) { pub fn create_orphan_branch(branch: &str) -> Result<(), GitError> {
// git checkout --orphan orphan-branch let tmp_worktree = mktemp::Temp::new_path();
// git rm -rf . let tmp_worktree_pathbuf = tmp_worktree.to_path_buf();
// git clean -fdx . let Some(tmp_worktree_dir) = tmp_worktree_pathbuf.as_path().to_str() else {
// return Err(GitError::Oops);
// echo "hello world" > test-file };
// git add test-file
// git commit -m 'first commit, again' let result = std::process::Command::new("git")
// .args([
// git checkout dev "worktree",
"add",
"--orphan",
"-b",
branch,
tmp_worktree_dir,
])
.output()?;
if !result.status.success() {
println!("stdout: {}", std::str::from_utf8(&result.stdout).unwrap());
println!("stderr: {}", std::str::from_utf8(&result.stderr).unwrap());
return Err(GitError::Oops);
}
let mut readme_filename = tmp_worktree_pathbuf.clone();
readme_filename.push("README.md");
println!("readme_filename: {:?}", readme_filename);
println!("tmp_worktree_pathbuf: {:?}", tmp_worktree_pathbuf);
let mut readme = std::fs::File::create(readme_filename)?;
write!(
readme,
"This branch is used by entomologist to track issues."
)?;
let result = std::process::Command::new("git")
.args(["add", "README.md"])
.current_dir(tmp_worktree_dir)
.output()?;
if !result.status.success() {
println!("stdout: {}", std::str::from_utf8(&result.stdout).unwrap());
println!("stderr: {}", std::str::from_utf8(&result.stderr).unwrap());
return Err(GitError::Oops);
}
let result = std::process::Command::new("git")
.args(["commit", "-m", "create entomologist issue branch"])
.current_dir(tmp_worktree_dir)
.output()?;
if !result.status.success() {
println!("stdout: {}", std::str::from_utf8(&result.stdout).unwrap());
println!("stderr: {}", std::str::from_utf8(&result.stderr).unwrap());
return Err(GitError::Oops);
}
// git worktree prune
Ok(())
} }
#[cfg(test)] #[cfg(test)]
@ -56,4 +104,9 @@ mod tests {
checkout_branch_in_worktree("main", worktree_dir).unwrap(); checkout_branch_in_worktree("main", worktree_dir).unwrap();
remove_worktree(worktree_dir).unwrap(); remove_worktree(worktree_dir).unwrap();
} }
#[test]
fn test_create_orphan_branch() {
create_orphan_branch("bloppers").unwrap();
}
} }