Skip to content

Commit

Permalink
make sure that the commit is in the local
Browse files Browse the repository at this point in the history
e.g. shallow cloned repository
  • Loading branch information
shogo82148 committed Jan 13, 2020
1 parent 48b7a6a commit 421981d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 25 deletions.
52 changes: 37 additions & 15 deletions gitinfo.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -41,6 +42,14 @@ func collectGitInfo(ref string) *Git {
log.Print("git information is omitted")
return nil
}

if ref != "HEAD" {
// make sure that the commit is in the local
// e.g. shallow cloned repository
_, _ = runCommand(gitPath, "fetch", "--depth=1", "origin", ref)
// ignore errors because we don't have enough information about the origin.
}

for key, args := range gitCmds {
if key == "branch" {
if envBranch := loadBranchFromEnv(); envBranch != "" {
Expand All @@ -49,33 +58,46 @@ func collectGitInfo(ref string) *Git {
}
}

cmd := exec.Command(gitPath, args...)
ret, err := cmd.CombinedOutput()
ret, err := runCommand(gitPath, args...)
if err != nil {
if strings.Contains(string(ret), `Not a git repository`) {
return nil
}
log.Fatalf("%v: %v", err, string(ret))
log.Printf(`fail to run "%s %s": %v`, gitPath, strings.Join(args, " "), err)
log.Print("git information is omitted")
return nil
}
s := string(ret)
s = strings.TrimRight(s, "\n")
results[key] = s
results[key] = ret
}
h := Head{
ID: strings.Split(results["id"], "\n")[0],
AuthorName: strings.Split(results["aname"], "\n")[0],
AuthorEmail: strings.Split(results["aemail"], "\n")[0],
CommitterName: strings.Split(results["cname"], "\n")[0],
CommitterEmail: strings.Split(results["cemail"], "\n")[0],
ID: firstLine(results["id"]),
AuthorName: firstLine(results["aname"]),
AuthorEmail: firstLine(results["aemail"]),
CommitterName: firstLine(results["cname"]),
CommitterEmail: firstLine(results["cemail"]),
Message: results["message"],
}
g := &Git{
Head: h,
Branch: strings.Split(results["branch"], "\n")[0],
Branch: firstLine(results["branch"]),
}
return g
}

func runCommand(gitPath string, args ...string) (string, error) {
cmd := exec.Command(gitPath, args...)
ret, err := cmd.CombinedOutput()
if err != nil {
return "", err
}
ret = bytes.TrimRight(ret, "\n")
return string(ret), nil
}

func firstLine(s string) string {
if idx := strings.Index(s, "\n"); idx >= 0 {
return s[:idx]
}
return s
}

var varNames = [...]string{
"GIT_BRANCH",

Expand Down
22 changes: 12 additions & 10 deletions goveralls.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ type Job struct {
SourceFiles []*SourceFile `json:"source_files"`
Parallel *bool `json:"parallel,omitempty"`
Git *Git `json:"git,omitempty"`
CommitSHA string `json:"commit_sha,omitempty"`
RunAt time.Time `json:"run_at"`
}

Expand Down Expand Up @@ -254,6 +253,7 @@ func process() error {
//

// flags are never nil, so no nil check needed
githubEvent := getGithubEvent()
var jobId string
if *customJobId != "" {
jobId = *customJobId
Expand All @@ -280,7 +280,7 @@ func process() error {
} else if githubSha := os.Getenv("GITHUB_SHA"); githubSha != "" {
githubShortSha := githubSha[0:9]
if os.Getenv("GITHUB_EVENT_NAME") == "pull_request" {
number := getGithubEvent()["number"].(float64)
number := githubEvent["number"].(float64)
jobId = fmt.Sprintf(`%s-PR-%d`, githubShortSha, int(number))
} else {
jobId = githubShortSha
Expand All @@ -290,7 +290,8 @@ func process() error {
if *repotoken == "" {
repotoken = nil // remove the entry from json
}
var sha string

head := "HEAD"
var pullRequest string
if prNumber := os.Getenv("CIRCLE_PR_NUMBER"); prNumber != "" {
// for Circle CI (pull request from forked repo)
Expand All @@ -313,10 +314,12 @@ func process() error {
} else if prNumber := os.Getenv("CI_PR_NUMBER"); prNumber != "" {
pullRequest = prNumber
} else if os.Getenv("GITHUB_EVENT_NAME") == "pull_request" {
event := getGithubEvent()
number := event["number"].(float64)
number := githubEvent["number"].(float64)
pullRequest = strconv.Itoa(int(number))
sha = event["pull_request"].(map[string]interface{})["head"].(map[string]interface{})["sha"].(string)

ghPR := githubEvent["pull_request"].(map[string]interface{})
ghHead := ghPR["head"].(map[string]interface{})
head = ghHead["sha"].(string)
}

if *service == "" && os.Getenv("TRAVIS_JOB_ID") != "" {
Expand All @@ -333,10 +336,9 @@ func process() error {
RepoToken: repotoken,
ServicePullRequest: pullRequest,
Parallel: parallel,
// Git: collectGitInfo("HEAD"),
CommitSHA: sha,
SourceFiles: sourceFiles,
ServiceName: *service,
Git: collectGitInfo(head),
SourceFiles: sourceFiles,
ServiceName: *service,
}

// Only include a job ID if it's known, otherwise, Coveralls looks
Expand Down

0 comments on commit 421981d

Please sign in to comment.