load_project.rs

  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;
 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    let _open_buffers = apply_edit_history(example, &project, &mut cx).await?;
 31
 32    let (buffer, cursor_position) = cursor_position(example, &project, &mut cx).await?;
 33    let (example_buffer, language_name) = buffer.read_with(&cx, |buffer, _cx| {
 34        let cursor_point = cursor_position.to_point(&buffer);
 35        let language_name = buffer
 36            .language()
 37            .map(|l| l.name().to_string())
 38            .unwrap_or_else(|| "Unknown".to_string());
 39        (
 40            ExampleBuffer {
 41                content: buffer.text(),
 42                cursor_row: cursor_point.row,
 43                cursor_column: cursor_point.column,
 44                cursor_offset: cursor_position.to_offset(&buffer),
 45            },
 46            language_name,
 47        )
 48    })?;
 49
 50    progress.set_info(language_name, InfoStyle::Normal);
 51
 52    example.buffer = Some(example_buffer);
 53    example.state = Some(ExampleState {
 54        buffer,
 55        project,
 56        cursor_position,
 57        _open_buffers,
 58    });
 59    Ok(())
 60}
 61
 62async fn cursor_position(
 63    example: &Example,
 64    project: &Entity<Project>,
 65    cx: &mut AsyncApp,
 66) -> Result<(Entity<Buffer>, Anchor)> {
 67    let language_registry = project.read_with(cx, |project, _| project.languages().clone())?;
 68    let result = language_registry
 69        .load_language_for_file_path(&example.spec.cursor_path)
 70        .await;
 71
 72    if let Err(error) = result
 73        && !error.is::<LanguageNotFound>()
 74    {
 75        return Err(error);
 76    }
 77
 78    let cursor_path = project
 79        .read_with(cx, |project, cx| {
 80            project.find_project_path(&example.spec.cursor_path, cx)
 81        })?
 82        .with_context(|| {
 83            format!(
 84                "failed to find cursor path {}",
 85                example.spec.cursor_path.display()
 86            )
 87        })?;
 88
 89    let cursor_buffer = project
 90        .update(cx, |project, cx| project.open_buffer(cursor_path, cx))?
 91        .await?;
 92
 93    let (cursor_excerpt, cursor_offset_within_excerpt) = example.spec.cursor_excerpt()?;
 94
 95    let excerpt_offset = cursor_buffer.read_with(cx, |buffer, _cx| {
 96        let text = buffer.text();
 97
 98        let mut matches = text.match_indices(&cursor_excerpt);
 99        let (excerpt_offset, _) = matches.next().with_context(|| {
100            format!(
101                "\nExcerpt:\n\n{cursor_excerpt}\nBuffer text:\n{text}\n.Example: {}\nCursor excerpt did not exist in buffer.",
102                example.spec.name
103            )
104        })?;
105        anyhow::ensure!(
106            matches.next().is_none(),
107            "More than one cursor position match found for {}",
108            &example.spec.name
109        );
110        Ok(excerpt_offset)
111    })??;
112
113    let cursor_offset = excerpt_offset + cursor_offset_within_excerpt;
114    let cursor_anchor =
115        cursor_buffer.read_with(cx, |buffer, _| buffer.anchor_after(cursor_offset))?;
116
117    Ok((cursor_buffer, cursor_anchor))
118}
119
120async fn setup_project(
121    example: &mut Example,
122    app_state: &Arc<EpAppState>,
123    step_progress: &StepProgress,
124    cx: &mut AsyncApp,
125) -> Result<Entity<Project>> {
126    let ep_store = cx
127        .update(|cx| EditPredictionStore::try_global(cx))?
128        .context("Store should be initialized at init")?;
129
130    let worktree_path = setup_worktree(example, step_progress).await?;
131
132    if let Some(project) = app_state.project_cache.get(&example.spec.repository_url) {
133        ep_store.update(cx, |ep_store, _| {
134            ep_store.clear_history_for_project(&project);
135        })?;
136        let buffer_store = project.read_with(cx, |project, _| project.buffer_store().clone())?;
137        let buffers = buffer_store.read_with(cx, |buffer_store, _| {
138            buffer_store.buffers().collect::<Vec<_>>()
139        })?;
140        for buffer in buffers {
141            buffer
142                .update(cx, |buffer, cx| buffer.reload(cx))?
143                .await
144                .ok();
145        }
146        return Ok(project);
147    }
148
149    let project = cx.update(|cx| {
150        Project::local(
151            app_state.client.clone(),
152            app_state.node_runtime.clone(),
153            app_state.user_store.clone(),
154            app_state.languages.clone(),
155            app_state.fs.clone(),
156            None,
157            false,
158            cx,
159        )
160    })?;
161
162    project
163        .update(cx, |project, cx| {
164            project.disable_worktree_scanner(cx);
165            project.create_worktree(&worktree_path, true, cx)
166        })?
167        .await?;
168
169    app_state
170        .project_cache
171        .insert(example.spec.repository_url.clone(), project.clone());
172
173    let buffer_store = project.read_with(cx, |project, _| project.buffer_store().clone())?;
174    cx.subscribe(&buffer_store, {
175        let project = project.clone();
176        move |_, event, cx| match event {
177            BufferStoreEvent::BufferAdded(buffer) => {
178                ep_store.update(cx, |store, cx| store.register_buffer(&buffer, &project, cx));
179            }
180            _ => {}
181        }
182    })?
183    .detach();
184
185    Ok(project)
186}
187
188async fn setup_worktree(example: &Example, step_progress: &StepProgress) -> Result<PathBuf> {
189    let repo_name = example.repo_name().context("failed to get repo name")?;
190    let repo_dir = git::repo_path_for_url(&example.spec.repository_url)?;
191    let worktree_path = repo_name.worktree_path();
192    let repo_lock = git::lock_repo(&repo_dir).await;
193
194    if !repo_dir.is_dir() {
195        step_progress.set_substatus(format!("cloning {}", repo_name.name));
196        fs::create_dir_all(&repo_dir)?;
197        git::run_git(&repo_dir, &["init"]).await?;
198        git::run_git(
199            &repo_dir,
200            &["remote", "add", "origin", &example.spec.repository_url],
201        )
202        .await?;
203    }
204
205    // Resolve the example to a revision, fetching it if needed.
206    step_progress.set_substatus("fetching");
207    let revision = git::fetch_if_needed(&repo_dir, &example.spec.revision).await?;
208
209    // Create the worktree for this example if needed.
210    step_progress.set_substatus("preparing worktree");
211    if worktree_path.is_dir() {
212        git::run_git(&worktree_path, &["clean", "--force", "-d"]).await?;
213        git::run_git(&worktree_path, &["reset", "--hard", "HEAD"]).await?;
214        git::run_git(&worktree_path, &["checkout", revision.as_str()]).await?;
215    } else {
216        let worktree_path_string = worktree_path.to_string_lossy();
217        let branch_name = example.spec.filename();
218        git::run_git(
219            &repo_dir,
220            &["branch", "-f", &branch_name, revision.as_str()],
221        )
222        .await?;
223        git::run_git(
224            &repo_dir,
225            &["worktree", "add", "-f", &worktree_path_string, &branch_name],
226        )
227        .await?;
228    }
229    drop(repo_lock);
230
231    // Apply the uncommitted diff for this example.
232    if !example.spec.uncommitted_diff.is_empty() {
233        step_progress.set_substatus("applying diff");
234        let mut apply_process = smol::process::Command::new("git")
235            .current_dir(&worktree_path)
236            .args(&["apply", "-"])
237            .stdin(std::process::Stdio::piped())
238            .spawn()?;
239
240        let mut stdin = apply_process.stdin.take().context("Failed to get stdin")?;
241        stdin
242            .write_all(example.spec.uncommitted_diff.as_bytes())
243            .await?;
244        stdin.close().await?;
245        drop(stdin);
246
247        let apply_result = apply_process.output().await?;
248        anyhow::ensure!(
249            apply_result.status.success(),
250            "Failed to apply uncommitted diff patch with status: {}\nstderr:\n{}\nstdout:\n{}",
251            apply_result.status,
252            String::from_utf8_lossy(&apply_result.stderr),
253            String::from_utf8_lossy(&apply_result.stdout),
254        );
255    }
256
257    step_progress.clear_substatus();
258    Ok(worktree_path)
259}
260
261async fn apply_edit_history(
262    example: &Example,
263    project: &Entity<Project>,
264    cx: &mut AsyncApp,
265) -> Result<OpenedBuffers> {
266    edit_prediction::udiff::apply_diff(&example.spec.edit_history, project, cx).await
267}