Skip to content

Commit

Permalink
feat: gix clone <url> is now permitted without specifying a destina…
Browse files Browse the repository at this point in the history
…tion directory. (#683)

Note that the implementation doesn't take into account potential redirects and renames
as it's implemented only with the first URL it sees (not the redirected ones).
  • Loading branch information
Byron committed Jan 3, 2023
1 parent 41fc2bb commit b81f650
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
16 changes: 12 additions & 4 deletions gitoxide-core/src/repository/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ pub const PROGRESS_RANGE: std::ops::RangeInclusive<u8> = 1..=3;
pub(crate) mod function {
use std::ffi::OsStr;

use anyhow::bail;
use anyhow::{bail, Context};
use git_repository as git;
use git_repository::{bstr::BString, remote::fetch::Status, Progress};

use super::Options;
use crate::{repository::fetch::function::print_updates, OutputFormat};

pub fn clone<P>(
remote: impl AsRef<OsStr>,
directory: impl AsRef<std::path::Path>,
url: impl AsRef<OsStr>,
directory: Option<impl Into<std::path::PathBuf>>,
overrides: Vec<BString>,
mut progress: P,
mut out: impl std::io::Write,
Expand All @@ -41,8 +41,16 @@ pub(crate) mod function {
bail!("JSON output isn't yet supported for fetching.");
}

let url: git::Url = url.as_ref().try_into()?;
let directory = directory.map(|dir| Ok(dir.into())).unwrap_or_else(|| {
git::path::from_bstr(url.path.as_ref())
.as_ref()
.file_stem()
.map(Into::into)
.context("Filename extraction failed - path too short")
})?;
let mut prepare = git::clone::PrepareFetch::new(
remote.as_ref(),
url,
directory,
bare.then(|| git::create::Kind::Bare)
.unwrap_or(git::create::Kind::WithWorktree),
Expand Down
2 changes: 1 addition & 1 deletion src/plumbing/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ pub mod clone {
pub remote: OsString,

/// The directory to initialize with the new repository and to which all data should be written.
pub directory: PathBuf,
pub directory: Option<PathBuf>,
}
}

Expand Down

0 comments on commit b81f650

Please sign in to comment.