Skip to content

Commit fa26bf1

Browse files
committed
Basic apply and unapply
The idea is to develop both at the same time so it's easier to test that 'unapply' truly goes back to the (mostly) original state.
1 parent f0c03d1 commit fa26bf1

File tree

6 files changed

+115
-5
lines changed

6 files changed

+115
-5
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/// Returned by [function::apply()].
2+
pub struct Outcome {
3+
/// The newly created graph, useful to project a workspace and see how the workspace looks like with the branch applied.
4+
pub graph: but_graph::Graph,
5+
}
6+
7+
pub(crate) mod function {
8+
use but_core::RefMetadata;
9+
10+
/// Apply `branch` to the given `workspace`, and possibly create the workspace reference in `repo` along with its `meta`-data
11+
/// if it doesn't exist yet.
12+
/// Otherwise, add it to the existing `workspace`, and update its metadata accordingly.
13+
pub fn apply<T: RefMetadata>(
14+
branch: &gix::refs::FullNameRef,
15+
repo: &mut gix::Repository,
16+
workspace: &gix::refs::FullNameRef,
17+
meta: &mut T,
18+
) -> anyhow::Result<but_graph::Graph> {
19+
todo!()
20+
}
21+
}

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,14 @@ pub(super) mod function {
220220
ref_name.shorten()
221221
);
222222
};
223-
position.resolve_commit(segment.commits.first().context(
224-
"BUG: empty segments aren't possible without workspace metadata",
225-
)?.into(), ws_base)?
223+
position.resolve_commit(
224+
segment
225+
.commits
226+
.first()
227+
.context("Cannot create reference on unborn branch")?
228+
.into(),
229+
ws_base,
230+
)?
226231
};
227232
(
228233
validate_id,

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,10 @@ impl Stack {
456456
}
457457
}
458458

459+
/// Functions and types related to applying a workspace branch.
460+
pub mod apply;
461+
pub use apply::function::apply;
462+
459463
/// related types for removing a workspace reference.
460464
pub mod remove_reference;
461465
pub use remove_reference::function::remove_reference;
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use crate::ref_info::with_workspace_commit::utils::named_read_only_in_memory_scenario;
2+
use crate::utils::r;
3+
use but_graph::init::Options;
4+
use but_testsupport::graph_workspace;
5+
use but_workspace::branch::ReferenceAnchor;
6+
use but_workspace::branch::ReferencePosition::Above;
7+
use std::borrow::Cow;
8+
9+
#[test]
10+
#[ignore = "TBD: idempotent"]
11+
fn apply_branch_already_in_workspace() -> anyhow::Result<()> {
12+
Ok(())
13+
}
14+
15+
#[test]
16+
#[ignore = "TBD: idempotent"]
17+
fn unapply_branch_not_in_workspace() -> anyhow::Result<()> {
18+
Ok(())
19+
}
20+
21+
#[test]
22+
#[ignore = "TBD"]
23+
fn unborn_apply_needs_base() -> anyhow::Result<()> {
24+
let (repo, mut meta) = named_read_only_in_memory_scenario("unborn-empty", "")?;
25+
let graph = but_graph::Graph::from_head(&repo, &*meta, Options::limited())?;
26+
let ws = graph.to_workspace()?;
27+
insta::assert_snapshot!(graph_workspace(&ws), @r"
28+
⌂:0:main <> ✓!
29+
└── ≡:0:main
30+
└── :0:main
31+
");
32+
33+
// Below first in history
34+
// let err = but_workspace::branch::apply(
35+
// new_name,
36+
// ReferenceAnchor::AtSegment {
37+
// ref_name: Cow::Borrowed(r("refs/heads/main")),
38+
// position: Above,
39+
// },
40+
// &repo,
41+
// &ws,
42+
// &mut *meta,
43+
// )
44+
// .unwrap_err();
45+
// assert_eq!(err.to_string(), "Cannot create reference on unborn branch");
46+
// THis can't work, but should fail gracefully.
47+
Ok(())
48+
}
49+
50+
#[test]
51+
#[ignore = "TBD"]
52+
fn apply_branch_resting_on_base() -> anyhow::Result<()> {
53+
// THis can't work, but should fail gracefully.
54+
Ok(())
55+
}

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use but_core::ref_metadata::ValueInfo;
88
use but_graph::init::Options;
99
use but_testsupport::{graph_workspace, id_at, id_by_rev, visualize_commit_graph_all};
1010
use but_workspace::branch::{ReferenceAnchor, ReferencePosition};
11+
use std::borrow::Cow;
1112

1213
mod with_workspace {
1314
use crate::ref_info::with_workspace_commit::utils::{
@@ -1068,6 +1069,30 @@ mod with_workspace {
10681069

10691070
#[test]
10701071
fn errors() -> anyhow::Result<()> {
1072+
let (repo, mut meta) = named_read_only_in_memory_scenario("unborn-empty", "")?;
1073+
let graph = but_graph::Graph::from_head(&repo, &*meta, Options::limited())?;
1074+
let ws = graph.to_workspace()?;
1075+
insta::assert_snapshot!(graph_workspace(&ws), @r"
1076+
⌂:0:main <> ✓!
1077+
└── ≡:0:main
1078+
└── :0:main
1079+
");
1080+
1081+
// Below first in history
1082+
let new_name = r("refs/heads/does-not-matter");
1083+
let err = but_workspace::branch::create_reference(
1084+
new_name,
1085+
ReferenceAnchor::AtSegment {
1086+
ref_name: Cow::Borrowed(r("refs/heads/main")),
1087+
position: Above,
1088+
},
1089+
&repo,
1090+
&ws,
1091+
&mut *meta,
1092+
)
1093+
.unwrap_err();
1094+
assert_eq!(err.to_string(), "Cannot create reference on unborn branch");
1095+
10711096
let (repo, mut meta) =
10721097
named_read_only_in_memory_scenario("with-remotes-no-workspace", "remote")?;
10731098
insta::assert_snapshot!(visualize_commit_graph_all(&repo)?, @r"
@@ -1087,7 +1112,6 @@ fn errors() -> anyhow::Result<()> {
10871112
");
10881113

10891114
let (id, ref_name) = id_at(&repo, "main");
1090-
let new_name = r("refs/heads/does-not-matter");
10911115
for anchor in [
10921116
ReferenceAnchor::at_id(id, Below),
10931117
ReferenceAnchor::at_segment(ref_name.as_ref(), Below),
@@ -1257,7 +1281,7 @@ fn errors() -> anyhow::Result<()> {
12571281
}
12581282

12591283
#[test]
1260-
fn journey() -> anyhow::Result<()> {
1284+
fn journey_with_commits() -> anyhow::Result<()> {
12611285
let (_tmp, repo, mut meta) = named_writable_scenario("single-branch-with-3-commits")?;
12621286
insta::assert_snapshot!(visualize_commit_graph_all(&repo)?, @r"
12631287
* 281da94 (HEAD -> main) 3
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
mod apply_unapply;
12
mod create_reference;
23
mod remove_reference;

0 commit comments

Comments
 (0)