From 0afdcc4aa9d55db40cb045f11600956866945946 Mon Sep 17 00:00:00 2001 From: Sebastian Kuzminsky Date: Sat, 5 Jul 2025 10:30:07 -0600 Subject: [PATCH] WIP git::create_orphan_branch() --- Cargo.toml | 1 + src/git.rs | 73 ++++++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 64 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c017ff3..03db057 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ edition = "2024" [dependencies] anyhow = "1.0.95" clap = { version = "4.5.26", features = ["derive"] } +mktemp = "0.5.1" serde = { version = "1.0.217", features = ["derive"] } thiserror = "2.0.11" toml = "0.8.19" diff --git a/src/git.rs b/src/git.rs index 3ca250f..adfb147 100644 --- a/src/git.rs +++ b/src/git.rs @@ -1,3 +1,5 @@ +use std::io::Write; + #[derive(Debug, thiserror::Error)] pub enum GitError { #[error(transparent)] @@ -34,16 +36,62 @@ pub fn remove_worktree(worktree_dir: &std::path::Path) -> Result<(), GitError> { Ok(()) } -pub fn create_orphan_branch(branch: &str) { - // git checkout --orphan orphan-branch - // git rm -rf . - // git clean -fdx . - // - // echo "hello world" > test-file - // git add test-file - // git commit -m 'first commit, again' - // - // git checkout dev +pub fn create_orphan_branch(branch: &str) -> Result<(), GitError> { + let tmp_worktree = mktemp::Temp::new_path(); + let tmp_worktree_pathbuf = tmp_worktree.to_path_buf(); + let Some(tmp_worktree_dir) = tmp_worktree_pathbuf.as_path().to_str() else { + return Err(GitError::Oops); + }; + + let result = std::process::Command::new("git") + .args([ + "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)] @@ -56,4 +104,9 @@ mod tests { checkout_branch_in_worktree("main", worktree_dir).unwrap(); remove_worktree(worktree_dir).unwrap(); } + + #[test] + fn test_create_orphan_branch() { + create_orphan_branch("bloppers").unwrap(); + } }