Skip to main content

git_tailor/
lib.rs

1// Copyright 2026 Thomas Johannesson
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// Core library for git-tailor
16
17pub mod app;
18pub mod editor;
19pub mod fragmap;
20pub mod mergetool;
21pub mod repo;
22pub mod static_views;
23pub mod views;
24
25/// Represents commit metadata extracted from git repository.
26///
27/// This is a pure data structure containing commit information
28/// without any git2 object dependencies.
29#[derive(Debug, Clone)]
30pub struct CommitInfo {
31    pub oid: String,
32    pub summary: String,
33    /// Author name. `None` for synthetic pseudo-commits (staged/unstaged changes).
34    pub author: Option<String>,
35    /// Raw commit timestamp (seconds since epoch). `None` for synthetic pseudo-commits.
36    pub date: Option<String>,
37    pub parent_oids: Vec<String>,
38    /// Full commit message including body (all lines).
39    pub message: String,
40    /// Author email address. `None` for synthetic pseudo-commits.
41    pub author_email: Option<String>,
42    /// Author date with timezone. `None` for synthetic pseudo-commits.
43    pub author_date: Option<time::OffsetDateTime>,
44    /// Committer name. `None` for synthetic pseudo-commits.
45    pub committer: Option<String>,
46    /// Committer email address. `None` for synthetic pseudo-commits.
47    pub committer_email: Option<String>,
48    /// Commit date with timezone. `None` for synthetic pseudo-commits.
49    pub commit_date: Option<time::OffsetDateTime>,
50}
51
52/// The kind of change a diff line represents.
53///
54/// When Git compares two versions of a file, each line in the output falls
55/// into one of three categories:
56#[derive(Debug, Clone, Copy, PartialEq, Eq)]
57pub enum DiffLineKind {
58    /// An unchanged line shown for surrounding context. These lines exist
59    /// identically in both the old and new versions of the file.
60    Context,
61    /// A line that was added in the new version. It does not exist in the
62    /// old version. Shown with a "+" prefix in traditional diff output.
63    Addition,
64    /// A line that was removed from the old version. It does not exist in
65    /// the new version. Shown with a "-" prefix in traditional diff output.
66    Deletion,
67}
68
69/// A single line from a diff, along with what kind of change it represents.
70#[derive(Debug, Clone)]
71pub struct DiffLine {
72    pub kind: DiffLineKind,
73    /// The text content of the line (without the +/- prefix).
74    pub content: String,
75}
76
77/// A "hunk" is a standard Git concept: a contiguous group of changed lines
78/// within a single file, together with a few surrounding unchanged (context)
79/// lines for orientation.
80///
81/// When a file has changes in multiple separate regions, Git produces one
82/// hunk per region rather than one giant diff. For example, if lines 10-12
83/// and lines 50-55 were modified, that file's diff would contain two hunks.
84///
85/// The line numbers refer to positions in the old (before) and new (after)
86/// versions of the file:
87/// - `old_start` / `old_lines`: where this hunk begins and how many lines
88///   it spans in the original file.
89/// - `new_start` / `new_lines`: the same for the modified file.
90///
91/// These correspond to the `@@ -old_start,old_lines +new_start,new_lines @@`
92/// header you see in unified diff output.
93#[derive(Debug, Clone)]
94pub struct Hunk {
95    pub old_start: u32,
96    pub old_lines: u32,
97    pub new_start: u32,
98    pub new_lines: u32,
99    /// The individual lines in this hunk (context, additions, and deletions).
100    pub lines: Vec<DiffLine>,
101}
102
103/// Git delta status indicating the type of file change.
104#[derive(Debug, Clone, Copy, PartialEq, Eq)]
105pub enum DeltaStatus {
106    Unmodified,
107    Added,
108    Deleted,
109    Modified,
110    Renamed,
111    Copied,
112    Ignored,
113    Untracked,
114    Typechange,
115    Unreadable,
116    Conflicted,
117}
118
119/// The diff for a single file between a commit and its parent commit.
120///
121/// Represents all changes made to one file in a single commit. A file may
122/// have been added (old_path is None), deleted (new_path is None), renamed
123/// (both paths differ), or modified (both paths are the same).
124#[derive(Debug, Clone)]
125pub struct FileDiff {
126    /// Path in the old (parent) version, or None if the file was newly added.
127    pub old_path: Option<String>,
128    /// Path in the new (commit) version, or None if the file was deleted.
129    pub new_path: Option<String>,
130    /// The git delta status indicating the type of change.
131    pub status: DeltaStatus,
132    /// The list of changed regions in this file. A simple one-line change
133    /// produces one hunk; scattered edits produce multiple hunks.
134    pub hunks: Vec<Hunk>,
135}
136
137/// All diff information for a single commit.
138///
139/// Combines the commit metadata with the complete list of file changes,
140/// giving a full picture of what a commit modified.
141#[derive(Debug, Clone)]
142pub struct CommitDiff {
143    pub commit: CommitInfo,
144    /// Every file that was added, modified, renamed, or deleted in this commit.
145    pub files: Vec<FileDiff>,
146}