Skip to content

Commit 5bb9ba3

Browse files
committed
Perform git gc after fetch rather than before
1 parent e2abbe5 commit 5bb9ba3

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

src/cargo/sources/git/utils.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,23 @@ pub fn fetch(
701701
url: &str,
702702
refspec: &str,
703703
config: &Config,
704+
) -> CargoResult<()> {
705+
fetch_impl(repo, url, refspec, config)?;
706+
// We reuse repositories quite a lot, so now that we have updated the repo,
707+
// check to see if it's a little too old and could benefit from a gc.
708+
// `git gc` goes through lengths to avoid removing precious data. In
709+
// principle, we could be running it in the background while we're fetching,
710+
// but better safe(r) than sorry, we just spawn it once we're done. It will
711+
// be spawned in the background (so effectively, cargo will likely have
712+
// terminated before `git gc` finishes), but only if necessary.
713+
maybe_gc_repo(repo)
714+
}
715+
716+
fn fetch_impl(
717+
repo: &mut git2::Repository,
718+
url: &str,
719+
refspec: &str,
720+
config: &Config,
704721
) -> CargoResult<()> {
705722
if config.frozen() {
706723
anyhow::bail!(
@@ -729,12 +746,6 @@ pub fn fetch(
729746
}
730747
}
731748

732-
// We reuse repositories quite a lot, so before we go through and update the
733-
// repo check to see if it's a little too old and could benefit from a gc.
734-
// In theory this shouldn't be too too expensive compared to the network
735-
// request we're about to issue.
736-
maybe_gc_repo(repo)?;
737-
738749
// Unfortunately `libgit2` is notably lacking in the realm of authentication
739750
// when compared to the `git` command line. As a result, allow an escape
740751
// hatch for users that would prefer to use `git`-the-CLI for fetching
@@ -860,9 +871,7 @@ fn maybe_gc_repo(repo: &mut git2::Repository) -> CargoResult<()> {
860871
}
861872
Err(e) => debug!("git-gc failed to spawn: {}", e),
862873
}
863-
864-
// Alright all else failed, let's start over.
865-
reinitialize(repo)
874+
Ok(())
866875
}
867876

868877
fn reinitialize(repo: &mut git2::Repository) -> CargoResult<()> {

0 commit comments

Comments
 (0)