From 672f108d224bf43dd70d6c91ce72bfc51fbc5c5d Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Fri, 25 Aug 2023 11:02:05 +0200 Subject: [PATCH] feat(git): add SHA1 to commit metadata --- src/segments/git.go | 5 ++++- src/segments/git_test.go | 4 +++- website/docs/segments/git.mdx | 1 + 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/segments/git.go b/src/segments/git.go index df120ab9..f99b9d36 100644 --- a/src/segments/git.go +++ b/src/segments/git.go @@ -21,6 +21,7 @@ type Commit struct { Committer *User Subject string Timestamp time.Time + Sha string } type User struct { @@ -195,7 +196,7 @@ func (g *Git) Commit() *Commit { Author: &User{}, Committer: &User{}, } - commitBody := g.getGitCommandOutput("log", "-1", "--pretty=format:an:%an%nae:%ae%ncn:%cn%nce:%ce%nat:%at%nsu:%s") + commitBody := g.getGitCommandOutput("log", "-1", "--pretty=format:an:%an%nae:%ae%ncn:%cn%nce:%ce%nat:%at%nsu:%s%nha:%H") splitted := strings.Split(strings.TrimSpace(commitBody), "\n") for _, line := range splitted { line = strings.TrimSpace(line) @@ -219,6 +220,8 @@ func (g *Git) Commit() *Commit { } case "su:": g.commit.Subject = line + case "ha:": + g.commit.Sha = line } } return g.commit diff --git a/src/segments/git_test.go b/src/segments/git_test.go index 8f112bab..ecbcf593 100644 --- a/src/segments/git_test.go +++ b/src/segments/git_test.go @@ -942,6 +942,7 @@ func TestGitCommit(t *testing.T) { ce:jan@ohmyposh.dev at:1673176335 su:docs(error): you can't use cross segment properties + ha:1234567891011121314 `, Expected: &Commit{ Author: &User{ @@ -954,6 +955,7 @@ func TestGitCommit(t *testing.T) { }, Subject: "docs(error): you can't use cross segment properties", Timestamp: time.Unix(1673176335, 0), + Sha: "1234567891011121314", }, }, { @@ -997,7 +999,7 @@ func TestGitCommit(t *testing.T) { for _, tc := range cases { env := new(mock.MockedEnvironment) - env.MockGitCommand("", tc.Output, "log", "-1", "--pretty=format:an:%an%nae:%ae%ncn:%cn%nce:%ce%nat:%at%nsu:%s") + env.MockGitCommand("", tc.Output, "log", "-1", "--pretty=format:an:%an%nae:%ae%ncn:%cn%nce:%ce%nat:%at%nsu:%s%nha:%H") g := &Git{ scm: scm{ env: env, diff --git a/website/docs/segments/git.mdx b/website/docs/segments/git.mdx index c6bd539c..b084d0ed 100644 --- a/website/docs/segments/git.mdx +++ b/website/docs/segments/git.mdx @@ -183,6 +183,7 @@ Local changes use the following syntax: | `.Committer` | `User` | the comitter of the commit (see below) | | `.Subject` | `string` | the commit subject | | `.Timestamp` | `time.Time` | the commit timestamp | +| `.Sha` | `string` | the commit SHA1 | ### User