1use std::cmp::Ordering;
16use std::fmt::{Debug, Error, Formatter};
17use std::hash::{Hash, Hasher};
18use std::sync::Arc;
19
20use crate::backend;
21use crate::backend::{ChangeId, CommitId, Signature, TreeId};
22use crate::repo_path::RepoPath;
23use crate::store::Store;
24use crate::tree::Tree;
25
26#[derive(Clone)]
27pub struct Commit {
28 store: Arc<Store>,
29 id: CommitId,
30 data: Arc<backend::Commit>,
31}
32
33impl Debug for Commit {
34 fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
35 f.debug_struct("Commit").field("id", &self.id).finish()
36 }
37}
38
39impl PartialEq for Commit {
40 fn eq(&self, other: &Self) -> bool {
41 self.id == other.id
42 }
43}
44
45impl Eq for Commit {}
46
47impl Ord for Commit {
48 fn cmp(&self, other: &Self) -> Ordering {
49 self.id.cmp(&other.id)
50 }
51}
52
53impl PartialOrd for Commit {
54 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
55 Some(self.id.cmp(&other.id))
56 }
57}
58
59impl Hash for Commit {
60 fn hash<H: Hasher>(&self, state: &mut H) {
61 self.id.hash(state)
62 }
63}
64
65impl Commit {
66 pub fn new(store: Arc<Store>, id: CommitId, data: Arc<backend::Commit>) -> Self {
67 Commit { store, id, data }
68 }
69
70 pub fn store(&self) -> &Arc<Store> {
71 &self.store
72 }
73
74 pub fn id(&self) -> &CommitId {
75 &self.id
76 }
77
78 pub fn parent_ids(&self) -> &[CommitId] {
79 &self.data.parents
80 }
81
82 pub fn parents(&self) -> Vec<Commit> {
83 self.data
84 .parents
85 .iter()
86 .map(|id| self.store.get_commit(id).unwrap())
87 .collect()
88 }
89
90 pub fn predecessor_ids(&self) -> &[CommitId] {
91 &self.data.predecessors
92 }
93
94 pub fn predecessors(&self) -> Vec<Commit> {
95 self.data
96 .predecessors
97 .iter()
98 .map(|id| self.store.get_commit(id).unwrap())
99 .collect()
100 }
101
102 pub fn tree(&self) -> Tree {
103 self.store
104 .get_tree(&RepoPath::root(), &self.data.root_tree)
105 .unwrap()
106 }
107
108 pub fn tree_id(&self) -> &TreeId {
109 &self.data.root_tree
110 }
111
112 pub fn change_id(&self) -> &ChangeId {
113 &self.data.change_id
114 }
115
116 pub fn store_commit(&self) -> &backend::Commit {
117 &self.data
118 }
119
120 pub fn description(&self) -> &str {
121 &self.data.description
122 }
123
124 pub fn author(&self) -> &Signature {
125 &self.data.author
126 }
127
128 pub fn committer(&self) -> &Signature {
129 &self.data.committer
130 }
131}