@@ -73,10 +73,8 @@ pub struct Options {
73
73
pub ( crate ) mod function {
74
74
use super :: { Options , Outcome , WorkspaceReferenceNaming } ;
75
75
use crate :: ref_info:: WorkspaceExt ;
76
- use anyhow:: { Context , bail} ;
77
- use but_core:: ref_metadata:: { StackId , ValueInfo , WorkspaceStack , WorkspaceStackBranch } ;
78
- use but_core:: { RefMetadata , ref_metadata} ;
79
- use but_graph:: init:: Overlay ;
76
+ use anyhow:: bail;
77
+ use but_core:: RefMetadata ;
80
78
use but_graph:: projection:: WorkspaceKind ;
81
79
use std:: borrow:: Cow ;
82
80
@@ -104,11 +102,21 @@ pub(crate) mod function {
104
102
workspace_reference_naming,
105
103
} : Options ,
106
104
) -> anyhow:: Result < Outcome < ' graph > > {
107
- if workspace. refname_is_segment ( branch) {
108
- return Ok ( Outcome {
109
- graph : Cow :: Borrowed ( workspace. graph ) ,
110
- workspace_ref_created : false ,
111
- } ) ;
105
+ if workspace. is_reachable_from_entrypoint ( branch) {
106
+ if workspace. is_entrypoint ( )
107
+ || workspace
108
+ . stacks
109
+ . iter ( )
110
+ . flat_map ( |s| s. segments . iter ( ) . filter_map ( |s| s. ref_name . as_ref ( ) ) )
111
+ . any ( |rn| rn. as_ref ( ) == branch)
112
+ {
113
+ return Ok ( Outcome {
114
+ graph : Cow :: Borrowed ( workspace. graph ) ,
115
+ workspace_ref_created : false ,
116
+ } ) ;
117
+ }
118
+ } else if workspace. refname_is_segment ( branch) {
119
+ todo ! ( "checkout workspace so the to-be-applied branch becomes visible" )
112
120
}
113
121
114
122
if let Some ( ws_ref_name) = workspace. ref_name ( ) {
@@ -125,98 +133,86 @@ pub(crate) mod function {
125
133
bail ! ( "Refusing to work on workspace whose workspace commit isn't at the top" ) ;
126
134
}
127
135
128
- let ( workspace_ref_name_to_update, ws_ref_metadata, branch_to_apply_metadata, graph) =
129
- match & workspace. kind {
130
- WorkspaceKind :: Managed { ref_name }
131
- | WorkspaceKind :: ManagedMissingWorkspaceCommit { ref_name } => (
132
- ref_name. clone ( ) ,
133
- meta. workspace ( ref_name. as_ref ( ) ) ?,
134
- None ,
135
- Cow :: Borrowed ( workspace. graph ) ,
136
- ) ,
137
- WorkspaceKind :: AdHoc => {
138
- // We need to switch over to a possibly existing workspace.
139
- // We know that the current branch is *not* reachable from the workspace or isn't naturally included,
140
- // so it needs to be added as well.
141
- let next_ws_ref_name = match workspace_reference_naming {
142
- WorkspaceReferenceNaming :: Default => {
143
- gix:: refs:: FullName :: try_from ( "refs/heads/gitbutler/workspace" )
144
- . expect ( "known statically" )
145
- }
146
- WorkspaceReferenceNaming :: Given ( name) => name,
147
- } ;
148
- let ws_ref_id = match repo. try_find_reference ( next_ws_ref_name. as_ref ( ) ) ? {
149
- None => {
150
- // Create a workspace reference later at the current AdHoc workspace id
151
- let ws_id = workspace
152
- . stacks
153
- . first ( )
154
- . and_then ( |s| s. segments . first ( ) )
155
- . and_then ( |s| s. commits . first ( ) . map ( |c| c. id ) )
156
- . context ( "BUG: how can an empty ad-hoc workspace exist? Should have at least one stack-segment with commit" ) ?;
157
- ws_id
158
- }
159
- Some ( mut existing_workspace_reference) => {
160
- let id = existing_workspace_reference. peel_to_id_in_place ( ) ?;
161
- id. detach ( )
162
- }
163
- } ;
164
-
165
- // Get as close as possible to what would happen if the branch was already part of it (without merging),
166
- // and branch-metadata can disambiguate. Having this data also isn't harmful.
167
- // We want to see if the branch is naturally included in workspace already.
168
- let branch_md = meta. branch ( branch) ?;
169
- let ( branch_md_override, branch_md_to_set) = if branch_md. is_default ( ) {
170
- (
171
- Some ( ( branch. to_owned ( ) , ( * branch_md) . clone ( ) ) ) ,
172
- Some ( branch_md) ,
173
- )
174
- } else {
175
- ( None , None )
176
- } ;
177
- let mut ws_md = meta. workspace ( next_ws_ref_name. as_ref ( ) ) ?;
178
- {
179
- let ws_mut: & mut ref_metadata:: Workspace = & mut * ws_md;
180
- ws_mut. stacks . push ( WorkspaceStack {
181
- id : StackId :: generate ( ) ,
182
- branches : vec ! [ WorkspaceStackBranch {
183
- ref_name: branch. to_owned( ) ,
184
- archived: false ,
185
- } ] ,
186
- } )
187
- }
188
- let ws_md_override = Some ( ( next_ws_ref_name. clone ( ) , ( * ws_md) . clone ( ) ) ) ;
189
-
190
- let graph = workspace. graph . redo_traversal_with_overlay (
191
- repo,
192
- meta,
193
- Overlay :: default ( )
194
- . with_entrypoint ( ws_ref_id, Some ( next_ws_ref_name) )
195
- . with_branch_metadata_override ( branch_md_override)
196
- . with_workspace_metadata_override ( ws_md_override) ,
197
- ) ?;
198
-
199
- let ws = graph. to_workspace ( ) ?;
200
- if ws. refname_is_segment ( branch) {
201
- todo ! (
202
- "no need to add this branch, but we need to add the current ad-hoc branch instead"
203
- ) ;
136
+ // In general, we only have to deal with one branch to apply. But when we are on an adhoc workspace,
137
+ // we need to assure both branches go into the existing or the new workspace.
138
+ let ( workspace_ref_name_to_update, branches_to_apply) = match & workspace. kind {
139
+ WorkspaceKind :: Managed { ref_name }
140
+ | WorkspaceKind :: ManagedMissingWorkspaceCommit { ref_name } => {
141
+ ( ref_name. clone ( ) , vec ! [ branch] )
142
+ }
143
+ WorkspaceKind :: AdHoc => {
144
+ // We need to switch over to a possibly existing workspace.
145
+ // We know that the current branch is *not* reachable from the workspace or isn't naturally included,
146
+ // so it needs to be added as well.
147
+ let next_ws_ref_name = match workspace_reference_naming {
148
+ WorkspaceReferenceNaming :: Default => {
149
+ gix:: refs:: FullName :: try_from ( "refs/heads/gitbutler/workspace" )
150
+ . expect ( "known statically" )
204
151
}
205
-
206
- todo ! ( "put current ad-hoc stack into " )
207
- }
208
- } ;
152
+ WorkspaceReferenceNaming :: Given ( name) => name,
153
+ } ;
154
+ (
155
+ next_ws_ref_name,
156
+ workspace
157
+ . ref_name ( )
158
+ . into_iter ( )
159
+ . chain ( Some ( branch) )
160
+ . collect ( ) ,
161
+ )
162
+ // let ws_ref_id = match repo.try_find_reference(next_ws_ref_name.as_ref())? {
163
+ // None => {
164
+ // // Create a workspace reference later at the current AdHoc workspace id
165
+ // let ws_id = workspace
166
+ // .stacks
167
+ // .first()
168
+ // .and_then(|s| s.segments.first())
169
+ // .and_then(|s| s.commits.first().map(|c| c.id))
170
+ // .context("BUG: how can an empty ad-hoc workspace exist? Should have at least one stack-segment with commit")?;
171
+ // ws_id
172
+ // }
173
+ // Some(mut existing_workspace_reference) => {
174
+ // let id = existing_workspace_reference.peel_to_id_in_place()?;
175
+ // id.detach()
176
+ // }
177
+ // };
178
+
179
+ // let mut ws_md = meta.workspace(next_ws_ref_name.as_ref())?;
180
+ // {
181
+ // let ws_mut: &mut ref_metadata::Workspace = &mut *ws_md;
182
+ // ws_mut.stacks.push(WorkspaceStack {
183
+ // id: StackId::generate(),
184
+ // branches: vec![WorkspaceStackBranch {
185
+ // ref_name: branch.to_owned(),
186
+ // archived: false,
187
+ // }],
188
+ // })
189
+ // }
190
+ // let ws_md_override = Some((next_ws_ref_name.clone(), (*ws_md).clone()));
191
+
192
+ // let graph = workspace.graph.redo_traversal_with_overlay(
193
+ // repo,
194
+ // meta,
195
+ // Overlay::default()
196
+ // .with_entrypoint(ws_ref_id, Some(next_ws_ref_name))
197
+ // .with_branch_metadata_override(branch_md_override)
198
+ // .with_workspace_metadata_override(ws_md_override),
199
+ // )?;
200
+ }
201
+ } ;
209
202
210
203
// Everything worked? Assure the ref exists now that (nearly nothing) can go wrong anymore.
211
- let workspace_ref_created = false ; // TODO: use rval of reference update to know if it existed.
212
-
213
- if let Some ( branch_md) = branch_to_apply_metadata {
214
- meta. set_branch ( branch_md) ?;
215
- }
216
-
217
- Ok ( Outcome {
218
- graph,
219
- workspace_ref_created,
220
- } )
204
+ let _workspace_ref_created = false ; // TODO: use rval of reference update to know if it existed.
205
+
206
+ // if let Some(branch_md) = branch_to_apply_metadata {
207
+ // meta.set_branch(branch_md)?;
208
+ // }
209
+
210
+ todo ! (
211
+ "prepare outcome once all values were written out and the graph was regenerated - the simulation is now reality"
212
+ ) ;
213
+ // Ok(Outcome {
214
+ // graph: Cow::Borrowed(workspace.graph),
215
+ // workspace_ref_created,
216
+ // })
221
217
}
222
218
}
0 commit comments