github.com/andrewrech/lazygit@v0.8.1/pkg/git/testdata/testPatchBefore2.diff (about) 1 diff --git a/pkg/git/patch_modifier.go b/pkg/git/patch_modifier.go 2 index a8fc600..6d8f7d7 100644 3 --- a/pkg/git/patch_modifier.go 4 +++ b/pkg/git/patch_modifier.go 5 @@ -36,18 +36,34 @@ func (p *PatchModifier) ModifyPatchForHunk(patch string, hunkStarts []int, curre 6 hunkEnd = hunkStarts[nextHunkStartIndex] 7 } 8 9 - headerLength := 4 10 + headerLength, err := getHeaderLength(lines) 11 + if err != nil { 12 + return "", err 13 + } 14 + 15 output := strings.Join(lines[0:headerLength], "\n") + "\n" 16 output += strings.Join(lines[hunkStart:hunkEnd], "\n") + "\n" 17 18 return output, nil 19 } 20 21 +func getHeaderLength(patchLines []string) (int, error) { 22 + for index, line := range patchLines { 23 + if strings.HasPrefix(line, "@@") { 24 + return index, nil 25 + } 26 + } 27 + return 0, errors.New("Could not find any hunks in this patch") 28 +} 29 + 30 // ModifyPatchForLine takes the original patch, which may contain several hunks, 31 // and the line number of the line we want to stage 32 func (p *PatchModifier) ModifyPatchForLine(patch string, lineNumber int) (string, error) { 33 lines := strings.Split(patch, "\n") 34 - headerLength := 4 35 + headerLength, err := getHeaderLength(lines) 36 + if err != nil { 37 + return "", err 38 + } 39 output := strings.Join(lines[0:headerLength], "\n") + "\n" 40 41 hunkStart, err := p.getHunkStart(lines, lineNumber) 42 @@ -124,13 +140,8 @@ func (p *PatchModifier) getModifiedHunk(patchLines []string, hunkStart int, line 43 // @@ -14,8 +14,9 @@ import ( 44 func (p *PatchModifier) updatedHeader(currentHeader string, lineChanges int) (string, error) { 45 // current counter is the number after the second comma 46 - re := regexp.MustCompile(`^[^,]+,[^,]+,(\d+)`) 47 - matches := re.FindStringSubmatch(currentHeader) 48 - if len(matches) < 2 { 49 - re = regexp.MustCompile(`^[^,]+,[^+]+\+(\d+)`) 50 - matches = re.FindStringSubmatch(currentHeader) 51 - } 52 - prevLengthString := matches[1] 53 + re := regexp.MustCompile(`(\d+) @@`) 54 + prevLengthString := re.FindStringSubmatch(currentHeader)[1] 55 56 prevLength, err := strconv.Atoi(prevLengthString) 57 if err != nil {