Skip to main content

git_policy_cli/lock/
stale_ref_lock_found.rs

1//! What:
2//!  The record of one judged reference lock, and the outcome of one clearing pass.
3//! Why:
4//!  The callers decide what to do from the outcome (run the command again only when a lock was cleared) and the notice is
5//!  worded from the same record, so what was printed is what was decided.
6//!
7//! In TS you'd write (pseudocode):
8//! ```ts
9//! type Outcome = { cleared: Found[]; left: Found[]; displaced: string[] };
10//! ```
11
12use crate::stale_ref_lock_judge::Verdict;
13use std::path::PathBuf;
14use std::time::SystemTime;
15
16/// What the pid file beside a lock says.
17#[derive(Clone, Debug, Eq, PartialEq)]
18pub enum PidFile {
19    /// No pid file, or one that holds no readable number yet.
20    Absent,
21    /// A pid file born before the lock: written by an earlier holder, so it names nobody who holds this lock.
22    Older(i64),
23    /// A pid file born at or after the lock, naming this process.
24    Names(i64),
25}
26
27/// One judged reference lock.
28#[derive(Clone, Debug)]
29pub struct Found {
30    /// The lock file.
31    pub lock: PathBuf,
32    /// What the judgement concluded.
33    pub verdict: Verdict,
34    /// The size of the lock when it was judged.
35    pub size: u64,
36    /// When the lock was born, where the file system keeps it.
37    pub born: Option<SystemTime>,
38    /// The pid file beside the lock.
39    pub pid_file: PidFile,
40    /// The files that go with the lock and are removed or named with it: its pid file and, for `packed-refs.lock`, the
41    ///  staging file `packed-refs.new`. Only those that exist.
42    pub companions: Vec<PathBuf>,
43}
44
45/// The result of one clearing pass.
46#[derive(Clone, Debug, Default)]
47pub struct Outcome {
48    /// Locks that were removed.
49    pub cleared: Vec<Found>,
50    /// Locks that were left alone, with the reason each was not removed.
51    pub left: Vec<Found>,
52    /// Files that were set aside and could not be put back or removed because the path had been taken again.
53    pub displaced: Vec<PathBuf>,
54}
55
56impl Outcome {
57    /// Whether a lock was removed, which is the one reason to run the blocked command again.
58    pub fn cleared_any(&self) -> bool {
59        return !self.cleared.is_empty();
60    }
61}