mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-11-28 04:04:25 +01:00
54 lines
1.4 KiB
Go
54 lines
1.4 KiB
Go
package detect
|
|
|
|
// Location represents a location in a file
|
|
type Location struct {
|
|
startLine int
|
|
endLine int
|
|
startColumn int
|
|
endColumn int
|
|
startLineIndex int
|
|
endLineIndex int
|
|
}
|
|
|
|
func location(fragment Fragment, matchIndex []int) Location {
|
|
var (
|
|
prevNewLine int
|
|
location Location
|
|
lineSet bool
|
|
_lineNum int
|
|
)
|
|
|
|
start := matchIndex[0]
|
|
end := matchIndex[1]
|
|
|
|
for lineNum, pair := range fragment.newlineIndices {
|
|
_lineNum = lineNum
|
|
newLineByteIndex := pair[0]
|
|
if prevNewLine <= start && start < newLineByteIndex {
|
|
lineSet = true
|
|
location.startLine = lineNum
|
|
location.endLine = lineNum
|
|
location.startColumn = (start - prevNewLine) + 1 // +1 because counting starts at 1
|
|
location.startLineIndex = prevNewLine
|
|
location.endLineIndex = newLineByteIndex
|
|
}
|
|
if prevNewLine < end && end <= newLineByteIndex {
|
|
location.endLine = lineNum
|
|
location.endColumn = (end - prevNewLine)
|
|
location.endLineIndex = newLineByteIndex
|
|
}
|
|
prevNewLine = pair[0]
|
|
}
|
|
|
|
if !lineSet {
|
|
// if lines never get set then that means the secret is most likely
|
|
// on the last line of the diff output and the diff output does not have
|
|
// a newline
|
|
location.startColumn = (start - prevNewLine) + 1 // +1 because counting starts at 1
|
|
location.endColumn = (end - prevNewLine)
|
|
location.startLine = _lineNum + 1
|
|
location.endLine = _lineNum + 1
|
|
}
|
|
return location
|
|
}
|