use crate::auto_push_single_flight::{PushAttempt, SingleFlightError};
use crate::bounded_push::{Patience, run_bounded_push_once};
use crate::diagnostic_log::warn;
use crate::git_directory_roots::roots_of_command;
use crate::stale_ref_lock_judge::Judge;
use crate::stale_ref_locks::clear_stale_ref_locks;
use std::ffi::OsString;
use std::process::Command;
use std::time::Duration;
const TAG: &str = "cli-git";
const REFERENCE_LOCK_REFUSAL: &str = "cannot lock ref";
pub fn run_clearing_locks(
command: Command,
patience: Patience,
grace: Duration,
environment: &[(OsString, OsString)],
judge: &Judge,
) -> Result<PushAttempt, SingleFlightError> {
let outcome = clear_stale_ref_locks(roots_of_command(&command).as_slice(), judge);
let notice: String = outcome.notice();
let mut attempt: PushAttempt = run_bounded_push_once(command, patience, grace, environment)?;
let failed: bool = attempt.exit_code != 0 || attempt.output.contains(REFERENCE_LOCK_REFUSAL);
if failed && !notice.is_empty() {
attempt.output = if attempt.output.is_empty() { notice.trim_end().to_owned() } else { format!("{}\n{}", attempt.output, notice.trim_end()) };
} else if !notice.is_empty() {
warn(TAG, notice.trim_end());
}
return Ok(attempt);
}
#[cfg(test)]
#[path = "push_lock_clearing_tests.rs"]
mod tests;