Compare two sequences of lines; generate the delta as a context diff. Context diffs are a compact way of showing line changes and a few lines of context. The number of context lines is set by diff.Context which defaults to three. By default, the diff control lines (those with *** or ---) are creat
(writer io.Writer, diff ContextDiff)
| 675 | // The modification times are normally expressed in the ISO 8601 format. |
| 676 | // If not specified, the strings default to blanks. |
| 677 | func WriteContextDiff(writer io.Writer, diff ContextDiff) error { |
| 678 | buf := bufio.NewWriter(writer) |
| 679 | defer buf.Flush() |
| 680 | var diffErr error |
| 681 | wf := func(format string, args ...interface{}) { |
| 682 | _, err := buf.WriteString(fmt.Sprintf(format, args...)) |
| 683 | if diffErr == nil && err != nil { |
| 684 | diffErr = err |
| 685 | } |
| 686 | } |
| 687 | ws := func(s string) { |
| 688 | _, err := buf.WriteString(s) |
| 689 | if diffErr == nil && err != nil { |
| 690 | diffErr = err |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | if len(diff.Eol) == 0 { |
| 695 | diff.Eol = "\n" |
| 696 | } |
| 697 | |
| 698 | prefix := map[byte]string{ |
| 699 | 'i': "+ ", |
| 700 | 'd': "- ", |
| 701 | 'r': "! ", |
| 702 | 'e': " ", |
| 703 | } |
| 704 | |
| 705 | started := false |
| 706 | m := NewMatcher(diff.A, diff.B) |
| 707 | for _, g := range m.GetGroupedOpCodes(diff.Context) { |
| 708 | if !started { |
| 709 | started = true |
| 710 | fromDate := "" |
| 711 | if len(diff.FromDate) > 0 { |
| 712 | fromDate = "\t" + diff.FromDate |
| 713 | } |
| 714 | toDate := "" |
| 715 | if len(diff.ToDate) > 0 { |
| 716 | toDate = "\t" + diff.ToDate |
| 717 | } |
| 718 | if diff.FromFile != "" || diff.ToFile != "" { |
| 719 | wf("*** %s%s%s", diff.FromFile, fromDate, diff.Eol) |
| 720 | wf("--- %s%s%s", diff.ToFile, toDate, diff.Eol) |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | first, last := g[0], g[len(g)-1] |
| 725 | ws("***************" + diff.Eol) |
| 726 | |
| 727 | range1 := formatRangeContext(first.I1, last.I2) |
| 728 | wf("*** %s ****%s", range1, diff.Eol) |
| 729 | for _, c := range g { |
| 730 | if c.Tag == 'r' || c.Tag == 'd' { |
| 731 | for _, cc := range g { |
| 732 | if cc.Tag == 'i' { |
| 733 | continue |
| 734 | } |
no test coverage detected
searching dependent graphs…