From 7ccd1c8e6dadd3700b72839eb9038d7635030c01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Tron=C3=AD=C4=8Dek?= Date: Tue, 21 Jan 2025 13:08:11 +0000 Subject: [PATCH] `gp git-commit-message-helper` to use `git interpret-trailers` --- .../cmd/git-commit-message-helper.go | 27 +++++++++---------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/components/gitpod-cli/cmd/git-commit-message-helper.go b/components/gitpod-cli/cmd/git-commit-message-helper.go index a19f8c9a66510d..28d64b0cbf09d9 100644 --- a/components/gitpod-cli/cmd/git-commit-message-helper.go +++ b/components/gitpod-cli/cmd/git-commit-message-helper.go @@ -8,7 +8,7 @@ import ( "context" "fmt" "os" - "strings" + "os/exec" "time" "github.com/gitpod-io/gitpod/gitpod-cli/pkg/gitpod" @@ -32,25 +32,22 @@ var gitCommitMessageHelper = &cobra.Command{ wsInfo, err := gitpod.GetWSInfo(ctx) if err != nil { - return err + log.WithError(err).Fatal("error getting workspace info") + return nil // don't block commit } - content, err := os.ReadFile(gitCommitMessageHelperOpts.CommitMessageFile) - if err != nil { - log.WithError(err).Fatal("error reading commit message file") - return err - } + trailerCmd := exec.Command("git", "interpret-trailers", + "--if-exists", "addIfDifferent", + "--trailer", fmt.Sprintf("Tool: gitpod/%s", wsInfo.GitpodApi.Host), + gitCommitMessageHelperOpts.CommitMessageFile) - toolAttribution := fmt.Sprintf("Tool: gitpod/%s", wsInfo.GitpodApi.Host) - - msg := string(content) - if strings.Contains(msg, toolAttribution) { - return nil + output, err := trailerCmd.Output() + if err != nil { + log.WithError(err).Fatal("error adding trailer") + return nil // don't block commit } - newMsg := fmt.Sprintf("%s\n\n%s", msg, toolAttribution) - - err = os.WriteFile(gitCommitMessageHelperOpts.CommitMessageFile, []byte(newMsg), 0644) + err = os.WriteFile(gitCommitMessageHelperOpts.CommitMessageFile, output, 0644) if err != nil { log.WithError(err).Fatal("error writing commit message file") return err