From bae16b525de1f05a7ad125b5f4a8cb8baa7d5fae Mon Sep 17 00:00:00 2001 From: IsaacHorvath Date: Wed, 16 Mar 2022 16:41:49 -0400 Subject: [PATCH] feat(git_branch): add 'ignore_branches' option (#3753) * git-branch: Add option 'ignore_branches' * git-branch: add 'ignore_branches' test * git-branch: comma-separated to toml array * git-branch: update ignore_branches test * git_branch: fix formatting * git-branch: fix formatting again * Initialize ignore_branches as empty Co-authored-by: Thomas O'Donnell * Use iter().any and branch_name as suggested * Fix formatting Co-authored-by: Thomas O'Donnell --- docs/config/README.md | 2 ++ src/configs/git_branch.rs | 2 ++ src/modules/git_branch.rs | 31 +++++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/docs/config/README.md b/docs/config/README.md index 7a160774..3499b261 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -1374,6 +1374,7 @@ The `git_branch` module shows the active branch of the repo in your current dire | `truncation_length` | `2^63 - 1` | Truncates a git branch to `N` graphemes. | | `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. | +| `ignore_branches` | `[]` | A list of names to avoid displaying. Useful for "master" or "main". | | `disabled` | `false` | Disables the `git_branch` module. | ### Variables @@ -1397,6 +1398,7 @@ The `git_branch` module shows the active branch of the repo in your current dire symbol = "🌱 " truncation_length = 4 truncation_symbol = "" +ignore_branches = ["master", "main"] ``` ## Git Commit diff --git a/src/configs/git_branch.rs b/src/configs/git_branch.rs index d9c40c9a..c2032159 100644 --- a/src/configs/git_branch.rs +++ b/src/configs/git_branch.rs @@ -12,6 +12,7 @@ pub struct GitBranchConfig<'a> { pub truncation_symbol: &'a str, pub only_attached: bool, pub always_show_remote: bool, + pub ignore_branches: Vec<&'a str>, pub disabled: bool, } @@ -25,6 +26,7 @@ impl<'a> Default for GitBranchConfig<'a> { truncation_symbol: "…", only_attached: false, always_show_remote: false, + ignore_branches: vec![], disabled: false, } } diff --git a/src/modules/git_branch.rs b/src/modules/git_branch.rs index 9962cb26..715f342a 100644 --- a/src/modules/git_branch.rs +++ b/src/modules/git_branch.rs @@ -37,6 +37,14 @@ pub fn module<'a>(context: &'a Context) -> Option> { let branch_name = repo.branch.as_ref()?; let mut graphemes: Vec<&str> = branch_name.graphemes(true).collect(); + if config + .ignore_branches + .iter() + .any(|ignored| branch_name.eq(ignored)) + { + return None; + } + let mut remote_branch_graphemes: Vec<&str> = Vec::new(); let mut remote_name_graphemes: Vec<&str> = Vec::new(); if let Some(remote) = repo.remote.as_ref() { @@ -365,6 +373,29 @@ mod tests { repo_dir.close() } + #[test] + fn test_ignore_branches() -> io::Result<()> { + let repo_dir = fixture_repo(FixtureProvider::Git)?; + + create_command("git")? + .args(&["checkout", "-b", "test_branch"]) + .current_dir(repo_dir.path()) + .output()?; + + let actual = ModuleRenderer::new("git_branch") + .config(toml::toml! { + [git_branch] + ignore_branches = ["dummy", "test_branch"] + }) + .path(&repo_dir.path()) + .collect(); + + let expected = None; + + assert_eq!(expected, actual); + repo_dir.close() + } + // This test is not possible until we switch to `git status --porcelain` // where we can mock the env for the specific git process. This is because // git2 does not care about our mocking and when we set the real `GIT_DIR`