Compare commits
No commits in common. "ba47a4bcd67cd78b5909b012b08c12498aab3383" and "4fb4ed4ea30f497c733e4ca63a93743b3d2d7a39" have entirely different histories.
ba47a4bcd6
...
4fb4ed4ea3
2 changed files with 24 additions and 51 deletions
|
|
@ -7,7 +7,6 @@ edition = "2024"
|
||||||
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"
|
mktemp = "0.5.1"
|
||||||
rand = "0.9.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"
|
||||||
|
|
|
||||||
74
src/git.rs
74
src/git.rs
|
|
@ -23,7 +23,8 @@ pub fn checkout_branch_in_worktree(
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn git_worktree_prune() -> Result<(), GitError> {
|
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")
|
let result = std::process::Command::new("git")
|
||||||
.args(["worktree", "prune"])
|
.args(["worktree", "prune"])
|
||||||
.output()?;
|
.output()?;
|
||||||
|
|
@ -35,46 +36,22 @@ pub fn git_worktree_prune() -> Result<(), GitError> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn git_remove_branch(branch: &str) -> Result<(), GitError> {
|
|
||||||
let result = std::process::Command::new("git")
|
|
||||||
.args(["branch", "-D", 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 create_orphan_branch(branch: &str) -> Result<(), GitError> {
|
pub fn create_orphan_branch(branch: &str) -> Result<(), GitError> {
|
||||||
{
|
let tmp_worktree = mktemp::Temp::new_path();
|
||||||
let tmp_worktree = mktemp::Temp::new_path();
|
let tmp_worktree_pathbuf = tmp_worktree.to_path_buf();
|
||||||
create_orphan_branch_at_path(branch, &tmp_worktree.to_path_buf())?;
|
let Some(tmp_worktree_dir) = tmp_worktree_pathbuf.as_path().to_str() else {
|
||||||
}
|
|
||||||
// The temp dir is now removed / cleaned up.
|
|
||||||
|
|
||||||
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(())
|
|
||||||
}
|
|
||||||
pub fn create_orphan_branch_at_path(
|
|
||||||
branch: &str,
|
|
||||||
worktree_path: &std::path::PathBuf,
|
|
||||||
) -> Result<(), GitError> {
|
|
||||||
let Some(worktree_dir) = worktree_path.to_str() else {
|
|
||||||
return Err(GitError::Oops);
|
return Err(GitError::Oops);
|
||||||
};
|
};
|
||||||
|
|
||||||
let result = std::process::Command::new("git")
|
let result = std::process::Command::new("git")
|
||||||
.args(["worktree", "add", "--orphan", "-b", branch, worktree_dir])
|
.args([
|
||||||
|
"worktree",
|
||||||
|
"add",
|
||||||
|
"--orphan",
|
||||||
|
"-b",
|
||||||
|
branch,
|
||||||
|
tmp_worktree_dir,
|
||||||
|
])
|
||||||
.output()?;
|
.output()?;
|
||||||
if !result.status.success() {
|
if !result.status.success() {
|
||||||
println!("stdout: {}", std::str::from_utf8(&result.stdout).unwrap());
|
println!("stdout: {}", std::str::from_utf8(&result.stdout).unwrap());
|
||||||
|
|
@ -82,8 +59,10 @@ pub fn create_orphan_branch_at_path(
|
||||||
return Err(GitError::Oops);
|
return Err(GitError::Oops);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut readme_filename = worktree_path.clone();
|
let mut readme_filename = tmp_worktree_pathbuf.clone();
|
||||||
readme_filename.push("README.md");
|
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)?;
|
let mut readme = std::fs::File::create(readme_filename)?;
|
||||||
write!(
|
write!(
|
||||||
readme,
|
readme,
|
||||||
|
|
@ -92,7 +71,7 @@ pub fn create_orphan_branch_at_path(
|
||||||
|
|
||||||
let result = std::process::Command::new("git")
|
let result = std::process::Command::new("git")
|
||||||
.args(["add", "README.md"])
|
.args(["add", "README.md"])
|
||||||
.current_dir(worktree_dir)
|
.current_dir(tmp_worktree_dir)
|
||||||
.output()?;
|
.output()?;
|
||||||
if !result.status.success() {
|
if !result.status.success() {
|
||||||
println!("stdout: {}", std::str::from_utf8(&result.stdout).unwrap());
|
println!("stdout: {}", std::str::from_utf8(&result.stdout).unwrap());
|
||||||
|
|
@ -102,7 +81,7 @@ pub fn create_orphan_branch_at_path(
|
||||||
|
|
||||||
let result = std::process::Command::new("git")
|
let result = std::process::Command::new("git")
|
||||||
.args(["commit", "-m", "create entomologist issue branch"])
|
.args(["commit", "-m", "create entomologist issue branch"])
|
||||||
.current_dir(worktree_path)
|
.current_dir(tmp_worktree_dir)
|
||||||
.output()?;
|
.output()?;
|
||||||
if !result.status.success() {
|
if !result.status.success() {
|
||||||
println!("stdout: {}", std::str::from_utf8(&result.stdout).unwrap());
|
println!("stdout: {}", std::str::from_utf8(&result.stdout).unwrap());
|
||||||
|
|
@ -110,6 +89,8 @@ pub fn create_orphan_branch_at_path(
|
||||||
return Err(GitError::Oops);
|
return Err(GitError::Oops);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// git worktree prune
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -119,20 +100,13 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_worktree() {
|
fn test_worktree() {
|
||||||
{
|
let worktree_dir = std::path::Path::new("/tmp/boo");
|
||||||
let temp_worktree = mktemp::Temp::new_path();
|
checkout_branch_in_worktree("origin/main", worktree_dir).unwrap();
|
||||||
checkout_branch_in_worktree("origin/main", temp_worktree.as_path()).unwrap();
|
remove_worktree(worktree_dir).unwrap();
|
||||||
// The temporary worktree directory is removed when the Temp variable is dropped.
|
|
||||||
}
|
|
||||||
git_worktree_prune().unwrap();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_create_orphan_branch() {
|
fn test_create_orphan_branch() {
|
||||||
let rnd: u128 = rand::random();
|
create_orphan_branch("bloppers").unwrap();
|
||||||
let mut branch = std::string::String::from("entomologist-test-branch-");
|
|
||||||
branch.push_str(&format!("{:0x}", rnd));
|
|
||||||
create_orphan_branch(&branch).unwrap();
|
|
||||||
git_remove_branch(&branch).unwrap();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue