Skip to content

Commit f03dcd6

Browse files
committed
Auto merge of #3755 - jtgeibel:fix-3630, r=pietroalbini
Fetch only the master branch when cloning the index I've tested this locally with by running `GIT_REPO_URL=https://github.com/rust-lang/crates.io-index cargo run --bin background-worker` and then verifying that the repo in `/tmp/git*/` contained only the master branch and its history. Fixes: #3630 r? `@pietroalbini`
2 parents 3a01128 + 806085f commit f03dcd6

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/git.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,14 @@ impl Repository {
153153

154154
let repository = git2::build::RepoBuilder::new()
155155
.fetch_options(Self::fetch_options(&repository_config.credentials))
156+
.remote_create(|repo, name, url| {
157+
// Manually create the remote with a fetchspec, to avoid cloning old snaphots
158+
repo.remote_with_fetch(
159+
name,
160+
url,
161+
&format!("+refs/heads/master:refs/remotes/{}/master", name),
162+
)
163+
})
156164
.clone(
157165
repository_config.index_location.as_str(),
158166
checkout_path.path(),
@@ -253,7 +261,11 @@ impl Repository {
253261
let mut origin = self.repository.find_remote("origin")?;
254262
let original_head = self.head_oid()?;
255263
origin.fetch(
256-
&["refs/heads/*:refs/heads/*"],
264+
// Force overwrite (`+` prefix) local master branch with the server's master branch.
265+
// The git CLI will refuse to fetch into the current branch of a non-bare repo
266+
// but libgit2 doesn't seem to prevent this potential footgun.
267+
// The entire point is to do a hard reset, so this footgun is not a concern.
268+
&["+refs/heads/master:refs/heads/master"],
257269
Some(&mut Self::fetch_options(&self.credentials)),
258270
None,
259271
)?;

0 commit comments

Comments
 (0)