Skip to content

Commit

Permalink
feat(git_commit): Show the hash of commits when detached HEAD (starsh…
Browse files Browse the repository at this point in the history
  • Loading branch information
nesmyslny authored and dagbrown committed Jul 15, 2021
1 parent edb23d2 commit 9c2058a
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 21 deletions.
23 changes: 8 additions & 15 deletions docs/config/README.md
Expand Up @@ -493,30 +493,23 @@ 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

```toml
# ~/.config/starship.toml

[git_commit]
disabled = false
commit_hash_length = 4
```

Expand Down
4 changes: 3 additions & 1 deletion src/configs/git_commit.rs
Expand Up @@ -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,
}

Expand All @@ -22,7 +23,8 @@ impl<'a> RootModuleConfig<'a> for GitCommitConfig<'a> {
prefix: "(",
suffix: ") ",
style: Color::Green.bold(),
disabled: true,
only_detached: true,
disabled: false,
}
}
}
8 changes: 5 additions & 3 deletions src/modules/git_commit.rs
Expand Up @@ -9,9 +9,6 @@ use crate::configs::git_commit::GitCommitConfig;
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let mut module = context.new_module("git_commit");
let config = GitCommitConfig::try_load(module.config);
if config.disabled {
return None;
};

module
.get_prefix()
Expand All @@ -27,6 +24,11 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
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();
Expand Down
52 changes: 50 additions & 2 deletions tests/testsuite/git_commit.rs
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand All @@ -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(())
}

0 comments on commit 9c2058a

Please sign in to comment.