Skip to content

Commit 72d7d61

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 72d7d61

File tree

6 files changed

+108
-5
lines changed

6 files changed

+108
-5
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/// Returned by [function::apply()].
2+
#[derive(Debug)]
3+
pub struct Outcome {
4+
/// The newly created graph, useful to project a workspace and see how the workspace looks like with the branch applied.
5+
pub graph: but_graph::Graph,
6+
/// `true` if we created the given workspace ref as it didn't exist yet.
7+
pub workspace_ref_created: bool,
8+
}
9+
10+
pub(crate) mod function {
11+
use super::Outcome;
12+
use but_core::RefMetadata;
13+
14+
/// Apply `branch` to the given `workspace`, and possibly create the workspace reference in `repo`
15+
/// along with its `meta`-data if it doesn't exist yet.
16+
/// Otherwise, add it to the existing `workspace`, and update its metadata accordingly.
17+
///
18+
/// On `error`, neither `repo` nor `meta` will have been changed.
19+
/// Otherwise, objects will have been persisted, and references and metadata will have been updated.
20+
pub fn apply<T: RefMetadata>(
21+
branch: &gix::refs::FullNameRef,
22+
workspace: &but_graph::projection::Workspace,
23+
repo: &mut gix::Repository,
24+
meta: &mut T,
25+
) -> anyhow::Result<Outcome> {
26+
todo!()
27+
}
28+
}

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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
6+
#[test]
7+
#[ignore = "TBD: idempotent"]
8+
fn apply_branch_already_in_workspace() -> anyhow::Result<()> {
9+
Ok(())
10+
}
11+
12+
#[test]
13+
#[ignore = "TBD: idempotent"]
14+
fn unapply_branch_not_in_workspace() -> anyhow::Result<()> {
15+
Ok(())
16+
}
17+
18+
#[test]
19+
fn unborn_apply_needs_base() -> anyhow::Result<()> {
20+
let (mut repo, mut meta) = named_read_only_in_memory_scenario("unborn-empty", "")?;
21+
let graph = but_graph::Graph::from_head(&repo, &*meta, Options::limited())?;
22+
let ws = graph.to_workspace()?;
23+
insta::assert_snapshot!(graph_workspace(&ws), @r"
24+
⌂:0:main <> ✓!
25+
└── ≡:0:main
26+
└── :0:main
27+
");
28+
29+
// Below first in history
30+
let err =
31+
but_workspace::branch::apply(r("refs/heads/new"), &ws, &mut repo, &mut *meta).unwrap_err();
32+
assert_eq!(err.to_string(), "Cannot create reference on unborn branch");
33+
Ok(())
34+
}
35+
36+
#[test]
37+
#[ignore = "TBD"]
38+
fn apply_branch_resting_on_base() -> anyhow::Result<()> {
39+
// THis can't work, but should fail gracefully.
40+
Ok(())
41+
}

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)