1use crate::blame::Blame;
2use anyhow::{Context, Result};
3use collections::HashMap;
4use git2::{BranchType, StatusShow};
5use parking_lot::Mutex;
6use rope::Rope;
7use serde::{Deserialize, Serialize};
8use std::{
9 cmp::Ordering,
10 path::{Component, Path, PathBuf},
11 sync::Arc,
12 time::SystemTime,
13};
14use sum_tree::{MapSeekTarget, TreeMap};
15use util::{paths::PathExt, ResultExt};
16
17pub use git2::Repository as LibGitRepository;
18
19#[derive(Clone, Debug, Hash, PartialEq)]
20pub struct Branch {
21 pub name: Box<str>,
22 /// Timestamp of most recent commit, normalized to Unix Epoch format.
23 pub unix_timestamp: Option<i64>,
24}
25
26pub trait GitRepository: Send {
27 fn reload_index(&self);
28
29 /// Loads a git repository entry's contents.
30 /// Note that for symlink entries, this will return the contents of the symlink, not the target.
31 fn load_index_text(&self, relative_file_path: &Path) -> Option<String>;
32
33 /// Returns the URL of the remote with the given name.
34 fn remote_url(&self, name: &str) -> Option<String>;
35 fn branch_name(&self) -> Option<String>;
36
37 /// Returns the SHA of the current HEAD.
38 fn head_sha(&self) -> Option<String>;
39
40 /// Get the statuses of all of the files in the index that start with the given
41 /// path and have changes with respect to the HEAD commit. This is fast because
42 /// the index stores hashes of trees, so that unchanged directories can be skipped.
43 fn staged_statuses(&self, path_prefix: &Path) -> TreeMap<RepoPath, GitFileStatus>;
44
45 /// Get the status of a given file in the working directory with respect to
46 /// the index. In the common case, when there are no changes, this only requires
47 /// an index lookup. The index stores the mtime of each file when it was added,
48 /// so there's no work to do if the mtime matches.
49 fn unstaged_status(&self, path: &RepoPath, mtime: SystemTime) -> Option<GitFileStatus>;
50
51 /// Get the status of a given file in the working directory with respect to
52 /// the HEAD commit. In the common case, when there are no changes, this only
53 /// requires an index lookup and blob comparison between the index and the HEAD
54 /// commit. The index stores the mtime of each file when it was added, so there's
55 /// no need to consider the working directory file if the mtime matches.
56 fn status(&self, path: &RepoPath, mtime: SystemTime) -> Option<GitFileStatus>;
57
58 fn branches(&self) -> Result<Vec<Branch>>;
59 fn change_branch(&self, _: &str) -> Result<()>;
60 fn create_branch(&self, _: &str) -> Result<()>;
61
62 fn blame(&self, path: &Path, content: Rope) -> Result<crate::blame::Blame>;
63}
64
65impl std::fmt::Debug for dyn GitRepository {
66 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
67 f.debug_struct("dyn GitRepository<...>").finish()
68 }
69}
70
71pub struct RealGitRepository {
72 pub repository: LibGitRepository,
73 pub git_binary_path: PathBuf,
74}
75
76impl RealGitRepository {
77 pub fn new(repository: LibGitRepository, git_binary_path: Option<PathBuf>) -> Self {
78 Self {
79 repository,
80 git_binary_path: git_binary_path.unwrap_or_else(|| PathBuf::from("git")),
81 }
82 }
83}
84
85impl GitRepository for RealGitRepository {
86 fn reload_index(&self) {
87 if let Ok(mut index) = self.repository.index() {
88 _ = index.read(false);
89 }
90 }
91
92 fn load_index_text(&self, relative_file_path: &Path) -> Option<String> {
93 fn logic(repo: &LibGitRepository, relative_file_path: &Path) -> Result<Option<String>> {
94 const STAGE_NORMAL: i32 = 0;
95 let index = repo.index()?;
96
97 // This check is required because index.get_path() unwraps internally :(
98 check_path_to_repo_path_errors(relative_file_path)?;
99
100 let oid = match index.get_path(relative_file_path, STAGE_NORMAL) {
101 Some(entry) => entry.id,
102 None => return Ok(None),
103 };
104
105 let content = repo.find_blob(oid)?.content().to_owned();
106 Ok(Some(String::from_utf8(content)?))
107 }
108
109 match logic(&self.repository, relative_file_path) {
110 Ok(value) => return value,
111 Err(err) => log::error!("Error loading head text: {:?}", err),
112 }
113 None
114 }
115
116 fn remote_url(&self, name: &str) -> Option<String> {
117 let remote = self.repository.find_remote(name).ok()?;
118 remote.url().map(|url| url.to_string())
119 }
120
121 fn branch_name(&self) -> Option<String> {
122 let head = self.repository.head().log_err()?;
123 let branch = String::from_utf8_lossy(head.shorthand_bytes());
124 Some(branch.to_string())
125 }
126
127 fn head_sha(&self) -> Option<String> {
128 let head = self.repository.head().ok()?;
129 head.target().map(|oid| oid.to_string())
130 }
131
132 fn staged_statuses(&self, path_prefix: &Path) -> TreeMap<RepoPath, GitFileStatus> {
133 let mut map = TreeMap::default();
134
135 let mut options = git2::StatusOptions::new();
136 options.pathspec(path_prefix);
137 options.show(StatusShow::Index);
138
139 if let Some(statuses) = self.repository.statuses(Some(&mut options)).log_err() {
140 for status in statuses.iter() {
141 let path = RepoPath(PathBuf::try_from_bytes(status.path_bytes()).unwrap());
142 let status = status.status();
143 if !status.contains(git2::Status::IGNORED) {
144 if let Some(status) = read_status(status) {
145 map.insert(path, status)
146 }
147 }
148 }
149 }
150 map
151 }
152
153 fn unstaged_status(&self, path: &RepoPath, mtime: SystemTime) -> Option<GitFileStatus> {
154 // If the file has not changed since it was added to the index, then
155 // there can't be any changes.
156 if matches_index(&self.repository, path, mtime) {
157 return None;
158 }
159
160 let mut options = git2::StatusOptions::new();
161 options.pathspec(&path.0);
162 options.disable_pathspec_match(true);
163 options.include_untracked(true);
164 options.recurse_untracked_dirs(true);
165 options.include_unmodified(true);
166 options.show(StatusShow::Workdir);
167
168 let statuses = self.repository.statuses(Some(&mut options)).log_err()?;
169 let status = statuses.get(0).and_then(|s| read_status(s.status()));
170 status
171 }
172
173 fn status(&self, path: &RepoPath, mtime: SystemTime) -> Option<GitFileStatus> {
174 let mut options = git2::StatusOptions::new();
175 options.pathspec(&path.0);
176 options.disable_pathspec_match(true);
177 options.include_untracked(true);
178 options.recurse_untracked_dirs(true);
179 options.include_unmodified(true);
180
181 // If the file has not changed since it was added to the index, then
182 // there's no need to examine the working directory file: just compare
183 // the blob in the index to the one in the HEAD commit.
184 if matches_index(&self.repository, path, mtime) {
185 options.show(StatusShow::Index);
186 }
187
188 let statuses = self.repository.statuses(Some(&mut options)).log_err()?;
189 let status = statuses.get(0).and_then(|s| read_status(s.status()));
190 status
191 }
192
193 fn branches(&self) -> Result<Vec<Branch>> {
194 let local_branches = self.repository.branches(Some(BranchType::Local))?;
195 let valid_branches = local_branches
196 .filter_map(|branch| {
197 branch.ok().and_then(|(branch, _)| {
198 let name = branch.name().ok().flatten().map(Box::from)?;
199 let timestamp = branch.get().peel_to_commit().ok()?.time();
200 let unix_timestamp = timestamp.seconds();
201 let timezone_offset = timestamp.offset_minutes();
202 let utc_offset =
203 time::UtcOffset::from_whole_seconds(timezone_offset * 60).ok()?;
204 let unix_timestamp =
205 time::OffsetDateTime::from_unix_timestamp(unix_timestamp).ok()?;
206 Some(Branch {
207 name,
208 unix_timestamp: Some(unix_timestamp.to_offset(utc_offset).unix_timestamp()),
209 })
210 })
211 })
212 .collect();
213 Ok(valid_branches)
214 }
215 fn change_branch(&self, name: &str) -> Result<()> {
216 let revision = self.repository.find_branch(name, BranchType::Local)?;
217 let revision = revision.get();
218 let as_tree = revision.peel_to_tree()?;
219 self.repository.checkout_tree(as_tree.as_object(), None)?;
220 self.repository.set_head(
221 revision
222 .name()
223 .ok_or_else(|| anyhow::anyhow!("Branch name could not be retrieved"))?,
224 )?;
225 Ok(())
226 }
227 fn create_branch(&self, name: &str) -> Result<()> {
228 let current_commit = self.repository.head()?.peel_to_commit()?;
229 self.repository.branch(name, ¤t_commit, false)?;
230
231 Ok(())
232 }
233
234 fn blame(&self, path: &Path, content: Rope) -> Result<crate::blame::Blame> {
235 let working_directory = self
236 .repository
237 .workdir()
238 .with_context(|| format!("failed to get git working directory for file {:?}", path))?;
239
240 const REMOTE_NAME: &str = "origin";
241 let remote_url = self.remote_url(REMOTE_NAME);
242
243 crate::blame::Blame::for_path(
244 &self.git_binary_path,
245 working_directory,
246 path,
247 &content,
248 remote_url,
249 )
250 }
251}
252
253fn matches_index(repo: &LibGitRepository, path: &RepoPath, mtime: SystemTime) -> bool {
254 if let Some(index) = repo.index().log_err() {
255 if let Some(entry) = index.get_path(path, 0) {
256 if let Some(mtime) = mtime.duration_since(SystemTime::UNIX_EPOCH).log_err() {
257 if entry.mtime.seconds() == mtime.as_secs() as i32
258 && entry.mtime.nanoseconds() == mtime.subsec_nanos()
259 {
260 return true;
261 }
262 }
263 }
264 }
265 false
266}
267
268fn read_status(status: git2::Status) -> Option<GitFileStatus> {
269 if status.contains(git2::Status::CONFLICTED) {
270 Some(GitFileStatus::Conflict)
271 } else if status.intersects(
272 git2::Status::WT_MODIFIED
273 | git2::Status::WT_RENAMED
274 | git2::Status::INDEX_MODIFIED
275 | git2::Status::INDEX_RENAMED,
276 ) {
277 Some(GitFileStatus::Modified)
278 } else if status.intersects(git2::Status::WT_NEW | git2::Status::INDEX_NEW) {
279 Some(GitFileStatus::Added)
280 } else {
281 None
282 }
283}
284
285#[derive(Debug, Clone, Default)]
286pub struct FakeGitRepository {
287 state: Arc<Mutex<FakeGitRepositoryState>>,
288}
289
290#[derive(Debug, Clone, Default)]
291pub struct FakeGitRepositoryState {
292 pub index_contents: HashMap<PathBuf, String>,
293 pub blames: HashMap<PathBuf, Blame>,
294 pub worktree_statuses: HashMap<RepoPath, GitFileStatus>,
295 pub branch_name: Option<String>,
296}
297
298impl FakeGitRepository {
299 pub fn open(state: Arc<Mutex<FakeGitRepositoryState>>) -> Arc<Mutex<dyn GitRepository>> {
300 Arc::new(Mutex::new(FakeGitRepository { state }))
301 }
302}
303
304impl GitRepository for FakeGitRepository {
305 fn reload_index(&self) {}
306
307 fn load_index_text(&self, path: &Path) -> Option<String> {
308 let state = self.state.lock();
309 state.index_contents.get(path).cloned()
310 }
311
312 fn remote_url(&self, _name: &str) -> Option<String> {
313 None
314 }
315
316 fn branch_name(&self) -> Option<String> {
317 let state = self.state.lock();
318 state.branch_name.clone()
319 }
320
321 fn head_sha(&self) -> Option<String> {
322 None
323 }
324
325 fn staged_statuses(&self, path_prefix: &Path) -> TreeMap<RepoPath, GitFileStatus> {
326 let mut map = TreeMap::default();
327 let state = self.state.lock();
328 for (repo_path, status) in state.worktree_statuses.iter() {
329 if repo_path.0.starts_with(path_prefix) {
330 map.insert(repo_path.to_owned(), status.to_owned());
331 }
332 }
333 map
334 }
335
336 fn unstaged_status(&self, _path: &RepoPath, _mtime: SystemTime) -> Option<GitFileStatus> {
337 None
338 }
339
340 fn status(&self, path: &RepoPath, _mtime: SystemTime) -> Option<GitFileStatus> {
341 let state = self.state.lock();
342 state.worktree_statuses.get(path).cloned()
343 }
344
345 fn branches(&self) -> Result<Vec<Branch>> {
346 Ok(vec![])
347 }
348
349 fn change_branch(&self, name: &str) -> Result<()> {
350 let mut state = self.state.lock();
351 state.branch_name = Some(name.to_owned());
352 Ok(())
353 }
354
355 fn create_branch(&self, name: &str) -> Result<()> {
356 let mut state = self.state.lock();
357 state.branch_name = Some(name.to_owned());
358 Ok(())
359 }
360
361 fn blame(&self, path: &Path, _content: Rope) -> Result<crate::blame::Blame> {
362 let state = self.state.lock();
363 state
364 .blames
365 .get(path)
366 .with_context(|| format!("failed to get blame for {:?}", path))
367 .cloned()
368 }
369}
370
371fn check_path_to_repo_path_errors(relative_file_path: &Path) -> Result<()> {
372 match relative_file_path.components().next() {
373 None => anyhow::bail!("repo path should not be empty"),
374 Some(Component::Prefix(_)) => anyhow::bail!(
375 "repo path `{}` should be relative, not a windows prefix",
376 relative_file_path.to_string_lossy()
377 ),
378 Some(Component::RootDir) => {
379 anyhow::bail!(
380 "repo path `{}` should be relative",
381 relative_file_path.to_string_lossy()
382 )
383 }
384 Some(Component::CurDir) => {
385 anyhow::bail!(
386 "repo path `{}` should not start with `.`",
387 relative_file_path.to_string_lossy()
388 )
389 }
390 Some(Component::ParentDir) => {
391 anyhow::bail!(
392 "repo path `{}` should not start with `..`",
393 relative_file_path.to_string_lossy()
394 )
395 }
396 _ => Ok(()),
397 }
398}
399
400#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
401pub enum GitFileStatus {
402 Added,
403 Modified,
404 Conflict,
405}
406
407impl GitFileStatus {
408 pub fn merge(
409 this: Option<GitFileStatus>,
410 other: Option<GitFileStatus>,
411 prefer_other: bool,
412 ) -> Option<GitFileStatus> {
413 if prefer_other {
414 return other;
415 }
416
417 match (this, other) {
418 (Some(GitFileStatus::Conflict), _) | (_, Some(GitFileStatus::Conflict)) => {
419 Some(GitFileStatus::Conflict)
420 }
421 (Some(GitFileStatus::Modified), _) | (_, Some(GitFileStatus::Modified)) => {
422 Some(GitFileStatus::Modified)
423 }
424 (Some(GitFileStatus::Added), _) | (_, Some(GitFileStatus::Added)) => {
425 Some(GitFileStatus::Added)
426 }
427 _ => None,
428 }
429 }
430}
431
432#[derive(Clone, Debug, Ord, Hash, PartialOrd, Eq, PartialEq)]
433pub struct RepoPath(pub PathBuf);
434
435impl RepoPath {
436 pub fn new(path: PathBuf) -> Self {
437 debug_assert!(path.is_relative(), "Repo paths must be relative");
438
439 RepoPath(path)
440 }
441}
442
443impl From<&Path> for RepoPath {
444 fn from(value: &Path) -> Self {
445 RepoPath::new(value.to_path_buf())
446 }
447}
448
449impl From<PathBuf> for RepoPath {
450 fn from(value: PathBuf) -> Self {
451 RepoPath::new(value)
452 }
453}
454
455impl Default for RepoPath {
456 fn default() -> Self {
457 RepoPath(PathBuf::new())
458 }
459}
460
461impl AsRef<Path> for RepoPath {
462 fn as_ref(&self) -> &Path {
463 self.0.as_ref()
464 }
465}
466
467impl std::ops::Deref for RepoPath {
468 type Target = PathBuf;
469
470 fn deref(&self) -> &Self::Target {
471 &self.0
472 }
473}
474
475#[derive(Debug)]
476pub struct RepoPathDescendants<'a>(pub &'a Path);
477
478impl<'a> MapSeekTarget<RepoPath> for RepoPathDescendants<'a> {
479 fn cmp_cursor(&self, key: &RepoPath) -> Ordering {
480 if key.starts_with(self.0) {
481 Ordering::Greater
482 } else {
483 self.0.cmp(key)
484 }
485 }
486}