Don't create unrequired wip commits

This commit is contained in:
Caleb Owens 2024-07-25 15:25:51 +02:00
parent 9e2f065109
commit 2f54e51a33
3 changed files with 9 additions and 10 deletions

View File

@ -8,7 +8,6 @@ use crate::{
NameConflictResolution, VirtualBranchesExt,
};
use anyhow::{anyhow, Context, Result};
use git2::build::TreeUpdateBuilder;
use gitbutler_branch::{Branch, BranchExt, BranchId};
use gitbutler_commit::commit_headers::CommitHeadersV2;
use gitbutler_oplog::SnapshotExt;
@ -180,16 +179,16 @@ impl BranchManager<'_> {
vbranch.source_refname = Some(Refname::try_from(&branch)?);
vb_state.set_branch(vbranch.clone())?;
self.build_metadata_commit(vbranch, &branch)?;
self.build_wip_commit(vbranch, &branch)?;
Ok(branch)
}
fn build_metadata_commit(
fn build_wip_commit(
&self,
vbranch: &mut Branch,
branch: &git2::Branch<'_>,
) -> Result<git2::Oid> {
) -> Result<Option<git2::Oid>> {
let repo = self.project_repository.repo();
// Build wip tree as either any uncommitted changes or an empty tree
@ -199,7 +198,8 @@ impl BranchManager<'_> {
let tree = if vbranch_head_tree.id() != vbranch_wip_tree.id() {
vbranch_wip_tree
} else {
repo.find_tree(TreeUpdateBuilder::new().create_updated(repo, &vbranch_head_tree)?)?
// Don't create the wip commit if not required
return Ok(None);
};
// Build commit message
@ -227,6 +227,6 @@ impl BranchManager<'_> {
vbranch.not_in_workspace_wip_change_id = Some(commit_headers.change_id);
vb_state.set_branch(vbranch.clone())?;
Ok(commit_oid)
Ok(Some(commit_oid))
}
}

View File

@ -103,8 +103,7 @@ async fn rebase_commit() {
assert_eq!(branches.len(), 1);
assert_eq!(branches[0].id, branch1_id);
assert_eq!(branches[0].files.len(), 0);
// TODO: THIS SHOULD BE 1
assert_eq!(branches[0].commits.len(), 2);
assert_eq!(branches[0].commits.len(), 1);
assert!(branches[0].active);
assert!(!branches[0].conflicted);

View File

@ -132,7 +132,7 @@ mod applied_branch {
assert!(branches[0].conflicted);
assert!(branches[0].base_current);
assert_eq!(branches[0].files.len(), 1);
assert_eq!(branches[0].commits.len(), 2);
assert_eq!(branches[0].commits.len(), 1);
assert_eq!(
std::fs::read_to_string(repository.path().join("file.txt")).unwrap(),
"<<<<<<< ours\nconflict\n=======\nsecond\n>>>>>>> theirs\n"
@ -208,7 +208,7 @@ mod applied_branch {
assert!(branches[0].conflicted);
assert!(branches[0].base_current);
assert_eq!(branches[0].files.len(), 1);
assert_eq!(branches[0].commits.len(), 2);
assert_eq!(branches[0].commits.len(), 1);
assert_eq!(
std::fs::read_to_string(repository.path().join("file.txt")).unwrap(),
"<<<<<<< ours\nconflict\n=======\nsecond\n>>>>>>> theirs\n"