@@ -791,15 +791,17 @@ func formatRangeUnified(start, stop int) string {
791791}
792792
793793// Unified diff parameters
794- type UnifiedDiff struct {
795- A []string // First sequence lines
796- FromFile string // First file name
797- FromDate string // First file time
798- B []string // Second sequence lines
799- ToFile string // Second file name
800- ToDate string // Second file time
801- Eol string // Headers end of line, defaults to LF
802- Context int // Number of context lines
794+ type LineDiffParams struct {
795+ A []string // First sequence lines
796+ FromFile string // First file name
797+ FromDate string // First file time
798+ B []string // Second sequence lines
799+ ToFile string // Second file name
800+ ToDate string // Second file time
801+ Eol string // Headers end of line, defaults to LF
802+ Context int // Number of context lines
803+ AutoJunk bool // If true, use autojunking
804+ IsJunkLine func (string )bool // How to spot junk lines
803805}
804806
805807// Compare two sequences of lines; generate the delta as a unified diff.
@@ -821,7 +823,7 @@ type UnifiedDiff struct {
821823// times. Any or all of these may be specified using strings for
822824// 'fromfile', 'tofile', 'fromfiledate', and 'tofiledate'.
823825// The modification times are normally expressed in the ISO 8601 format.
824- func WriteUnifiedDiff (writer io.Writer , diff UnifiedDiff ) error {
826+ func WriteUnifiedDiff (writer io.Writer , diff LineDiffParams ) error {
825827 //buf := bufio.NewWriter(writer)
826828 //defer buf.Flush()
827829 var bld strings.Builder
@@ -841,6 +843,9 @@ func WriteUnifiedDiff(writer io.Writer, diff UnifiedDiff) error {
841843
842844 started := false
843845 m := NewMatcher (diff .A , diff .B )
846+ if diff .AutoJunk || diff .IsJunkLine != nil {
847+ m = NewMatcherWithJunk (diff .A , diff .B , diff .AutoJunk , diff .IsJunkLine )
848+ }
844849 for _ , g := range m .GetGroupedOpCodes (diff .Context ) {
845850 if ! started {
846851 started = true
@@ -902,7 +907,7 @@ func WriteUnifiedDiff(writer io.Writer, diff UnifiedDiff) error {
902907}
903908
904909// Like WriteUnifiedDiff but returns the diff a string.
905- func GetUnifiedDiffString (diff UnifiedDiff ) (string , error ) {
910+ func GetUnifiedDiffString (diff LineDiffParams ) (string , error ) {
906911 w := & bytes.Buffer {}
907912 err := WriteUnifiedDiff (w , diff )
908913 return string (w .Bytes ()), err
@@ -922,7 +927,9 @@ func formatRangeContext(start, stop int) string {
922927 return fmt .Sprintf ("%d,%d" , beginning , beginning + length - 1 )
923928}
924929
925- type ContextDiff UnifiedDiff
930+ // For backward compatibility. Ugh.
931+ type ContextDiff = LineDiffParams
932+ type UnifiedDiff = LineDiffParams
926933
927934// Compare two sequences of lines; generate the delta as a context diff.
928935//
@@ -941,7 +948,7 @@ type ContextDiff UnifiedDiff
941948// strings for diff.FromFile, diff.ToFile, diff.FromDate, diff.ToDate.
942949// The modification times are normally expressed in the ISO 8601 format.
943950// If not specified, the strings default to blanks.
944- func WriteContextDiff (writer io.Writer , diff ContextDiff ) error {
951+ func WriteContextDiff (writer io.Writer , diff LineDiffParams ) error {
945952 buf := bufio .NewWriter (writer )
946953 defer buf .Flush ()
947954 var diffErr error
@@ -971,6 +978,9 @@ func WriteContextDiff(writer io.Writer, diff ContextDiff) error {
971978
972979 started := false
973980 m := NewMatcher (diff .A , diff .B )
981+ if diff .AutoJunk || diff .IsJunkLine != nil {
982+ m = NewMatcherWithJunk (diff .A , diff .B , diff .AutoJunk , diff .IsJunkLine )
983+ }
974984 for _ , g := range m .GetGroupedOpCodes (diff .Context ) {
975985 if ! started {
976986 started = true
@@ -1026,15 +1036,15 @@ func WriteContextDiff(writer io.Writer, diff ContextDiff) error {
10261036 return diffErr
10271037}
10281038
1029- // Like WriteContextDiff but returns the diff a string.
1030- func GetContextDiffString (diff ContextDiff ) (string , error ) {
1039+ // Like WriteContextDiff but returns the diff as a string.
1040+ func GetContextDiffString (diff LineDiffParams ) (string , error ) {
10311041 w := & bytes.Buffer {}
10321042 err := WriteContextDiff (w , diff )
10331043 return string (w .Bytes ()), err
10341044}
10351045
10361046// Split a string on "\n" while preserving them. The output can be used
1037- // as input for UnifiedDiff and ContextDiff structures .
1047+ // as input for LineDiffParams .
10381048func SplitLines (s string ) []string {
10391049 lines := strings .SplitAfter (s , "\n " )
10401050 lines [len (lines )- 1 ] += "\n "
0 commit comments