diff --git a/src/git.rs b/src/git.rs new file mode 100644 index 0000000..4fb8ee4 --- /dev/null +++ b/src/git.rs @@ -0,0 +1,47 @@ +#[derive(Debug, thiserror::Error)] +pub enum GitError { + #[error(transparent)] + StdIoError(#[from] std::io::Error), + #[error("Oops, something went wrong")] + Oops, +} + +pub fn checkout_branch_in_worktree( + branch: &str, + worktree_dir: &std::path::Path, +) -> Result<(), GitError> { + let result = std::process::Command::new("git") + .args(["worktree", "add", &worktree_dir.to_string_lossy(), branch]) + .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); + } + Ok(()) +} + +pub fn remove_worktree(worktree_dir: &std::path::Path) -> Result<(), GitError> { + std::fs::remove_dir_all(worktree_dir)?; + let result = std::process::Command::new("git") + .args(["worktree", "prune"]) + .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); + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn worktree() { + let worktree_dir = std::path::Path::new("/tmp/boo"); + checkout_branch_in_worktree("main", worktree_dir).unwrap(); + remove_worktree(worktree_dir).unwrap(); + } +} diff --git a/src/lib.rs b/src/lib.rs index 713acd1..cca1c90 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,2 +1,3 @@ +pub mod git; pub mod issue; pub mod issues;