Skip to content

Commit 789d5db

Browse files
committed
Remove change_ids dependency from squash function
This commit refactors the squash function, removing its dependency on change_ids for identifying the source and destination commits. All internal logic now uses commit ids directly, which simplifies the process of finding and validating the relevant commits in the squash operation. This should prevent issues arising from mismatched or missing change_ids, and aligns the function more closely with the rest of the codebase's usage of commit ids.
1 parent 19a530e commit 789d5db

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

crates/gitbutler-branch-actions/src/squash.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ fn do_squash_commits(
5858
ctx: &CommandContext,
5959
stack_id: StackId,
6060
mut source_ids: Vec<git2::Oid>,
61-
desitnation_id: git2::Oid,
61+
destination_id: git2::Oid,
6262
perm: &mut WorktreeWritePermission,
6363
) -> Result<git2::Oid> {
6464
let old_workspace = WorkspaceState::create(ctx, perm.read_permission())?;
@@ -87,7 +87,7 @@ fn do_squash_commits(
8787
source_ids_in_order.push(*id);
8888
false
8989
}
90-
id if *id == desitnation_id => {
90+
id if *id == destination_id => {
9191
// Add the destination id to the source ids in order
9292
source_ids_in_order.push(*id);
9393
true
@@ -105,7 +105,7 @@ fn do_squash_commits(
105105
if let Some(pos) = branch
106106
.commit_ids
107107
.iter()
108-
.position(|&id| id == desitnation_id)
108+
.position(|&id| id == destination_id)
109109
{
110110
branch.commit_ids.splice(pos..=pos, source_ids.clone());
111111
}
@@ -117,6 +117,8 @@ fn do_squash_commits(
117117
None
118118
};
119119

120+
let mut destination_id = destination_id;
121+
120122
// update source ids from the mapping if present
121123
if let Some(mapping) = mapping {
122124
for (_, old, new) in mapping.iter() {
@@ -128,6 +130,11 @@ fn do_squash_commits(
128130
.unwrap();
129131
source_ids[index] = new.to_git2();
130132
}
133+
134+
// if destination_id is old, replace it with new
135+
if destination_id == old.to_git2() {
136+
destination_id = new.to_git2();
137+
}
131138
}
132139
};
133140

@@ -147,24 +154,16 @@ fn do_squash_commits(
147154
.collect_vec();
148155

149156
// Find the new destination commit using the change id, error if not found
150-
let destination_change_id = ctx.repo().find_commit(desitnation_id)?.change_id();
151157
let destination_commit = branch_commits
152158
.iter()
153-
.find(|c| c.change_id() == destination_change_id)
159+
.find(|c| c.id() == destination_id)
154160
.context("Destination commit not found in the stack")?;
155161

156162
// Find the new source commits using the change ids, error if not found
157163
let source_commits = source_ids
158164
.iter()
159165
.filter_map(|id| ctx.repo().find_commit(*id).ok())
160-
.map(|c| {
161-
branch_commits
162-
.iter()
163-
.find(|b| b.change_id() == c.change_id())
164-
.cloned()
165-
.context("Source commit not found in the stack")
166-
})
167-
.collect::<Result<Vec<_>>>()?;
166+
.collect::<Vec<_>>();
168167

169168
validate(
170169
ctx,

0 commit comments

Comments
 (0)