Skip to content

Commit

Permalink
fix: handling of zero line case
Browse files Browse the repository at this point in the history
  • Loading branch information
didroe committed Nov 21, 2023
1 parent e0e592d commit cad7cde
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
12 changes: 8 additions & 4 deletions internal/git/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func (chunks Chunks) getClosestChunk(baseLineNumber int) *Chunk {
var result *Chunk

for i, chunk := range chunks {
if chunk.From.LineNumber > baseLineNumber {
if chunk.From.StartLineNumber() > baseLineNumber {
break
}

Expand All @@ -209,12 +209,16 @@ func (chunk Chunk) EndDelta() int {
return chunk.To.EndLineNumber() - chunk.From.EndLineNumber()
}

func (chunkRange ChunkRange) EndLineNumber() int {
func (chunkRange ChunkRange) StartLineNumber() int {
if chunkRange.LineCount == 0 {
return chunkRange.LineNumber
return chunkRange.LineNumber + 1
}

return chunkRange.LineNumber + chunkRange.LineCount - 1
return chunkRange.LineNumber
}

func (chunkRange ChunkRange) EndLineNumber() int {
return chunkRange.StartLineNumber() + chunkRange.LineCount - 1
}

func (chunkRange ChunkRange) Overlap(other ChunkRange) bool {
Expand Down
25 changes: 25 additions & 0 deletions internal/git/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,18 @@ var _ = Describe("Diff", func() {
})

var _ = Describe("ChunkRange", func() {
Describe("StartLineNumber", func() {
It("returns the line number", func() {
Expect(git.ChunkRange{LineNumber: 2, LineCount: 1}.StartLineNumber()).To(Equal(2))
})

When("there are no lines in the range", func() {
It("returns the next line after the line number", func() {
Expect(git.ChunkRange{LineNumber: 2, LineCount: 0}.StartLineNumber()).To(Equal(3))
})
})
})

Describe("EndLineNumber", func() {
It("returns the (inclusive) end line number", func() {
Expect(git.ChunkRange{LineNumber: 2, LineCount: 1}.EndLineNumber()).To(Equal(2))
Expand Down Expand Up @@ -228,6 +240,19 @@ var _ = Describe("Chunks", func() {
})
})

When("the base range is at an add chunk", func() {
chunks := git.Chunks{{
From: git.ChunkRange{LineNumber: 1, LineCount: 0},
To: git.ChunkRange{LineNumber: 2, LineCount: 1},
}}

It("returns a range shifted by the add", func() {
Expect(chunks.TranslateRange(git.ChunkRange{LineNumber: 2, LineCount: 2})).To(
Equal(git.ChunkRange{LineNumber: 3, LineCount: 2}),
)
})
})

When("the base range surrounds a remove chunk", func() {
chunks := git.Chunks{{
From: git.ChunkRange{LineNumber: 3, LineCount: 1},
Expand Down
3 changes: 2 additions & 1 deletion internal/git/git_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"os/exec"
"path"
"strings"
"testing"

. "github.com/onsi/ginkgo/v2"
Expand All @@ -22,7 +23,7 @@ func runGit(dir string, args ...string) {

output, err := command.CombinedOutput()
if err != nil {
Fail(fmt.Sprintf("failed to run git command: %s\n%s", err, output))
Fail(fmt.Sprintf("failed to run git command [%s]: %s\n%s", strings.Join(args, " "), err, output))
}
}

Expand Down

0 comments on commit cad7cde

Please sign in to comment.