Skip to content

Commit bab3949

Browse files
committed
Test it!
1 parent 7ccd1c8 commit bab3949

File tree

2 files changed

+95
-14
lines changed

2 files changed

+95
-14
lines changed

components/gitpod-cli/cmd/git-commit-message-helper.go

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,25 @@ var gitCommitMessageHelperOpts struct {
2020
CommitMessageFile string
2121
}
2222

23+
func addGitpodTrailer(commitMsgFile string, hostName string) error {
24+
trailerCmd := exec.Command("git", "interpret-trailers",
25+
"--if-exists", "addIfDifferent",
26+
"--trailer", fmt.Sprintf("Tool: gitpod/%s", hostName),
27+
commitMsgFile)
28+
29+
output, err := trailerCmd.Output()
30+
if err != nil {
31+
return fmt.Errorf("error adding trailer: %w", err)
32+
}
33+
34+
err = os.WriteFile(commitMsgFile, output, 0644)
35+
if err != nil {
36+
return fmt.Errorf("error writing commit message file: %w", err)
37+
}
38+
39+
return nil
40+
}
41+
2342
var gitCommitMessageHelper = &cobra.Command{
2443
Use: "git-commit-message-helper",
2544
Short: "Gitpod's Git commit message helper",
@@ -36,23 +55,11 @@ var gitCommitMessageHelper = &cobra.Command{
3655
return nil // don't block commit
3756
}
3857

39-
trailerCmd := exec.Command("git", "interpret-trailers",
40-
"--if-exists", "addIfDifferent",
41-
"--trailer", fmt.Sprintf("Tool: gitpod/%s", wsInfo.GitpodApi.Host),
42-
gitCommitMessageHelperOpts.CommitMessageFile)
43-
44-
output, err := trailerCmd.Output()
45-
if err != nil {
46-
log.WithError(err).Fatal("error adding trailer")
58+
if err := addGitpodTrailer(gitCommitMessageHelperOpts.CommitMessageFile, wsInfo.GitpodApi.Host); err != nil {
59+
log.WithError(err).Fatal("failed to add gitpod trailer")
4760
return nil // don't block commit
4861
}
4962

50-
err = os.WriteFile(gitCommitMessageHelperOpts.CommitMessageFile, output, 0644)
51-
if err != nil {
52-
log.WithError(err).Fatal("error writing commit message file")
53-
return err
54-
}
55-
5663
return nil
5764
},
5865
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright (c) 2025 Gitpod GmbH. All rights reserved.
2+
// Licensed under the GNU Affero General Public License (AGPL).
3+
// See License.AGPL.txt in the project root for license information.
4+
5+
package cmd
6+
7+
import (
8+
"os"
9+
"testing"
10+
11+
"github.com/google/go-cmp/cmp"
12+
)
13+
14+
func TestAddGitpodTrailer(t *testing.T) {
15+
tests := []struct {
16+
Name string
17+
CommitMsg string
18+
HostName string
19+
Expected string
20+
ExpectError bool
21+
}{
22+
{
23+
Name: "adds trailer to simple message",
24+
CommitMsg: "Initial commit",
25+
HostName: "gitpod.io",
26+
Expected: "Initial commit\n\nTool: gitpod/gitpod.io\n",
27+
ExpectError: false,
28+
},
29+
{
30+
Name: "doesn't duplicate existing trailer",
31+
CommitMsg: "Initial commit\n\nTool: gitpod/gitpod.io\n",
32+
HostName: "gitpod.io",
33+
Expected: "Initial commit\n\nTool: gitpod/gitpod.io\n",
34+
ExpectError: false,
35+
},
36+
{
37+
Name: "preserves other trailers",
38+
CommitMsg: "Initial commit\n\nSigned-off-by: Kyle <[email protected]>\n",
39+
HostName: "gitpod.io",
40+
Expected: "Initial commit\n\nSigned-off-by: Kyle <[email protected]>\nTool: gitpod/gitpod.io\n",
41+
ExpectError: false,
42+
},
43+
}
44+
45+
for _, tt := range tests {
46+
t.Run(tt.Name, func(t *testing.T) {
47+
tmpfile, err := os.CreateTemp("", "commit-msg-*")
48+
if err != nil {
49+
t.Fatal(err)
50+
}
51+
defer os.Remove(tmpfile.Name())
52+
53+
if err := os.WriteFile(tmpfile.Name(), []byte(tt.CommitMsg), 0644); err != nil {
54+
t.Fatal(err)
55+
}
56+
57+
err = addGitpodTrailer(tmpfile.Name(), tt.HostName)
58+
if (err != nil) != tt.ExpectError {
59+
t.Errorf("addGitpodTrailer() error = %v, wantErr %v", err, tt.ExpectError)
60+
return
61+
}
62+
63+
got, err := os.ReadFile(tmpfile.Name())
64+
if err != nil {
65+
t.Fatal(err)
66+
}
67+
68+
equal := cmp.Equal(string(got), tt.Expected)
69+
if !equal {
70+
t.Fatalf(`Detected git command info was incorrect, got: %v, expected: %v.`, string(got), tt.Expected)
71+
}
72+
})
73+
}
74+
}

0 commit comments

Comments
 (0)