gitpoulpi_core/domain/diff.rs
1use std::path::PathBuf;
2
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
6pub enum FileStatus {
7 Untracked,
8 Modified,
9 Staged,
10 Conflicted,
11 Renamed,
12 Deleted,
13 Added,
14}
15
16/// One `git status` row. `index`/`worktree` are independent because a file
17/// can be staged *and* have further unstaged changes at the same time
18/// (porcelain v2's XY columns) - collapsing that into a single FileStatus
19/// would lose information the staging UI needs.
20#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
21pub struct StatusEntry {
22 pub path: PathBuf,
23 pub index: Option<FileStatus>,
24 pub worktree: Option<FileStatus>,
25 /// Set on the new path of a rename (or copy): the path it came from.
26 /// Staged (`index == Renamed`): git's own pairing. Unstaged: a tracked
27 /// file deleted in the working tree plus an untracked (or
28 /// intent-to-add) one of similar content (`pair_unstaged_renames`).
29 /// The old path's own entry stays in the list, unchanged.
30 #[serde(default, skip_serializing_if = "Option::is_none")]
31 pub renamed_from: Option<PathBuf>,
32}
33
34#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
35pub enum LineOrigin {
36 Add,
37 Delete,
38 Context,
39}
40
41#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
42pub struct DiffLine {
43 pub origin: LineOrigin,
44 pub content: String,
45 pub staged: bool,
46}
47
48#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
49pub struct HunkId(pub u32);
50
51#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
52pub struct Hunk {
53 pub id: HunkId,
54 pub old_start: u32,
55 pub old_lines: u32,
56 pub new_start: u32,
57 pub new_lines: u32,
58 pub lines: Vec<DiffLine>,
59}
60
61#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
62pub struct FileDiff {
63 pub path: PathBuf,
64 pub status: FileStatus,
65 pub hunks: Vec<Hunk>,
66 /// A renamed (or copied) file: the path the content came from. `path`
67 /// is the new name and `hunks` the content diff against `renamed_from`
68 /// (none for a pure rename).
69 #[serde(default, skip_serializing_if = "Option::is_none")]
70 pub renamed_from: Option<PathBuf>,
71 /// The rename's similarity in percent (100 for a pure rename).
72 #[serde(default, skip_serializing_if = "Option::is_none")]
73 pub similarity: Option<u8>,
74}
75
76/// What `GitBackend::diff_files` answers: the files `diff` lists for the
77/// same target, in the same order with the same statuses.
78#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
79pub struct DiffFileList {
80 pub files: Vec<FileDiff>,
81 /// `true`: `files` *is* the `diff` answer, hunks and all - the
82 /// backend could not list more cheaply than it diffs, so it did both
83 /// at once. `false`: every `hunks` is empty and the hunks are still
84 /// to be asked of `diff`. Never inferred from empty `hunks`: a binary
85 /// file or a mode-only change has none in the full diff either.
86 pub hunks_included: bool,
87}
88
89/// What one commit changed, in numbers (the Log's "Changes" column): the
90/// commit against its first parent (a root commit against nothing), the
91/// same comparison as `DiffTarget::CommitVsParent`. Renames are not
92/// detected, so a rename counts as two files; a binary file counts as a
93/// file with no lines.
94#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
95pub struct CommitStat {
96 pub id: super::Oid,
97 pub files: u32,
98 pub insertions: u32,
99 pub deletions: u32,
100}
101
102#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
103pub enum DiffTarget {
104 WorkingTreeVsIndex,
105 IndexVsHead,
106 CommitVsParent(super::Oid),
107 CommitRange(super::Oid, super::Oid),
108 /// The commit's tree against the working tree: what a user would have
109 /// to do to the commit to get what is on disk (staged and unstaged
110 /// changes of tracked files; untracked files are not part of any diff
111 /// target). The "WIP against a commit" comparison.
112 CommitVsWorkingTree(super::Oid),
113 /// From the *first parent of* `.0` (the empty tree for a root commit)
114 /// to `.1`: the combined changes of a run of commits whose oldest is
115 /// `.0` and whose newest is `.1`.
116 CommitSpan(super::Oid, super::Oid),
117}
118
119/// Which lines within a hunk a line-level stage/unstage selection covers.
120#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
121pub struct LineSelector {
122 pub hunk: HunkId,
123 pub line_indices: Vec<u32>,
124}
125
126/// Unifies every staging granularity (line, hunk, file, folder, all) behind
127/// one call so frontends never need separate stage_file/stage_hunk/... methods.
128#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
129pub enum StageTarget {
130 Line(PathBuf, LineSelector),
131 Hunk(PathBuf, HunkId),
132 File(PathBuf),
133 Folder(PathBuf),
134 /// Several paths (files or folders) as ONE operation: one index write,
135 /// one journal entry, one undo step. A moved file is two paths for git
136 /// (the deleted old one and the new one) and stages or unstages as one.
137 Files(Vec<PathBuf>),
138 All,
139}
140
141/// Part of one file's *unstaged* changes to throw away:
142/// The "Discard Hunk" and "Discard this line" actions in the diff.
143///
144/// Numbered exactly like [`StageTarget::Hunk`] / [`StageTarget::Line`]
145/// against the working-tree-vs-index diff (`DiffTarget::WorkingTreeVsIndex`):
146/// the hunk ids that diff assigns, and indices into that hunk's `lines`.
147/// Whole files, folders and "everything" are not here on purpose - that
148/// is [`crate::GitBackend::discard`], which also deletes untracked files;
149/// this only ever edits the working-tree copy of a tracked file.
150#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
151pub enum DiscardTarget {
152 Hunk(PathBuf, HunkId),
153 Line(PathBuf, LineSelector),
154}