Move apply methods to an Applier type

This removes the distinction between "strict" and "fuzzy" application
by allowing future methods on Applier that control settings. It also
avoids state tracking in the text fragment apply signature by moving it
into the Applier type.

While in practice, an Applier will be used once and discarded, the
capability is provided to reset it for multiple uses.
This commit is contained in:
Billy Keyes
2020-01-23 23:02:31 -08:00
parent 3d1274d16e
commit 18058d1e15
4 changed files with 231 additions and 101 deletions
+6 -1
View File
@@ -1,6 +1,7 @@
package gitdiff
import (
"errors"
"fmt"
"os"
)
@@ -64,6 +65,10 @@ func (f *TextFragment) Header() string {
// Validate checks that the fragment is self-consistent and appliable. Validate
// returns an error if and only if the fragment is invalid.
func (f *TextFragment) Validate() error {
if f == nil {
return errors.New("nil fragment")
}
var (
oldLines, newLines int64
leadingContext, trailingContext int64
@@ -117,7 +122,7 @@ func (f *TextFragment) Validate() error {
// if a file is being created, it can only contain additions
if f.OldPosition == 0 && f.OldLines != 0 {
return fmt.Errorf("file creation fragment contains context or deletion lines")
return errors.New("file creation fragment contains context or deletion lines")
}
return nil