Skip to content

Commit

Permalink
Merge pull request #10 from antgroup/merge-algo
Browse files Browse the repository at this point in the history
merge: support config diff.algorithm and merge.conflictStyle and merge-file command
  • Loading branch information
fcharlie authored Dec 18, 2024
2 parents e0c7cb0 + 5af0cb5 commit c77bf09
Show file tree
Hide file tree
Showing 21 changed files with 612 additions and 244 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ bin/*
local/*
.DS_Store
*.gop1
*.tomlp1
*.rej
/out/
/.vscode/
Expand Down
1 change: 1 addition & 0 deletions cmd/zeta/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type App struct {
MergeBase command.MergeBase `cmd:"merge-base" help:"Find optimal common ancestors for merge"`
LsFiles command.LsFiles `cmd:"ls-files" help:"Show information about files in the index and the working tree"`
HashObject command.HashObject `cmd:"hash-object" help:"Compute hash or create object"`
MergeFile command.MergeFile `cmd:"merge-file" help:"Run a three-way file merge"`
Version command.Version `cmd:"version" help:"Display version information"`
Debug bool `name:"debug" help:"Enable debug mode; analyze timing"`
}
Expand Down
55 changes: 36 additions & 19 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 @@ -36,27 +38,42 @@ const (
Patience
)

var (
ErrUnsupportedAlgorithm = errors.New("unsupport algorithm")
)

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

func (a Algorithm) String() string {
switch a {
case Unspecified:
return "Unspecified"
case Histogram:
return "Histogram"
case Myers:
return "Myers"
case Minimal:
return "Minimal"
case ONP:
return "O(NP)"
case Patience:
return "Patience"
n, ok := algorithmNameMap[a]
if ok {
return n
}
return "Unknown"
return "unspecified"
}

var (
ErrUnsupportedAlgorithm = errors.New("unsupport algorithm")
)
func AlgorithmFromName(s string) (Algorithm, error) {
if a, ok := algorithmValueMap[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 {
Expand Down Expand Up @@ -139,10 +156,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 c77bf09

Please sign in to comment.