Skip to content

Commit 1674407

Browse files
committed
Clean stale git temp files
1 parent 92d8826 commit 1674407

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

Diff for: src/cargo/sources/git/utils.rs

+40
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use serde::Serialize;
1414
use std::borrow::Cow;
1515
use std::env;
1616
use std::fmt;
17+
use std::fs;
1718
use std::path::{Path, PathBuf};
1819
use std::process::Command;
1920
use std::str;
@@ -812,6 +813,8 @@ pub fn fetch(
812813
// request we're about to issue.
813814
maybe_gc_repo(repo)?;
814815

816+
clean_repo_temp_files(repo);
817+
815818
// Translate the reference desired here into an actual list of refspecs
816819
// which need to get fetched. Additionally record if we're fetching tags.
817820
let mut refspecs = Vec::new();
@@ -1001,6 +1004,43 @@ fn maybe_gc_repo(repo: &mut git2::Repository) -> CargoResult<()> {
10011004
reinitialize(repo)
10021005
}
10031006

1007+
/// Removes temporary files left from previous activity.
1008+
///
1009+
/// If libgit2 is interrupted while indexing pack files, it will leave behind
1010+
/// some temporary files that it doesn't clean up. These can be quite large in
1011+
/// size, so this tries to clean things up.
1012+
///
1013+
/// This intentionally ignores errors. This is only an opportunistic cleaning,
1014+
/// and we don't really care if there are issues (there's unlikely anything
1015+
/// that can be done).
1016+
///
1017+
/// The git CLI has similar behavior (its temp files look like
1018+
/// `objects/pack/tmp_pack_9kUSA8`). Those files are normally deleted via `git
1019+
/// prune` which is run by `git gc`. However, it doesn't know about libgit2's
1020+
/// filenames, so they never get cleaned up.
1021+
fn clean_repo_temp_files(repo: &git2::Repository) {
1022+
let path = repo.path().join("objects/pack/pack_git2_*");
1023+
let pattern = match path.to_str() {
1024+
Some(p) => p,
1025+
None => {
1026+
log::warn!("cannot convert {path:?} to a string");
1027+
return;
1028+
}
1029+
};
1030+
let paths = match glob::glob(pattern) {
1031+
Ok(paths) => paths,
1032+
Err(_) => return,
1033+
};
1034+
for path in paths {
1035+
if let Ok(path) = path {
1036+
match fs::remove_file(&path) {
1037+
Ok(_) => log::debug!("removed stale temp git file {path:?}"),
1038+
Err(e) => log::warn!("failed to remove {path:?} while cleaning temp files: {e}"),
1039+
}
1040+
}
1041+
}
1042+
}
1043+
10041044
fn reinitialize(repo: &mut git2::Repository) -> CargoResult<()> {
10051045
// Here we want to drop the current repository object pointed to by `repo`,
10061046
// so we initialize temporary repository in a sub-folder, blow away the

Diff for: tests/testsuite/git.rs

+29
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::sync::Arc;
1010
use std::thread;
1111

1212
use cargo_test_support::paths::{self, CargoPathExt};
13+
use cargo_test_support::registry::Package;
1314
use cargo_test_support::{basic_lib_manifest, basic_manifest, git, main_file, path2url, project};
1415
use cargo_test_support::{sleep_ms, t, Project};
1516

@@ -3619,3 +3620,31 @@ fn _corrupted_checkout(with_cli: bool) {
36193620
e.run();
36203621
assert!(ok.exists());
36213622
}
3623+
3624+
#[cargo_test]
3625+
fn cleans_temp_pack_files() {
3626+
// Checks that cargo removes temp files left by libgit2 when it is
3627+
// interrupted (see clean_repo_temp_files).
3628+
Package::new("bar", "1.0.0").publish();
3629+
let p = project()
3630+
.file(
3631+
"Cargo.toml",
3632+
r#"
3633+
[package]
3634+
name = "foo"
3635+
version = "0.1.0"
3636+
3637+
[dependencies]
3638+
bar = "1.0"
3639+
"#,
3640+
)
3641+
.file("src/lib.rs", "")
3642+
.build();
3643+
p.cargo("fetch").run();
3644+
// Simulate what happens when libgit2 is interrupted while indexing a pack file.
3645+
let tmp_path = super::git_gc::find_index().join(".git/objects/pack/pack_git2_91ab40da04fdc2e7");
3646+
fs::write(&tmp_path, "test").unwrap();
3647+
// Trigger an index update.
3648+
p.cargo("generate-lockfile").run();
3649+
assert!(!tmp_path.exists());
3650+
}

Diff for: tests/testsuite/git_gc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use cargo_test_support::registry::Package;
1111

1212
use url::Url;
1313

14-
fn find_index() -> PathBuf {
14+
pub fn find_index() -> PathBuf {
1515
let dir = paths::home().join(".cargo/registry/index");
1616
dir.read_dir().unwrap().next().unwrap().unwrap().path()
1717
}

0 commit comments

Comments
 (0)