Skip to content

Commit

Permalink
feat: Add only_detached option to git_commit module (#706)
Browse files Browse the repository at this point in the history
  • Loading branch information
ybc37 committed Dec 15, 2019
1 parent 7c05412 commit a885fc6
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
15 changes: 8 additions & 7 deletions docs/config/README.md
Expand Up @@ -461,13 +461,14 @@ 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` | `false` | Only show git commit hash when in detached HEAD state |
| `disabled` | `true` | Disables the `git_commit` module. |

### Example

Expand Down
2 changes: 2 additions & 0 deletions 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,6 +23,7 @@ impl<'a> RootModuleConfig<'a> for GitCommitConfig<'a> {
prefix: "(",
suffix: ") ",
style: Color::Green.bold(),
only_detached: false,
disabled: true,
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/modules/git_commit.rs
Expand Up @@ -27,6 +27,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
24 changes: 24 additions & 0 deletions tests/testsuite/git_commit.rs
Expand Up @@ -66,3 +66,27 @@ fn test_render_commit_hash_len_override() -> io::Result<()> {
assert_eq!(expected, actual);
Ok(())
}

#[test]
fn test_render_commit_hash_only_detached() -> io::Result<()> {
let repo_dir = common::create_fixture_repo()?;

Command::new("git")
.args(&["checkout", "@~1"])
.current_dir(repo_dir.as_path());

let output = common::render_module("git_commit")
.use_config(toml::toml! {
[git_commit]
disabled = false
only_detached = true
})
.arg("--path")
.arg(repo_dir)
.output()?;

let actual = String::from_utf8(output.stdout).unwrap();

assert_eq!("", actual);
Ok(())
}

0 comments on commit a885fc6

Please sign in to comment.