From eacab648b0715dc5eda1fac19bbdd204d61dad74 Mon Sep 17 00:00:00 2001 From: Jun Wu Date: Wed, 10 Mar 2021 22:28:39 -0800 Subject: [PATCH] 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. --- lib/src/working_copy.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/src/working_copy.rs b/lib/src/working_copy.rs index bf05e1416..970a917d2 100644 --- a/lib/src/working_copy.rs +++ b/lib/src/working_copy.rs @@ -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())];