Skip to content

Commit

Permalink
diff/merge: support config diff.algorithm and merge.conflictStyle
Browse files Browse the repository at this point in the history
  • Loading branch information
fcharlie committed Dec 17, 2024
1 parent c8acb26 commit ec38d3c
Show file tree
Hide file tree
Showing 10 changed files with 269 additions and 159 deletions.
23 changes: 21 additions & 2 deletions modules/diferenco/diferenco.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package diferenco
import (
"context"
"errors"
"fmt"
"io"
"slices"
"strings"
)

// https://github.com/Wilfred/difftastic/wiki/Line-Based-Diffs
Expand Down Expand Up @@ -58,6 +60,23 @@ var (
ErrUnsupportedAlgorithm = errors.New("unsupport algorithm")
)

var (
diffAlgorithms = map[string]Algorithm{
"histogram": Histogram,
"onp": ONP,
"myers": Myers,
"patience": Patience,
"minimal": Minimal,
}
)

func ParseAlgorithm(s string) (Algorithm, error) {
if a, ok := diffAlgorithms[strings.ToLower(s)]; ok {
return a, nil
}
return Unspecified, fmt.Errorf("unsupport algoritm '%s' %w", s, ErrUnsupportedAlgorithm)
}

// commonPrefixLength returns the length of the common prefix of two T slices.
func commonPrefixLength[E comparable](a, b []E) int {
n := min(len(a), len(b))
Expand Down Expand Up @@ -139,10 +158,10 @@ type Options struct {
From, To *File
S1, S2 string
R1, R2 io.Reader
A Algorithm
A Algorithm // algorithm
}

func diffInternal(ctx context.Context, L1, L2 []int, a Algorithm) ([]Change, error) {
func diffInternal[E comparable](ctx context.Context, L1, L2 []E, a Algorithm) ([]Change, error) {
if a == Unspecified {
switch {
case len(L1) < 5000 && len(L2) < 5000:
Expand Down
Loading

0 comments on commit ec38d3c

Please sign in to comment.