MCPcopy Create free account
hub / github.com/pmezard/go-difflib / WriteContextDiff

Function WriteContextDiff

difflib/difflib.go:674–757  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

672// The modification times are normally expressed in the ISO 8601 format.
673// If not specified, the strings default to blanks.
674func WriteContextDiff(writer io.Writer, diff ContextDiff) error {
675 buf := bufio.NewWriter(writer)
676 defer buf.Flush()
677 var diffErr error
678 wf := func(format string, args ...interface{}) {
679 _, err := buf.WriteString(fmt.Sprintf(format, args...))
680 if diffErr == nil && err != nil {
681 diffErr = err
682 }
683 }
684 ws := func(s string) {
685 _, err := buf.WriteString(s)
686 if diffErr == nil && err != nil {
687 diffErr = err
688 }
689 }
690
691 if len(diff.Eol) == 0 {
692 diff.Eol = "\n"
693 }
694
695 prefix := map[byte]string{
696 'i': "+ ",
697 'd': "- ",
698 'r': "! ",
699 'e': " ",
700 }
701
702 started := false
703 m := NewMatcher(diff.A, diff.B)
704 for _, g := range m.GetGroupedOpCodes(diff.Context) {
705 if !started {
706 started = true
707 fromDate := ""
708 if len(diff.FromDate) > 0 {
709 fromDate = "\t" + diff.FromDate
710 }
711 toDate := ""
712 if len(diff.ToDate) > 0 {
713 toDate = "\t" + diff.ToDate
714 }
715 if diff.FromFile != "" || diff.ToFile != "" {
716 wf("*** %s%s%s", diff.FromFile, fromDate, diff.Eol)
717 wf("--- %s%s%s", diff.ToFile, toDate, diff.Eol)
718 }
719 }
720
721 first, last := g[0], g[len(g)-1]
722 ws("***************" + diff.Eol)
723
724 range1 := formatRangeContext(first.I1, last.I2)
725 wf("*** %s ****%s", range1, diff.Eol)
726 for _, c := range g {
727 if c.Tag == 'r' || c.Tag == 'd' {
728 for _, cc := range g {
729 if cc.Tag == 'i' {
730 continue
731 }

Callers 1

GetContextDiffStringFunction · 0.85

Calls 3

NewMatcherFunction · 0.85
formatRangeContextFunction · 0.85
GetGroupedOpCodesMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…