Permalink
Browse files

Use git library to get blob id

* Add fallback blob method
  • Loading branch information...
ale7714 authored and markbates committed Apr 15, 2017
1 parent 2b77440 commit f1d50a311d71b5fbd6f753ef923da477716b54a0
Showing 1,423 changed files with 525,251 additions and 1,058 deletions.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
No changes.
@@ -3,6 +3,7 @@ package env
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
@@ -11,6 +12,9 @@ import (
"strings"

"github.com/pkg/errors"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/object"
)

type Git struct {
@@ -30,6 +34,25 @@ func (g Git) String() string {
return out.String()
}

func GetHead() (*object.Commit, error) {
r, err := git.PlainOpen(".")
if err != nil {
return nil, errors.WithStack(err)
}

ref, err := r.Head()
if err != nil {
return nil, errors.WithStack(err)
}

commit, err := r.CommitObject(ref.Hash())
if err != nil {
return nil, errors.WithStack(err)
}

return commit, nil
}

func findGitInfo() (Git, error) {
_, err := exec.LookPath("git")
if err != nil {
@@ -86,23 +109,27 @@ func GitSHA(path string) (string, error) {
return strings.TrimSpace(string(out)), nil
}

var GitBlob = func(path string) (string, error) {
sha, err := GitSHA(path)
var GitBlob = func(path string, commit *object.Commit) (string, error) {
file, err := commit.File(path)

if err != nil {
return "", errors.WithStack(err)
}
cmd := exec.Command("git", "ls-tree", sha, "--", path)
cmd.Stderr = os.Stderr
out, err := cmd.Output()

res := strings.TrimSpace(file.Hash.String())
return res, nil
}

func FallbackBlob(path string) (string, error) {
file, err := ioutil.ReadFile(path)

if err != nil {
return "", errors.WithStack(err)
}
res := strings.TrimSpace(string(out))
matches := blobRegex.FindStringSubmatch(res)
if len(matches) == 0 {
return "", errors.Errorf("could not find blob id for file %s in %s", path, res)
}
return matches[1], nil

hash := plumbing.ComputeHash(plumbing.BlobObject, []byte(file))
res := hash.String()
return res, nil
}

func loadGitFromENV() (Git, error) {
@@ -29,6 +29,7 @@ RUN git commit -m "testing"
RUN bundle exec rake; exit 0

ENV CC_TEST_REPORTER_ID=c4881e09870b0fac1291c93339b36ffe36210a2645c1ad25e52d8fda3943fb4d

RUN test-reporter after-build -s 5 -d
# RUN test-reporter format-coverage -d
# RUN test-reporter upload-coverage -d -s 5
@@ -9,6 +9,7 @@ import (

"github.com/Sirupsen/logrus"
"github.com/codeclimate/test-reporter/formatters"
"github.com/codeclimate/test-reporter/env"
"github.com/markbates/pop/nulls"
"github.com/pkg/errors"
)
@@ -44,7 +45,8 @@ func (f *Formatter) Parse() error {
for _, line := range bytes.Split(b, []byte("\n")) {
if bytes.HasPrefix(line, []byte("SF:")) {
name := string(bytes.TrimSpace(bytes.TrimPrefix(line, []byte("SF:"))))
sf, err = formatters.NewSourceFile(name)
var gitHead, _ = env.GetHead()
sf, err = formatters.NewSourceFile(name, gitHead)
if err != nil {
return errors.WithStack(err)
}
@@ -8,6 +8,7 @@ import (
"time"

"github.com/Sirupsen/logrus"
"github.com/codeclimate/test-reporter/env"
"github.com/codeclimate/test-reporter/formatters"
"github.com/pkg/errors"
)
@@ -55,8 +56,9 @@ func (f *Formatter) Parse() error {
Timestamp: v.Timestamp.Time(),
SourceFiles: make([]formatters.SourceFile, 0, len(v.Coverage)),
}
gitHead, _ := env.GetHead()
for n, ls := range v.Coverage {
fe, err := formatters.NewSourceFile(n)
fe, err := formatters.NewSourceFile(n, gitHead)
if err != nil {
return errors.WithStack(err)
}
@@ -6,6 +6,8 @@ import (
"os"
"strings"

"gopkg.in/src-d/go-git.v4/plumbing/object"

"github.com/codeclimate/test-reporter/env"
"github.com/gobuffalo/envy"
"github.com/markbates/pop/nulls"
@@ -67,15 +69,23 @@ func (sf *SourceFile) CalcLineCounts() {
sf.CoveredPercent = lc.CoveredPercent()
}

func NewSourceFile(name string) (SourceFile, error) {
func NewSourceFile(name string, commit *object.Commit) (SourceFile, error) {
if prefix, err := envy.MustGet("PREFIX"); err == nil {
prefix := fmt.Sprintf("%s%s", prefix, string(os.PathSeparator))
name = strings.TrimPrefix(name, prefix)
}

sf := SourceFile{Name: name}
var err error
sf.BlobID, err = env.GitBlob(name)

if commit != nil {
fmt.Print("git")
sf.BlobID, err = env.GitBlob(name, commit)
} else {
fmt.Print("fallback")
sf.BlobID, err = env.FallbackBlob(name)
}

return sf, err
}

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit f1d50a3

Please sign in to comment.