lib: add Commit::is_discardable()

This commit is contained in:
Samuel Tardieu 2023-03-05 21:27:03 +01:00
parent 5d184e6694
commit 616058c2fa
2 changed files with 12 additions and 5 deletions

View file

@ -128,4 +128,15 @@ impl Commit {
pub fn committer(&self) -> &Signature {
&self.data.committer
}
/// A commit is discardable if it has one parent, no change from its
/// parent, and an empty description.
pub fn is_discardable(&self) -> bool {
if self.description().is_empty() {
if let [parent_commit] = &*self.parents() {
return self.tree_id() == parent_commit.tree_id();
}
}
false
}
}

View file

@ -743,11 +743,7 @@ impl MutableRepo {
.store()
.get_commit(&wc_commit_id)
.map_err(EditCommitError::WorkingCopyCommitNotFound)?;
if wc_commit.parent_ids().len() == 1
&& wc_commit.parents()[0].tree_id() == wc_commit.tree_id()
&& wc_commit.description().is_empty()
&& self.view().heads().contains(wc_commit.id())
{
if wc_commit.is_discardable() && self.view().heads().contains(wc_commit.id()) {
// Abandon the working-copy commit we're leaving if it's empty and a head commit
self.record_abandoned_commit(wc_commit_id);
}