(ctx context.Context, c *Change)
| 47 | } |
| 48 | |
| 49 | func filePatchWithContext(ctx context.Context, c *Change) (fdiff.FilePatch, error) { |
| 50 | from, to, err := c.Files() |
| 51 | if err != nil { |
| 52 | return nil, err |
| 53 | } |
| 54 | fromContent, fIsBinary, err := fileContent(from) |
| 55 | if err != nil { |
| 56 | return nil, err |
| 57 | } |
| 58 | |
| 59 | toContent, tIsBinary, err := fileContent(to) |
| 60 | if err != nil { |
| 61 | return nil, err |
| 62 | } |
| 63 | |
| 64 | if fIsBinary || tIsBinary { |
| 65 | return &textFilePatch{from: c.From, to: c.To}, nil |
| 66 | } |
| 67 | |
| 68 | diffs := diff.Do(fromContent, toContent) |
| 69 | |
| 70 | var chunks []fdiff.Chunk |
| 71 | for _, d := range diffs { |
| 72 | select { |
| 73 | case <-ctx.Done(): |
| 74 | return nil, ErrCanceled |
| 75 | default: |
| 76 | } |
| 77 | |
| 78 | var op fdiff.Operation |
| 79 | switch d.Type { |
| 80 | case dmp.DiffEqual: |
| 81 | op = fdiff.Equal |
| 82 | case dmp.DiffDelete: |
| 83 | op = fdiff.Delete |
| 84 | case dmp.DiffInsert: |
| 85 | op = fdiff.Add |
| 86 | } |
| 87 | |
| 88 | chunks = append(chunks, &textChunk{d.Text, op}) |
| 89 | } |
| 90 | |
| 91 | return &textFilePatch{ |
| 92 | chunks: chunks, |
| 93 | from: c.From, |
| 94 | to: c.To, |
| 95 | }, nil |
| 96 | |
| 97 | } |
| 98 | |
| 99 | func fileContent(f *File) (content string, isBinary bool, err error) { |
| 100 | if f == nil { |
no test coverage detected
searching dependent graphs…