feat(git_commit): Show the hash of commits when detached HEAD (#738)

This commit is contained in:
nesmyslny 2020-02-12 20:56:29 +01:00 committed by GitHub
parent a485fd6896
commit 0312c7b91e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 66 additions and 21 deletions

View File

@ -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
```

View File

@ -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,
}
}
}

View File

@ -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()
@ -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();

View File

@ -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(())
}