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