1use anyhow::{Result, bail};
4use mj_core::config::{Config, TargetTemplate, is_bare_project_target};
5use mj_core::state::{ManagedWorktreeTarget, SessionRecord};
6
7pub use mj_core::config::DEFAULT_CONTAINER_IMAGE as DEFAULT_IMAGE;
12
13pub fn managed_worktree_target(template: &TargetTemplate) -> Result<ManagedWorktreeTarget> {
16 match template {
17 TargetTemplate::LocalBare => Ok(ManagedWorktreeTarget::Local),
18 TargetTemplate::SshBare { ssh, .. } => {
19 let destination = match &ssh.user {
20 Some(user) => format!("{user}@{}", ssh.host),
21 None => ssh.host.clone(),
22 };
23 let ssh_args = mj_core::targets::ssh_args_with_identity(
27 &ssh.extra_args,
28 ssh.identity_file.as_deref(),
29 );
30 Ok(ManagedWorktreeTarget::Ssh {
31 destination,
32 ssh_args,
33 })
34 }
35 _ => bail!("managed raw worktrees require a bare target"),
36 }
37}
38
39#[derive(Debug, Clone, Copy, PartialEq, Eq)]
41pub enum ResumePlan {
42 InPlace,
44 RawToWorkspace,
49 WorkspaceToRaw,
51}
52
53pub fn resume_compatibility(
60 session: &SessionRecord,
61 config: &Config,
62 target_id: &str,
63) -> Result<ResumePlan, String> {
64 let Some(target) = config.targets.get(target_id) else {
65 return Err(format!("target {target_id} is no longer configured"));
66 };
67 let Some(project_directory) = &session.project_directory else {
68 if matches!(target, TargetTemplate::LocalBare) {
69 return workspace_to_raw_compatibility(session, config);
70 }
71 return Ok(ResumePlan::InPlace);
72 };
73 let directory = project_directory.display();
74 let Some(worktree) = &session.managed_worktree else {
75 let Some(previous) = config.targets.get(&session.target_template_id) else {
76 return Err(
77 "the bare target this session last used is no longer configured".to_owned(),
78 );
79 };
80 if is_bare_project_target(target) {
81 if matches!(previous, TargetTemplate::LocalBare)
82 == matches!(target, TargetTemplate::LocalBare)
83 {
84 return Ok(ResumePlan::InPlace);
85 }
86 return Err(format!(
87 "this session opens {directory} directly on its host; resume it on the same kind of bare target"
88 ));
89 }
90 if matches!(previous, TargetTemplate::LocalBare) {
95 return Ok(ResumePlan::RawToWorkspace);
96 }
97 return Err(format!(
98 "this session opens {directory} on an SSH host; resume it on a bare target there"
99 ));
100 };
101 match managed_worktree_target(target) {
102 Ok(resume_target) if resume_target.same_location(&worktree.target) => {
103 Ok(ResumePlan::InPlace)
104 }
105 Ok(_) => Err(format!(
106 "this session's working tree lives on {}; resume it there",
107 managed_worktree_location(&worktree.target)
108 )),
109 Err(_) if worktree.target != ManagedWorktreeTarget::Local => Err(format!(
110 "this session works directly in {directory} on {}; resume it on a bare target there",
111 managed_worktree_location(&worktree.target)
112 )),
113 Err(_) if Some(&worktree.worktree_root) == session.project_directory.as_ref() => {
116 Ok(ResumePlan::RawToWorkspace)
117 }
118 Err(_) => Err(format!(
119 "this session opens {directory}, a subdirectory of its checkout; resume it on a bare target"
120 )),
121 }
122}
123
124const BUNDLE_ON_LOCAL_BARE: &str = "this session was created from a project bundle; a local bare target only hosts raw project sessions — resume it on a container, SSH, or EC2 target";
127
128fn workspace_to_raw_compatibility(
131 session: &SessionRecord,
132 config: &Config,
133) -> Result<ResumePlan, String> {
134 let Some(bundle) = config.bundles.get(&session.bundle_id) else {
135 return Err(BUNDLE_ON_LOCAL_BARE.to_owned());
136 };
137 let [repository] = bundle.repositories.as_slice() else {
138 return Err(format!(
139 "this session's project has {} repositories; a local bare target holds one checkout — resume it on a container, SSH, or EC2 target",
140 bundle.repositories.len()
141 ));
142 };
143 if repository.local.is_none() {
144 return Err(
145 "this session's project came from GitHub; resume it on a container, SSH, or EC2 target"
146 .to_owned(),
147 );
148 }
149 Ok(ResumePlan::WorkspaceToRaw)
150}
151
152fn managed_worktree_location(target: &ManagedWorktreeTarget) -> String {
155 match target {
156 ManagedWorktreeTarget::Local => "this machine".to_owned(),
157 ManagedWorktreeTarget::Ssh { destination, .. } => destination.clone(),
158 }
159}