Path: blob/main/components/gitpod-cli/cmd/git-commit-message-helper.go
2498 views
// Copyright (c) 2025 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package cmd56import (7"context"8"fmt"9"os"10"os/exec"11"time"1213"github.com/gitpod-io/gitpod/gitpod-cli/pkg/gitpod"14log "github.com/sirupsen/logrus"15"github.com/spf13/cobra"16)1718var gitCommitMessageHelperOpts struct {19CommitMessageFile string20}2122func addGitpodTrailer(commitMsgFile string, hostName string) error {23trailerCmd := exec.Command("git", "interpret-trailers",24"--if-exists", "addIfDifferent",25"--trailer", fmt.Sprintf("Tool: gitpod/%s", hostName),26commitMsgFile)2728output, err := trailerCmd.Output()29if err != nil {30return fmt.Errorf("error adding trailer: %w", err)31}3233err = os.WriteFile(commitMsgFile, output, 0644)34if err != nil {35return fmt.Errorf("error writing commit message file: %w", err)36}3738return nil39}4041var gitCommitMessageHelper = &cobra.Command{42Use: "git-commit-message-helper",43Short: "Gitpod's Git commit message helper",44Long: "Automatically adds Tool information to Git commit messages",45Args: cobra.ExactArgs(0),46Hidden: true,47RunE: func(cmd *cobra.Command, args []string) error {48ctx, cancel := context.WithTimeout(cmd.Context(), 5*time.Second)49defer cancel()5051wsInfo, err := gitpod.GetWSInfo(ctx)52if err != nil {53log.WithError(err).Fatal("error getting workspace info")54return nil // don't block commit55}5657if err := addGitpodTrailer(gitCommitMessageHelperOpts.CommitMessageFile, wsInfo.GitpodApi.Host); err != nil {58log.WithError(err).Fatal("failed to add gitpod trailer")59return nil // don't block commit60}6162return nil63},64}6566func init() {67rootCmd.AddCommand(gitCommitMessageHelper)68gitCommitMessageHelper.Flags().StringVarP(&gitCommitMessageHelperOpts.CommitMessageFile, "file", "f", "", "Path to the commit message file")69_ = gitCommitMessageHelper.MarkFlagRequired("file")70}717273