add clone function
This commit is contained in:
@@ -57,15 +57,34 @@ impl GitCache {
|
||||
.with_context(|| format!("failed to create {}", parent.display()))?;
|
||||
}
|
||||
|
||||
clone_repo(clone_urls, &path)?;
|
||||
self.open(addr)?
|
||||
.ok_or_else(|| anyhow::anyhow!("clone finished but the repository cannot be opened"))
|
||||
}
|
||||
}
|
||||
|
||||
/// Clone a repository into `path` from the first working URL in
|
||||
/// `clone_urls` (the announcement's `clone` tag), then fetch the
|
||||
/// `refs/nostr/*` PR refs like the cache clone does. The destination must
|
||||
/// not exist yet; it is created by the clone. The first URL that works
|
||||
/// wins; when none do, the error of the last failing URL is returned.
|
||||
///
|
||||
/// Unlike [`GitCache::ensure_clone`], the clone is not kept in any cache;
|
||||
/// callers open it themselves if they need a [`gix::Repository`].
|
||||
pub fn clone_repo(clone_urls: &[String], path: &Path) -> Result<()> {
|
||||
if path.exists() {
|
||||
bail!("destination {} already exists", path.display());
|
||||
}
|
||||
|
||||
let mut last_err: Option<anyhow::Error> = None;
|
||||
|
||||
for url in clone_urls {
|
||||
match clone(url, &path) {
|
||||
match clone(url, path) {
|
||||
Ok(repo) => {
|
||||
// The initial clone uses the default refspecs; also
|
||||
// fetch the `refs/nostr/*` PR refs.
|
||||
fetch_all(&repo).ok();
|
||||
return Ok(repo);
|
||||
return Ok(());
|
||||
}
|
||||
Err(e) => last_err = Some(e),
|
||||
}
|
||||
@@ -76,7 +95,6 @@ impl GitCache {
|
||||
None => bail!("no clone URLs provided"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Fetch all configured refspecs from `origin`, plus the `refs/nostr/*`
|
||||
/// namespace where GRASP mirrors serve pull request branches (one ref per
|
||||
@@ -246,12 +264,13 @@ fn git_in(dir: &Path, args: &[&str]) -> Result<String> {
|
||||
Ok(String::from_utf8_lossy(&output.stdout).trim().to_owned())
|
||||
}
|
||||
|
||||
/// Map an untrusted repository id to a safe single path component.
|
||||
/// Map an untrusted repository id (or display name) to a safe single path
|
||||
/// component.
|
||||
///
|
||||
/// Replaces everything outside `[A-Za-z0-9._-]` with `_`, and rejects the
|
||||
/// special components `.` and `..` so the id can't escape the cache root
|
||||
/// when joined onto the owner directory.
|
||||
fn sanitize_path_component(id: &str) -> String {
|
||||
/// special components `.` and `..` so the id can't escape a directory it is
|
||||
/// joined onto.
|
||||
pub fn sanitize_path_component(id: &str) -> String {
|
||||
let sanitized: String = id
|
||||
.chars()
|
||||
.map(|c| {
|
||||
|
||||
@@ -8,8 +8,8 @@ use dock::{BasePanel, DockArea, DockPlacement, Panel, PanelEvent, panel_handle};
|
||||
use gix::Repository;
|
||||
use gpui::prelude::*;
|
||||
use gpui::{
|
||||
AnyElement, App, Context, Entity, EventEmitter, FocusHandle, Focusable, Pixels, Render,
|
||||
SharedString, Size, Subscription, Task, WeakEntity, Window, div, px, size,
|
||||
AnyElement, App, Context, Entity, EventEmitter, FocusHandle, Focusable, PathPromptOptions,
|
||||
Pixels, Render, SharedString, Size, Subscription, Task, WeakEntity, Window, div, px, size,
|
||||
};
|
||||
use gpui_component::avatar::{Avatar, AvatarGroup};
|
||||
use gpui_component::button::{Button, ButtonVariants};
|
||||
@@ -121,6 +121,8 @@ pub struct RepoDetailView {
|
||||
item_sizes: Rc<Vec<Size<Pixels>>>,
|
||||
/// A clone/fetch is in flight.
|
||||
loading: bool,
|
||||
/// The header clone button is cloning into a user-chosen folder.
|
||||
cloning: bool,
|
||||
error: Option<SharedString>,
|
||||
/// Commit HEAD currently points to, shown in the header button.
|
||||
head_commit: Option<FileCommit>,
|
||||
@@ -221,6 +223,7 @@ impl RepoDetailView {
|
||||
scroll_handle: VirtualListScrollHandle::new(),
|
||||
item_sizes: Rc::new(Vec::new()),
|
||||
loading: true,
|
||||
cloning: false,
|
||||
error: None,
|
||||
head_commit: None,
|
||||
branch_select,
|
||||
@@ -403,6 +406,85 @@ impl RepoDetailView {
|
||||
}
|
||||
}
|
||||
|
||||
/// Clone the repository into a folder chosen by the user (outside the
|
||||
/// cache), then open the new clone in the system file manager. Like
|
||||
/// ngit's clone, this resolves the announcement's `clone` URLs and
|
||||
/// clones from the first working git server.
|
||||
fn clone_to_folder(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
||||
if self.cloning {
|
||||
return;
|
||||
}
|
||||
|
||||
let (clone_urls, name) = {
|
||||
let announcement = self.announcement(cx);
|
||||
let addr = announcement.addr();
|
||||
let clone_urls: Vec<String> =
|
||||
announcement.clone.iter().map(ToString::to_string).collect();
|
||||
// Directory name: the display name, falling back to the repo id;
|
||||
// both sanitized to a safe single path component.
|
||||
let name = announcement
|
||||
.name
|
||||
.as_ref()
|
||||
.map(|name| name.to_string())
|
||||
.filter(|name| !name.trim().is_empty())
|
||||
.unwrap_or_else(|| addr.identifier.clone());
|
||||
let name = signed_git::sanitize_path_component(&name);
|
||||
let name = if name.is_empty() {
|
||||
"repository".to_owned()
|
||||
} else {
|
||||
name
|
||||
};
|
||||
(clone_urls, name)
|
||||
};
|
||||
|
||||
self.cloning = true;
|
||||
cx.notify();
|
||||
|
||||
let prompt = cx.prompt_for_paths(PathPromptOptions {
|
||||
files: false,
|
||||
directories: true,
|
||||
multiple: false,
|
||||
prompt: Some("Choose folder".into()),
|
||||
});
|
||||
|
||||
let task = cx.spawn_in(window, async move |this, cx| {
|
||||
// `Ok(Ok(Some(paths)))` means the user picked a folder; a
|
||||
// cancel (or a picker failure) resolves to anything else.
|
||||
let picked = match prompt.await {
|
||||
Ok(Ok(Some(mut paths))) => paths.pop(),
|
||||
_ => None,
|
||||
};
|
||||
let Some(folder) = picked else {
|
||||
this.update_in(cx, |this, _window, cx| {
|
||||
this.cloning = false;
|
||||
cx.notify();
|
||||
})?;
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
let destination = folder.join(&name);
|
||||
let destination_for_open = destination.clone();
|
||||
let result = cx
|
||||
.background_spawn(async move { signed_git::clone_repo(&clone_urls, &destination) })
|
||||
.await;
|
||||
|
||||
this.update_in(cx, |this, _window, cx| {
|
||||
this.cloning = false;
|
||||
match result {
|
||||
Ok(_) => cx.open_with_system(&destination_for_open),
|
||||
Err(error) => {
|
||||
this.error = Some(format!("Failed to clone: {error}").into());
|
||||
}
|
||||
}
|
||||
cx.notify();
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
});
|
||||
|
||||
self.tasks.push(task);
|
||||
}
|
||||
|
||||
/// Preview the file at `path` (relative to the worktree root).
|
||||
fn open_file(&mut self, path: &str, window: &mut Window, cx: &mut Context<Self>) {
|
||||
self.selected_file = Some(path.into());
|
||||
@@ -1042,8 +1124,12 @@ impl RepoDetailView {
|
||||
.child(
|
||||
Button::new("clone")
|
||||
.icon(CustomIconName::GitClone)
|
||||
.tooltip("Clone")
|
||||
.primary(),
|
||||
.tooltip("Clone to folder…")
|
||||
.loading(self.cloning)
|
||||
.primary()
|
||||
.on_click(cx.listener(|this, _event, window, cx| {
|
||||
this.clone_to_folder(window, cx);
|
||||
})),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user