Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hotfix: don't early exit with error if root is not a git dir #224

Merged
merged 3 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 13 additions & 20 deletions command/report/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ package report

import (
"bytes"
"fmt"
"os"
"os/exec"
"strings"

"github.com/getsentry/sentry-go"
)

// gitGetHead accepts a git directory and returns head commit OID / error
Expand All @@ -18,11 +15,21 @@ func gitGetHead(workspaceDir string) (headOID string, warning string, err error)
return
}

// get the top commit manually, using git command
// Check if the `GIT_COMMIT_SHA` environment variable exists. If yes, return this as
// the latest commit sha.
// This is used in cases when the user wants to report tcv from inside a docker container in which they are running tests.
// In this scenario, the container doesn't have data about the latest git commit sha so
// it is injected by the user manually while running the container.
// Example:
// GIT_COMMIT_SHA=$(git --no-pager rev-parse HEAD | tr -d '\n')
// docker run -e DEEPSOURCE_DSN -e GIT_COMMIT_SHA ...
if injectedSHA, isManuallyInjectedSHA := os.LookupEnv("GIT_COMMIT_SHA"); isManuallyInjectedSHA {
return injectedSHA, "", nil
}

// get the top commit manually, using git command. We will be using this if there's no env variable set for extracting commit.
headOID, err = fetchHeadManually(workspaceDir)
if err != nil {
fmt.Println(err)
sentry.CaptureException(err)
return
}

Expand All @@ -38,20 +45,6 @@ func gitGetHead(workspaceDir string) (headOID string, warning string, err error)
return
}

// Check if the `GIT_COMMIT_SHA` environment variable exists. If yes, return this as
// the latest commit sha.
// This is used in cases when the user:
// 1. Is using a CI other than GH Actions/ Travis CI (handled above)
// 2. Wants to report tcv from inside a docker container in which they are running tests.
// In this scenario, the container doesn't have data about the latest git commit sha so
// it is injected by the user manually while running the container.
// Example:
// GIT_COMMIT_SHA=$(git --no-pager rev-parse HEAD | tr -d '\n')
// docker run -e DEEPSOURCE_DSN -e GIT_COMMIT_SHA ...
if _, isManuallyInjectedSHA := os.LookupEnv("GIT_COMMIT_SHA"); isManuallyInjectedSHA {
return os.Getenv("GIT_COMMIT_SHA"), "", nil
}

// If we are here, it means there weren't any special cases. Return the manually found headOID.
return
}
Expand Down
2 changes: 2 additions & 0 deletions command/report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"log"
"os"
"strings"
"time"
Expand Down Expand Up @@ -182,6 +183,7 @@ func (opts *ReportOptions) Run() int {
headCommitOID, warning, err := gitGetHead(currentDir)
if err != nil {
fmt.Fprintln(os.Stderr, "DeepSource | Error | Unable to get commit OID HEAD. Make sure you are running the CLI from a git repository")
log.Println(err)
sentry.CaptureException(err)
return 1
}
Expand Down