Skip to content

Commit

Permalink
Various improvements to address the PR review (part 1).
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Apr 17, 2023
1 parent 29f0760 commit 5716fe5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/cargo/sources/git/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ impl<'cfg> GitSource<'cfg> {
}
}

/// Create an identifier from a URL, essentially turning `proto://host/path/repo` into `repo-<hash-of-url>`.
fn ident(id: &SourceId) -> String {
let ident = id
.canonical_url()
Expand All @@ -82,6 +83,10 @@ fn ident(id: &SourceId) -> String {
format!("{}-{}", ident, short_hash(id.canonical_url()))
}

/// Like `ident()`, but appends `-shallow` to it, turning `proto://host/path/repo` into `repo-<hash-of-url>-shallow`.
///
/// It's important to separate shallow from non-shallow clones for reasons of backwards compatibility - older
/// cargo's aren't necessarily handling shallow clones correctly.
fn ident_shallow(id: &SourceId, is_shallow: bool) -> String {
let mut ident = ident(id);
if is_shallow {
Expand Down
8 changes: 6 additions & 2 deletions src/cargo/sources/git/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl GitRemote {
cargo_config,
history,
)
.context(format!("failed to fetch into: {}", into.display()))?;
.with_context(|| format!("failed to fetch into: {}", into.display()))?;
match locked_rev {
Some(rev) => {
if db.contains(rev) {
Expand Down Expand Up @@ -142,7 +142,7 @@ impl GitRemote {
cargo_config,
history,
)
.context(format!("failed to clone into: {}", into.display()))?;
.with_context(|| format!("failed to clone into: {}", into.display()))?;
let rev = match locked_rev {
Some(rev) => rev,
None => reference.resolve(&repo)?,
Expand Down Expand Up @@ -302,6 +302,10 @@ impl<'a> GitCheckout<'a> {
.with_checkout(checkout)
.fetch_options(fopts)
.clone(url.as_str(), into)?;
// `git2` doesn't seem to handle shallow repos correctly when doing a local clone.
// Fortunately all that's needed is the copy of the one file that defines the
// shallow boundary, the commits which have their parents omitted as part of the
// shallow clone.
if database.repo.is_shallow() {
std::fs::copy(
database.repo.path().join("shallow"),
Expand Down
1 change: 0 additions & 1 deletion tests/testsuite/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2183,7 +2183,6 @@ fn gitoxide_clones_git_dependency_with_shallow_protocol_and_follow_up_fetch_main
.map(|path| -> anyhow::Result<usize> {
let path = path?;
let dep_checkout = gix::open_opts(&path, gix::open::Options::isolated())?;
dbg!(dep_checkout.git_dir());
assert_eq!(
dep_checkout.is_shallow(),
path.to_string_lossy().contains("-shallow"),
Expand Down

0 comments on commit 5716fe5

Please sign in to comment.