Skip to main content

gitstack/
compare.rs

1//! ブランチ比較機能
2//!
3//! 2つのブランチ間の差分コミットを一覧表示
4
5use chrono::{DateTime, Local};
6
7/// 比較用コミット情報
8#[derive(Debug, Clone)]
9pub struct CompareCommit {
10    /// コミットハッシュ(短縮形)
11    pub hash: String,
12    /// コミットメッセージ(1行目)
13    pub message: String,
14    /// 著者名
15    pub author: String,
16    /// コミット日時
17    pub date: DateTime<Local>,
18}
19
20/// ブランチ比較結果
21#[derive(Debug, Clone)]
22pub struct BranchCompare {
23    /// ベースブランチ名
24    pub base_branch: String,
25    /// ターゲットブランチ名
26    pub target_branch: String,
27    /// ターゲットがベースより先行しているコミット
28    pub ahead_commits: Vec<CompareCommit>,
29    /// ターゲットがベースより遅れているコミット
30    pub behind_commits: Vec<CompareCommit>,
31    /// マージベースのコミットハッシュ
32    pub merge_base: String,
33}
34
35impl BranchCompare {
36    /// ahead/behindの合計コミット数
37    pub fn total_commits(&self) -> usize {
38        self.ahead_commits.len() + self.behind_commits.len()
39    }
40
41    /// 差分がないかどうか
42    pub fn is_synced(&self) -> bool {
43        self.ahead_commits.is_empty() && self.behind_commits.is_empty()
44    }
45}
46
47/// 比較ビューのタブ
48#[derive(Debug, Clone, Copy, PartialEq, Default)]
49pub enum CompareTab {
50    #[default]
51    Ahead,
52    Behind,
53}
54
55impl CompareTab {
56    /// タブを切り替え
57    pub fn toggle(&self) -> Self {
58        match self {
59            CompareTab::Ahead => CompareTab::Behind,
60            CompareTab::Behind => CompareTab::Ahead,
61        }
62    }
63}