Issue: make a helper function to commit an Issue

This improves code reuse and streamlines the code a bit.
This commit is contained in:
Sebastian Kuzminsky 2025-07-15 16:27:48 -06:00
parent e0d9d45a6a
commit 5e5508a2ee

View file

@ -243,8 +243,7 @@ impl Issue {
None => issue.edit_description_file()?, None => issue.edit_description_file()?,
}; };
crate::git::add(&issue_dir)?; issue.commit(&format!("create new issue {}", issue_id))?;
crate::git::commit(&issue_dir, &format!("create new issue {}", issue_id))?;
Ok(issue) Ok(issue)
} }
@ -253,21 +252,15 @@ impl Issue {
pub fn edit_description(&mut self) -> Result<(), IssueError> { pub fn edit_description(&mut self) -> Result<(), IssueError> {
self.edit_description_file()?; self.edit_description_file()?;
let description_filename = self.description_filename(); let description_filename = self.description_filename();
crate::git::add(&description_filename)?; self.commit(&format!(
if crate::git::worktree_is_dirty(&self.dir.to_string_lossy())? { "edit description of issue {}",
crate::git::commit( description_filename
&description_filename.parent().unwrap(), .parent()
&format!( .unwrap()
"edit description of issue {}", .file_name()
description_filename .unwrap()
.parent() .to_string_lossy(),
.unwrap() ))?;
.file_name()
.unwrap()
.to_string_lossy()
),
)?;
}
Ok(()) Ok(())
} }
@ -286,18 +279,12 @@ impl Issue {
state_filename.push("state"); state_filename.push("state");
let mut state_file = std::fs::File::create(&state_filename)?; let mut state_file = std::fs::File::create(&state_filename)?;
write!(state_file, "{}", new_state)?; write!(state_file, "{}", new_state)?;
crate::git::add(&state_filename)?; self.commit(&format!(
if crate::git::worktree_is_dirty(&self.dir.to_string_lossy())? { "change state of issue {}, {} -> {}",
crate::git::commit( self.dir.file_name().unwrap().to_string_lossy(),
&self.dir, old_state,
&format!( new_state,
"change state of issue {}, {} -> {}", ))?;
self.dir.file_name().unwrap().to_string_lossy(),
old_state,
new_state,
),
)?;
}
Ok(()) Ok(())
} }
@ -319,18 +306,12 @@ impl Issue {
assignee_filename.push("assignee"); assignee_filename.push("assignee");
let mut assignee_file = std::fs::File::create(&assignee_filename)?; let mut assignee_file = std::fs::File::create(&assignee_filename)?;
write!(assignee_file, "{}", new_assignee)?; write!(assignee_file, "{}", new_assignee)?;
crate::git::add(&assignee_filename)?; self.commit(&format!(
if crate::git::worktree_is_dirty(&self.dir.to_string_lossy())? { "change assignee of issue {}, {} -> {}",
crate::git::commit( self.dir.file_name().unwrap().to_string_lossy(),
&self.dir, old_assignee,
&format!( new_assignee,
"change assignee of issue {}, {} -> {}", ))?;
self.dir.file_name().unwrap().to_string_lossy(),
old_assignee,
new_assignee,
),
)?;
}
Ok(()) Ok(())
} }
@ -444,7 +425,15 @@ impl Issue {
for tag in &self.tags { for tag in &self.tags {
writeln!(tags_file, "{}", tag)?; writeln!(tags_file, "{}", tag)?;
} }
crate::git::add(&tags_filename)?; self.commit(commit_message)?;
Ok(())
}
fn commit(&self, commit_message: &str) -> Result<(), IssueError> {
crate::git::add(&self.dir)?;
if !crate::git::worktree_is_dirty(&self.dir.to_string_lossy())? {
return Ok(());
}
crate::git::commit(&self.dir, commit_message)?; crate::git::commit(&self.dir, commit_message)?;
Ok(()) Ok(())
} }