feat(git_branch): add remote branch name if different than local branch (#1915)

* feat(git_branch): add remote branch name if different than local branch

* feat(git_branch): Implement a more customizable remote branch

* feat(git_branch): Use more explicit API function name

* feat(git_branch): Remove forgotten draft documentation

* feat(git_branch): Set less verbose defaults

* feat(git_branch): Handle case to always display remote

* feat(git_branch): Fix error in rebase operation
This commit is contained in:
Sagittarius-a 2020-11-23 22:07:16 +01:00 committed by GitHub
parent cf297ff25d
commit 688f1b3457
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 4 deletions

View File

@ -1025,6 +1025,7 @@ The `git_branch` module shows the active branch of the repo in your current dire
| Option | Default | Description |
| ------------------- | -------------------------------- | ---------------------------------------------------------------------------------------- |
| `always_show_remote`| `false` | Shows the remote tracking branch name, even if it is equal to the local branch name. |
| `format` | `"on [$symbol$branch]($style) "` | The format for the module. Use `"$branch"` to refer to the current branch name. |
| `symbol` | `" "` | A format string representing the symbol of git branch. |
| `style` | `"bold purple"` | The style for the module. |
@ -1038,6 +1039,7 @@ The `git_branch` module shows the active branch of the repo in your current dire
| Variable | Example | Description |
| -------- | -------- | ---------------------------------------------------------------------------------------------------- |
| 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. |
| symbol | | Mirrors the value of option `symbol` |
| style\* | | Mirrors the value of option `style` |

View File

@ -10,18 +10,20 @@ pub struct GitBranchConfig<'a> {
pub truncation_length: i64,
pub truncation_symbol: &'a str,
pub only_attached: bool,
pub always_show_remote: bool,
pub disabled: bool,
}
impl<'a> RootModuleConfig<'a> for GitBranchConfig<'a> {
fn new() -> Self {
GitBranchConfig {
format: "on [$symbol$branch]($style) ",
format: "on [$symbol$branch]($style)(:[$remote]($style)) ",
symbol: "",
style: "bold purple",
truncation_length: std::i64::MAX,
truncation_symbol: "",
only_attached: false,
always_show_remote: false,
disabled: false,
}
}

View File

@ -171,11 +171,12 @@ 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));
Ok(Repo {
branch,
root,
state,
remote,
})
})
}
@ -310,6 +311,9 @@ pub struct Repo {
/// State
pub state: Option<RepositoryState>,
/// Remote branch name
pub remote: Option<String>,
}
// A struct of Criteria which will be used to verify current PathBuf is
@ -376,6 +380,21 @@ fn get_current_branch(repository: &Repository) -> Option<String> {
shorthand.map(std::string::ToString::to_string)
}
fn get_remote_branch(repository: &Repository) -> Option<String> {
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) {
Ok(remote_ref) => remote_ref.as_str()?.to_owned(),
Err(_) => return None,
};
let remote = remote_ref.split('/').last().map(|r| r.to_owned())?;
return Some(remote);
}
}
None
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Shell {
Bash,

View File

@ -35,16 +35,30 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
};
let branch_name = repo.branch.as_ref()?;
let mut graphemes: Vec<&str> = branch_name.graphemes(true).collect();
let trunc_len = len.min(graphemes.len());
let mut remote_graphemes: Vec<&str> = Vec::new();
if let Some(remote_branch) = repo.remote.as_ref() {
remote_graphemes = remote_branch.graphemes(true).collect();
}
let trunc_len = len.min(graphemes.len());
if trunc_len < graphemes.len() {
// The truncation symbol should only be added if we truncate
graphemes[trunc_len] = truncation_symbol;
graphemes.truncate(trunc_len + 1)
}
let trunc_len = len.min(remote_graphemes.len());
if trunc_len < remote_graphemes.len() {
// The truncation symbol should only be added if we truncate
remote_graphemes[trunc_len] = truncation_symbol;
remote_graphemes.truncate(trunc_len + 1);
}
let show_remote = config.always_show_remote
|| (!graphemes.eq(&remote_graphemes) && !remote_graphemes.is_empty());
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
formatter
.map_meta(|var, _| match var {
@ -57,6 +71,13 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
})
.map(|variable| match variable {
"branch" => Some(Ok(graphemes.concat())),
"remote" => {
if show_remote {
Some(Ok(remote_graphemes.concat()))
} else {
None
}
}
_ => None,
})
.parse(None)