Skip to content

Commit

Permalink
diff: reimplement diff to reduce resource consumption
Browse files Browse the repository at this point in the history
  • Loading branch information
fcharlie committed Dec 16, 2024
1 parent a8e99bb commit 8fa2b2c
Show file tree
Hide file tree
Showing 46 changed files with 685 additions and 4,990 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ SOURCE_DIR := $(abspath $(dir $(lastword ${MAKEFILE_LIST})))
BUILD_DIR := ${SOURCE_DIR}/_build
BUILD_TIME := $(shell date +'%Y-%m-%dT%H:%M:%S%z')
BUILD_COMMIT := $(shell git rev-parse --short HEAD 2>/dev/null || echo 'none')
BUILD_VERSION := $(shell cat VERSION || echo '0.14.0')
BUILD_VERSION := $(shell cat VERSION || echo '0.15.0')
GO_PACKAGES := $(shell go list ./... | grep -v '^${PKG}/mock/' | grep -v '^${PKG}/proto/')
GO_LDFLAGS := -ldflags '-X ${PKG}/pkg/version.version=${BUILD_VERSION} -X ${PKG}/pkg/version.buildTime=${BUILD_TIME} -X ${PKG}/pkg/version.buildCommit=${BUILD_COMMIT}'

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.14.0
0.15.0
2 changes: 1 addition & 1 deletion bali.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "zeta"
summary = "HugeSCM - A next generation cloud-based version control system"
description = "HugeSCM - A next generation cloud-based version control system"
package-name = "alipay-linkc-zeta"
version = "0.14.0"
version = "0.15.0"
license = "MIT"
prefix = "/usr/local"
packager = "江二"
Expand Down
2 changes: 1 addition & 1 deletion cmd/zeta-mc/crate.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "zeta-mc"
description = "zeta-mc - Migrate Git repository to zeta"
destination = "bin"
version = "0.14.0"
version = "0.15.0"
goflags = [
"-ldflags",
"-X github.com/antgroup/hugescm/pkg/version.version=$BUILD_VERSION -X github.com/antgroup/hugescm/pkg/version.buildTime=$BUILD_TIME -X github.com/antgroup/hugescm/pkg/version.buildCommit=$BUILD_COMMIT",
Expand Down
2 changes: 1 addition & 1 deletion cmd/zeta/crate.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "zeta"
description = "HugeSCM - A next generation cloud-based version control system"
destination = "bin"
version = "0.14.0"
version = "0.15.0"
goflags = [
"-ldflags",
"-X github.com/antgroup/hugescm/pkg/version.version=$BUILD_VERSION -X github.com/antgroup/hugescm/pkg/version.buildTime=$BUILD_TIME -X github.com/antgroup/hugescm/pkg/version.buildCommit=$BUILD_COMMIT",
Expand Down
12 changes: 6 additions & 6 deletions modules/diferenco/diferenco.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ type StringDiff struct {
Text string
}

type Stats struct {
Del, Ins, Hunks int
type FileStat struct {
Addition, Deletion, Hunks int
}

type Options struct {
Expand Down Expand Up @@ -167,7 +167,7 @@ func diffInternal(ctx context.Context, L1, L2 []int, a Algorithm) ([]Change, err
}
}

func DoStats(ctx context.Context, opts *Options) (*Stats, error) {
func Stat(ctx context.Context, opts *Options) (*FileStat, error) {
sink := &Sink{
Index: make(map[string]int),
}
Expand All @@ -183,12 +183,12 @@ func DoStats(ctx context.Context, opts *Options) (*Stats, error) {
if err != nil {
return nil, err
}
stats := &Stats{
stats := &FileStat{
Hunks: len(changes),
}
for _, ch := range changes {
stats.Del += ch.Del
stats.Ins += ch.Ins
stats.Addition += ch.Ins
stats.Deletion += ch.Del
}
return stats, nil
}
48 changes: 36 additions & 12 deletions modules/diferenco/diferenco_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestDiff(t *testing.T) {
now := time.Now()
u, err := DoUnified(context.Background(), &Options{
From: &File{
Path: "a.txt",
Name: "a.txt",
},
To: nil,
S1: textA,
Expand Down Expand Up @@ -64,12 +64,12 @@ func TestPatchFD(t *testing.T) {
textB := string(bytesB)
u, err := DoUnified(context.Background(), &Options{
From: &File{
Path: "a.txt",
Name: "a.txt",
Hash: "4789568",
Mode: 0o10644,
},
To: &File{
Path: "b.txt",
Name: "b.txt",
Hash: "6547898",
Mode: 0o10644,
},
Expand Down Expand Up @@ -101,12 +101,12 @@ func TestPatch(t *testing.T) {
textB := string(bytesB)
u, err := DoUnified(context.Background(), &Options{
From: &File{
Path: "a.txt",
Name: "a.txt",
Hash: "4789568",
Mode: 0o10644,
},
To: &File{
Path: "b.txt",
Name: "b.txt",
Hash: "6547898",
Mode: 0o10644,
},
Expand All @@ -133,7 +133,7 @@ func TestPatchNew(t *testing.T) {
u, err := DoUnified(context.Background(), &Options{
From: nil,
To: &File{
Path: "a.txt",
Name: "a.txt",
Hash: "6547898",
Mode: 0o10644,
},
Expand All @@ -159,7 +159,7 @@ func TestPatchDelete(t *testing.T) {
textA := string(bytesA)
u, err := DoUnified(context.Background(), &Options{
From: &File{
Path: "a.txt",
Name: "a.txt",
Hash: "6547898",
Mode: 0o10644,
},
Expand Down Expand Up @@ -189,7 +189,7 @@ foo bar
31df1778815171897c907daf454c4419cfaa46f9`
u, err := DoUnified(context.Background(), &Options{
From: &File{
Path: "a.txt",
Name: "a.txt",
Hash: "6547898",
Mode: 0o10644,
},
Expand Down Expand Up @@ -222,12 +222,12 @@ func TestPatchScss(t *testing.T) {
textB := string(bytesB)
u, err := DoUnified(context.Background(), &Options{
From: &File{
Path: "a.txt",
Name: "a.txt",
Hash: "4789568",
Mode: 0o10644,
},
To: &File{
Path: "b.txt",
Name: "b.txt",
Hash: "6547898",
Mode: 0o10644,
},
Expand Down Expand Up @@ -259,12 +259,12 @@ func TestPatchCss(t *testing.T) {
textB := string(bytesB)
u, err := DoUnified(context.Background(), &Options{
From: &File{
Path: "a.txt",
Name: "a.txt",
Hash: "4789568",
Mode: 0o10644,
},
To: &File{
Path: "b.txt",
Name: "b.txt",
Hash: "6547898",
Mode: 0o10644,
},
Expand All @@ -278,3 +278,27 @@ func TestPatchCss(t *testing.T) {
e.SetColor(color.NewColorConfig())
_ = e.Encode([]*Unified{u})
}

func TestShowPatch(t *testing.T) {
patch := []*Unified{
{
From: &File{
Name: "docs/a.png",
Hash: "1ab12893fc666524ed79caae503e12c20a748e2f92db7730c8be09d981970f96",
Mode: 33188,
},
IsBinary: true,
},
{
To: &File{
Name: "images/windows7.iso",
Hash: "adba50d9794b9ef3f7ec8cbc680f7f1fa3fbf9df0ac8d1f9b9ccab6d941bc11b",
Mode: 33188,
},
IsFragments: true,
},
}
e := NewUnifiedEncoder(os.Stderr)
e.SetColor(color.NewColorConfig())
_ = e.Encode(patch)
}
4 changes: 2 additions & 2 deletions modules/diferenco/histogram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func TestHistogram(t *testing.T) {
a := sink.SplitLines(textA)
b := sink.SplitLines(textB)
changes, _ := HistogramDiff(context.Background(), a, b)
u := sink.ToUnified(&File{Path: "a.txt"}, &File{Path: "b.txt"}, changes, a, b, DefaultContextLines)
u := sink.ToUnified(&File{Name: "a.txt"}, &File{Name: "b.txt"}, changes, a, b, DefaultContextLines)
e := NewUnifiedEncoder(os.Stderr)
e.SetColor(color.NewColorConfig())
e.Encode([]*Unified{u})
_ = e.Encode([]*Unified{u})
}
4 changes: 2 additions & 2 deletions modules/diferenco/minimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func TestMinimalDiff(t *testing.T) {
a := sink.SplitLines(textA)
b := sink.SplitLines(textB)
changes, _ := MinimalDiff(context.Background(), a, b)
u := sink.ToUnified(&File{Path: "a.txt"}, &File{Path: "b.txt"}, changes, a, b, DefaultContextLines)
u := sink.ToUnified(&File{Name: "a.txt"}, &File{Name: "b.txt"}, changes, a, b, DefaultContextLines)
e := NewUnifiedEncoder(os.Stderr)
e.SetColor(color.NewColorConfig())
e.Encode([]*Unified{u})
_ = e.Encode([]*Unified{u})
}
4 changes: 2 additions & 2 deletions modules/diferenco/myers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestMyersDiff2(t *testing.T) {
a := sink.SplitLines(textA)
b := sink.SplitLines(textB)
changes, _ := MyersDiff(context.Background(), a, b)
u := sink.ToUnified(&File{Path: "a.txt"}, &File{Path: "b.txt"}, changes, a, b, DefaultContextLines)
u := sink.ToUnified(&File{Name: "a.txt"}, &File{Name: "b.txt"}, changes, a, b, DefaultContextLines)
fmt.Fprintf(os.Stderr, "diff:\n%s\n", u.String())
}

Expand All @@ -91,6 +91,6 @@ func TestMyersDiff3(t *testing.T) {
a := sink.SplitLines(textA)
b := sink.SplitLines(textB)
changes, _ := MyersDiff(context.Background(), a, b)
u := sink.ToUnified(&File{Path: "a.txt"}, &File{Path: "b.txt"}, changes, a, b, DefaultContextLines)
u := sink.ToUnified(&File{Name: "a.txt"}, &File{Name: "b.txt"}, changes, a, b, DefaultContextLines)
fmt.Fprintf(os.Stderr, "diff:\n%s\n", u.String())
}
55 changes: 39 additions & 16 deletions modules/diferenco/unified.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,35 @@ import (
const DefaultContextLines = 3

type File struct {
Path string
Hash string
Mode uint32
Name string `json:"name"`
Hash string `json:"hash"`
Mode uint32 `json:"mode"`
}

// unified represents a set of edits as a unified diff.
type Unified struct {
// From is the name of the original file.
From *File
From *File `json:"from,omitempty"`
// To is the name of the modified file.
To *File
To *File `json:"to,omitempty"`
// IsBinary returns true if this patch is representing a binary file.
IsBinary bool
IsBinary bool `json:"binary"`
// Fragments returns true if this patch is representing a fragments file.
IsFragments bool
IsFragments bool `json:"fragments"`
// Message prefix, eg: warning: something
Message string
Message string `json:"message"`
// Hunks is the set of edit Hunks needed to transform the file content.
Hunks []*Hunk
Hunks []*Hunk `json:"hunks,omitempty"`
}

func (u Unified) Stat() FileStat {
s := FileStat{Hunks: len(u.Hunks)}
for _, h := range u.Hunks {
ins, del := h.Stat()
s.Addition += ins
s.Deletion += del
}
return s
}

// String converts a unified diff to the standard textual form for that diff.
Expand All @@ -43,12 +53,12 @@ func (u Unified) String() string {
}
b := new(strings.Builder)
if u.From != nil {
fmt.Fprintf(b, "--- %s\n", u.From.Path)
fmt.Fprintf(b, "--- %s\n", u.From.Name)
} else {
fmt.Fprintf(b, "--- /dev/null\n")
}
if u.To != nil {
fmt.Fprintf(b, "+++ %s\n", u.To.Path)
fmt.Fprintf(b, "+++ %s\n", u.To.Name)
} else {
fmt.Fprintf(b, "+++ /dev/null\n")
}
Expand Down Expand Up @@ -104,16 +114,29 @@ func (u Unified) String() string {
// Hunk represents a contiguous set of line edits to apply.
type Hunk struct {
// The line in the original source where the hunk starts.
FromLine int
FromLine int `json:"from_line"`
// The line in the original source where the hunk finishes.
ToLine int
ToLine int `json:"to_line"`
// The set of line based edits to apply.
Lines []Line
Lines []Line `json:"lines,omitempty"`
}

func (h Hunk) Stat() (int, int) {
var ins, del int
for _, l := range h.Lines {
switch l.Kind {
case Delete:
del++
case Insert:
ins++
}
}
return ins, del
}

type Line struct {
Kind Operation
Content string
Kind Operation `json:"kind"`
Content string `json:"content"`
}

func DoUnified(ctx context.Context, opts *Options) (*Unified, error) {
Expand Down
Loading

0 comments on commit 8fa2b2c

Please sign in to comment.