@@ -28,23 +28,23 @@ const (
28
28
)
29
29
30
30
// GetRawDiff dumps diff results of repository in given commit ID to io.Writer.
31
- func GetRawDiff (repoPath , commitID string , diffType RawDiffType , writer io.Writer ) error {
32
- return GetRawDiffForFile (repoPath , "" , commitID , diffType , "" , writer )
31
+ func GetRawDiff (ctx context. Context , repoPath , commitID string , diffType RawDiffType , writer io.Writer ) error {
32
+ return GetRawDiffForFile (ctx , repoPath , "" , commitID , diffType , "" , writer )
33
33
}
34
34
35
35
// GetRawDiffForFile dumps diff results of file in given commit ID to io.Writer.
36
- func GetRawDiffForFile (repoPath , startCommit , endCommit string , diffType RawDiffType , file string , writer io.Writer ) error {
36
+ func GetRawDiffForFile (ctx context. Context , repoPath , startCommit , endCommit string , diffType RawDiffType , file string , writer io.Writer ) error {
37
37
repo , err := OpenRepository (repoPath )
38
38
if err != nil {
39
39
return fmt .Errorf ("OpenRepository: %v" , err )
40
40
}
41
41
defer repo .Close ()
42
42
43
- return GetRepoRawDiffForFile (repo , startCommit , endCommit , diffType , file , writer )
43
+ return GetRepoRawDiffForFile (ctx , repo , startCommit , endCommit , diffType , file , writer )
44
44
}
45
45
46
46
// GetRepoRawDiffForFile dumps diff results of file in given commit ID to io.Writer according given repository
47
- func GetRepoRawDiffForFile (repo * Repository , startCommit , endCommit string , diffType RawDiffType , file string , writer io.Writer ) error {
47
+ func GetRepoRawDiffForFile (ctx context. Context , repo * Repository , startCommit , endCommit string , diffType RawDiffType , file string , writer io.Writer ) error {
48
48
commit , err := repo .GetCommit (endCommit )
49
49
if err != nil {
50
50
return fmt .Errorf ("GetCommit: %v" , err )
@@ -54,7 +54,7 @@ func GetRepoRawDiffForFile(repo *Repository, startCommit, endCommit string, diff
54
54
fileArgs = append (fileArgs , "--" , file )
55
55
}
56
56
// FIXME: graceful: These commands should have a timeout
57
- ctx , cancel := context .WithCancel (DefaultContext )
57
+ ctx , cancel := context .WithCancel (ctx )
58
58
defer cancel ()
59
59
60
60
var cmd * exec.Cmd
@@ -132,10 +132,10 @@ func isHeader(lof string) bool {
132
132
// CutDiffAroundLine cuts a diff of a file in way that only the given line + numberOfLine above it will be shown
133
133
// it also recalculates hunks and adds the appropriate headers to the new diff.
134
134
// Warning: Only one-file diffs are allowed.
135
- func CutDiffAroundLine (originalDiff io.Reader , line int64 , old bool , numbersOfLine int ) string {
135
+ func CutDiffAroundLine (originalDiff io.Reader , line int64 , old bool , numbersOfLine int ) ( string , error ) {
136
136
if line == 0 || numbersOfLine == 0 {
137
137
// no line or num of lines => no diff
138
- return ""
138
+ return "" , nil
139
139
}
140
140
scanner := bufio .NewScanner (originalDiff )
141
141
hunk := make ([]string , 0 )
@@ -213,15 +213,18 @@ func CutDiffAroundLine(originalDiff io.Reader, line int64, old bool, numbersOfLi
213
213
}
214
214
}
215
215
}
216
+ if err := scanner .Err (); err != nil {
217
+ return "" , err
218
+ }
216
219
217
220
// No hunk found
218
221
if currentLine == 0 {
219
- return ""
222
+ return "" , nil
220
223
}
221
224
// headerLines + hunkLine (1) = totalNonCodeLines
222
225
if len (hunk )- headerLines - 1 <= numbersOfLine {
223
226
// No need to cut the hunk => return existing hunk
224
- return strings .Join (hunk , "\n " )
227
+ return strings .Join (hunk , "\n " ), nil
225
228
}
226
229
var oldBegin , oldNumOfLines , newBegin , newNumOfLines int64
227
230
if old {
@@ -256,5 +259,5 @@ func CutDiffAroundLine(originalDiff io.Reader, line int64, old bool, numbersOfLi
256
259
// construct the new hunk header
257
260
newHunk [headerLines ] = fmt .Sprintf ("@@ -%d,%d +%d,%d @@" ,
258
261
oldBegin , oldNumOfLines , newBegin , newNumOfLines )
259
- return strings .Join (newHunk , "\n " )
262
+ return strings .Join (newHunk , "\n " ), nil
260
263
}
0 commit comments