Skip to content

Commit bbfacac

Browse files
committed
A more general way to create a typical workspace commit.
Let's make the type specific to what we actually need.
1 parent c374b37 commit bbfacac

File tree

4 files changed

+32
-15
lines changed

4 files changed

+32
-15
lines changed

crates/but-workspace/src/commit.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
use crate::WorkspaceCommit;
2-
use bstr::ByteSlice;
2+
use crate::ui::StackEntryNoOpt;
3+
use bstr::{BString, ByteSlice};
4+
5+
/// A minimal stack for use by [WorkspaceCommit::new_from_stacks()].
6+
pub struct Stack {
7+
/// The tip of the top-most branch, i.e., the most recent commit that would become the parent of new commits of the topmost stack branch.
8+
pub tip: gix::ObjectId,
9+
/// The short name of the stack, which is the name of the top-most branch, like `main` or `feature/branch` or `origin/tracking-some-PR`
10+
/// or something entirely made up.
11+
pub name: Option<BString>,
12+
}
13+
14+
impl From<StackEntryNoOpt> for Stack {
15+
fn from(value: StackEntryNoOpt) -> Self {
16+
Stack {
17+
tip: value.tip,
18+
name: value.name().map(ToOwned::to_owned),
19+
}
20+
}
21+
}
322

423
/// Construction
524
impl<'repo> WorkspaceCommit<'repo> {
@@ -20,10 +39,11 @@ impl<'repo> WorkspaceCommit<'repo> {
2039
/// It still needs its tree set to something non-empty.
2140
///
2241
/// `object_hash` is needed to create an empty tree hash.
23-
pub(crate) fn create_commit_from_vb_state(
24-
stacks: &[crate::ui::StackEntryNoOpt],
42+
pub(crate) fn new_from_stacks(
43+
stacks: impl IntoIterator<Item = impl Into<Stack>>,
2544
object_hash: gix::hash::Kind,
2645
) -> gix::objs::Commit {
46+
let stacks = stacks.into_iter().map(Into::into).collect::<Vec<_>>();
2747
// message that says how to get back to where they were
2848
let mut message = Self::GITBUTLER_WORKSPACE_COMMIT_TITLE.to_string();
2949
message.push_str("\n\n");
@@ -44,8 +64,8 @@ impl<'repo> WorkspaceCommit<'repo> {
4464
message.push_str("If you commit on this branch, GitButler will throw it away.\n\n");
4565
if !stacks.is_empty() {
4666
message.push_str("Here are the branches that are currently applied:\n");
47-
for branch in stacks {
48-
if let Some(name) = branch.name() {
67+
for branch in &stacks {
68+
if let Some(name) = &branch.name {
4969
message.push_str(" - ");
5070
message.push_str(name.to_str_lossy().as_ref());
5171
message.push('\n');

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -515,10 +515,7 @@ pub fn create_commit_and_update_refs(
515515
.map(|stack| crate::ui::StackEntryNoOpt::try_new(repo, stack))
516516
.collect::<Result<_, _>>()?;
517517
stacks.sort_by(|a, b| a.name().cmp(&b.name()));
518-
let new_wc = WorkspaceCommit::create_commit_from_vb_state(
519-
&stacks,
520-
repo.object_hash(),
521-
);
518+
let new_wc = WorkspaceCommit::new_from_stacks(stacks, repo.object_hash());
522519
repo.write_object(&new_wc)?.detach()
523520
} else {
524521
workspace_tip

crates/but-workspace/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ pub mod snapshot;
6161

6262
mod changeset;
6363

64-
mod commit;
64+
/// Utility types for the [`WorkspaceCommit`].
65+
pub(crate) mod commit;
6566

6667
/// Types used only when obtaining head-information.
6768
///

crates/but-workspace/tests/workspace/branch/apply_unapply_commit_uncommit.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -234,13 +234,12 @@ fn auto_checkout_of_enclosing_workspace_flat() -> anyhow::Result<()> {
234234
let graph =
235235
but_graph::Graph::from_commit_traversal(b_id, b_ref.clone(), &meta, Default::default())?;
236236
let ws = graph.to_workspace()?;
237-
// TODO: fix this - the entrypoint shouldn't alter the stack setup.
238237
insta::assert_snapshot!(graph_workspace(&ws), @r"
239-
📕🏘️⚠️:1:gitbutler/workspace <> ✓!
240-
└── ≡👉📙:0:B
241-
├── 👉📙:0:B
238+
📕🏘️⚠️:1:gitbutler/workspace <> ✓! on e5d0542
239+
├── ≡👉📙:3:B on e5d0542
240+
│ └── 👉📙:3:B
241+
└── ≡📙:2:A on e5d0542
242242
└── 📙:2:A
243-
└── ·e5d0542 (🏘️) ►main
244243
");
245244

246245
// Already applied (the HEAD points to it, it literally IS the workspace).

0 commit comments

Comments
 (0)