working_copy: clean up ".git" automatically

TreeState::write_tree leaves a ".git" file in the working copy. This is
undesirable but more problematic on Windows - The second time
TreeState::write_tree would panic because Repository::init_opts will fail
with a Permission Denied error.

This seems to be a libgit2 defect. But for now let's just remove ".git"
automatically. This makes `cargo test --test smoke_test` pass on Windows.
This commit is contained in:
Jun Wu 2021-03-10 22:28:39 -08:00
parent 4cd29a2130
commit eacab648b0

View File

@ -292,6 +292,19 @@ impl TreeState {
let git_repo_dir = tempfile::tempdir().unwrap();
let mut git_repo_options = RepositoryInitOptions::new();
git_repo_options.workdir_path(&self.working_copy_path);
// Repository::init_opts creates a ".git" file in the working copy,
// which is undesired. On Windows it's worse because that ".git" makes
// the next Repository::init_opts fail with "Permission Denied".
// Automatically remove it.
let _cleanup_dot_git = {
struct Cleanup(PathBuf);
impl Drop for Cleanup {
fn drop(&mut self) {
let _ = fs::remove_file(&self.0);
}
}
Cleanup(self.working_copy_path.join(".git"))
};
let git_repo = Repository::init_opts(git_repo_dir.path(), &git_repo_options).unwrap();
let mut work = vec![(DirRepoPath::root(), self.working_copy_path.clone())];