Skip to content

Commit 2de6339

Browse files
committed
record hunk start position
1 parent 99814f6 commit 2de6339

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

diff/diff.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ type Hunk struct {
4545

4646
Section string // optional section heading
4747

48+
// 0-indexed line offset in unified file diff (including section headers);
49+
// this is only set when Hunks are read from entire file diff (i.e., when ReadAllHunks is called)
50+
// This accounts for hunk headers, too, so the StartPosition of the first hunk will be 1.
51+
StartPosition int
52+
4853
Body []byte // hunk body (lines prefixed with '-', '+', or ' ')
4954
}
5055

diff/parse.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,13 +492,17 @@ func normalizeHeader(header string) (string, string, error) {
492492
// reported.
493493
func (r *HunksReader) ReadAllHunks() ([]*Hunk, error) {
494494
var hunks []*Hunk
495+
linesRead := 0
495496
for {
496497
hunk, err := r.ReadHunk()
497498
if err == io.EOF {
498499
return hunks, nil
499500
}
500501
if hunk != nil {
502+
linesRead++ // account for the hunk header line
503+
hunk.StartPosition = linesRead
501504
hunks = append(hunks, hunk)
505+
linesRead += bytes.Count(hunk.Body, []byte{'\n'})
502506
}
503507
if err != nil {
504508
return hunks, err

0 commit comments

Comments
 (0)