1use crate::{
2 example::{Example, ExampleBuffer, ExampleState},
3 git,
4 headless::EpAppState,
5 progress::{InfoStyle, Progress, Step, StepProgress},
6};
7use anyhow::{Context as _, Result};
8use edit_prediction::EditPredictionStore;
9use edit_prediction::udiff::{OpenedBuffers, refresh_worktree_entries};
10use futures::AsyncWriteExt as _;
11use gpui::{AsyncApp, Entity};
12use language::{Anchor, Buffer, LanguageNotFound, ToOffset, ToPoint};
13use project::Project;
14use project::buffer_store::BufferStoreEvent;
15use std::{fs, path::PathBuf, sync::Arc};
16
17pub async fn run_load_project(
18 example: &mut Example,
19 app_state: Arc<EpAppState>,
20 mut cx: AsyncApp,
21) -> Result<()> {
22 if example.state.is_some() {
23 return Ok(());
24 }
25
26 let progress = Progress::global().start(Step::LoadProject, &example.spec.name);
27
28 let project = setup_project(example, &app_state, &progress, &mut cx).await?;
29
30 progress.set_substatus("applying edit history");
31 let open_buffers = apply_edit_history(example, &project, &mut cx).await?;
32
33 progress.set_substatus("resolving cursor");
34 let (buffer, cursor_position) =
35 cursor_position(example, &project, &open_buffers, &mut cx).await?;
36 let (example_buffer, language_name) = buffer.read_with(&cx, |buffer, _cx| {
37 let cursor_point = cursor_position.to_point(&buffer);
38 let language_name = buffer
39 .language()
40 .map(|l| l.name().to_string())
41 .unwrap_or_else(|| "Unknown".to_string());
42 (
43 ExampleBuffer {
44 content: buffer.text(),
45 cursor_row: cursor_point.row,
46 cursor_column: cursor_point.column,
47 cursor_offset: cursor_position.to_offset(&buffer),
48 },
49 language_name,
50 )
51 })?;
52
53 progress.set_info(language_name, InfoStyle::Normal);
54
55 example.buffer = Some(example_buffer);
56 example.state = Some(ExampleState {
57 buffer,
58 project,
59 cursor_position,
60 _open_buffers: open_buffers,
61 });
62 Ok(())
63}
64
65async fn cursor_position(
66 example: &Example,
67 project: &Entity<Project>,
68 open_buffers: &OpenedBuffers,
69 cx: &mut AsyncApp,
70) -> Result<(Entity<Buffer>, Anchor)> {
71 let language_registry = project.read_with(cx, |project, _| project.languages().clone())?;
72 let result = language_registry
73 .load_language_for_file_path(&example.spec.cursor_path)
74 .await;
75
76 if let Err(error) = result
77 && !error.is::<LanguageNotFound>()
78 {
79 return Err(error);
80 }
81
82 let cursor_path_str = example.spec.cursor_path.to_string_lossy();
83 // We try open_buffers first because the file might be new and not saved to disk
84 let cursor_buffer = if let Some(buffer) = open_buffers.get(&cursor_path_str) {
85 buffer.clone()
86 } else {
87 // Since the worktree scanner is disabled, manually refresh entries for the cursor path.
88 if let Some(worktree) = project.read_with(cx, |project, cx| project.worktrees(cx).next())? {
89 refresh_worktree_entries(&worktree, [&*example.spec.cursor_path], cx).await?;
90 }
91
92 let cursor_path = project
93 .read_with(cx, |project, cx| {
94 project.find_project_path(&example.spec.cursor_path, cx)
95 })?
96 .with_context(|| {
97 format!(
98 "failed to find cursor path {}",
99 example.spec.cursor_path.display()
100 )
101 })?;
102
103 project
104 .update(cx, |project, cx| project.open_buffer(cursor_path, cx))?
105 .await?
106 };
107
108 let (cursor_excerpt, cursor_offset_within_excerpt) = example.spec.cursor_excerpt()?;
109
110 let excerpt_offset = cursor_buffer.read_with(cx, |buffer, _cx| {
111 let text = buffer.text();
112
113 let mut matches = text.match_indices(&cursor_excerpt);
114 let (excerpt_offset, _) = matches.next().with_context(|| {
115 format!(
116 "\nExcerpt:\n\n{cursor_excerpt}\nBuffer text:\n{text}\n.Example: {}\nCursor excerpt did not exist in buffer.",
117 example.spec.name
118 )
119 })?;
120 anyhow::ensure!(
121 matches.next().is_none(),
122 "More than one cursor position match found for {}",
123 &example.spec.name
124 );
125 Ok(excerpt_offset)
126 })??;
127
128 let cursor_offset = excerpt_offset + cursor_offset_within_excerpt;
129 let cursor_anchor =
130 cursor_buffer.read_with(cx, |buffer, _| buffer.anchor_after(cursor_offset))?;
131
132 Ok((cursor_buffer, cursor_anchor))
133}
134
135async fn setup_project(
136 example: &mut Example,
137 app_state: &Arc<EpAppState>,
138 step_progress: &StepProgress,
139 cx: &mut AsyncApp,
140) -> Result<Entity<Project>> {
141 let ep_store = cx
142 .update(|cx| EditPredictionStore::try_global(cx))?
143 .context("Store should be initialized at init")?;
144
145 let worktree_path = setup_worktree(example, step_progress).await?;
146
147 if let Some(project) = app_state.project_cache.get(&example.spec.repository_url) {
148 ep_store.update(cx, |ep_store, _| {
149 ep_store.clear_history_for_project(&project);
150 })?;
151 let buffer_store = project.read_with(cx, |project, _| project.buffer_store().clone())?;
152 let buffers = buffer_store.read_with(cx, |buffer_store, _| {
153 buffer_store.buffers().collect::<Vec<_>>()
154 })?;
155 for buffer in buffers {
156 buffer
157 .update(cx, |buffer, cx| buffer.reload(cx))?
158 .await
159 .ok();
160 }
161 return Ok(project);
162 }
163
164 let project = cx.update(|cx| {
165 Project::local(
166 app_state.client.clone(),
167 app_state.node_runtime.clone(),
168 app_state.user_store.clone(),
169 app_state.languages.clone(),
170 app_state.fs.clone(),
171 None,
172 false,
173 cx,
174 )
175 })?;
176
177 project
178 .update(cx, |project, cx| {
179 project.disable_worktree_scanner(cx);
180 project.create_worktree(&worktree_path, true, cx)
181 })?
182 .await?;
183
184 app_state
185 .project_cache
186 .insert(example.spec.repository_url.clone(), project.clone());
187
188 let buffer_store = project.read_with(cx, |project, _| project.buffer_store().clone())?;
189 cx.subscribe(&buffer_store, {
190 let project = project.clone();
191 move |_, event, cx| match event {
192 BufferStoreEvent::BufferAdded(buffer) => {
193 ep_store.update(cx, |store, cx| store.register_buffer(&buffer, &project, cx));
194 }
195 _ => {}
196 }
197 })?
198 .detach();
199
200 Ok(project)
201}
202
203async fn setup_worktree(example: &Example, step_progress: &StepProgress) -> Result<PathBuf> {
204 let repo_name = example.repo_name().context("failed to get repo name")?;
205 let repo_dir = git::repo_path_for_url(&example.spec.repository_url)?;
206 let worktree_path = repo_name.worktree_path();
207 let repo_lock = git::lock_repo(&repo_dir).await;
208
209 // Clean up any stale git lock files from previous crashed runs.
210 // Safe-ish since we have our own lock.
211 // WARNING: Can corrupt worktrees if multiple processes of the CLI are running.
212 let worktree_git_dir = repo_dir
213 .join(".git/worktrees")
214 .join(repo_name.name.as_ref());
215 let index_lock = worktree_git_dir.join("index.lock");
216 if index_lock.exists() {
217 fs::remove_file(&index_lock).ok();
218 }
219
220 if !repo_dir.is_dir() {
221 step_progress.set_substatus(format!("cloning {}", repo_name.name));
222 fs::create_dir_all(&repo_dir)?;
223 git::run_git(&repo_dir, &["init"]).await?;
224 git::run_git(
225 &repo_dir,
226 &["remote", "add", "origin", &example.spec.repository_url],
227 )
228 .await?;
229 }
230
231 // Resolve the example to a revision, fetching it if needed.
232 step_progress.set_substatus("fetching");
233 let revision = git::fetch_if_needed(&repo_dir, &example.spec.revision).await?;
234
235 // Create the worktree for this example if needed.
236 step_progress.set_substatus("preparing worktree");
237 if worktree_path.is_dir() {
238 git::run_git(&worktree_path, &["clean", "--force", "-d"]).await?;
239 git::run_git(&worktree_path, &["reset", "--hard", "HEAD"]).await?;
240 git::run_git(&worktree_path, &["checkout", revision.as_str()]).await?;
241 } else {
242 let worktree_path_string = worktree_path.to_string_lossy();
243 let branch_name = example.spec.filename();
244 git::run_git(
245 &repo_dir,
246 &["branch", "-f", &branch_name, revision.as_str()],
247 )
248 .await?;
249 git::run_git(
250 &repo_dir,
251 &["worktree", "add", "-f", &worktree_path_string, &branch_name],
252 )
253 .await?;
254 }
255 drop(repo_lock);
256
257 // Apply the uncommitted diff for this example.
258 if !example.spec.uncommitted_diff.is_empty() {
259 step_progress.set_substatus("applying diff");
260 let mut apply_process = smol::process::Command::new("git")
261 .current_dir(&worktree_path)
262 .args(&["apply", "-"])
263 .stdin(std::process::Stdio::piped())
264 .spawn()?;
265
266 let mut stdin = apply_process.stdin.take().context("Failed to get stdin")?;
267 stdin
268 .write_all(example.spec.uncommitted_diff.as_bytes())
269 .await?;
270 stdin.close().await?;
271 drop(stdin);
272
273 let apply_result = apply_process.output().await?;
274 anyhow::ensure!(
275 apply_result.status.success(),
276 "Failed to apply uncommitted diff patch with status: {}\nstderr:\n{}\nstdout:\n{}",
277 apply_result.status,
278 String::from_utf8_lossy(&apply_result.stderr),
279 String::from_utf8_lossy(&apply_result.stdout),
280 );
281 }
282
283 step_progress.clear_substatus();
284 Ok(worktree_path)
285}
286
287async fn apply_edit_history(
288 example: &Example,
289 project: &Entity<Project>,
290 cx: &mut AsyncApp,
291) -> Result<OpenedBuffers> {
292 edit_prediction::udiff::apply_diff(&example.spec.edit_history, project, cx).await
293}