Skip to content

Commit 052b82e

Browse files
committed
Avoid branch::create_reference types to be available from multiple locations.
1 parent 485463c commit 052b82e

File tree

6 files changed

+115
-127
lines changed

6 files changed

+115
-127
lines changed

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

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::commands::stack::create_reference::Anchor;
22
use crate::{App, error::Error};
33
use anyhow::{Context, anyhow};
4-
use but_settings::AppSettings;
54
use but_workspace::branch::{ReferenceAnchor, ReferencePosition};
65
use gitbutler_branch_actions::internal::PushResult;
76
use gitbutler_branch_actions::stack::CreateSeriesRequest;
@@ -57,9 +56,9 @@ pub mod create_reference {
5756
}
5857
}
5958

60-
pub fn create_reference(_app: &App, params: create_reference::Params) -> Result<(), Error> {
59+
pub fn create_reference(app: &App, params: create_reference::Params) -> Result<(), Error> {
6160
let project = gitbutler_project::get(params.project_id)?;
62-
let ctx = CommandContext::open(&project, AppSettings::load_from_default_path_creating()?)?;
61+
let ctx = CommandContext::open(&project, app.app_settings.get()?.clone())?;
6362
let create_reference::Request { new_name, anchor } = params.request;
6463
let new_ref = Category::LocalBranch
6564
.to_full_name(new_name.as_str())
@@ -101,10 +100,10 @@ pub fn create_reference(_app: &App, params: create_reference::Params) -> Result<
101100
Ok(())
102101
}
103102

104-
pub fn create_branch(_app: &App, params: CreateBranchParams) -> Result<(), Error> {
103+
pub fn create_branch(app: &App, params: CreateBranchParams) -> Result<(), Error> {
105104
let project = gitbutler_project::get(params.project_id)?;
106-
let ctx = CommandContext::open(&project, AppSettings::load_from_default_path_creating()?)?;
107-
if ctx.app_settings().feature_flags.ws3 {
105+
let ctx = CommandContext::open(&project, app.app_settings.get()?.clone())?;
106+
if app.app_settings.get()?.feature_flags.ws3 {
108107
use ReferencePosition::Above;
109108
let mut guard = project.exclusive_worktree_access();
110109
let (repo, mut meta, graph) = ctx.graph_and_meta_mut_and_repo(guard.write_permission())?;
@@ -165,11 +164,11 @@ pub struct RemoveBranchParams {
165164
pub branch_name: String,
166165
}
167166

168-
pub fn remove_branch(_app: &App, params: RemoveBranchParams) -> Result<(), Error> {
167+
pub fn remove_branch(app: &App, params: RemoveBranchParams) -> Result<(), Error> {
169168
let project = gitbutler_project::get(params.project_id)?;
170-
let ctx = CommandContext::open(&project, AppSettings::load_from_default_path_creating()?)?;
169+
let ctx = CommandContext::open(&project, app.app_settings.get()?.clone())?;
171170
let mut guard = project.exclusive_worktree_access();
172-
if ctx.app_settings().feature_flags.ws3 {
171+
if app.app_settings.get()?.feature_flags.ws3 {
173172
let (repo, mut meta, graph) = ctx.graph_and_meta_mut_and_repo(guard.write_permission())?;
174173
let ws = graph.to_workspace()?;
175174
let ref_name = Category::LocalBranch
@@ -205,9 +204,9 @@ pub struct UpdateBranchNameParams {
205204
pub new_name: String,
206205
}
207206

208-
pub fn update_branch_name(_app: &App, params: UpdateBranchNameParams) -> Result<(), Error> {
207+
pub fn update_branch_name(app: &App, params: UpdateBranchNameParams) -> Result<(), Error> {
209208
let project = gitbutler_project::get(params.project_id)?;
210-
let ctx = CommandContext::open(&project, AppSettings::load_from_default_path_creating()?)?;
209+
let ctx = CommandContext::open(&project, app.app_settings.get()?.clone())?;
211210
gitbutler_branch_actions::stack::update_branch_name(
212211
&ctx,
213212
params.stack_id,
@@ -227,11 +226,11 @@ pub struct UpdateBranchDescriptionParams {
227226
}
228227

229228
pub fn update_branch_description(
230-
_app: &App,
229+
app: &App,
231230
params: UpdateBranchDescriptionParams,
232231
) -> Result<(), Error> {
233232
let project = gitbutler_project::get(params.project_id)?;
234-
let ctx = CommandContext::open(&project, AppSettings::load_from_default_path_creating()?)?;
233+
let ctx = CommandContext::open(&project, app.app_settings.get()?.clone())?;
235234
gitbutler_branch_actions::stack::update_branch_description(
236235
&ctx,
237236
params.stack_id,
@@ -250,12 +249,9 @@ pub struct UpdateBranchPrNumberParams {
250249
pub pr_number: Option<usize>,
251250
}
252251

253-
pub fn update_branch_pr_number(
254-
_app: &App,
255-
params: UpdateBranchPrNumberParams,
256-
) -> Result<(), Error> {
252+
pub fn update_branch_pr_number(app: &App, params: UpdateBranchPrNumberParams) -> Result<(), Error> {
257253
let project = gitbutler_project::get(params.project_id)?;
258-
let ctx = CommandContext::open(&project, AppSettings::load_from_default_path_creating()?)?;
254+
let ctx = CommandContext::open(&project, app.app_settings.get()?.clone())?;
259255
gitbutler_branch_actions::stack::update_branch_pr_number(
260256
&ctx,
261257
params.stack_id,
@@ -275,9 +271,9 @@ pub struct PushStackParams {
275271
pub branch: String,
276272
}
277273

278-
pub fn push_stack(_app: &App, params: PushStackParams) -> Result<PushResult, Error> {
274+
pub fn push_stack(app: &App, params: PushStackParams) -> Result<PushResult, Error> {
279275
let project = gitbutler_project::get(params.project_id)?;
280-
let ctx = CommandContext::open(&project, AppSettings::load_from_default_path_creating()?)?;
276+
let ctx = CommandContext::open(&project, app.app_settings.get()?.clone())?;
281277
gitbutler_branch_actions::stack::push_stack(
282278
&ctx,
283279
params.stack_id,
@@ -297,9 +293,9 @@ pub struct PushStackToReviewParams {
297293
pub user: User,
298294
}
299295

300-
pub fn push_stack_to_review(_app: &App, params: PushStackToReviewParams) -> Result<String, Error> {
296+
pub fn push_stack_to_review(app: &App, params: PushStackToReviewParams) -> Result<String, Error> {
301297
let project = gitbutler_project::get(params.project_id)?;
302-
let ctx = CommandContext::open(&project, AppSettings::load_from_default_path_creating()?)?;
298+
let ctx = CommandContext::open(&project, app.app_settings.get()?.clone())?;
303299
let review_id = gitbutler_sync::stack_upload::push_stack_to_review(
304300
&ctx,
305301
&params.user,

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};
@@ -601,22 +601,21 @@ pub fn create_reference(
601601
) -> anyhow::Result<()> {
602602
let (repo, project, graph, mut meta) =
603603
repo_and_maybe_project_and_graph(args, RepositoryOpenMode::General)?;
604-
let resolve =
605-
|spec: &str, position: ReferencePosition| -> anyhow::Result<ReferenceAnchor<'_>> {
606-
Ok(match repo.try_find_reference(spec)? {
607-
None => ReferenceAnchor::AtCommit {
608-
commit_id: repo.rev_parse_single(spec)?.detach(),
609-
position,
610-
},
611-
Some(rn) => ReferenceAnchor::AtSegment {
612-
ref_name: Cow::Owned(rn.inner.name),
613-
position,
614-
},
615-
})
616-
};
604+
let resolve = |spec: &str, position: Position| -> anyhow::Result<Anchor<'_>> {
605+
Ok(match repo.try_find_reference(spec)? {
606+
None => Anchor::AtCommit {
607+
commit_id: repo.rev_parse_single(spec)?.detach(),
608+
position,
609+
},
610+
Some(rn) => Anchor::AtSegment {
611+
ref_name: Cow::Owned(rn.inner.name),
612+
position,
613+
},
614+
})
615+
};
617616
let anchor = above
618-
.map(|spec| resolve(spec, ReferencePosition::Above))
619-
.or_else(|| below.map(|spec| resolve(spec, ReferencePosition::Below)))
617+
.map(|spec| resolve(spec, Position::Above))
618+
.or_else(|| below.map(|spec| resolve(spec, Position::Below)))
620619
.transpose()?;
621620

622621
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
}) => {
@@ -200,7 +198,7 @@ pub(super) mod function {
200198

201199
(validate_id, ref_target_id, instruction)
202200
}
203-
Some(ReferenceAnchor::AtSegment { ref_name, position }) => {
201+
Some(Anchor::AtSegment { ref_name, position }) => {
204202
let mut validate_id = true;
205203
let ref_target_id = if workspace.has_metadata() {
206204
let (stack_idx, seg_idx) = workspace
@@ -396,8 +394,8 @@ pub(super) mod function {
396394
let branches = &mut ws_meta.stacks[stack_idx].branches;
397395
branches.insert(
398396
match position {
399-
ReferencePosition::Above => branch_idx,
400-
ReferencePosition::Below => branch_idx + 1,
397+
Position::Above => branch_idx,
398+
Position::Below => branch_idx + 1,
401399
},
402400
WorkspaceStackBranch {
403401
ref_name: new_ref.to_owned(),
@@ -417,7 +415,7 @@ pub(super) mod function {
417415
ws: &but_graph::projection::Workspace<'_>,
418416
anchor_id: gix::ObjectId,
419417
) -> anyhow::Result<Instruction<'static>> {
420-
use ReferencePosition::*;
418+
use Position::*;
421419
let (anchor_stack_idx, anchor_seg_idx, _anchor_commit_idx) = ws
422420
.find_owner_indexes_by_commit_id(anchor_id)
423421
.with_context(|| {
@@ -468,7 +466,7 @@ pub(super) mod function {
468466
DependentInStack(StackId),
469467
Dependent {
470468
ref_name: Cow<'a, gix::refs::FullNameRef>,
471-
position: ReferencePosition,
469+
position: Position,
472470
},
473471
}
474472
}

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)