From 9c2058a8dc9671fe67d0595ae263df599efe09a7 Mon Sep 17 00:00:00 2001 From: nesmyslny Date: Wed, 12 Feb 2020 20:56:29 +0100 Subject: [PATCH] feat(git_commit): Show the hash of commits when detached HEAD (#738) --- docs/config/README.md | 23 ++++++---------- src/configs/git_commit.rs | 4 ++- src/modules/git_commit.rs | 8 ++++-- tests/testsuite/git_commit.rs | 52 +++++++++++++++++++++++++++++++++-- 4 files changed, 66 insertions(+), 21 deletions(-) diff --git a/docs/config/README.md b/docs/config/README.md index 83da25d2e782..b150ad901300 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -493,22 +493,16 @@ truncation_symbol = "" The `git_commit` module shows the current commit hash of the repo in your current directory. -::: tip - -This module is disabled by default. -To enable it, set `disabled` to `false` in your configuration file. - -::: - ### Options -| Variable | Default | Description | -| -------------------- | -------------- | ------------------------------------------------ | -| `commit_hash_length` | `7` | The length of the displayed git commit hash. | -| `prefix` | `"("` | Prefix to display immediately before git commit. | -| `suffix` | `")"` | Suffix to display immediately after git commit. | -| `style` | `"bold green"` | The style for the module. | -| `disabled` | `true` | Disables the `git_commit` module. | +| Variable | Default | Description | +| -------------------- | -------------- | ----------------------------------------------------- | +| `commit_hash_length` | `7` | The length of the displayed git commit hash. | +| `prefix` | `"("` | Prefix to display immediately before git commit. | +| `suffix` | `")"` | Suffix to display immediately after git commit. | +| `style` | `"bold green"` | The style for the module. | +| `only_detached` | `true` | Only show git commit hash when in detached HEAD state | +| `disabled` | `false` | Disables the `git_commit` module. | ### Example @@ -516,7 +510,6 @@ To enable it, set `disabled` to `false` in your configuration file. # ~/.config/starship.toml [git_commit] -disabled = false commit_hash_length = 4 ``` diff --git a/src/configs/git_commit.rs b/src/configs/git_commit.rs index d6f65fdcb3db..2efe0e3fb64e 100644 --- a/src/configs/git_commit.rs +++ b/src/configs/git_commit.rs @@ -10,6 +10,7 @@ pub struct GitCommitConfig<'a> { pub prefix: &'a str, pub suffix: &'a str, pub style: Style, + pub only_detached: bool, pub disabled: bool, } @@ -22,7 +23,8 @@ impl<'a> RootModuleConfig<'a> for GitCommitConfig<'a> { prefix: "(", suffix: ") ", style: Color::Green.bold(), - disabled: true, + only_detached: true, + disabled: false, } } } diff --git a/src/modules/git_commit.rs b/src/modules/git_commit.rs index cacfa39821f6..36e68913de35 100644 --- a/src/modules/git_commit.rs +++ b/src/modules/git_commit.rs @@ -9,9 +9,6 @@ use crate::configs::git_commit::GitCommitConfig; pub fn module<'a>(context: &'a Context) -> Option> { let mut module = context.new_module("git_commit"); let config = GitCommitConfig::try_load(module.config); - if config.disabled { - return None; - }; module .get_prefix() @@ -27,6 +24,11 @@ pub fn module<'a>(context: &'a Context) -> Option> { 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(); diff --git a/tests/testsuite/git_commit.rs b/tests/testsuite/git_commit.rs index c7dd50bbe683..fdae075ab908 100644 --- a/tests/testsuite/git_commit.rs +++ b/tests/testsuite/git_commit.rs @@ -19,7 +19,7 @@ fn test_render_commit_hash() -> io::Result<()> { let output = common::render_module("git_commit") .use_config(toml::toml! { [git_commit] - disabled = false + only_detached = false }) .arg("--path") .arg(repo_dir) @@ -50,7 +50,7 @@ fn test_render_commit_hash_len_override() -> io::Result<()> { let output = common::render_module("git_commit") .use_config(toml::toml! { [git_commit] - disabled = false + only_detached = false commit_hash_length = 14 }) .arg("--path") @@ -66,3 +66,51 @@ fn test_render_commit_hash_len_override() -> io::Result<()> { assert_eq!(expected, actual); Ok(()) } + +#[test] +fn test_render_commit_hash_only_detached_on_branch() -> io::Result<()> { + let repo_dir = common::create_fixture_repo()?; + + let output = common::render_module("git_commit") + .arg("--path") + .arg(repo_dir) + .output()?; + + let actual = String::from_utf8(output.stdout).unwrap(); + + assert_eq!("", actual); + Ok(()) +} + +#[test] +fn test_render_commit_hash_only_detached_on_detached() -> io::Result<()> { + let repo_dir = common::create_fixture_repo()?; + + Command::new("git") + .args(&["checkout", "@~1"]) + .current_dir(repo_dir.as_path()) + .output()?; + + let mut git_output = Command::new("git") + .args(&["rev-parse", "HEAD"]) + .current_dir(repo_dir.as_path()) + .output()? + .stdout; + git_output.truncate(7); + let expected_hash = str::from_utf8(&git_output).unwrap(); + + let output = common::render_module("git_commit") + .arg("--path") + .arg(repo_dir) + .output()?; + + let actual = String::from_utf8(output.stdout).unwrap(); + + let expected = Color::Green + .bold() + .paint(format!("({}) ", expected_hash)) + .to_string(); + + assert_eq!(expected, actual); + Ok(()) +}