load_project.rs

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