Skip to content
Snippets Groups Projects
Commit 258774eb authored by Nick Thomas's avatar Nick Thomas
Browse files

Introduce our own types into the git package

parent e7f1644c
No related branches found
No related tags found
1 merge request!1Initial implementation of an elasticsearch indexer in Go
Pipeline #
Loading
Loading
@@ -14,7 +14,7 @@ var (
endError = fmt.Errorf("Finished") // not really an error
)
 
type Repo struct {
type goGitRepository struct {
Repo *git.Repository
 
FromHash plumbing.Hash
Loading
Loading
@@ -24,8 +24,8 @@ type Repo struct {
ToCommit *object.Commit
}
 
func NewRepo(projectPath string, fromSHA string, toSHA string) (*Repo, error) {
out := &Repo{}
func NewGoGitRepository(projectPath string, fromSHA string, toSHA string) (*goGitRepository, error) {
out := &goGitRepository{}
 
repo, err := git.PlainOpen(projectPath)
if err != nil {
Loading
Loading
@@ -67,7 +67,7 @@ func NewRepo(projectPath string, fromSHA string, toSHA string) (*Repo, error) {
return out, nil
}
 
func (r *Repo) Diff() (object.Changes, error) {
func (r *goGitRepository) diff() (object.Changes, error) {
var fromTree, toTree *object.Tree
 
if r.FromCommit != nil {
Loading
Loading
@@ -87,14 +87,32 @@ func (r *Repo) Diff() (object.Changes, error) {
return object.DiffTree(fromTree, toTree)
}
 
type FileFunc func(file *object.File, fromCommit, toCommit *object.Commit) error
func goGitBuildSignature(sig object.Signature) Signature {
return Signature{
Name: sig.Name,
Email: sig.Email,
When: sig.When,
}
}
func goGitBuildFile(change object.ChangeEntry, file *object.File) *File {
return &File{
Path: change.Name,
Oid: file.ID().String(),
Blob: file.Blob.Reader,
Size: file.Size,
}
}
 
func (r *Repo) EachFileChange(ins, mod, del FileFunc) error {
changes, err := r.Diff()
func (r *goGitRepository) EachFileChange(ins, mod, del FileFunc) error {
changes, err := r.diff()
if err != nil {
return err
}
 
fromCommitStr := r.FromHash.String()
toCommitStr := r.ToHash.String()
for _, change := range changes {
// FIXME(nick): submodules may need better support
// https://github.com/src-d/go-git/issues/317
Loading
Loading
@@ -114,14 +132,11 @@ func (r *Repo) EachFileChange(ins, mod, del FileFunc) error {
 
switch action {
case merkletrie.Insert:
toF.Name = change.To.Name
err = ins(toF, r.FromCommit, r.ToCommit)
err = ins(goGitBuildFile(change.To, toF), fromCommitStr, toCommitStr)
case merkletrie.Modify:
toF.Name = change.To.Name
err = mod(toF, r.FromCommit, r.ToCommit)
err = mod(goGitBuildFile(change.To, toF), fromCommitStr, toCommitStr)
case merkletrie.Delete:
fromF.Name = change.From.Name
err = del(fromF, r.FromCommit, r.ToCommit)
err = del(goGitBuildFile(change.From, fromF), fromCommitStr, toCommitStr)
default:
err = fmt.Errorf("Unrecognised change calculating diff: %+v", change)
}
Loading
Loading
@@ -136,9 +151,16 @@ func (r *Repo) EachFileChange(ins, mod, del FileFunc) error {
 
// EachCommit runs `f` for each commit within `fromSHA`...`toSHA` (i.e., the
// inclusive set).
func (r *Repo) EachCommit(f func(*object.Commit) error) error {
func (r *goGitRepository) EachCommit(f CommitFunc) error {
err := object.WalkCommitHistory(r.ToCommit, func(c *object.Commit) error {
if err := f(c); err != nil {
commit := &Commit{
Message: c.Message,
Hash: c.Hash.String(),
Author: goGitBuildSignature(c.Author),
Committer: goGitBuildSignature(c.Committer),
}
if err := f(commit); err != nil {
return err
}
 
Loading
Loading
package git
import (
"io"
"time"
)
type File struct {
Path string
Blob func() (io.ReadCloser, error)
Oid string
Size int64
}
type Signature struct {
Name string
Email string
When time.Time
}
type Commit struct {
Author Signature
Committer Signature
Message string
Hash string
}
type Repository interface {
EachFileChange(ins, mod, del FileFunc) error
EachCommit(f CommitFunc) error
}
type FileFunc func(file *File, fromCommit, toCommit string) error
type CommitFunc func(commit *Commit) error
Loading
Loading
@@ -5,8 +5,7 @@ import (
"fmt"
"io/ioutil"
 
"gopkg.in/src-d/go-git.v4/plumbing/object"
"gitlab.com/gitlab-org/es-git-go/git"
"gitlab.com/gitlab-org/es-git-go/linguist"
)
 
Loading
Loading
@@ -59,14 +58,14 @@ func GenerateBlobID(parentID, filename string) string {
return fmt.Sprintf("%s_%s", parentID, filename)
}
 
func (i *Indexer) BuildBlob(file *object.File, commitSHA string) (*Blob, error) {
func (i *Indexer) BuildBlob(file *git.File, commitSHA string) (*Blob, error) {
parentID := i.Submitter.ParentID()
 
if file.Blob.Size > maxBlobSize {
if file.Size > maxBlobSize {
return nil, skipTooLargeBlob
}
 
reader, err := file.Blob.Reader()
reader, err := file.Blob()
if err != nil {
return nil, err
}
Loading
Loading
@@ -85,12 +84,12 @@ func (i *Indexer) BuildBlob(file *object.File, commitSHA string) (*Blob, error)
}
 
content := tryEncodeBytes(b)
filename := tryEncodeString(file.Name)
filename := tryEncodeString(file.Path)
 
return &Blob{
Type: "blob",
ID: GenerateBlobID(parentID, file.Name),
OID: file.Blob.Hash.String(),
ID: GenerateBlobID(parentID, filename),
OID: file.Oid,
RepoID: parentID,
CommitSHA: commitSHA,
Content: content,
Loading
Loading
Loading
Loading
@@ -3,7 +3,7 @@ package indexer
import (
"fmt"
 
"gopkg.in/src-d/go-git.v4/plumbing/object"
"gitlab.com/gitlab-org/es-git-go/git"
)
 
type Commit struct {
Loading
Loading
@@ -20,8 +20,8 @@ func GenerateCommitID(parentID, commitSHA string) string {
return fmt.Sprintf("%s_%s", parentID, commitSHA)
}
 
func (i *Indexer) BuildCommit(c *object.Commit) *Commit {
sha := c.Hash.String()
func (i *Indexer) BuildCommit(c *git.Commit) *Commit {
sha := c.Hash
parentID := i.Submitter.ParentID()
 
return &Commit{
Loading
Loading
Loading
Loading
@@ -4,8 +4,6 @@ import (
"fmt"
"log"
 
"gopkg.in/src-d/go-git.v4/plumbing/object"
"gitlab.com/gitlab-org/es-git-go/git"
)
 
Loading
Loading
@@ -19,48 +17,46 @@ type Submitter interface {
}
 
type Indexer struct {
Repo *git.Repo
git.Repository
Submitter
}
 
// FIXME: none of the indexers worry about encoding right now
func (i *Indexer) SubmitCommit(c *object.Commit) error {
func (i *Indexer) SubmitCommit(c *git.Commit) error {
commit := i.BuildCommit(c)
 
i.Submitter.Index(commit.ID, map[string]interface{}{"commit": commit})
return nil
}
 
func (i *Indexer) SubmitBlob(f *object.File, _, toCommit *object.Commit) error {
func (i *Indexer) SubmitBlob(f *git.File, _, toCommit string) error {
// FIXME(nick): Not sure commitSHA is right, or how it works at all
 
blob, err := i.BuildBlob(f, toCommit.Hash.String())
blob, err := i.BuildBlob(f, toCommit)
if err != nil {
if isSkipBlobErr(err) {
return nil
}
 
return fmt.Errorf("Blob %s: %s", f.Name, err)
return fmt.Errorf("Blob %s: %s", f.Path, err)
}
 
i.Submitter.Index(blob.ID, map[string]interface{}{"blob": blob})
return nil
}
 
func (i *Indexer) RemoveBlob(file *object.File, _, toCommit *object.Commit) error {
blobID := GenerateBlobID(toCommit.Hash.String(), i.Submitter.ParentID())
func (i *Indexer) RemoveBlob(file *git.File, _, toCommit string) error {
blobID := GenerateBlobID(toCommit, i.Submitter.ParentID())
 
i.Submitter.Remove(blobID)
return nil
}
 
func (i *Indexer) IndexCommits() error {
return i.Repo.EachCommit(i.SubmitCommit)
return i.Repository.EachCommit(i.SubmitCommit)
}
 
func (i *Indexer) IndexBlobs() error {
return i.Repo.EachFileChange(i.SubmitBlob, i.SubmitBlob, i.RemoveBlob)
return i.Repository.EachFileChange(i.SubmitBlob, i.SubmitBlob, i.RemoveBlob)
}
 
func (i *Indexer) Index() error {
Loading
Loading
Loading
Loading
@@ -3,7 +3,7 @@ package indexer
import (
"time"
 
"gopkg.in/src-d/go-git.v4/plumbing/object"
"gitlab.com/gitlab-org/es-git-go/git"
)
 
const (
Loading
Loading
@@ -20,7 +20,7 @@ func GenerateDate(t time.Time) string {
return t.Format(elasticTimeFormat)
}
 
func BuildPerson(p object.Signature) *Person {
func BuildPerson(p git.Signature) *Person {
return &Person{
Name: tryEncodeString(p.Name),
Email: tryEncodeString(p.Email),
Loading
Loading
Loading
Loading
@@ -19,7 +19,7 @@ func main() {
fromSHA := os.Getenv("FROM_SHA")
toSHA := os.Getenv("TO_SHA")
 
repo, err := git.NewRepo(projectPath, fromSHA, toSHA)
repo, err := git.NewGoGitRepository(projectPath, fromSHA, toSHA)
if err != nil {
log.Fatalf("Failed to open %s: %s", projectPath, err)
}
Loading
Loading
@@ -30,8 +30,8 @@ func main() {
}
 
idx := &indexer.Indexer{
Submitter: esClient,
Repo: repo,
Submitter: esClient,
Repository: repo,
}
 
log.Printf("Indexing from %s to %s", repo.FromHash, repo.ToHash)
Loading
Loading
@@ -39,6 +39,5 @@ func main() {
 
if err := idx.Index(); err != nil {
log.Fatalln("Indexing error: ", err)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment