| 1421 | |
| 1422 | impl GitIndexLockGuard { |
| 1423 | fn acquire(git: &GitRepository, attempts: u32) -> Result<Option<Self>, RepositoryError> { |
| 1424 | let path = git.path().join("index.lock"); |
| 1425 | for attempt in 0..attempts { |
| 1426 | match fs::OpenOptions::new() |
| 1427 | .write(true) |
| 1428 | .create_new(true) |
| 1429 | .open(&path) |
| 1430 | { |
| 1431 | Ok(_) => return Ok(Some(Self { path })), |
| 1432 | Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => { |
| 1433 | if attempt + 1 == attempts { |
| 1434 | return Ok(None); |
| 1435 | } |
| 1436 | std::thread::sleep(std::time::Duration::from_millis(25 * (attempt as u64 + 1))); |
| 1437 | } |
| 1438 | Err(error) => { |
| 1439 | return Err(git_map(format!( |
| 1440 | "cannot create the Git index lock '{}': {error}", |
| 1441 | path.display() |
| 1442 | ))) |
| 1443 | } |
| 1444 | } |
| 1445 | } |
| 1446 | Ok(None) |
| 1447 | } |
| 1448 | } |
| 1449 | |
| 1450 | impl Drop for GitIndexLockGuard { |