repository.rs

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