pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
    let mut module = context.new_module("git_commit");
    let config: GitCommitConfig = GitCommitConfig::try_load(module.config);

    let repo = context.get_repo().ok()?;
    let repo_root = repo.root.as_ref()?;
    let git_repo = Repository::open(repo_root).ok()?;

    let is_detached = git_repo.head_detached().ok()?;
    if config.only_detached && !is_detached {
        return None;
    };

    let git_head = git_repo.head().ok()?;
    let head_commit = git_head.peel_to_commit().ok()?;
    let commit_oid = head_commit.id();

    let mut parsed;

    parsed = StringFormatter::new(config.format).and_then(|formatter| {
        formatter
            .map_style(|variable| match variable {
                "style" => Some(Ok(config.style)),
                _ => None,
            })
            .map(|variable| match variable {
                "hash" => Some(Ok(id_to_hex_abbrev(
                    commit_oid.as_bytes(),
                    config.commit_hash_length,
                ))),
                _ => None,
            })
            .parse(None)
    });

    if !config.tag_disabled {
        // Let's get repo tags names
        let tag_names = git_repo.tag_names(None).ok()?;
        let tag_and_refs = tag_names.iter().flat_map(|name| {
            let full_tag = format!("refs/tags/{}", name.unwrap());
            git_repo
                .find_reference(&full_tag)
                .map(|reference| (String::from(name.unwrap()), reference))
        });

        let mut tag_name = String::new();
        // Let's check if HEAD has some tag. If several, only gets first...
        for (name, reference) in tag_and_refs {
            if commit_oid == reference.peel_to_commit().ok()?.id() {
                tag_name = name;
                break;
            }
        }
        // If we have tag...
        if !tag_name.is_empty() {
            parsed = StringFormatter::new(config.format).and_then(|formatter| {
                formatter
                    .map_style(|variable| match variable {
                        "style" => Some(Ok(config.style)),
                        _ => None,
                    })
                    .map(|variable| match variable {
                        "hash" => Some(Ok(id_to_hex_abbrev(
                            commit_oid.as_bytes(),
                            config.commit_hash_length,
                        ))),
                        _ => None,
                    })
                    .map(|variable| match variable {
                        "tag" => Some(Ok(format!(" {}{}", &config.tag_symbol, &tag_name))),
                        _ => None,
                    })
                    .parse(None)
            });
        }
    };

    module.set_segments(match parsed {
        Ok(segments) => segments,
        Err(error) => {
            log::warn!("Error in module `git_commit`:\n{}", error);
            return None;
        }
    });

    Some(module)
}
/// Creates a module with the Git commit in the current directory
///
/// Will display the commit hash if the current directory is a git repo
Creates a module with the Git commit in the current directory
Will display the commit hash if the current directory is a git repo
Creates a module with the Git commit in the current directory Will display the commit hash 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_commit");
    let config: GitCommitConfig = GitCommitConfig::try_load(module.config);
    let repo = context.get_repo().ok()?;
    let repo_root = repo.root.as_ref()?;
    let git_repo = Repository::open(repo_root).ok()?;
    let is_detached = git_repo.head_detached().ok()?;
    if config.only_detached && !is_detached {
        return None;
    };
    let git_head = git_repo.head().ok()?;
    let head_commit = git_head.peel_to_commit().ok()?;
    let commit_oid = head_commit.id();
    let mut parsed;
    parsed = StringFormatter::new(config.format).and_then(|formatter| {
        formatter
            .map_style(|variable| match variable {
                "style" => Some(Ok(config.style)),
                _ => None,
            })
            .map(|variable| match variable {
                "hash" => Some(Ok(id_to_hex_abbrev(
                    commit_oid.as_bytes(),
                    config.commit_hash_length,
                ))),
                _ => None,
            })
            .parse(None)
    });
    if !config.tag_disabled {
        // Let's get repo tags names
        let tag_names = git_repo.tag_names(None).ok()?;
        let tag_and_refs = tag_names.iter().flat_map(|name| {
            let full_tag = format!("refs/tags/{}", name.unwrap());
            git_repo
                .find_reference(&full_tag)
                .map(|reference| (String::from(name.unwrap()), reference))
        });
        let mut tag_name = String::new();
        // Let's check if HEAD has some tag. If several, only gets first...
        for (name, reference) in tag_and_refs {
            if commit_oid == reference.peel_to_commit().ok()?.id() {
                tag_name = name;
                break;
            }
        }
        // If we have tag...
        if !tag_name.is_empty() {
            parsed = StringFormatter::new(config.format).and_then(|formatter| {
                formatter
                    .map_style(|variable| match variable {
                        "style" => Some(Ok(config.style)),
                        _ => None,
                    })
                    .map(|variable| match variable {
                        "hash" => Some(Ok(id_to_hex_abbrev(
                            commit_oid.as_bytes(),
                            config.commit_hash_length,
                        ))),
                        _ => None,
                    })
                    .map(|variable| match variable {
                        "tag" => Some(Ok(format!(" {}{}", &config.tag_symbol, &tag_name))),
                        _ => None,
                    })
                    .parse(None)
            });
        }
    };
    module.set_segments(match parsed {
        Ok(segments) => segments,
        Err(error) => {
            log::warn!("Error in module `git_commit`:\n{}", error);
            return None;
        }
    });
    Some(module)
}
pub fn module < ' a > ( context : & ' a Context ) -> Option < Module < ' a > > { let mut module = context . new_module ( " " ) ; let config : GitCommitConfig = GitCommitConfig :: try_load ( module . config ) ; let repo = context . get_repo ( ) . ok ( ) ? ; let repo_root = repo . root . as_ref ( ) ? ; let git_repo = Repository :: open ( repo_root ) . ok ( ) ? ; let is_detached = git_repo . head_detached ( ) . ok ( ) ? ; if config . only_detached && ! is_detached { return None ; } ; let git_head = git_repo . head ( ) . ok ( ) ? ; let head_commit = git_head . peel_to_commit ( ) . ok ( ) ? ; let commit_oid = head_commit . id ( ) ; let mut parsed ; parsed = StringFormatter :: new ( config . format ) . and_then ( | formatter | { formatter . map_style ( | variable | match variable { " " => Some ( Ok ( config . style ) ) , _ => None , } ) . map ( | variable | match variable { " " => Some ( Ok ( id_to_hex_abbrev ( commit_oid . as_bytes ( ) , config . commit_hash_length , ) ) ) , _ => None , } ) . parse ( None ) } ) ; if ! config . tag_disabled { // Let's get repo tags names let tag_names = git_repo . tag_names ( None ) . ok ( ) ? ; let tag_and_refs = tag_names . iter ( ) . flat_map ( | name | { let full_tag = format ! ( " " name unwrap ( ) ) ; git_repo . find_reference ( & full_tag ) . map ( | reference | ( String :: from ( name . unwrap ( ) ) , reference ) ) } ) ; let mut tag_name = String :: new ( ) ; // Let's check if HEAD has some tag. If several, only gets first... for ( name , reference ) in tag_and_refs { if commit_oid == reference . peel_to_commit ( ) . ok ( ) ? . id ( ) { tag_name = name ; break ; } } // If we have tag... if ! tag_name . is_empty ( ) { parsed = StringFormatter :: new ( config . format ) . and_then ( | formatter | { formatter . map_style ( | variable | match variable { " " => Some ( Ok ( config . style ) ) , _ => None , } ) . map ( | variable | match variable { " " => Some ( Ok ( id_to_hex_abbrev ( commit_oid . as_bytes ( ) , config . commit_hash_length , ) ) ) , _ => None , } ) . map ( | variable | match variable { " " => Some ( Ok ( format ! ( " " config tag_symbol tag_name ) ) ) , _ => None , } ) . parse ( None ) } ) ; } } ; module . set_segments ( match parsed { Ok ( segments ) => segments , Err ( error ) => { log :: warn ! ( " \n " error ) ; return None ; } } ) ; Some ( module ) }
Creates a module with the Git commit in the current directory
Will display the commit hash if the current directory is a git repo
Creates a module with the Git commit in the current directory Will display the commit hash if the current directory is a git repo
context
&'a Context