empty-descriptions-and-dropped-worktrees #15

Merged
seb merged 10 commits from empty-descriptions-and-dropped-worktrees into main 2025-07-10 09:48:22 -06:00
Showing only changes of commit 211bf92dde - Show all commits

View file

@ -47,6 +47,8 @@ pub enum IssueError {
GitError(#[from] crate::git::GitError),
#[error("Failed to run editor")]
EditorError,
#[error("supplied description is empty")]
EmptyDescription,
}
impl FromStr for State {
@ -195,6 +197,9 @@ impl Issue {
}
pub fn set_description(&mut self, description: &str) -> Result<(), IssueError> {
if description.len() == 0 {
return Err(IssueError::EmptyDescription);
}
self.description = String::from(description);
let mut description_filename = std::path::PathBuf::from(&self.dir);
description_filename.push("description");
@ -214,6 +219,7 @@ impl Issue {
pub fn edit_description(&mut self) -> Result<(), IssueError> {
let mut description_filename = std::path::PathBuf::from(&self.dir);
description_filename.push("description");
let exists = description_filename.exists();
let result = std::process::Command::new("vi")
.arg(&description_filename.as_mut_os_str())
.spawn()?
@ -223,8 +229,31 @@ impl Issue {
println!("stderr: {}", std::str::from_utf8(&result.stderr).unwrap());
return Err(IssueError::EditorError);
}
crate::git::git_commit_file(&description_filename)?;
if description_filename.exists() && description_filename.metadata()?.len() > 0 {
crate::git::add_file(&description_filename)?;
} else {
// User saved an empty file, which means they changed their
// mind and no longer want to edit the description.
if exists {
crate::git::restore_file(&description_filename)?;
}
return Err(IssueError::EmptyDescription);
}
if crate::git::worktree_is_dirty(&self.dir.to_string_lossy())? {
crate::git::commit(
&description_filename.parent().unwrap(),
&format!(
"new description for issue {}",
description_filename
.parent()
.unwrap()
.file_name()
.unwrap()
.to_string_lossy()
),
)?;
self.read_description()?;
}
Ok(())
}