-
Notifications
You must be signed in to change notification settings - Fork 823
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
remove git2go dependency #1001
Closed
Closed
remove git2go dependency #1001
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,3 @@ issues: | |
- linters: | ||
- stylecheck | ||
text: "ST1003" | ||
run: | ||
skip-dirs: | ||
- git2go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,12 @@ | ||
.PHONY: test all build libgit2 | ||
.PHONY: test all build | ||
|
||
# default task invoked while running make | ||
all: libgit2 build | ||
all: build | ||
|
||
export CGO_ENABLED=1 | ||
|
||
# build and install libgit2 | ||
libgit2: | ||
-git submodule update --init --recursive | ||
cd git2go; make install-static | ||
|
||
# go build tags | ||
TAGS = "static" | ||
|
||
build: | ||
go build -v -tags=$(TAGS) . | ||
go build -v . | ||
|
||
test: | ||
go test -v -tags=$(TAGS) ./... | ||
go test -v ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,16 +9,15 @@ import ( | |
gitv5 "github.com/go-git/go-git/v5" | ||
configv5 "github.com/go-git/go-git/v5/config" | ||
plumbingv5 "github.com/go-git/go-git/v5/plumbing" | ||
objectv5 "github.com/go-git/go-git/v5/plumbing/object" | ||
"github.com/kubescape/go-git-url/apis" | ||
git2go "github.com/libgit2/git2go/v33" | ||
) | ||
|
||
type LocalGitRepository struct { | ||
goGitRepo *gitv5.Repository | ||
git2GoRepo *git2go.Repository | ||
head *plumbingv5.Reference | ||
config *configv5.Config | ||
fileToLastCommit map[string]*git2go.Commit | ||
fileToLastCommit map[string]*objectv5.Commit | ||
} | ||
|
||
func NewLocalGitRepository(path string) (*LocalGitRepository, error) { | ||
|
@@ -51,14 +50,6 @@ func NewLocalGitRepository(path string) (*LocalGitRepository, error) { | |
config: config, | ||
} | ||
|
||
if repoRoot, err := l.GetRootDir(); err == nil { | ||
git2GoRepo, err := git2go.OpenRepository(repoRoot) | ||
if err != nil { | ||
return l, err | ||
} | ||
l.git2GoRepo = git2GoRepo | ||
} | ||
|
||
return l, nil | ||
} | ||
|
||
|
@@ -98,13 +89,11 @@ func (g *LocalGitRepository) GetName() (string, error) { | |
|
||
// GetLastCommit get latest commit object | ||
func (g *LocalGitRepository) GetLastCommit() (*apis.Commit, error) { | ||
cIter, err := g.goGitRepo.Log(&gitv5.LogOptions{}) | ||
ref, err := g.goGitRepo.Head() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
commit, err := cIter.Next() | ||
defer cIter.Close() | ||
commit, err := g.goGitRepo.CommitObject(ref.Hash()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -122,82 +111,65 @@ func (g *LocalGitRepository) GetLastCommit() (*apis.Commit, error) { | |
}, nil | ||
} | ||
|
||
func (g *LocalGitRepository) getAllCommits() ([]*git2go.Commit, error) { | ||
logItr, itrErr := g.git2GoRepo.Walk() | ||
if itrErr != nil { | ||
|
||
return nil, itrErr | ||
func (g *LocalGitRepository) getAllCommits() ([]*objectv5.Commit, error) { | ||
ref, err := g.goGitRepo.Head() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
pushErr := logItr.PushHead() | ||
if pushErr != nil { | ||
return nil, pushErr | ||
logItr, err := g.goGitRepo.Log(&gitv5.LogOptions{From: ref.Hash()}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var allCommits []*git2go.Commit | ||
err := logItr.Iterate(func(commit *git2go.Commit) bool { | ||
var allCommits []*objectv5.Commit | ||
err = logItr.ForEach(func(commit *objectv5.Commit) error { | ||
if commit != nil { | ||
allCommits = append(allCommits, commit) | ||
return true | ||
} | ||
return false | ||
return nil | ||
}) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return allCommits, nil | ||
} | ||
|
||
func (g *LocalGitRepository) GetFileLastCommit(filePath string) (*apis.Commit, error) { | ||
if len(g.fileToLastCommit) == 0 { | ||
filePathToCommitTime := map[string]time.Time{} | ||
filePathToCommit := map[string]*git2go.Commit{} | ||
filePathToCommit := map[string]*objectv5.Commit{} | ||
allCommits, _ := g.getAllCommits() | ||
|
||
// builds a map of all files to their last commit | ||
for _, commit := range allCommits { | ||
// Ignore merge commits (2+ parents) | ||
if commit.ParentCount() <= 1 { | ||
if commit.NumParents() <= 1 { | ||
tree, err := commit.Tree() | ||
if err != nil { | ||
continue | ||
} | ||
|
||
// ParentCount can be either 1 or 0 (initial commit) | ||
// In case it's the initial commit, prevTree is nil | ||
var prevTree *git2go.Tree | ||
if commit.ParentCount() == 1 { | ||
prevCommit := commit.Parent(0) | ||
var prevTree *objectv5.Tree | ||
if commit.NumParents() == 1 { | ||
prevCommit, _ := commit.Parent(0) | ||
prevTree, err = prevCommit.Tree() | ||
if err != nil { | ||
continue | ||
} | ||
} | ||
|
||
diff, err := g.git2GoRepo.DiffTreeToTree(prevTree, tree, nil) | ||
changes, err := prevTree.Diff(tree) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is the slow part... merkle tree diffs are painfully slow |
||
if err != nil { | ||
continue | ||
} | ||
|
||
numDeltas, err := diff.NumDeltas() | ||
if err != nil { | ||
continue | ||
} | ||
|
||
for i := 0; i < numDeltas; i++ { | ||
delta, err := diff.Delta(i) | ||
if err != nil { | ||
continue | ||
} | ||
|
||
deltaFilePath := delta.NewFile.Path | ||
commitTime := commit.Author().When | ||
for _, change := range changes { | ||
deltaFilePath := change.To.Name | ||
commitTime := commit.Author.When | ||
|
||
// In case we have the commit information for the file which is not the latest - we override it | ||
if currentCommitTime, exists := filePathToCommitTime[deltaFilePath]; exists { | ||
|
@@ -222,15 +194,15 @@ func (g *LocalGitRepository) GetFileLastCommit(filePath string) (*apis.Commit, e | |
return nil, fmt.Errorf("failed to get commit information for file: %s", filePath) | ||
} | ||
|
||
func (g *LocalGitRepository) getCommit(commit *git2go.Commit) *apis.Commit { | ||
func (g *LocalGitRepository) getCommit(commit *objectv5.Commit) *apis.Commit { | ||
return &apis.Commit{ | ||
SHA: commit.Id().String(), | ||
SHA: commit.Hash.String(), | ||
Author: apis.Committer{ | ||
Name: commit.Author().Name, | ||
Email: commit.Author().Email, | ||
Date: commit.Author().When, | ||
Name: commit.Author.Name, | ||
Email: commit.Author.Email, | ||
Date: commit.Author.When, | ||
}, | ||
Message: commit.Message(), | ||
Message: commit.Message, | ||
Committer: apis.Committer{}, | ||
Files: []apis.Files{}, | ||
} | ||
|
Submodule git2go
deleted from
eae007
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@matthyx so if I understand correctly, this is the critical part that used to be slow (and might still be) and justified the use of the native git bindings? Is that correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
check the other comment