Implement the git status module (#45)

This commit is contained in:
Matan Kushner
2019-05-13 22:43:11 -06:00
committed by GitHub
parent c95bb60571
commit 90d6e6cf0b
10 changed files with 436 additions and 156 deletions
+15 -2
View File
@@ -10,6 +10,7 @@ pub struct Context<'a> {
pub dir_files: Vec<PathBuf>,
pub arguments: ArgMatches<'a>,
pub repo_root: Option<PathBuf>,
pub branch_name: Option<String>,
}
impl<'a> Context<'a> {
@@ -37,15 +38,20 @@ impl<'a> Context<'a> {
.map(|entry| entry.path())
.collect::<Vec<PathBuf>>();
let repo_root: Option<PathBuf> = Repository::discover(&current_dir)
.ok()
let repository = Repository::discover(&current_dir).ok();
let repo_root = repository
.as_ref()
.and_then(|repo| repo.workdir().map(|repo| repo.to_path_buf()));
let branch_name = repository
.as_ref()
.and_then(|repo| get_current_branch(&repo));
Context {
arguments,
current_dir,
dir_files,
repo_root,
branch_name,
}
}
@@ -192,3 +198,10 @@ mod tests {
assert_eq!(passing_criteria.scan(), true);
}
}
fn get_current_branch(repository: &Repository) -> Option<String> {
let head = repository.head().ok()?;
let shorthand = head.shorthand();
shorthand.map(|branch| branch.to_string())
}