Compare commits
4 commits
5b73a6b34c
...
4fb4ed4ea3
| Author | SHA1 | Date | |
|---|---|---|---|
| 4fb4ed4ea3 | |||
| 0afdcc4aa9 | |||
| f467e35ff2 | |||
| 9d9a30d90a |
3 changed files with 114 additions and 0 deletions
|
|
@ -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"
|
||||
|
|
|
|||
112
src/git.rs
Normal file
112
src/git.rs
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
use std::io::Write;
|
||||
|
||||
#[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(())
|
||||
}
|
||||
|
||||
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)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_worktree() {
|
||||
let worktree_dir = std::path::Path::new("/tmp/boo");
|
||||
checkout_branch_in_worktree("origin/main", worktree_dir).unwrap();
|
||||
remove_worktree(worktree_dir).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_create_orphan_branch() {
|
||||
create_orphan_branch("bloppers").unwrap();
|
||||
}
|
||||
}
|
||||
|
|
@ -1,2 +1,3 @@
|
|||
pub mod git;
|
||||
pub mod issue;
|
||||
pub mod issues;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue