git.rs 3.3 KB

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