1use std::time::SystemTime;
4
5use endringer_core::error::{anyhow_to_backend, Error as CrateError, NotFoundKind, Result};
6use endringer_core::backend::VcsBackend;
7use endringer_core::types::{
8 AheadBehind, BlameEntry, BranchInfo, BranchTrackingInfo, CommitId, CommitInfo,
9 CommitQuery, CommitQueryResult, ConflictSummary, DiffSummary, OperationState,
10 RefInfo, RefKind, RemoteInfo, RepositoryInfo, SortOrder, StashEntry,
11 StatusDigest, SubmoduleInfo, TagInfo, TreeEntry, WorktreeInfo, WorktreeStatus,
12};
13
14use crate::{blame, branch, commit, conflict, diff, graph, info, object, operation,
15 refs, stash, status, submodule, tag, tree, worktree};
16
17pub struct GitBackend {
24 inner: gix::ThreadSafeRepository,
25}
26
27impl GitBackend {
28 pub fn open(path: &std::path::Path) -> anyhow::Result<Self> {
33 let inner = gix::discover(path)?.into_sync();
34 Ok(GitBackend { inner })
35 }
36}
37
38macro_rules! repo {
42 ($self:expr) => {
43 $self.inner.to_thread_local()
44 };
45}
46
47macro_rules! be {
49 ($e:expr) => {
50 $e.map_err(anyhow_to_backend)
51 };
52}
53
54impl VcsBackend for GitBackend {
55 fn status_digest(&self) -> Result<StatusDigest> {
56 be!(commit::status_digest(&repo!(self)))
57 }
58
59 fn local_branches(&self) -> Result<Vec<BranchInfo>> {
60 be!(branch::local_branches(&repo!(self)))
61 }
62
63 fn remote_branches(&self) -> Result<Vec<BranchInfo>> {
64 be!(branch::remote_branches(&repo!(self)))
65 }
66
67 fn list_commits(&self) -> Result<Vec<CommitInfo>> {
68 be!(branch::list_commits(&repo!(self)))
69 }
70
71 fn list_commits_sorted(&self, order: SortOrder) -> Result<Vec<CommitInfo>> {
72 be!(branch::list_commits_sorted(&repo!(self), order))
73 }
74
75 fn log_since(&self, since: SystemTime, until: SystemTime) -> Result<Vec<CommitInfo>> {
76 be!(branch::log_since(&repo!(self), since, until))
77 }
78
79 fn find_commit(&self, id: &CommitId) -> Result<CommitInfo> {
80 branch::find_commit(&repo!(self), id).map_err(|e| {
81 let msg = e.to_string();
82 if msg.contains("not found") || msg.contains("commit '") && msg.contains("not found") {
83 CrateError::NotFound { kind: NotFoundKind::Commit, name: id.to_string() }
84 } else if msg.contains("not a commit") {
85 CrateError::NotACommit { id: id.clone() }
86 } else {
87 anyhow_to_backend(e)
88 }
89 })
90 }
91
92 fn query_commits(&self, query: CommitQuery) -> Result<CommitQueryResult> {
93 be!(branch::query_commits(&repo!(self), query))
94 }
95
96 fn list_tags(&self) -> Result<Vec<TagInfo>> {
97 be!(tag::list_tags(&repo!(self)))
98 }
99
100 fn list_tags_sorted(&self, order: SortOrder) -> Result<Vec<TagInfo>> {
101 be!(tag::list_tags_sorted(&repo!(self), order))
102 }
103
104 fn create_tag(&self, name: &str) -> Result<()> {
105 be!(tag::create_tag(&repo!(self), name))
106 }
107
108 fn create_annotated_tag(&self, name: &str, message: &str) -> Result<()> {
109 be!(tag::create_annotated_tag(&repo!(self), name, message))
110 }
111
112 fn delete_tag(&self, name: &str) -> Result<()> {
113 be!(tag::delete_tag(&repo!(self), name))
114 }
115
116 fn diff(&self, from: &CommitId, to: &CommitId) -> Result<DiffSummary> {
117 be!(diff::diff(&repo!(self), from, to))
118 }
119
120 fn remote_url(&self, name: &str) -> Result<Option<String>> {
121 let repo = repo!(self);
122 let remote = match repo.find_remote(name) {
123 Ok(r) => r,
124 Err(e) => {
125 let msg = e.to_string();
126 if msg.contains("did not exist")
129 || msg.contains("not found")
130 || msg.contains("does not exist")
131 {
132 return Ok(None);
133 }
134 return Err(anyhow_to_backend(anyhow::anyhow!(msg)));
135 }
136 };
137 let url = remote.url(gix::remote::Direction::Fetch);
138 Ok(url.map(|u| u.to_bstring().to_string()))
139 }
140
141 fn is_dirty(&self) -> Result<bool> {
142 be!(status::is_dirty(&repo!(self)))
143 }
144
145 fn merge_base(&self, a: &CommitId, b: &CommitId) -> Result<Option<CommitId>> {
146 be!(graph::merge_base(&repo!(self), a, b))
147 }
148
149 fn is_ancestor(&self, candidate: &CommitId, descendant: &CommitId) -> Result<bool> {
150 be!(graph::is_ancestor(&repo!(self), candidate, descendant))
151 }
152
153 fn ahead_behind(&self, local: &CommitId, upstream: &CommitId) -> Result<AheadBehind> {
154 be!(graph::ahead_behind(&repo!(self), local, upstream))
155 }
156
157 fn branch_ahead_behind(&self, branch: &str) -> Result<Option<AheadBehind>> {
158 be!(graph::branch_ahead_behind(&repo!(self), branch))
159 }
160
161 fn repository_info(&self) -> Result<RepositoryInfo> {
162 be!(info::repository_info(&repo!(self), endringer_core::types::BackendKind::Git))
163 }
164
165 fn branch_tracking(&self, branch: &str) -> Result<BranchTrackingInfo> {
166 be!(branch::branch_tracking(&repo!(self), branch))
167 }
168
169 fn local_branch_tracking(&self) -> Result<Vec<BranchTrackingInfo>> {
170 be!(branch::local_branch_tracking(&repo!(self)))
171 }
172
173 fn is_merged_into(&self, b: &str, target: &str) -> Result<bool> {
174 be!(branch::is_merged_into(&repo!(self), b, target))
175 }
176
177 fn blame(&self, path: &std::path::Path) -> Result<Vec<BlameEntry>> {
178 be!(blame::blame(&repo!(self), path))
179 }
180
181 fn worktree_status(&self) -> Result<WorktreeStatus> {
182 be!(status::worktree_status(&repo!(self)))
183 }
184
185 fn file_at_commit(&self, path: &std::path::Path, commit_id: &CommitId) -> Result<Vec<u8>> {
186 object::file_at_commit(&repo!(self), path, commit_id).map_err(|e| {
187 let msg = e.to_string();
188 if msg.contains("not found") || msg.contains("does not exist") {
189 CrateError::PathNotFound {
190 path: path.to_path_buf(),
191 commit: Some(commit_id.clone()),
192 }
193 } else {
194 anyhow_to_backend(e)
195 }
196 })
197 }
198
199 fn submodules(&self) -> Result<Vec<SubmoduleInfo>> {
200 be!(submodule::submodules(&repo!(self)))
201 }
202
203 fn stash_entries(&self) -> Result<Vec<StashEntry>> {
204 be!(stash::stash_entries(&repo!(self)))
205 }
206
207 fn worktrees(&self) -> Result<Vec<WorktreeInfo>> {
208 be!(worktree::worktrees(&repo!(self)))
209 }
210
211 fn operation_state(&self) -> Result<OperationState> {
212 be!(operation::operation_state(repo!(self).git_dir()))
213 }
214
215 fn unmerged_paths(&self) -> Result<Vec<std::path::PathBuf>> {
216 be!(conflict::unmerged_paths(&repo!(self)))
217 }
218
219 fn conflict_summary(&self) -> Result<ConflictSummary> {
220 be!(conflict::conflict_summary(&repo!(self)))
221 }
222
223 fn blame_at(&self, path: &std::path::Path, commit_id: &CommitId) -> Result<Vec<BlameEntry>> {
224 be!(blame::blame_at(&repo!(self), path, commit_id))
225 }
226
227 fn tree_at_commit(&self, commit_id: &CommitId) -> Result<Vec<TreeEntry>> {
228 be!(tree::tree_at_commit(&repo!(self), commit_id))
229 }
230
231 fn tree_at_path(&self, commit_id: &CommitId, path: &std::path::Path) -> Result<Vec<TreeEntry>> {
232 be!(tree::tree_at_path(&repo!(self), commit_id, path))
233 }
234
235 fn remotes(&self) -> Result<Vec<RemoteInfo>> {
236 be!(refs::remotes(&repo!(self)))
237 }
238
239 fn references(&self) -> Result<Vec<RefInfo>> {
240 be!(refs::references(&repo!(self)))
241 }
242
243 fn references_by_kind(&self, kind: RefKind) -> Result<Vec<RefInfo>> {
244 be!(refs::references_by_kind(&repo!(self), kind))
245 }
246}