Skip to content

Commit

Permalink
rework types, return early from checking diff
Browse files Browse the repository at this point in the history
  • Loading branch information
jazanne committed Nov 3, 2023
1 parent ebad7f8 commit 820f8b3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 26 deletions.
35 changes: 17 additions & 18 deletions diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"

lflags "github.com/launchdarkly/find-code-references-in-pull-request/flags"
"github.com/launchdarkly/find-code-references-in-pull-request/ignore"
i "github.com/launchdarkly/find-code-references-in-pull-request/ignore"
diff_util "github.com/launchdarkly/find-code-references-in-pull-request/internal/util/diff_util"
"github.com/launchdarkly/ld-find-code-refs/v2/aliases"
lsearch "github.com/launchdarkly/ld-find-code-refs/v2/search"
Expand All @@ -17,55 +17,54 @@ func PreprocessDiffs(dir string, multiFiles []*diff.FileDiff) aliases.FileConten
diffMap := make(map[string][]byte, len(multiFiles))

for _, parsedDiff := range multiFiles {
getPath := CheckDiff(parsedDiff, dir)
if getPath.Skip {
filePath, ignore := checkDiffFile(parsedDiff, dir)
if ignore {
continue
}

if _, ok := diffMap[getPath.FileToParse]; !ok {
diffMap[getPath.FileToParse] = make([]byte, 0)
if _, ok := diffMap[filePath]; !ok {
diffMap[filePath] = make([]byte, 0)
}

for _, hunk := range parsedDiff.Hunks {
diffMap[getPath.FileToParse] = append(diffMap[getPath.FileToParse], hunk.Body...)
diffMap[filePath] = append(diffMap[filePath], hunk.Body...)
}
}

return diffMap
}

func CheckDiff(parsedDiff *diff.FileDiff, workspace string) *DiffPaths {
diffPaths := DiffPaths{}
allIgnores := ignore.NewIgnore(workspace)
func checkDiffFile(parsedDiff *diff.FileDiff, workspace string) (filePath string, ignore bool) {
allIgnores := i.NewIgnore(workspace)

// If file is being renamed we don't want to check it for flags.
parsedFileA := strings.SplitN(parsedDiff.OrigName, "/", 2)
parsedFileB := strings.SplitN(parsedDiff.NewName, "/", 2)
fullPathToA := workspace + "/" + parsedFileA[1]
fullPathToB := workspace + "/" + parsedFileB[1]
info, err := os.Stat(fullPathToB)
if err != nil {
fmt.Println(err)
}
var isDir bool
// If there is no 'b' parse 'a', means file is deleted.
if info == nil {
isDir = false
diffPaths.FileToParse = fullPathToA
filePath = fullPathToA
} else {
isDir = info.IsDir()
diffPaths.FileToParse = fullPathToB
}
if err != nil {
fmt.Println(err)
filePath = fullPathToB
}
// Similar to ld-find-code-refs do not match dotfiles, and read in ignore files.
if strings.HasPrefix(parsedFileB[1], ".") && strings.HasPrefix(parsedFileA[1], ".") || allIgnores.Match(diffPaths.FileToParse, isDir) {
diffPaths.Skip = true
if strings.HasPrefix(parsedFileB[1], ".") && strings.HasPrefix(parsedFileA[1], ".") || allIgnores.Match(filePath, isDir) {
return filePath, true
}
// We don't want to run on renaming of files.
if (parsedFileA[1] != parsedFileB[1]) && (!strings.Contains(parsedFileB[1], "dev/null") && !strings.Contains(parsedFileA[1], "dev/null")) {
diffPaths.Skip = true
return filePath, true
}

return &diffPaths
return filePath, false
}

func ProcessDiffs(matcher lsearch.Matcher, contents []byte, builder *lflags.ReferenceBuilder) {
Expand Down
5 changes: 3 additions & 2 deletions diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@ func TestCheckDiff(t *testing.T) {
NewName: tc.newName,
Hunks: []*diff.Hunk{hunk},
}
results := CheckDiff(&diff, "../testdata")
assert.Equal(t, &DiffPaths{FileToParse: "../testdata/" + tc.fileName, Skip: tc.skip}, results, "")
filePath, ignore := checkDiffFile(&diff, "../testdata")
assert.Equal(t, tc.fileName, filePath)
assert.Equal(t, tc.skip, ignore)
})
}
}
Expand Down
6 changes: 0 additions & 6 deletions diff/types.go

This file was deleted.

0 comments on commit 820f8b3

Please sign in to comment.