Skip to content

Commit 22e3330

Browse files
committed
Avoid branch::create_reference types to be available from multiple locations.
1 parent ec44383 commit 22e3330

File tree

6 files changed

+106
-114
lines changed

6 files changed

+106
-114
lines changed

crates/but-api/src/commands/stack.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use crate::commands::stack::create_reference::Anchor;
1+
use crate::commands::stack::create_reference::Anchor as AnchorAPI;
22
use crate::{App, error::Error};
33
use anyhow::{Context, anyhow};
4-
use but_workspace::branch::{ReferenceAnchor, ReferencePosition};
4+
use but_workspace::branch::create_reference::{Anchor, Position};
55
use gitbutler_branch_actions::internal::PushResult;
66
use gitbutler_branch_actions::stack::CreateSeriesRequest;
77
use gitbutler_command_context::CommandContext;
@@ -40,11 +40,11 @@ pub mod create_reference {
4040
pub enum Anchor {
4141
AtCommit {
4242
commit_id: HexHash,
43-
position: but_workspace::branch::ReferencePosition,
43+
position: but_workspace::branch::create_reference::Position,
4444
},
4545
AtReference {
4646
short_name: String,
47-
position: but_workspace::branch::ReferencePosition,
47+
position: but_workspace::branch::create_reference::Position,
4848
},
4949
}
5050

@@ -66,17 +66,17 @@ pub fn create_reference(app: &App, params: create_reference::Params) -> Result<(
6666
let anchor = anchor
6767
.map(|anchor| -> Result<_, Error> {
6868
Ok(match anchor {
69-
Anchor::AtCommit {
69+
AnchorAPI::AtCommit {
7070
commit_id,
7171
position,
72-
} => but_workspace::branch::ReferenceAnchor::AtCommit {
72+
} => but_workspace::branch::create_reference::Anchor::AtCommit {
7373
commit_id: commit_id.into(),
7474
position,
7575
},
76-
Anchor::AtReference {
76+
AnchorAPI::AtReference {
7777
short_name,
7878
position,
79-
} => but_workspace::branch::ReferenceAnchor::AtSegment {
79+
} => but_workspace::branch::create_reference::Anchor::AtSegment {
8080
ref_name: Cow::Owned(
8181
Category::LocalBranch
8282
.to_full_name(short_name.as_str())
@@ -104,7 +104,7 @@ pub fn create_branch(app: &App, params: CreateBranchParams) -> Result<(), Error>
104104
let project = gitbutler_project::get(params.project_id)?;
105105
let ctx = CommandContext::open(&project, app.app_settings.get()?.clone())?;
106106
if app.app_settings.get()?.feature_flags.ws3 {
107-
use ReferencePosition::Above;
107+
use Position::Above;
108108
let mut guard = project.exclusive_worktree_access();
109109
let (repo, mut meta, graph) = ctx.graph_and_meta_mut_and_repo(guard.write_permission())?;
110110
let ws = graph.to_workspace()?;
@@ -128,12 +128,12 @@ pub fn create_branch(app: &App, params: CreateBranchParams) -> Result<(), Error>
128128
segment
129129
.ref_name
130130
.as_ref()
131-
.map(|rn| ReferenceAnchor::AtSegment {
131+
.map(|rn| Anchor::AtSegment {
132132
ref_name: Cow::Borrowed(rn.as_ref()),
133133
position: Above,
134134
})
135135
.or_else(|| {
136-
Some(ReferenceAnchor::AtCommit {
136+
Some(Anchor::AtCommit {
137137
commit_id: graph.tip_skip_empty(segment.id)?.id,
138138
position: Above,
139139
})

crates/but-api/tests/api/commands/stack.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ mod create_reference {
22
use but_api::commands::stack::create_reference;
33
use but_api::commands::stack::create_reference::{Params, Request};
44
use but_api::hex_hash::HexHash;
5-
use but_workspace::branch::ReferencePosition;
5+
use but_workspace::branch::create_reference::Position;
66
use gitbutler_project::ProjectId;
77
use std::str::FromStr;
88

@@ -34,7 +34,7 @@ mod create_reference {
3434
gix::ObjectId::from_str("5c69907b1244089142905dba380371728e2e8160")
3535
.unwrap(),
3636
),
37-
position: ReferencePosition::Above,
37+
position: Position::Above,
3838
}),
3939
},
4040
};
@@ -60,7 +60,7 @@ mod create_reference {
6060
new_name: "new-branch".to_string(),
6161
anchor: Some(create_reference::Anchor::AtReference {
6262
short_name: "anchor-ref".into(),
63-
position: ReferencePosition::Above,
63+
position: Position::Above,
6464
}),
6565
},
6666
};

crates/but-testing/src/command/mod.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use but_core::UnifiedDiff;
33
use but_db::poll::ItemKind;
44
use but_graph::VirtualBranchesTomlMetadata;
55
use but_settings::AppSettings;
6-
use but_workspace::branch::{ReferenceAnchor, ReferencePosition};
6+
use but_workspace::branch::create_reference::{Anchor, Position};
77
use but_workspace::{DiffSpec, HunkHeader};
88
use gitbutler_project::{Project, ProjectId};
99
use gix::bstr::{BString, ByteSlice};
@@ -611,22 +611,21 @@ pub fn create_reference(
611611
) -> anyhow::Result<()> {
612612
let (repo, project, graph, mut meta) =
613613
repo_and_maybe_project_and_graph(args, RepositoryOpenMode::General)?;
614-
let resolve =
615-
|spec: &str, position: ReferencePosition| -> anyhow::Result<ReferenceAnchor<'_>> {
616-
Ok(match repo.try_find_reference(spec)? {
617-
None => ReferenceAnchor::AtCommit {
618-
commit_id: repo.rev_parse_single(spec)?.detach(),
619-
position,
620-
},
621-
Some(rn) => ReferenceAnchor::AtSegment {
622-
ref_name: Cow::Owned(rn.inner.name),
623-
position,
624-
},
625-
})
626-
};
614+
let resolve = |spec: &str, position: Position| -> anyhow::Result<Anchor<'_>> {
615+
Ok(match repo.try_find_reference(spec)? {
616+
None => Anchor::AtCommit {
617+
commit_id: repo.rev_parse_single(spec)?.detach(),
618+
position,
619+
},
620+
Some(rn) => Anchor::AtSegment {
621+
ref_name: Cow::Owned(rn.inner.name),
622+
position,
623+
},
624+
})
625+
};
627626
let anchor = above
628-
.map(|spec| resolve(spec, ReferencePosition::Above))
629-
.or_else(|| below.map(|spec| resolve(spec, ReferencePosition::Below)))
627+
.map(|spec| resolve(spec, Position::Above))
628+
.or_else(|| below.map(|spec| resolve(spec, Position::Below)))
630629
.transpose()?;
631630

632631
let new_ref = Category::LocalBranch.to_full_name(short_name)?;

crates/but-workspace/src/branch/create_reference.rs

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use anyhow::Context;
22
use std::borrow::Cow;
33

4-
/// For use in [`ReferenceAnchor`].
4+
/// For use in [`Anchor`].
55
#[derive(Debug, Clone, Copy, serde::Serialize, serde::Deserialize)]
6-
pub enum ReferencePosition {
6+
pub enum Position {
77
/// The new dependent branch will appear above its anchor.
88
Above,
99
/// The new dependent branch will appear below its anchor.
@@ -33,7 +33,7 @@ impl<'a> From<&'a but_graph::projection::StackCommit> for MinimalCommit<'a> {
3333
}
3434
}
3535

36-
impl ReferencePosition {
36+
impl Position {
3737
fn resolve_commit(
3838
&self,
3939
commit: MinimalCommit<'_>,
@@ -43,15 +43,13 @@ impl ReferencePosition {
4343
return Ok(commit.id);
4444
}
4545
Ok(match self {
46-
ReferencePosition::Above => commit.id,
47-
ReferencePosition::Below => {
48-
commit.parent_ids.iter().cloned().next().with_context(|| {
49-
format!(
50-
"Commit {id} is the first in history and no branch can point below it",
51-
id = commit.id
52-
)
53-
})?
54-
}
46+
Position::Above => commit.id,
47+
Position::Below => commit.parent_ids.iter().cloned().next().with_context(|| {
48+
format!(
49+
"Commit {id} is the first in history and no branch can point below it",
50+
id = commit.id
51+
)
52+
})?,
5553
})
5654
}
5755
}
@@ -63,7 +61,7 @@ impl ReferencePosition {
6361
/// go just by commit. We must be specifying it in terms of above/below ref-name when possible,
6462
/// or else they will always go on top.
6563
#[derive(Debug, Clone)]
66-
pub enum ReferenceAnchor<'a> {
64+
pub enum Anchor<'a> {
6765
/// Use a commit as position, which means we always need unambiguous placement
6866
/// without a way to stack references on top of other references - only on top
6967
/// of commits their segments may own.
@@ -72,7 +70,7 @@ pub enum ReferenceAnchor<'a> {
7270
commit_id: gix::ObjectId,
7371
/// `Above` means the reference will point at `commit_id`, `Below` means it points at its
7472
/// parent if possible.
75-
position: ReferencePosition,
73+
position: Position,
7674
},
7775
/// Use a segment as reference for positioning the new reference.
7876
/// Without a workspace, this is the same as saying 'the commit that the segment points to'.
@@ -83,22 +81,22 @@ pub enum ReferenceAnchor<'a> {
8381
/// if it points to the same commit.
8482
/// `Below` means the reference will be right below the segment with `ref_name` even
8583
/// if it points to the same commit.
86-
position: ReferencePosition,
84+
position: Position,
8785
},
8886
}
8987

90-
impl<'a> ReferenceAnchor<'a> {
88+
impl<'a> Anchor<'a> {
9189
/// Create a new instance with an object ID as anchor.
92-
pub fn at_id(commit_id: impl Into<gix::ObjectId>, position: ReferencePosition) -> Self {
93-
ReferenceAnchor::AtCommit {
90+
pub fn at_id(commit_id: impl Into<gix::ObjectId>, position: Position) -> Self {
91+
Anchor::AtCommit {
9492
commit_id: commit_id.into(),
9593
position,
9694
}
9795
}
9896

9997
/// Create a new instance with a segment name as anchor.
100-
pub fn at_segment(ref_name: &'a gix::refs::FullNameRef, position: ReferencePosition) -> Self {
101-
ReferenceAnchor::AtSegment {
98+
pub fn at_segment(ref_name: &'a gix::refs::FullNameRef, position: Position) -> Self {
99+
Anchor::AtSegment {
102100
ref_name: Cow::Borrowed(ref_name),
103101
position,
104102
}
@@ -108,7 +106,7 @@ impl<'a> ReferenceAnchor<'a> {
108106
pub(super) mod function {
109107
#![expect(clippy::indexing_slicing)]
110108

111-
use crate::branch::{ReferenceAnchor, ReferencePosition};
109+
use crate::branch::create_reference::{Anchor, Position};
112110
use anyhow::{Context, bail};
113111
use but_core::ref_metadata::{StackId, WorkspaceStack, WorkspaceStackBranch};
114112
use but_core::{RefMetadata, ref_metadata};
@@ -130,7 +128,7 @@ pub(super) mod function {
130128
/// Return a regenerated Graph that contains the new reference, and from which a new workspace can be derived.
131129
pub fn create_reference<'name, T: RefMetadata>(
132130
ref_name: impl Borrow<gix::refs::FullNameRef>,
133-
anchor: impl Into<Option<ReferenceAnchor<'name>>>,
131+
anchor: impl Into<Option<Anchor<'name>>>,
134132
repo: &gix::Repository,
135133
workspace: &but_graph::projection::Workspace<'_>,
136134
meta: &mut T,
@@ -172,7 +170,7 @@ pub(super) mod function {
172170
Some(Instruction::Independent),
173171
)
174172
}
175-
Some(ReferenceAnchor::AtCommit {
173+
Some(Anchor::AtCommit {
176174
commit_id,
177175
position,
178176
}) => {
@@ -193,7 +191,7 @@ pub(super) mod function {
193191

194192
(validate_id, ref_target_id, instruction)
195193
}
196-
Some(ReferenceAnchor::AtSegment { ref_name, position }) => {
194+
Some(Anchor::AtSegment { ref_name, position }) => {
197195
let mut validate_id = true;
198196
let ref_target_id = if workspace.has_metadata() {
199197
let (stack_idx, seg_idx) = workspace
@@ -373,8 +371,8 @@ pub(super) mod function {
373371
let branches = &mut ws_meta.stacks[stack_idx].branches;
374372
branches.insert(
375373
match position {
376-
ReferencePosition::Above => branch_idx,
377-
ReferencePosition::Below => branch_idx + 1,
374+
Position::Above => branch_idx,
375+
Position::Below => branch_idx + 1,
378376
},
379377
WorkspaceStackBranch {
380378
ref_name: new_ref.to_owned(),
@@ -394,7 +392,7 @@ pub(super) mod function {
394392
ws: &but_graph::projection::Workspace<'_>,
395393
anchor_id: gix::ObjectId,
396394
) -> anyhow::Result<Instruction<'static>> {
397-
use ReferencePosition::*;
395+
use Position::*;
398396
let (anchor_stack_idx, anchor_seg_idx, _anchor_commit_idx) = ws
399397
.find_owner_indexes_by_commit_id(anchor_id)
400398
.with_context(|| {
@@ -444,7 +442,7 @@ pub(super) mod function {
444442
DependentInStack(StackId),
445443
Dependent {
446444
ref_name: Cow<'a, gix::refs::FullNameRef>,
447-
position: ReferencePosition,
445+
position: Position,
448446
},
449447
}
450448
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,6 @@ impl Stack {
460460
pub mod remove_reference;
461461
pub use remove_reference::function::remove_reference;
462462

463-
mod create_reference;
463+
/// related types for creating a workspace reference.
464+
pub mod create_reference;
464465
pub use create_reference::function::create_reference;
465-
pub use create_reference::*;

0 commit comments

Comments
 (0)