cutLinesFromFullFileDiff reads from r and writes to w headers and between startLine and endLine. if startLine and endLine is equal to 0 then it uses io.Copy warning: changes are possible and param endLine may not exist in the future nolint:gocognit
(w io.Writer, r io.Reader, startLine, endLine int)
| 94 | // |
| 95 | //nolint:gocognit |
| 96 | func cutLinesFromFullFileDiff(w io.Writer, r io.Reader, startLine, endLine int) error { |
| 97 | if startLine < 0 { |
| 98 | startLine = 0 |
| 99 | } |
| 100 | |
| 101 | if endLine < 0 { |
| 102 | endLine = 0 |
| 103 | } |
| 104 | |
| 105 | if startLine == 0 && endLine > 0 { |
| 106 | startLine = 1 |
| 107 | } |
| 108 | |
| 109 | if endLine < startLine { |
| 110 | endLine = 0 |
| 111 | } |
| 112 | |
| 113 | // no need for processing lines just copy the data |
| 114 | if startLine == 0 && endLine == 0 { |
| 115 | _, err := io.Copy(w, r) |
| 116 | return err |
| 117 | } |
| 118 | |
| 119 | linePos := 0 |
| 120 | start := false |
| 121 | scanner := bufio.NewScanner(r) |
| 122 | for scanner.Scan() { |
| 123 | line := scanner.Bytes() |
| 124 | |
| 125 | if start { |
| 126 | linePos++ |
| 127 | } |
| 128 | |
| 129 | if endLine > 0 && linePos > endLine { |
| 130 | break |
| 131 | } |
| 132 | |
| 133 | if linePos > 0 && |
| 134 | (startLine > 0 && linePos < startLine) { |
| 135 | continue |
| 136 | } |
| 137 | |
| 138 | if len(line) >= 2 && bytes.HasPrefix(line, []byte{'@', '@'}) { |
| 139 | hunk, ok := parser.ParseDiffHunkHeader(string(line)) // TBD: maybe reader? |
| 140 | if !ok { |
| 141 | return fmt.Errorf("failed to extract lines from diff, range [%d,%d] : %w", |
| 142 | startLine, endLine, ErrParseDiffHunkHeader) |
| 143 | } |
| 144 | line = modifyHeader(hunk, startLine, endLine) |
| 145 | start = true |
| 146 | } |
| 147 | |
| 148 | if _, err := w.Write(line); err != nil { |
| 149 | return err |
| 150 | } |
| 151 | |
| 152 | if _, err := w.Write([]byte{'\n'}); err != nil { |
| 153 | return err |
searching dependent graphs…