Skip to content

Commit

Permalink
Tag resource with remove changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ChanochShayner committed Jul 17, 2023
1 parent ddf8dd7 commit 2b9bb71
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 17 deletions.
24 changes: 21 additions & 3 deletions src/common/gitservice/blame.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,34 @@ type GitBlame struct {
GitUserEmail string
}

func NewGitBlame(filePath string, lines structure.Lines, blameResult *git.BlameResult, gitOrg string, gitRepository string, userEmail string) *GitBlame {
gitBlame := GitBlame{GitOrg: gitOrg, GitRepository: gitRepository, BlamesByLine: map[int]*git.Line{}, FilePath: filePath, GitUserEmail: userEmail}
func NewGitBlame(RelFilePath string, filePath string, lines structure.Lines, blameResult *git.BlameResult, g *GitService) *GitBlame {

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

View workflow job for this annotation

GitHub Actions / golangci-lint

[golangci-lint] reported by reviewdog 🐶 captLocal: `RelFilePath' should not be capitalized (gocritic) Raw Output: src/common/gitservice/blame.go:22:18: captLocal: `RelFilePath' should not be capitalized (gocritic) 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())
parentIter := commit.Parents()
previousCommit, _ := parentIter.Next()

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

for line := startLine; line <= endLine; line++ {
if line >= len(blameResult.Lines) {
logger.Warning(fmt.Sprintf("Index out of bound on parsed file %s", filePath))
logger.Warning(fmt.Sprintf("Index out of bound on parsed file %s", RelFilePath))
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.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
}
}
}

return &gitBlame
Expand Down
40 changes: 26 additions & 14 deletions src/common/gitservice/git_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ import (
)

type GitService struct {
gitRootDir string
scanPathFromRoot string
repository *git.Repository
remoteURL string
organization string
repoName string
BlameByFile *sync.Map
currentUserEmail string
gitRootDir string
scanPathFromRoot string
repository *git.Repository
remoteURL string
organization string
repoName string
BlameByFile *sync.Map
PreviousBlameByFile *sync.Map
currentUserEmail string
}

var gitGraphLock sync.Mutex
Expand Down Expand Up @@ -53,10 +54,11 @@ func NewGitService(rootDir string) (*GitService, error) {
scanPathFromRoot, _ := filepath.Rel(rootDirIter, scanAbsDir)

gitService := GitService{
gitRootDir: rootDir,
scanPathFromRoot: scanPathFromRoot,
repository: repository,
BlameByFile: &sync.Map{},
gitRootDir: rootDir,
scanPathFromRoot: scanPathFromRoot,
repository: repository,
BlameByFile: &sync.Map{},
PreviousBlameByFile: &sync.Map{},
}
err = gitService.setOrgAndName()
gitService.currentUserEmail = GetGitUserEmail()
Expand Down Expand Up @@ -115,7 +117,7 @@ func (g *GitService) GetBlameForFileLines(filePath string, lines structure.Lines
relativeFilePath := g.ComputeRelativeFilePath(filePath)
blame, ok := g.BlameByFile.Load(filePath)
if ok {
return NewGitBlame(relativeFilePath, lines, blame.(*git.BlameResult), g.organization, g.repoName, g.currentUserEmail), nil
return NewGitBlame(relativeFilePath, filePath, lines, blame.(*git.BlameResult), g), nil
}

var err error
Expand All @@ -126,7 +128,7 @@ func (g *GitService) GetBlameForFileLines(filePath string, lines structure.Lines

g.BlameByFile.Store(filePath, blame)

return NewGitBlame(relativeFilePath, lines, blame.(*git.BlameResult), g.organization, g.repoName, g.currentUserEmail), nil
return NewGitBlame(relativeFilePath, filePath, lines, blame.(*git.BlameResult), g), nil
}

func (g *GitService) GetOrganization() string {
Expand Down Expand Up @@ -157,11 +159,21 @@ func (g *GitService) GetFileBlame(filePath string) (*git.BlameResult, error) {
return nil, fmt.Errorf("failed to find commit %s ", head.Hash().String())
}

parentIter := selectedCommit.Parents()
previousCommit, err := parentIter.Next()
if err != nil {
return nil, fmt.Errorf("failed to get previous commit: %s", err)
}
blame, err = git.Blame(selectedCommit, relativeFilePath)
if err != nil {
return nil, fmt.Errorf("failed to get blame for latest commit of file %s because of error %s", filePath, err)
}
previousBlame, err := git.Blame(previousCommit, relativeFilePath)
if err != nil {
return nil, fmt.Errorf("failed to get blame for latest commit of file %s because of error %s", filePath, err)
}
g.BlameByFile.Store(filePath, blame)
g.PreviousBlameByFile.Store(filePath, previousBlame)

return blame.(*git.BlameResult), nil
}
Expand Down

0 comments on commit 2b9bb71

Please sign in to comment.