load_project.rs

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