gitpoulpi_core/domain/reflog.rs
1//! The reflog, read (the undo-safety-net design): where a ref
2//! was before, including moves made outside GitPoulpi, and the commits no
3//! ref reaches any more.
4
5use serde::{Deserialize, Serialize};
6
7use super::Oid;
8
9/// The all-zero object id git writes for "no object" (`old` of a ref's
10/// first reflog entry).
11pub const ZERO_OID: Oid = Oid([0; 20]);
12
13/// One line of a reflog.
14#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
15pub struct ReflogEntry {
16 /// The value before the move ([`ZERO_OID`] for the first entry).
17 pub old: Oid,
18 /// The value after the move: the position "restore to" returns to.
19 pub new: Oid,
20 /// `Name <email>` of whoever moved the ref.
21 pub committer: String,
22 /// Unix seconds.
23 pub time: i64,
24 /// Git's reason: `commit: fix typo`, `checkout: moving from a to b`,
25 /// `reset: moving to HEAD~1`, ...
26 pub message: String,
27}
28
29/// A page of a reflog, newest entry first.
30#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
31pub struct ReflogPage {
32 /// The full ref name read (`HEAD`, `refs/heads/main`).
33 pub refname: String,
34 pub entries: Vec<ReflogEntry>,
35 /// More entries exist past this page.
36 pub more: bool,
37}
38
39/// Full reflog name for what a user typed: `HEAD`, `refs/...` as is, any
40/// other name is a local branch.
41pub fn reflog_refname(name: &str) -> String {
42 let name = name.trim();
43 if name.is_empty() || name == "HEAD" {
44 "HEAD".to_string()
45 } else if name.starts_with("refs/") {
46 name.to_string()
47 } else {
48 format!("refs/heads/{name}")
49 }
50}
51
52/// Where in a reflog a ref held a given value: what a blocked undo offers
53/// ("restore main to where Commit left it").
54#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
55pub struct ReflogPosition {
56 /// Full ref name.
57 pub refname: String,
58 /// The value to restore the ref to.
59 pub oid: Oid,
60 /// 0 is the newest entry.
61 pub index: usize,
62 pub time: i64,
63 pub message: String,
64}
65
66/// Why a lost commit is still around.
67#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
68pub enum LostSource {
69 /// A reflog (`HEAD`'s, or a branch's) still names it.
70 Reflog,
71 /// Only a GitPoulpi undo pin keeps it (an old or expired journal entry).
72 Pin,
73 /// Nothing names it: a dangling object, as `git fsck --lost-found` finds.
74 Dangling,
75}
76
77/// A commit no branch, tag, remote-tracking ref or stash reaches.
78#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
79pub struct LostCommit {
80 pub oid: Oid,
81 pub summary: String,
82 pub author: String,
83 /// Author date, unix seconds.
84 pub time: i64,
85 pub parents: Vec<Oid>,
86 pub source: LostSource,
87}
88
89/// What `GitBackend::lost_commits` scans.
90#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
91#[serde(default)]
92pub struct LostCommitOptions {
93 pub skip: usize,
94 pub limit: usize,
95 /// Also look for dangling objects in the whole object database (the
96 /// part `git fsck --unreachable` would find). Bounded by `budget_ms`.
97 pub deep: bool,
98 /// The scan stops after this long (0: no limit) and says so in
99 /// `LostCommitsPage::partial`.
100 pub budget_ms: u64,
101}
102
103impl Default for LostCommitOptions {
104 fn default() -> Self {
105 Self {
106 skip: 0,
107 limit: 50,
108 deep: true,
109 budget_ms: 8000,
110 }
111 }
112}
113
114/// A page of lost commits, newest first.
115#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
116pub struct LostCommitsPage {
117 pub commits: Vec<LostCommit>,
118 pub more: bool,
119 /// The time budget ran out before the scan finished: the list is
120 /// correct but may be missing older commits.
121 pub partial: bool,
122}