summaryrefslogtreecommitdiffstats
path: root/src/context.rs
diff options
context:
space:
mode:
authorSagittarius-a <sagittarius-a@users.noreply.github.com>2020-12-08 11:12:53 +0100
committerGitHub <noreply@github.com>2020-12-08 11:12:53 +0100
commitd670212a083e9f1e9c9a2313e7ce5e72e908efa7 (patch)
tree7c9cc10c8c5a532a590d15d33319915ea75c0af9 /src/context.rs
parent26455df656a14fc90dfb887efb4c9c9508ad2e6a (diff)
feat(git_branch): show remote name (#1972)
* feat(git_branch): Show remote name in addition to remote branch * feat(git_branch): Fix table indentation in config README * feat(git_branch): Use a different method to fetch remote information * feat(git_branch): Fix the Clippy issue regarding string constant
Diffstat (limited to 'src/context.rs')
-rw-r--r--src/context.rs26
1 files changed, 20 insertions, 6 deletions
diff --git a/src/context.rs b/src/context.rs
index 1ad61a1c1..576f46233 100644
--- a/src/context.rs
+++ b/src/context.rs
@@ -171,7 +171,9 @@ impl<'a> Context<'a> {
.as_ref()
.and_then(|repo| repo.workdir().map(Path::to_path_buf));
let state = repository.as_ref().map(|repo| repo.state());
- let remote = repository.as_ref().and_then(|repo| get_remote_branch(repo));
+ let remote = repository
+ .as_ref()
+ .and_then(|repo| get_remote_repository_info(repo));
Ok(Repo {
branch,
root,
@@ -312,8 +314,14 @@ pub struct Repo {
/// State
pub state: Option<RepositoryState>,
- /// Remote branch name
- pub remote: Option<String>,
+ /// Remote repository
+ pub remote: Option<Remote>,
+}
+
+/// Remote repository
+pub struct Remote {
+ pub branch: Option<String>,
+ pub name: Option<String>,
}
// A struct of Criteria which will be used to verify current PathBuf is
@@ -380,7 +388,7 @@ fn get_current_branch(repository: &Repository) -> Option<String> {
shorthand.map(std::string::ToString::to_string)
}
-fn get_remote_branch(repository: &Repository) -> Option<String> {
+fn get_remote_repository_info(repository: &Repository) -> Option<Remote> {
if let Ok(head) = repository.head() {
if let Some(local_branch_ref) = head.name() {
let remote_ref = match repository.branch_upstream_name(local_branch_ref) {
@@ -388,8 +396,14 @@ fn get_remote_branch(repository: &Repository) -> Option<String> {
Err(_) => return None,
};
- let remote = remote_ref.split('/').last().map(|r| r.to_owned())?;
- return Some(remote);
+ let mut v = remote_ref.splitn(4, '/');
+ let remote_name = v.nth(2)?.to_owned();
+ let remote_branch = v.last()?.to_owned();
+
+ return Some(Remote {
+ branch: Some(remote_branch),
+ name: Some(remote_name),
+ });
}
}
None