From 8f8897dc8b964b6116f737c6e78ecd55af0f90dd Mon Sep 17 00:00:00 2001 From: Oliver Powell Date: Wed, 18 Oct 2023 14:43:31 -0300 Subject: [PATCH] fix(util): check if git command exists when setting release (#737) --- util.go | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/util.go b/util.go index 47f814850..2d2ee4ca1 100644 --- a/util.go +++ b/util.go @@ -78,24 +78,28 @@ func defaultRelease() (release string) { // v1.0.1-0-g9de4 // v2.0-8-g77df-dirty // 4f72d7 - cmd := exec.Command("git", "describe", "--long", "--always", "--dirty") - b, err := cmd.Output() - if err != nil { - // Either Git is not available or the current directory is not a - // Git repository. - var s strings.Builder - fmt.Fprintf(&s, "Release detection failed: %v", err) - if err, ok := err.(*exec.ExitError); ok && len(err.Stderr) > 0 { - fmt.Fprintf(&s, ": %s", err.Stderr) + if _, err := exec.LookPath("git"); err == nil { + cmd := exec.Command("git", "describe", "--long", "--always", "--dirty") + b, err := cmd.Output() + if err != nil { + // Either Git is not available or the current directory is not a + // Git repository. + var s strings.Builder + fmt.Fprintf(&s, "Release detection failed: %v", err) + if err, ok := err.(*exec.ExitError); ok && len(err.Stderr) > 0 { + fmt.Fprintf(&s, ": %s", err.Stderr) + } + Logger.Print(s.String()) + } else { + release = strings.TrimSpace(string(b)) + Logger.Printf("Using release from Git: %s", release) + return release } - Logger.Print(s.String()) - Logger.Print("Some Sentry features will not be available. See https://docs.sentry.io/product/releases/.") - Logger.Print("To stop seeing this message, pass a Release to sentry.Init or set the SENTRY_RELEASE environment variable.") - return "" } - release = strings.TrimSpace(string(b)) - Logger.Printf("Using release from Git: %s", release) - return release + + Logger.Print("Some Sentry features will not be available. See https://docs.sentry.io/product/releases/.") + Logger.Print("To stop seeing this message, pass a Release to sentry.Init or set the SENTRY_RELEASE environment variable.") + return "" } func revisionFromBuildInfo(info *debug.BuildInfo) string {