Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-cli/cmd/git-commit-message-helper.go
2498 views
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
"context"
9
"fmt"
10
"os"
11
"os/exec"
12
"time"
13
14
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/gitpod"
15
log "github.com/sirupsen/logrus"
16
"github.com/spf13/cobra"
17
)
18
19
var gitCommitMessageHelperOpts struct {
20
CommitMessageFile string
21
}
22
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
42
var gitCommitMessageHelper = &cobra.Command{
43
Use: "git-commit-message-helper",
44
Short: "Gitpod's Git commit message helper",
45
Long: "Automatically adds Tool information to Git commit messages",
46
Args: cobra.ExactArgs(0),
47
Hidden: true,
48
RunE: func(cmd *cobra.Command, args []string) error {
49
ctx, cancel := context.WithTimeout(cmd.Context(), 5*time.Second)
50
defer cancel()
51
52
wsInfo, err := gitpod.GetWSInfo(ctx)
53
if err != nil {
54
log.WithError(err).Fatal("error getting workspace info")
55
return nil // don't block commit
56
}
57
58
if err := addGitpodTrailer(gitCommitMessageHelperOpts.CommitMessageFile, wsInfo.GitpodApi.Host); err != nil {
59
log.WithError(err).Fatal("failed to add gitpod trailer")
60
return nil // don't block commit
61
}
62
63
return nil
64
},
65
}
66
67
func init() {
68
rootCmd.AddCommand(gitCommitMessageHelper)
69
gitCommitMessageHelper.Flags().StringVarP(&gitCommitMessageHelperOpts.CommitMessageFile, "file", "f", "", "Path to the commit message file")
70
_ = gitCommitMessageHelper.MarkFlagRequired("file")
71
}
72
73