Skip to content

Commit

Permalink
Some UT fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ChanochShayner committed Jul 17, 2023
1 parent 2b9bb71 commit 8f120ab
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions src/common/gitservice/blame.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gitservice

import (
"fmt"
"github.com/go-git/go-git/v5/plumbing/object"

Check failure on line 5 in src/common/gitservice/blame.go

View workflow job for this annotation

GitHub Actions / golangci-lint

[golangci-lint] reported by reviewdog 🐶 File is not `goimports`-ed (goimports) Raw Output: src/common/gitservice/blame.go:5: File is not `goimports`-ed (goimports) "github.com/go-git/go-git/v5/plumbing/object"
"strings"
"time"

Expand All @@ -19,29 +20,45 @@ type GitBlame struct {
GitUserEmail string
}

func NewGitBlame(RelFilePath string, filePath string, lines structure.Lines, blameResult *git.BlameResult, g *GitService) *GitBlame {
gitBlame := GitBlame{GitOrg: g.organization, GitRepository: g.repoName, BlamesByLine: map[int]*git.Line{}, FilePath: filePath, GitUserEmail: g.currentUserEmail}
startLine := lines.Start - 1 // the lines in blameResult.Lines start from zero while the lines range start from 1
endLine := lines.End - 1

ref, _ := g.repository.Head()
commit, _ := g.repository.CommitObject(ref.Hash())
func GetPreviousBlameResult(g *GitService, filePath string) (*git.BlameResult, *object.Commit) {
if g.repository == nil {
return nil, nil
}
ref, err := g.repository.Head()
if err != nil {
return nil, nil
}
commit, err := g.repository.CommitObject(ref.Hash())
if err != nil {
return nil, nil
}
parentIter := commit.Parents()
previousCommit, _ := parentIter.Next()
previousCommit, err := parentIter.Next()
if err != nil {
return nil, nil
}

var previousBlameResult *git.BlameResult
result, _ := g.PreviousBlameByFile.Load(filePath)
previousBlameResult = result.(*git.BlameResult)
return previousBlameResult, previousCommit
}

func NewGitBlame(realFilePath string, filePath string, lines structure.Lines, blameResult *git.BlameResult, g *GitService) *GitBlame {
gitBlame := GitBlame{GitOrg: g.organization, GitRepository: g.repoName, BlamesByLine: map[int]*git.Line{}, FilePath: realFilePath, GitUserEmail: g.currentUserEmail}
startLine := lines.Start - 1 // the lines in blameResult.Lines start from zero while the lines range start from 1
endLine := lines.End - 1
previousBlameResult, previousCommit := GetPreviousBlameResult(g, filePath)

for line := startLine; line <= endLine; line++ {
if line >= len(blameResult.Lines) {
logger.Warning(fmt.Sprintf("Index out of bound on parsed file %s", RelFilePath))
logger.Warning(fmt.Sprintf("Index out of bound on parsed file %s", realFilePath))
return &gitBlame
}
gitBlame.BlamesByLine[line+1] = blameResult.Lines[line]

// Check if the line has been removed in the current state of the file
if len(previousBlameResult.Lines) > len(blameResult.Lines) {
if previousBlameResult != nil && len(previousBlameResult.Lines) > len(blameResult.Lines) {
if previousBlameResult.Lines[line].Text != blameResult.Lines[line].Text {
// The line has been removed, so update the git commit id
gitBlame.BlamesByLine[line+1].Hash = previousCommit.Hash
Expand Down

0 comments on commit 8f120ab

Please sign in to comment.