Skip to content
This repository has been archived by the owner on May 19, 2024. It is now read-only.

Commit

Permalink
check if binary before processing a commit file
Browse files Browse the repository at this point in the history
  • Loading branch information
owenrumney committed Jan 21, 2022
1 parent 1300224 commit 9f55caf
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
33 changes: 17 additions & 16 deletions commenter/commenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (c *Commenter) checkCommentRelevant(filename string, line int) bool {

for _, file := range c.files {
if relevant := func(file *commitFileInfo) bool {
if file.FileName == filename {
if file.FileName == filename && !file.isBinary() {
if line >= file.hunkStart && line <= file.hunkEnd {
return true
}
Expand All @@ -172,7 +172,7 @@ func (c *Commenter) checkCommentRelevant(filename string, line int) bool {
func (c *Commenter) getFileInfo(file string, line int) (*commitFileInfo, error) {

for _, info := range c.files {
if info.FileName == file {
if info.FileName == file && !info.isBinary() {
if line >= info.hunkStart && line <= info.hunkEnd {
return info, nil
}
Expand All @@ -192,18 +192,18 @@ func buildComment(file, comment string, line int, info commitFileInfo) *github.P
}
}

func getCommitInfo(file *github.CommitFile) (*commitFileInfo, error) {
func getCommitInfo(file *github.CommitFile) (cfi *commitFileInfo, err error) {
var hunkStart, hunkEnd int
var isBinary bool
patch := file.GetPatch()
if patch == "" {
return nil, fmt.Errorf("ignoring [%s] because it has no patch informations, probably a binary?", *file.Filename)
}

groups := patchRegex.FindAllStringSubmatch(file.GetPatch(), -1)
if len(groups) < 1 {
return nil, fmt.Errorf("the patch details for [%s] could not be resolved", *file.Filename)
if patch != "" {
groups := patchRegex.FindAllStringSubmatch(file.GetPatch(), -1)
if len(groups) < 1 {
return nil, fmt.Errorf("the patch details for [%s] could not be resolved", *file.Filename)
}
hunkStart, _ = strconv.Atoi(groups[0][1])
hunkEnd, _ = strconv.Atoi(groups[0][2])
}
hunkStart, _ := strconv.Atoi(groups[0][1])
hunkEnd, _ := strconv.Atoi(groups[0][2])

shaGroups := commitRefRegex.FindAllStringSubmatch(file.GetContentsURL(), -1)
if len(shaGroups) < 1 {
Expand All @@ -212,9 +212,10 @@ func getCommitInfo(file *github.CommitFile) (*commitFileInfo, error) {
sha := shaGroups[0][1]

return &commitFileInfo{
FileName: *file.Filename,
hunkStart: hunkStart,
hunkEnd: hunkStart + (hunkEnd - 1),
sha: sha,
FileName: *file.Filename,
hunkStart: hunkStart,
hunkEnd: hunkStart + (hunkEnd - 1),
sha: sha,
likelyBinary: isBinary,
}, nil
}
13 changes: 9 additions & 4 deletions commenter/commitFileInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import (
)

type commitFileInfo struct {
FileName string
hunkStart int
hunkEnd int
sha string
FileName string
hunkStart int
hunkEnd int
sha string
likelyBinary bool
}

func getCommitFileInfo(ghConnector *connector) ([]*commitFileInfo, error) {
Expand Down Expand Up @@ -42,3 +43,7 @@ func (cfi commitFileInfo) calculatePosition(line int) *int {
position := line - cfi.hunkStart
return &position
}

func (cfi commitFileInfo) isBinary() bool {
return cfi.likelyBinary
}

0 comments on commit 9f55caf

Please sign in to comment.