Skip to content

Commit 441e3ce

Browse files
committed
Remove .git directory when downloading a crate from git(hub)
1 parent e2ec11a commit 441e3ce

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

collector/src/main.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,10 +1252,30 @@ fn download_from_git(target: &Path, url: &str) -> anyhow::Result<()> {
12521252
.status()
12531253
.expect("Git clone failed");
12541254
generate_lockfile(tmpdir.path());
1255+
1256+
// Save downloaded commit SHA to keep information about downloaded revision
1257+
let git_sha = get_git_sha(tmpdir.path())?;
1258+
std::fs::write(tmpdir.path().join("git-commit.txt"), git_sha)?;
1259+
1260+
// Remove .git directory to avoid messing with nested git repositories
1261+
if let Err(error) = std::fs::remove_dir_all(tmpdir.path().join(".git")) {
1262+
log::error!("Could not delete .git directory: {error:?}");
1263+
}
1264+
12551265
execute::rename(&tmpdir, &target)?;
12561266
Ok(())
12571267
}
12581268

1269+
fn get_git_sha(directory: &Path) -> anyhow::Result<String> {
1270+
let stdout = Command::new("git")
1271+
.arg("rev-parse")
1272+
.arg("HEAD")
1273+
.current_dir(directory)
1274+
.output()?
1275+
.stdout;
1276+
Ok(String::from_utf8(stdout)?)
1277+
}
1278+
12591279
fn download_from_crates_io(target_dir: &Path, krate: &str, version: &str) -> anyhow::Result<()> {
12601280
let url = format!("https://crates.io/api/v1/crates/{krate}/{version}/download");
12611281
let response = reqwest::blocking::get(url)

0 commit comments

Comments
 (0)