Compare commits

..

No commits in common. "e09e4b9cb72608c4eb7f83f7bae433d1c0180b3c" and "f5055a9ac70e0f74c8a768aa1a07af49ad20c2db" have entirely different histories.

4 changed files with 25 additions and 169 deletions

View file

@ -132,40 +132,27 @@ fn handle_command(args: &Args, issues_dir: &std::path::Path) -> anyhow::Result<(
} }
} }
Commands::New { description } => { Commands::New {
description: Some(description),
} => {
let mut issue = entomologist::issue::Issue::new(issues_dir)?; let mut issue = entomologist::issue::Issue::new(issues_dir)?;
let r = match description { issue.set_description(description)?;
Some(description) => issue.set_description(description), println!("created new issue '{}'", issue.title());
None => issue.edit_description(), }
};
match r { Commands::New { description: None } => {
Err(entomologist::issue::IssueError::EmptyDescription) => { let mut issue = entomologist::issue::Issue::new(issues_dir)?;
println!("no new issue created"); issue.edit_description()?;
return Ok(()); println!("created new issue '{}'", issue.title());
}
Err(e) => {
return Err(e.into());
}
Ok(()) => {
println!("created new issue '{}'", issue.title());
}
}
} }
Commands::Edit { issue_id } => { Commands::Edit { issue_id } => {
let mut issues = let mut issues =
entomologist::issues::Issues::new_from_dir(std::path::Path::new(issues_dir))?; entomologist::issues::Issues::new_from_dir(std::path::Path::new(issues_dir))?;
match issues.get_mut_issue(issue_id) { match issues.get_mut_issue(issue_id) {
Some(issue) => match issue.edit_description() { Some(issue) => {
Err(entomologist::issue::IssueError::EmptyDescription) => { issue.edit_description()?;
println!("aborted issue edit"); }
return Ok(());
}
Err(e) => {
return Err(e.into());
}
Ok(()) => (),
},
None => { None => {
return Err(anyhow::anyhow!("issue {} not found", issue_id)); return Err(anyhow::anyhow!("issue {} not found", issue_id));
} }
@ -240,20 +227,12 @@ fn handle_command(args: &Args, issues_dir: &std::path::Path) -> anyhow::Result<(
return Err(anyhow::anyhow!("issue {} not found", issue_id)); return Err(anyhow::anyhow!("issue {} not found", issue_id));
}; };
let mut comment = issue.new_comment()?; let mut comment = issue.new_comment()?;
let r = match description { match description {
Some(description) => comment.set_description(description), Some(description) => {
None => comment.edit_description(), comment.set_description(description)?;
};
match r {
Err(entomologist::comment::CommentError::EmptyDescription) => {
println!("aborted new comment");
return Ok(());
} }
Err(e) => { None => {
return Err(e.into()); comment.edit_description()?;
}
Ok(()) => {
println!("created new comment {}", &comment.uuid);
} }
} }
} }

View file

@ -22,8 +22,6 @@ pub enum CommentError {
GitError(#[from] crate::git::GitError), GitError(#[from] crate::git::GitError),
#[error("Failed to run editor")] #[error("Failed to run editor")]
EditorError, EditorError,
#[error("supplied description is empty")]
EmptyDescription,
} }
impl Comment { impl Comment {
@ -62,9 +60,6 @@ impl Comment {
} }
pub fn set_description(&mut self, description: &str) -> Result<(), CommentError> { pub fn set_description(&mut self, description: &str) -> Result<(), CommentError> {
if description.len() == 0 {
return Err(CommentError::EmptyDescription);
}
self.description = String::from(description); self.description = String::from(description);
let mut description_filename = std::path::PathBuf::from(&self.dir); let mut description_filename = std::path::PathBuf::from(&self.dir);
description_filename.push("description"); description_filename.push("description");
@ -84,7 +79,6 @@ impl Comment {
pub fn edit_description(&mut self) -> Result<(), CommentError> { pub fn edit_description(&mut self) -> Result<(), CommentError> {
let mut description_filename = std::path::PathBuf::from(&self.dir); let mut description_filename = std::path::PathBuf::from(&self.dir);
description_filename.push("description"); description_filename.push("description");
let exists = description_filename.exists();
let result = std::process::Command::new("vi") let result = std::process::Command::new("vi")
.arg(&description_filename.as_mut_os_str()) .arg(&description_filename.as_mut_os_str())
.spawn()? .spawn()?
@ -94,31 +88,8 @@ impl Comment {
println!("stderr: {}", std::str::from_utf8(&result.stderr).unwrap()); println!("stderr: {}", std::str::from_utf8(&result.stderr).unwrap());
return Err(CommentError::EditorError); return Err(CommentError::EditorError);
} }
if description_filename.exists() && description_filename.metadata()?.len() > 0 { crate::git::git_commit_file(&description_filename)?;
crate::git::add_file(&description_filename)?; self.read_description()?;
} 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(CommentError::EmptyDescription);
}
if crate::git::worktree_is_dirty(&self.dir.to_string_lossy())? {
crate::git::commit(
&description_filename.parent().unwrap(),
&format!(
"new description for comment {}",
description_filename
.parent()
.unwrap()
.file_name()
.unwrap()
.to_string_lossy()
),
)?;
self.read_description()?;
}
Ok(()) Ok(())
} }
} }

View file

@ -20,24 +20,9 @@ pub struct Worktree {
impl Drop for Worktree { impl Drop for Worktree {
fn drop(&mut self) { fn drop(&mut self) {
let result = std::process::Command::new("git") let _result = std::process::Command::new("git")
.args([ .args(["worktree", "remove", &self.path.path().to_string_lossy()])
"worktree",
"remove",
"--force",
&self.path.path().to_string_lossy(),
])
.output(); .output();
match result {
Err(e) => {
println!("failed to run git: {:#?}", e);
}
Ok(result) => {
if !result.status.success() {
println!("failed to remove git worktree: {:#?}", result);
}
}
}
} }
} }
@ -106,56 +91,6 @@ pub fn git_branch_exists(branch: &str) -> Result<bool, GitError> {
return Ok(result.status.success()); return Ok(result.status.success());
} }
pub fn worktree_is_dirty(dir: &str) -> Result<bool, GitError> {
// `git status --porcelain` prints a terse list of files added or
// modified (both staged and not), and new untracked files. So if
// says *anything at all* it means the worktree is dirty.
let result = std::process::Command::new("git")
.args(["status", "--porcelain", "--untracked-files=no"])
.current_dir(dir)
.output()?;
return Ok(result.stdout.len() > 0);
}
pub fn add_file(file: &std::path::Path) -> Result<(), GitError> {
let result = std::process::Command::new("git")
.args(["add", &file.to_string_lossy()])
.current_dir(file.parent().unwrap())
.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);
}
return Ok(());
}
pub fn restore_file(file: &std::path::Path) -> Result<(), GitError> {
let result = std::process::Command::new("git")
.args(["restore", &file.to_string_lossy()])
.current_dir(file.parent().unwrap())
.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);
}
return Ok(());
}
pub fn commit(dir: &std::path::Path, msg: &str) -> Result<(), GitError> {
let result = std::process::Command::new("git")
.args(["commit", "-m", msg])
.current_dir(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);
}
Ok(())
}
pub fn git_commit_file(file: &std::path::Path) -> Result<(), GitError> { pub fn git_commit_file(file: &std::path::Path) -> Result<(), GitError> {
let mut git_dir = std::path::PathBuf::from(file); let mut git_dir = std::path::PathBuf::from(file);
git_dir.pop(); git_dir.pop();

View file

@ -47,8 +47,6 @@ pub enum IssueError {
GitError(#[from] crate::git::GitError), GitError(#[from] crate::git::GitError),
#[error("Failed to run editor")] #[error("Failed to run editor")]
EditorError, EditorError,
#[error("supplied description is empty")]
EmptyDescription,
} }
impl FromStr for State { impl FromStr for State {
@ -197,9 +195,6 @@ impl Issue {
} }
pub fn set_description(&mut self, description: &str) -> Result<(), IssueError> { pub fn set_description(&mut self, description: &str) -> Result<(), IssueError> {
if description.len() == 0 {
return Err(IssueError::EmptyDescription);
}
self.description = String::from(description); self.description = String::from(description);
let mut description_filename = std::path::PathBuf::from(&self.dir); let mut description_filename = std::path::PathBuf::from(&self.dir);
description_filename.push("description"); description_filename.push("description");
@ -219,7 +214,6 @@ impl Issue {
pub fn edit_description(&mut self) -> Result<(), IssueError> { pub fn edit_description(&mut self) -> Result<(), IssueError> {
let mut description_filename = std::path::PathBuf::from(&self.dir); let mut description_filename = std::path::PathBuf::from(&self.dir);
description_filename.push("description"); description_filename.push("description");
let exists = description_filename.exists();
let result = std::process::Command::new("vi") let result = std::process::Command::new("vi")
.arg(&description_filename.as_mut_os_str()) .arg(&description_filename.as_mut_os_str())
.spawn()? .spawn()?
@ -229,31 +223,8 @@ impl Issue {
println!("stderr: {}", std::str::from_utf8(&result.stderr).unwrap()); println!("stderr: {}", std::str::from_utf8(&result.stderr).unwrap());
return Err(IssueError::EditorError); return Err(IssueError::EditorError);
} }
if description_filename.exists() && description_filename.metadata()?.len() > 0 { crate::git::git_commit_file(&description_filename)?;
crate::git::add_file(&description_filename)?; self.read_description()?;
} 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(()) Ok(())
} }