Repository: starship/starship
File: src/modules/git_branch.rs (Rust)

Code before:
///
/// Will display the branch name if the current directory is a git repo
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
    let mut module = context.new_module("git_branch");
    let config = GitBranchConfig::try_load(module.config);

    let truncation_symbol = get_first_grapheme(config.truncation_symbol);

    let len = if config.truncation_length <= 0 {
        log::warn!(
            "\"truncation_length\" should be a positive value, found {}",
            config.truncation_length
        );
        usize::MAX
    } else {
        config.truncation_length as usize
    };

    let repo = context.get_repo().ok()?;

    let gix_repo = repo.open();
    if config.ignore_bare_repo && gix_repo.is_bare() {
        return None;
    }

    if config.only_attached && repo.open().head().ok()?.is_detached() {
        return None;
    }

    let branch_name = repo.branch.as_deref().unwrap_or("HEAD");
    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() {
        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();
        }
    }


Review comment (suggestion):
Let's reuse the repo in the following line:
```suggestion
    if config.only_attached && gix_repo.head().ok()?.is_detached() {
```

Code after:
///
/// Will display the branch name if the current directory is a git repo
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
    let mut module = context.new_module("git_branch");
    let config = GitBranchConfig::try_load(module.config);

    let truncation_symbol = get_first_grapheme(config.truncation_symbol);

    let len = if config.truncation_length <= 0 {
        log::warn!(
            "\"truncation_length\" should be a positive value, found {}",
            config.truncation_length
        );
        usize::MAX
    } else {
        config.truncation_length as usize
    };

    let repo = context.get_repo().ok()?;

    let gix_repo = repo.open();
    if config.ignore_bare_repo && gix_repo.is_bare() {
        return None;
    }

    if config.only_attached && gix_repo.head().ok()?.is_detached() {
        return None;
    }

    let branch_name = repo.branch.as_deref().unwrap_or("HEAD");
    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() {
        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();
        }
    }