mirror of
https://github.com/starship/starship.git
synced 2026-06-24 02:01:36 +07:00
Implement the git status module (#45)
This commit is contained in:
+15
-2
@@ -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(¤t_dir)
|
||||
.ok()
|
||||
let repository = Repository::discover(¤t_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())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user