Compare two sequences of lines; generate the delta as a unified diff. Unified diffs are a compact way of showing line changes and a few lines of context. The number of context lines is set by 'n' which defaults to three. By default, the diff control lines (those with ---, +++, or @@) are created
(writer io.Writer, diff UnifiedDiff)
| 565 | // 'fromfile', 'tofile', 'fromfiledate', and 'tofiledate'. |
| 566 | // The modification times are normally expressed in the ISO 8601 format. |
| 567 | func WriteUnifiedDiff(writer io.Writer, diff UnifiedDiff) error { |
| 568 | buf := bufio.NewWriter(writer) |
| 569 | defer buf.Flush() |
| 570 | wf := func(format string, args ...interface{}) error { |
| 571 | _, err := fmt.Fprintf(buf, format, args...) |
| 572 | return err |
| 573 | } |
| 574 | ws := func(s string) error { |
| 575 | _, err := buf.WriteString(s) |
| 576 | return err |
| 577 | } |
| 578 | |
| 579 | if len(diff.Eol) == 0 { |
| 580 | diff.Eol = "\n" |
| 581 | } |
| 582 | |
| 583 | started := false |
| 584 | m := NewMatcher(diff.A, diff.B) |
| 585 | for _, g := range m.GetGroupedOpCodes(diff.Context) { |
| 586 | if !started { |
| 587 | started = true |
| 588 | fromDate := "" |
| 589 | if len(diff.FromDate) > 0 { |
| 590 | fromDate = "\t" + diff.FromDate |
| 591 | } |
| 592 | toDate := "" |
| 593 | if len(diff.ToDate) > 0 { |
| 594 | toDate = "\t" + diff.ToDate |
| 595 | } |
| 596 | if diff.FromFile != "" || diff.ToFile != "" { |
| 597 | err := wf("--- %s%s%s", diff.FromFile, fromDate, diff.Eol) |
| 598 | if err != nil { |
| 599 | return err |
| 600 | } |
| 601 | err = wf("+++ %s%s%s", diff.ToFile, toDate, diff.Eol) |
| 602 | if err != nil { |
| 603 | return err |
| 604 | } |
| 605 | } |
| 606 | } |
| 607 | first, last := g[0], g[len(g)-1] |
| 608 | range1 := formatRangeUnified(first.I1, last.I2) |
| 609 | range2 := formatRangeUnified(first.J1, last.J2) |
| 610 | if err := wf("@@ -%s +%s @@%s", range1, range2, diff.Eol); err != nil { |
| 611 | return err |
| 612 | } |
| 613 | for _, c := range g { |
| 614 | i1, i2, j1, j2 := c.I1, c.I2, c.J1, c.J2 |
| 615 | if c.Tag == 'e' { |
| 616 | for _, line := range diff.A[i1:i2] { |
| 617 | if err := ws(" " + line); err != nil { |
| 618 | return err |
| 619 | } |
| 620 | } |
| 621 | continue |
| 622 | } |
| 623 | if c.Tag == 'r' || c.Tag == 'd' { |
| 624 | for _, line := range diff.A[i1:i2] { |
no test coverage detected