Skip to content

Commit

Permalink
feat: import upstream package and update patches
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Dec 13, 2023
1 parent 7ba4ccc commit a2d9def
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 67 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
From 19e4b9d424cb70882b6cdbac1be5977684cf0127 Mon Sep 17 00:00:00 2001
From 225b4fb5cb35cf333d28ce64b9bb9f9bc492cce7 Mon Sep 17 00:00:00 2001
From: Ayman Bagabas <[email protected]>
Date: Sun, 16 Apr 2023 02:49:56 -0400
Subject: [PATCH 1/1] fix: remove testenv
Date: Wed, 13 Dec 2023 12:38:49 -0500
Subject: [PATCH 1/2] fix: remove testenv package

Signed-off-by: Ayman Bagabas <[email protected]>
---
diff_test.go | 2 --
diff_test.go | 3 ---
difftest/difftest_test.go | 2 --
2 files changed, 4 deletions(-)
2 files changed, 5 deletions(-)

diff --git a/diff_test.go b/diff_test.go
index 90a343b078b2..5edf55ea6315 100644
index a5e18dbc06eb..dc0063db13ef 100644
--- a/diff_test.go
+++ b/diff_test.go
@@ -17,7 +17,6 @@ import (
Expand All @@ -21,19 +21,27 @@ index 90a343b078b2..5edf55ea6315 100644
)

func TestApply(t *testing.T) {
@@ -112,7 +111,6 @@ func TestLineEdits(t *testing.T) {
@@ -120,7 +119,6 @@ func TestLineEdits(t *testing.T) {
}

func TestToUnified(t *testing.T) {
- testenv.NeedsTool(t, "patch")
for _, tc := range difftest.TestCases {
t.Run(tc.Name, func(t *testing.T) {
unified, err := diff.ToUnified(difftest.FileA, difftest.FileB, tc.In, tc.Edits)
unified, err := diff.ToUnified(difftest.FileA, difftest.FileB, tc.In, tc.Edits, diff.DefaultContextLines)
@@ -156,7 +154,6 @@ func TestToUnified(t *testing.T) {
t.Errorf("applying unified failed: got\n%q, wanted\n%q unified\n%q",
got, tc.Out, unified)
}
-
})
}
}
diff --git a/difftest/difftest_test.go b/difftest/difftest_test.go
index d2dbc5c531a7..da5a3348a189 100644
index 02cfca0ccc3b..4ccfe7c43bba 100644
--- a/difftest/difftest_test.go
+++ b/difftest/difftest_test.go
@@ -16,11 +16,9 @@ import (
@@ -15,11 +15,9 @@ import (
"testing"

"github.com/aymanbagabas/go-udiff/difftest"
Expand All @@ -46,5 +54,5 @@ index d2dbc5c531a7..da5a3348a189 100644
t.Run(test.Name, func(t *testing.T) {
if test.NoDiff {
--
2.40.0
2.43.0

Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
From 2d0980789148ee5b2c822cb1746e0f8cba36ff67 Mon Sep 17 00:00:00 2001
From b347a6a816ada3af6e374a480d13d469979d8c34 Mon Sep 17 00:00:00 2001
From: Ayman Bagabas <[email protected]>
Date: Tue, 20 Jun 2023 13:11:56 -0400
Subject: [PATCH] fix: re-export symbols
Date: Wed, 13 Dec 2023 12:42:58 -0500
Subject: [PATCH 2/2] fix: re-export symbols

Signed-off-by: Ayman Bagabas <[email protected]>
---
unified.go | 116 ++++++++++++++++++++++++++---------------------------
1 file changed, 58 insertions(+), 58 deletions(-)
unified.go | 122 ++++++++++++++++++++++++++---------------------------
1 file changed, 61 insertions(+), 61 deletions(-)

diff --git a/unified.go b/unified.go
index 098bc157cc3b..98b15f4f523a 100644
index 85523a1b435a..268ad228825b 100644
--- a/unified.go
+++ b/unified.go
@@ -36,57 +36,57 @@ func ToUnified(oldLabel, newLabel, content string, edits []Edit) (string, error)
@@ -41,57 +41,57 @@ func ToUnified(oldLabel, newLabel, content string, edits []Edit, contextLines in

// unified represents a set of edits as a unified diff.
type unified struct {
Expand All @@ -26,7 +26,7 @@ index 098bc157cc3b..98b15f4f523a 100644
+ From string
+ // To is the name of the modified file.
+ To string
+ // Hunks is the set of edit hunks needed to transform the file content.
+ // Hunks is the set of edit Hunks needed to transform the file content.
+ Hunks []*hunk
}

Expand All @@ -50,7 +50,7 @@ index 098bc157cc3b..98b15f4f523a 100644
- // content is the content of this line.
+ // Kind is the type of line this represents, deletion, insertion or copy.
+ Kind OpKind
+ // Content is the content of this line.
+ // Content is the Content of this line.
// For deletion it is the line being removed, for all others it is the line
// to put in the output.
- content string
Expand Down Expand Up @@ -95,9 +95,9 @@ index 098bc157cc3b..98b15f4f523a 100644
return "equal"
default:
panic("unknown operation kind")
@@ -102,8 +102,8 @@ const (
// a unified diff that represents those edits.
func toUnified(fromName, toName string, content string, edits []Edit) (unified, error) {
@@ -103,8 +103,8 @@ func (k opKind) String() string {
func toUnified(fromName, toName string, content string, edits []Edit, contextLines int) (unified, error) {
gap := contextLines * 2
u := unified{
- from: fromName,
- to: toName,
Expand All @@ -106,10 +106,22 @@ index 098bc157cc3b..98b15f4f523a 100644
}
if len(edits) == 0 {
return u, nil
@@ -137,26 +137,26 @@ func toUnified(fromName, toName string, content string, edits []Edit) (unified,
@@ -129,35 +129,35 @@ func toUnified(fromName, toName string, content string, edits []Edit, contextLin

switch {
case h != nil && start == last:
- //direct extension
+ // direct extension
case h != nil && start <= last+gap:
- //within range of previous lines, add the joiners
+ // within range of previous lines, add the joiners
addEqualLines(h, lines, last, start)
default:
- //need to start a new hunk
+ // need to start a new hunk
if h != nil {
// add the edge to the previous hunk
addEqualLines(h, lines, last, last+edge)
addEqualLines(h, lines, last, last+contextLines)
- u.hunks = append(u.hunks, h)
+ u.Hunks = append(u.Hunks, h)
}
Expand All @@ -121,7 +133,7 @@ index 098bc157cc3b..98b15f4f523a 100644
+ ToLine: toLine + 1,
}
// add the edge to the new hunk
delta := addEqualLines(h, lines, start-edge, start)
delta := addEqualLines(h, lines, start-contextLines, start)
- h.fromLine -= delta
- h.toLine -= delta
+ h.FromLine -= delta
Expand All @@ -140,16 +152,16 @@ index 098bc157cc3b..98b15f4f523a 100644
toLine++
}
}
@@ -164,7 +164,7 @@ func toUnified(fromName, toName string, content string, edits []Edit) (unified,
@@ -165,7 +165,7 @@ func toUnified(fromName, toName string, content string, edits []Edit, contextLin
if h != nil {
// add the edge to the final hunk
addEqualLines(h, lines, last, last+edge)
addEqualLines(h, lines, last, last+contextLines)
- u.hunks = append(u.hunks, h)
+ u.Hunks = append(u.Hunks, h)
}
return u, nil
}
@@ -186,7 +186,7 @@ func addEqualLines(h *hunk, lines []string, start, end int) int {
@@ -187,7 +187,7 @@ func addEqualLines(h *hunk, lines []string, start, end int) int {
if i >= len(lines) {
return delta
}
Expand All @@ -158,7 +170,7 @@ index 098bc157cc3b..98b15f4f523a 100644
delta++
}
return delta
@@ -195,19 +195,19 @@ func addEqualLines(h *hunk, lines []string, start, end int) int {
@@ -196,19 +196,19 @@ func addEqualLines(h *hunk, lines []string, start, end int) int {
// String converts a unified diff to the standard textual form for that diff.
// The output of this function can be passed to tools like patch.
func (u unified) String() string {
Expand Down Expand Up @@ -186,7 +198,7 @@ index 098bc157cc3b..98b15f4f523a 100644
toCount++
default:
fromCount++
@@ -216,32 +216,32 @@ func (u unified) String() string {
@@ -217,32 +217,32 @@ func (u unified) String() string {
}
fmt.Fprint(b, "@@")
if fromCount > 1 {
Expand Down Expand Up @@ -234,5 +246,5 @@ index 098bc157cc3b..98b15f4f523a 100644
}
}
--
2.41.0
2.43.0

3 changes: 1 addition & 2 deletions diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func TestLineEdits(t *testing.T) {
func TestToUnified(t *testing.T) {
for _, tc := range difftest.TestCases {
t.Run(tc.Name, func(t *testing.T) {
unified, err := diff.ToUnified(difftest.FileA, difftest.FileB, tc.In, tc.Edits)
unified, err := diff.ToUnified(difftest.FileA, difftest.FileB, tc.In, tc.Edits, diff.DefaultContextLines)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -154,7 +154,6 @@ func TestToUnified(t *testing.T) {
t.Errorf("applying unified failed: got\n%q, wanted\n%q unified\n%q",
got, tc.Out, unified)
}

})
}
}
Expand Down
2 changes: 1 addition & 1 deletion difftest/difftest.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ func DiffTest(t *testing.T, compute func(before, after string) []diff.Edit) {
if err != nil {
t.Fatalf("Apply failed: %v", err)
}
unified, err := diff.ToUnified(FileA, FileB, test.In, edits)
unified, err := diff.ToUnified(FileA, FileB, test.In, edits, diff.DefaultContextLines)
if err != nil {
t.Fatalf("ToUnified: %v", err)
}
Expand Down
18 changes: 4 additions & 14 deletions ndiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func Strings(before, after string) []Edit {
return nil // common case
}

if stringIsASCII(before) && stringIsASCII(after) {
if isASCII(before) && isASCII(after) {
// TODO(adonovan): opt: specialize diffASCII for strings.
return diffASCII([]byte(before), []byte(after))
}
Expand All @@ -32,7 +32,7 @@ func Bytes(before, after []byte) []Edit {
return nil // common case
}

if bytesIsASCII(before) && bytesIsASCII(after) {
if isASCII(before) && isASCII(after) {
return diffASCII(before, after)
}
return diffRunes(runes(before), runes(after))
Expand Down Expand Up @@ -88,18 +88,8 @@ func runesLen(runes []rune) (len int) {
return len
}

// stringIsASCII reports whether s contains only ASCII.
// TODO(adonovan): combine when x/tools allows generics.
func stringIsASCII(s string) bool {
for i := 0; i < len(s); i++ {
if s[i] >= utf8.RuneSelf {
return false
}
}
return true
}

func bytesIsASCII(s []byte) bool {
// isASCII reports whether s contains only ASCII.
func isASCII[S string | []byte](s S) bool {
for i := 0; i < len(s); i++ {
if s[i] >= utf8.RuneSelf {
return false
Expand Down
37 changes: 19 additions & 18 deletions unified.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,29 @@ import (
"strings"
)

// DefaultContextLines is the number of unchanged lines of surrounding
// context displayed by Unified. Use ToUnified to specify a different value.
const DefaultContextLines = 3

// Unified returns a unified diff of the old and new strings.
// The old and new labels are the names of the old and new files.
// If the strings are equal, it returns the empty string.
func Unified(oldLabel, newLabel, old, new string) string {
edits := Strings(old, new)
unified, err := ToUnified(oldLabel, newLabel, old, edits)
unified, err := ToUnified(oldLabel, newLabel, old, edits, DefaultContextLines)
if err != nil {
// Can't happen: edits are consistent.
log.Fatalf("internal error in diff.Unified: %v", err)
}
return unified
}

// ToUnified applies the edits to content and returns a unified diff.
// ToUnified applies the edits to content and returns a unified diff,
// with contextLines lines of (unchanged) context around each diff hunk.
// The old and new labels are the names of the content and result files.
// It returns an error if the edits are inconsistent; see ApplyEdits.
func ToUnified(oldLabel, newLabel, content string, edits []Edit) (string, error) {
u, err := toUnified(oldLabel, newLabel, content, edits)
func ToUnified(oldLabel, newLabel, content string, edits []Edit, contextLines int) (string, error) {
u, err := toUnified(oldLabel, newLabel, content, edits, contextLines)
if err != nil {
return "", err
}
Expand All @@ -40,7 +45,7 @@ type unified struct {
From string
// To is the name of the modified file.
To string
// Hunks is the set of edit hunks needed to transform the file content.
// Hunks is the set of edit Hunks needed to transform the file content.
Hunks []*hunk
}

Expand All @@ -58,7 +63,7 @@ type hunk struct {
type line struct {
// Kind is the type of line this represents, deletion, insertion or copy.
Kind OpKind
// Content is the content of this line.
// Content is the Content of this line.
// For deletion it is the line being removed, for all others it is the line
// to put in the output.
Content string
Expand Down Expand Up @@ -93,14 +98,10 @@ func (k OpKind) String() string {
}
}

const (
edge = 3
gap = edge * 2
)

// toUnified takes a file contents and a sequence of edits, and calculates
// a unified diff that represents those edits.
func toUnified(fromName, toName string, content string, edits []Edit) (unified, error) {
func toUnified(fromName, toName string, content string, edits []Edit, contextLines int) (unified, error) {
gap := contextLines * 2
u := unified{
From: fromName,
To: toName,
Expand Down Expand Up @@ -128,15 +129,15 @@ func toUnified(fromName, toName string, content string, edits []Edit) (unified,

switch {
case h != nil && start == last:
//direct extension
// direct extension
case h != nil && start <= last+gap:
//within range of previous lines, add the joiners
// within range of previous lines, add the joiners
addEqualLines(h, lines, last, start)
default:
//need to start a new hunk
// need to start a new hunk
if h != nil {
// add the edge to the previous hunk
addEqualLines(h, lines, last, last+edge)
addEqualLines(h, lines, last, last+contextLines)
u.Hunks = append(u.Hunks, h)
}
toLine += start - last
Expand All @@ -145,7 +146,7 @@ func toUnified(fromName, toName string, content string, edits []Edit) (unified,
ToLine: toLine + 1,
}
// add the edge to the new hunk
delta := addEqualLines(h, lines, start-edge, start)
delta := addEqualLines(h, lines, start-contextLines, start)
h.FromLine -= delta
h.ToLine -= delta
}
Expand All @@ -163,7 +164,7 @@ func toUnified(fromName, toName string, content string, edits []Edit) (unified,
}
if h != nil {
// add the edge to the final hunk
addEqualLines(h, lines, last, last+edge)
addEqualLines(h, lines, last, last+contextLines)
u.Hunks = append(u.Hunks, h)
}
return u, nil
Expand Down

0 comments on commit a2d9def

Please sign in to comment.