| 298 | } |
| 299 | |
| 300 | fn remove_history(project_dir: &Path, attempt: Option<u8>) -> Result<()> { |
| 301 | let git_dir = project_dir.join(".git"); |
| 302 | if git_dir.exists() && git_dir.is_dir() { |
| 303 | if let Err(e) = remove_dir_all(git_dir) { |
| 304 | // see https://github.com/cargo-generate/cargo-generate/issues/375 |
| 305 | if e.to_string().contains( |
| 306 | "The process cannot access the file because it is being used by another process.", |
| 307 | ) { |
| 308 | let attempt = attempt.unwrap_or(1); |
| 309 | if attempt == 5 { |
| 310 | warn!("megflow-quickstart was not able to delete the git history after {} retries. Please delete the `.git` sub-folder manually", attempt); |
| 311 | return Ok(()); |
| 312 | } |
| 313 | let wait_for = Duration::from_secs(2_u64.pow(attempt.sub(1).into())); |
| 314 | warn!("Git history cleanup failed with a windows process blocking error. [Retry in {:?}]", wait_for); |
| 315 | sleep(wait_for); |
| 316 | remove_history(project_dir, Some(attempt.add(1)))? |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | Ok(()) |
| 321 | } |
| 322 | |
| 323 | pub fn init(project_dir: &Path, branch: &str) -> Result<Repository> { |
| 324 | Repository::discover(project_dir).or_else(|_| { |