repository.rs

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