mirror of
https://github.com/Llewellynvdm/starship.git
synced 2025-04-07 01:31:50 +00:00
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
This commit is contained in:
parent
26455df656
commit
d670212a08
@ -1031,17 +1031,18 @@ The `git_branch` module shows the active branch of the repo in your current dire
|
|||||||
| `style` | `"bold purple"` | The style for the module. |
|
| `style` | `"bold purple"` | The style for the module. |
|
||||||
| `truncation_length` | `2^63 - 1` | Truncates a git branch to X graphemes. |
|
| `truncation_length` | `2^63 - 1` | Truncates a git branch to X graphemes. |
|
||||||
| `truncation_symbol` | `"…"` | The symbol used to indicate a branch name was truncated. You can use `""` for no symbol. |
|
| `truncation_symbol` | `"…"` | The symbol used to indicate a branch name was truncated. You can use `""` for no symbol. |
|
||||||
| `only_attached` | `false` | Only show the branch name when not in a detached HEAD state. |
|
| `only_attached` | `false` | Only show the branch name when not in a detached HEAD state. |
|
||||||
| `disabled` | `false` | Disables the `git_branch` module. |
|
| `disabled` | `false` | Disables the `git_branch` module. |
|
||||||
|
|
||||||
### Variables
|
### Variables
|
||||||
|
|
||||||
| Variable | Example | Description |
|
| Variable | Example | Description |
|
||||||
| -------- | -------- | ---------------------------------------------------------------------------------------------------- |
|
| ----------------| -------- | ---------------------------------------------------------------------------------------------------- |
|
||||||
| branch | `master` | The current branch name, falls back to `HEAD` if there's no current branch (e.g. git detached HEAD). |
|
| branch | `master` | The current branch name, falls back to `HEAD` if there's no current branch (e.g. git detached HEAD). |
|
||||||
| remote | `master` | The remote branch name. |
|
| remote_name | `origin` | The remote name. |
|
||||||
| symbol | | Mirrors the value of option `symbol` |
|
| remote_branch | `master` | The name of the branch tracked on `remote_name`. |
|
||||||
| style\* | | Mirrors the value of option `style` |
|
| symbol | | Mirrors the value of option `symbol` |
|
||||||
|
| style\* | | Mirrors the value of option `style` |
|
||||||
|
|
||||||
\*: This variable can only be used as a part of a style string
|
\*: This variable can only be used as a part of a style string
|
||||||
|
|
||||||
|
@ -171,7 +171,9 @@ impl<'a> Context<'a> {
|
|||||||
.as_ref()
|
.as_ref()
|
||||||
.and_then(|repo| repo.workdir().map(Path::to_path_buf));
|
.and_then(|repo| repo.workdir().map(Path::to_path_buf));
|
||||||
let state = repository.as_ref().map(|repo| repo.state());
|
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 {
|
Ok(Repo {
|
||||||
branch,
|
branch,
|
||||||
root,
|
root,
|
||||||
@ -312,8 +314,14 @@ pub struct Repo {
|
|||||||
/// State
|
/// State
|
||||||
pub state: Option<RepositoryState>,
|
pub state: Option<RepositoryState>,
|
||||||
|
|
||||||
/// Remote branch name
|
/// Remote repository
|
||||||
pub remote: Option<String>,
|
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
|
// 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)
|
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 Ok(head) = repository.head() {
|
||||||
if let Some(local_branch_ref) = head.name() {
|
if let Some(local_branch_ref) = head.name() {
|
||||||
let remote_ref = match repository.branch_upstream_name(local_branch_ref) {
|
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,
|
Err(_) => return None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let remote = remote_ref.split('/').last().map(|r| r.to_owned())?;
|
let mut v = remote_ref.splitn(4, '/');
|
||||||
return Some(remote);
|
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
|
None
|
||||||
|
@ -37,27 +37,33 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
|||||||
let branch_name = repo.branch.as_ref()?;
|
let branch_name = repo.branch.as_ref()?;
|
||||||
let mut graphemes: Vec<&str> = branch_name.graphemes(true).collect();
|
let mut graphemes: Vec<&str> = branch_name.graphemes(true).collect();
|
||||||
|
|
||||||
let mut remote_graphemes: Vec<&str> = Vec::new();
|
let mut remote_branch_graphemes: Vec<&str> = Vec::new();
|
||||||
if let Some(remote_branch) = repo.remote.as_ref() {
|
let mut remote_name_graphemes: Vec<&str> = Vec::new();
|
||||||
remote_graphemes = remote_branch.graphemes(true).collect();
|
if let Some(remote) = repo.remote.as_ref() {
|
||||||
|
if let Some(branch) = &remote.branch {
|
||||||
|
remote_branch_graphemes = branch.graphemes(true).collect()
|
||||||
|
};
|
||||||
|
if let Some(name) = &remote.name {
|
||||||
|
remote_name_graphemes = name.graphemes(true).collect()
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
let trunc_len = len.min(graphemes.len());
|
// Truncate fields if need be
|
||||||
if trunc_len < graphemes.len() {
|
for e in vec![
|
||||||
// The truncation symbol should only be added if we truncate
|
&mut graphemes,
|
||||||
graphemes[trunc_len] = truncation_symbol;
|
&mut remote_branch_graphemes,
|
||||||
graphemes.truncate(trunc_len + 1)
|
&mut remote_name_graphemes,
|
||||||
}
|
] {
|
||||||
|
let trunc_len = len.min(e.len());
|
||||||
let trunc_len = len.min(remote_graphemes.len());
|
if trunc_len < e.len() {
|
||||||
if trunc_len < remote_graphemes.len() {
|
// The truncation symbol should only be added if we truncate
|
||||||
// The truncation symbol should only be added if we truncate
|
e[trunc_len] = truncation_symbol;
|
||||||
remote_graphemes[trunc_len] = truncation_symbol;
|
e.truncate(trunc_len + 1);
|
||||||
remote_graphemes.truncate(trunc_len + 1);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let show_remote = config.always_show_remote
|
let show_remote = config.always_show_remote
|
||||||
|| (!graphemes.eq(&remote_graphemes) && !remote_graphemes.is_empty());
|
|| (!graphemes.eq(&remote_branch_graphemes) && !remote_branch_graphemes.is_empty());
|
||||||
|
|
||||||
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
|
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
|
||||||
formatter
|
formatter
|
||||||
@ -71,9 +77,16 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
|||||||
})
|
})
|
||||||
.map(|variable| match variable {
|
.map(|variable| match variable {
|
||||||
"branch" => Some(Ok(graphemes.concat())),
|
"branch" => Some(Ok(graphemes.concat())),
|
||||||
"remote" => {
|
"remote_branch" => {
|
||||||
if show_remote {
|
if show_remote && !remote_branch_graphemes.is_empty() {
|
||||||
Some(Ok(remote_graphemes.concat()))
|
Some(Ok(remote_branch_graphemes.concat()))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"remote_name" => {
|
||||||
|
if show_remote && !remote_name_graphemes.is_empty() {
|
||||||
|
Some(Ok(remote_name_graphemes.concat()))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user