Skip to content

Commit 93199fd

Browse files
committed
Implement snapshot::create_tree() with worktree snapshot supports
1 parent 815a4bd commit 93199fd

File tree

11 files changed

+581
-246
lines changed

11 files changed

+581
-246
lines changed

crates/but-core/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,9 +332,9 @@ pub enum IgnoredWorktreeTreeChangeStatus {
332332
pub struct IgnoredWorktreeChange {
333333
/// The worktree-relative path to the change.
334334
#[serde(serialize_with = "gitbutler_serde::bstring_lossy::serialize")]
335-
path: BString,
335+
pub path: BString,
336336
/// The status that caused this change to be ignored.
337-
status: IgnoredWorktreeTreeChangeStatus,
337+
pub status: IgnoredWorktreeTreeChangeStatus,
338338
}
339339

340340
/// The type returned by [`worktree_changes()`](diff::worktree_changes).

crates/but-workspace/src/commit_engine/mod.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,28 @@ pub fn create_commit(
231231
bail!("cannot currently handle more than 1 parent")
232232
}
233233

234+
let target_tree = match &destination {
235+
Destination::NewCommit {
236+
parent_commit_id: None,
237+
..
238+
} => gix::ObjectId::empty_tree(repo.object_hash()),
239+
Destination::NewCommit {
240+
parent_commit_id: Some(base_commit),
241+
..
242+
}
243+
| Destination::AmendCommit {
244+
commit_id: base_commit,
245+
..
246+
} => but_core::Commit::from_id(base_commit.attach(repo))?
247+
.tree_id_or_auto_resolution()?
248+
.detach(),
249+
};
250+
234251
let CreateTreeOutcome {
235252
rejected_specs,
236253
destination_tree,
237254
changed_tree_pre_cherry_pick,
238-
} = create_tree(repo, &destination, move_source, changes, context_lines)?;
255+
} = create_tree(repo, target_tree, move_source, changes, context_lines)?;
239256
let new_commit = if let Some(new_tree) = destination_tree {
240257
match destination {
241258
Destination::NewCommit {

crates/but-workspace/src/commit_engine/tree/mod.rs

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::commit_engine::{Destination, MoveSourceCommit, RejectionReason, apply_hunks};
1+
use crate::commit_engine::{MoveSourceCommit, RejectionReason, apply_hunks};
22
use crate::{DiffSpec, HunkHeader};
33
use bstr::{BStr, ByteSlice};
44
use but_core::{RepositoryExt, UnifiedDiff};
@@ -17,8 +17,8 @@ pub struct CreateTreeOutcome {
1717
/// when merging the workspace commit, or because the specified hunks didn't match exactly due to changes
1818
/// that happened in the meantime, or if a file without a change was specified.
1919
pub rejected_specs: Vec<(RejectionReason, DiffSpec)>,
20-
/// The newly created seen from tree that acts as the destination of the changes, or `None` if no commit could be
21-
/// created as all changes-requests were rejected.
20+
/// The newly created seen from tree that acts as the destination of the changes, or `None` if no tree could be
21+
/// created as all changes-requests were rejected (or there was no change).
2222
pub destination_tree: Option<gix::ObjectId>,
2323
/// If `destination_tree` is `Some(_)`, this field is `Some(_)` as well and denotes the base-tree + all changes.
2424
/// If the applied changes were from the worktree, it's `HEAD^{tree}` + changes.
@@ -29,28 +29,11 @@ pub struct CreateTreeOutcome {
2929
/// Like [`create_commit()`], but lower-level and only returns a new tree, without finally associating it with a commit.
3030
pub fn create_tree(
3131
repo: &gix::Repository,
32-
destination: &Destination,
32+
target_tree: gix::ObjectId,
3333
move_source: Option<MoveSourceCommit>,
3434
changes: Vec<DiffSpec>,
3535
context_lines: u32,
3636
) -> anyhow::Result<CreateTreeOutcome> {
37-
let target_tree = match destination {
38-
Destination::NewCommit {
39-
parent_commit_id: None,
40-
..
41-
} => gix::ObjectId::empty_tree(repo.object_hash()),
42-
Destination::NewCommit {
43-
parent_commit_id: Some(base_commit),
44-
..
45-
}
46-
| Destination::AmendCommit {
47-
commit_id: base_commit,
48-
..
49-
} => but_core::Commit::from_id(base_commit.attach(repo))?
50-
.tree_id_or_auto_resolution()?
51-
.detach(),
52-
};
53-
5437
let mut changes: Vec<_> = changes.into_iter().map(Ok).collect();
5538
let (new_tree, changed_tree_pre_cherry_pick) = if changes.is_empty() {
5639
(Some(target_tree), None)
@@ -81,7 +64,7 @@ pub fn create_tree(
8164
let tree_with_changes = if new_tree == actual_base_tree
8265
&& changes.iter().all(|c| {
8366
c.is_ok()
84-
// Some rejections are OK and we want to create a commit anyway.
67+
// Some rejections are OK, and we want to create a commit anyway.
8568
|| !matches!(
8669
c,
8770
Err((RejectionReason::CherryPickMergeConflict,_))

crates/but-workspace/src/snapshot.rs

Lines changed: 0 additions & 217 deletions
This file was deleted.

0 commit comments

Comments
 (0)