Repository: sharkdp/bat
File: src/syntax_mapping.rs (Rust)

Code before:
            "*.network",
            "*.nspawn",
            "*.path",
            "*.service",
            "*.scope",
            "*.slice",
            "*.socket",
            "*.swap",
            "*.target",
            "*.timer",
        ] {
            mapping.insert(glob, MappingTarget::MapTo("INI")).unwrap();
        }

        // pacman hooks
        mapping
            .insert("*.hook", MappingTarget::MapTo("INI"))
            .unwrap();

        // global git config files
        // `$XDG_CONFIG_HOME/git`, or `$HOME/.config/git` if `$XDG_CONFIG_HOME` is not set or empty
        // see https://git-scm.com/docs/git-config#FILES
        if let Some(git_config_path) = std::env::var_os("XDG_CONFIG_HOME")
            .and_then(|val| (!val.is_empty()).then(|| val))
            .map(PathBuf::from)
            .or_else(|| std::env::var_os("HOME").map(|home| Path::new(&home).join(".config")))
            .map(|mut config_home| {
                config_home.push("git");
                config_home
            })
        {
            mapping
                .insert(
                    &git_config_path.join("config").to_string_lossy(),
                    MappingTarget::MapTo("Git Config"),
                )
                .ok();

            mapping
                .insert(
                    &git_config_path.join("ignore").to_string_lossy(),
                    MappingTarget::MapTo("Git Ignore"),
                )
                .ok();

            mapping
                .insert(
                    &git_config_path.join("attributes").to_string_lossy(),
                    MappingTarget::MapTo("Git Attributes"),
                )
                .ok();

Review comment (refactor):
Thanks you for taking the time to open a PR!

I think we want to highlight `$HOME/.config/git/config` regardless of what `XDG_CONFIG_HOME` is set to though, right? I think it is perfectly reasonable to assume that the former is a git file even if the latter is set.

So it seems to me like you should move the code inside the if to a helper function and then apply it to both the possible paths to `.../git/config`.

Code after:
            "*.network",
            "*.nspawn",
            "*.path",
            "*.service",
            "*.scope",
            "*.slice",
            "*.socket",
            "*.swap",
            "*.target",
            "*.timer",
        ] {
            mapping.insert(glob, MappingTarget::MapTo("INI")).unwrap();
        }

        // pacman hooks
        mapping
            .insert("*.hook", MappingTarget::MapTo("INI"))
            .unwrap();

        // Global git config files rooted in `$XDG_CONFIG_HOME/git/` or `$HOME/.config/git/`
        // See e.g. https://git-scm.com/docs/git-config#FILES
        if let Some(xdg_config_home) =
            std::env::var_os("XDG_CONFIG_HOME").filter(|val| !val.is_empty())
        {
            insert_git_config_global(&mut mapping, &xdg_config_home);
        }
        if let Some(default_config_home) = std::env::var_os("HOME")
            .filter(|val| !val.is_empty())
            .map(|home| Path::new(&home).join(".config"))
        {
            insert_git_config_global(&mut mapping, &default_config_home);
        }

        fn insert_git_config_global(mapping: &mut SyntaxMapping, config_home: impl AsRef<Path>) {
            let git_config_path = config_home.as_ref().join("git");

            mapping
                .insert(
                    &git_config_path.join("config").to_string_lossy(),
                    MappingTarget::MapTo("Git Config"),
                )
                .ok();

            mapping
                .insert(
                    &git_config_path.join("ignore").to_string_lossy(),
                    MappingTarget::MapTo("Git Ignore"),
                )
                .ok();

            mapping