git.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. use std::path::{Path, PathBuf};
  2. use git2;
  3. use fs::fields as f;
  4. /// Container of Git statuses for all the files in this folder's Git repository.
  5. pub struct Git {
  6. statuses: Vec<(PathBuf, git2::Status)>,
  7. }
  8. impl Git {
  9. /// Discover a Git repository on or above this directory, scanning it for
  10. /// the files' statuses if one is found.
  11. pub fn scan(path: &Path) -> Result<Git, git2::Error> {
  12. info!("Scanning for Git repository under {:?}", path);
  13. let repo = git2::Repository::discover(path)?;
  14. let workdir = match repo.workdir() {
  15. Some(w) => w,
  16. None => return Ok(Git { statuses: vec![] }), // bare repo
  17. };
  18. let statuses = repo.statuses(None)?.iter()
  19. .map(|e| (workdir.join(Path::new(e.path().unwrap())), e.status()))
  20. .collect();
  21. Ok(Git { statuses: statuses })
  22. }
  23. /// Get the status for the file at the given path, if present.
  24. pub fn status(&self, path: &Path) -> f::Git {
  25. let status = self.statuses.iter()
  26. .find(|p| p.0.as_path() == path);
  27. match status {
  28. Some(&(_, s)) => f::Git { staged: index_status(s), unstaged: working_tree_status(s) },
  29. None => f::Git { staged: f::GitStatus::NotModified, unstaged: f::GitStatus::NotModified }
  30. }
  31. }
  32. /// Get the combined status for all the files whose paths begin with the
  33. /// path that gets passed in. This is used for getting the status of
  34. /// directories, which don't really have an 'official' status.
  35. pub fn dir_status(&self, dir: &Path) -> f::Git {
  36. let s = self.statuses.iter()
  37. .filter(|p| p.0.starts_with(dir))
  38. .fold(git2::Status::empty(), |a, b| a | b.1);
  39. f::Git { staged: index_status(s), unstaged: working_tree_status(s) }
  40. }
  41. }
  42. /// The character to display if the file has been modified, but not staged.
  43. fn working_tree_status(status: git2::Status) -> f::GitStatus {
  44. match status {
  45. s if s.contains(git2::STATUS_WT_NEW) => f::GitStatus::New,
  46. s if s.contains(git2::STATUS_WT_MODIFIED) => f::GitStatus::Modified,
  47. s if s.contains(git2::STATUS_WT_DELETED) => f::GitStatus::Deleted,
  48. s if s.contains(git2::STATUS_WT_RENAMED) => f::GitStatus::Renamed,
  49. s if s.contains(git2::STATUS_WT_TYPECHANGE) => f::GitStatus::TypeChange,
  50. _ => f::GitStatus::NotModified,
  51. }
  52. }
  53. /// The character to display if the file has been modified, and the change
  54. /// has been staged.
  55. fn index_status(status: git2::Status) -> f::GitStatus {
  56. match status {
  57. s if s.contains(git2::STATUS_INDEX_NEW) => f::GitStatus::New,
  58. s if s.contains(git2::STATUS_INDEX_MODIFIED) => f::GitStatus::Modified,
  59. s if s.contains(git2::STATUS_INDEX_DELETED) => f::GitStatus::Deleted,
  60. s if s.contains(git2::STATUS_INDEX_RENAMED) => f::GitStatus::Renamed,
  61. s if s.contains(git2::STATUS_INDEX_TYPECHANGE) => f::GitStatus::TypeChange,
  62. _ => f::GitStatus::NotModified,
  63. }
  64. }