Skip to content
This repository was archived by the owner on Dec 25, 2024. It is now read-only.
47 changes: 28 additions & 19 deletions patchutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,26 +241,32 @@ func MixedModePath(oldSourcePath, newSourcePath string, oldDiff, newDiff io.Read
switch {
case !oldSourceStat.IsDir() && !newSourceStat.IsDir():
// Both sources are files
oldD, err := diff.NewFileDiffReader(oldDiff).Read()
oldDiffs, err := diff.NewMultiFileDiffReader(oldDiff).ReadAllFiles()
if err != nil {
return "", fmt.Errorf("parsing oldDiff for %q: %w",
oldSourcePath, err)
}

if oldSourcePath != oldD.OrigName {
return "", fmt.Errorf("filenames mismatch for oldSourcePath: %q and oldDiff: %q",
oldSourcePath, oldD.OrigName)
var oldD *diff.FileDiff
if len(oldDiffs) > 0 {
oldD = oldDiffs[0]
if oldSourcePath != oldD.OrigName {
return "", fmt.Errorf("filenames mismatch for oldSourcePath: %q and oldDiff: %q",
oldSourcePath, oldD.OrigName)
}
}

newD, err := diff.NewFileDiffReader(newDiff).Read()
newDiffs, err := diff.NewMultiFileDiffReader(newDiff).ReadAllFiles()
if err != nil {
return "", fmt.Errorf("parsing newDiff for %q: %w",
newSourcePath, err)
}

if newSourcePath != newD.OrigName {
return "", fmt.Errorf("filenames mismatch for newSourcePath: %q and newDiff: %q",
newSourcePath, newD.OrigName)
var newD *diff.FileDiff
if len(newDiffs) > 0 {
newD = newDiffs[0]
if newSourcePath != newD.OrigName {
return "", fmt.Errorf("filenames mismatch for newSourcePath: %q and newDiff: %q",
newSourcePath, newD.OrigName)
}
}

resultString, err := mixedModeFilePath(oldSourcePath, newSourcePath, oldD, newD)
Expand Down Expand Up @@ -312,6 +318,9 @@ func applyDiff(source string, diffFile *diff.FileDiff) (string, error) {
if strings.HasPrefix(line, "+") {
newBody = append(newBody, line[1:])
} else {
if currentOrgSourceI > int32(len(sourceBody)) {
return "", errors.New("diff content is out of source content")
}
if line[1:] != sourceBody[currentOrgSourceI-1] {
return "", fmt.Errorf(
"line %d in source (%q) and diff (%q): %w",
Expand Down Expand Up @@ -525,7 +534,7 @@ func mixedModeDirPath(oldSourcePath, newSourcePath string, oldDiff, newDiff io.R
}

// New files have been added to the new version
for filename := range oldFileDiffs {
for filename := range newFileDiffs {
result += fmt.Sprintf("Only in %s: %s\n", filepath.Dir(filename),
filepath.Base(filename))
}
Expand Down Expand Up @@ -561,18 +570,18 @@ func convertChunksIntoFileDiff(chunks []dbd.Chunk, fileDiff *diff.FileDiff) {
NewStartLine: currentNewI,
}
// Delete empty chunks in the beginning
for len(chunks) > 0 && len(chunks[0].Added) == 0 && len(chunks[0].Deleted) == 0 && len(chunks[0].Equal) == 0 {
for len(chunks) > 0 && chunks[0].Added == nil && chunks[0].Deleted == nil && chunks[0].Equal == nil {
chunks = chunks[1:]
}
// Delete empty chunks in the end
last := len(chunks) - 1
for len(chunks) > 0 && len(chunks[last].Added) == 0 && len(chunks[last].Deleted) == 0 && len(chunks[last].Equal) == 0 {
for len(chunks) > 0 && chunks[last].Added == nil && chunks[last].Deleted == nil && chunks[last].Equal == nil {
chunks = chunks[:last]
last--
}

// If chunks contains only one element with only unchanged lines
if len(chunks) == 1 && len(chunks[0].Added) == 0 && len(chunks[0].Deleted) == 0 {
if len(chunks) == 1 && chunks[0].Added == nil && chunks[0].Deleted == nil {
return
}

Expand All @@ -584,7 +593,7 @@ func convertChunksIntoFileDiff(chunks []dbd.Chunk, fileDiff *diff.FileDiff) {
}

// If first chunk contains only equal lines, we are adding last contextLines to currentHunk
if len(chunks[0].Added) == 0 && len(chunks[0].Deleted) == 0 {
if chunks[0].Added == nil && chunks[0].Deleted == nil {
currentOldI += int32(len(chunks[0].Equal))
currentNewI += int32(len(chunks[0].Equal))
if len(chunks[0].Equal) > contextLines {
Expand Down Expand Up @@ -616,7 +625,7 @@ func convertChunksIntoFileDiff(chunks []dbd.Chunk, fileDiff *diff.FileDiff) {
}
}
// Removing processed equal lines from last chunk
chunks[last].Equal = []string{}
chunks[last].Equal = nil
}

for _, c := range chunks {
Expand All @@ -637,8 +646,8 @@ func convertChunksIntoFileDiff(chunks []dbd.Chunk, fileDiff *diff.FileDiff) {
for _, line := range c.Equal[:contextLines] {
currentHunkBody = append(currentHunkBody, " "+line)
}
currentHunk.OrigLines = currentOldI + contextLines + 1 - currentHunk.OrigStartLine
currentHunk.NewLines = currentNewI + contextLines + 1 - currentHunk.NewStartLine
currentHunk.OrigLines = currentOldI + contextLines - currentHunk.OrigStartLine
currentHunk.NewLines = currentNewI + contextLines - currentHunk.NewStartLine
currentHunk.Body = []byte(strings.Join(currentHunkBody, "\n") + "\n")
fileDiff.Hunks = append(fileDiff.Hunks, currentHunk)
}
Expand All @@ -653,7 +662,7 @@ func convertChunksIntoFileDiff(chunks []dbd.Chunk, fileDiff *diff.FileDiff) {

// Clean currentHunkBody
currentHunkBody = []string{}
for _, line := range c.Equal[len(c.Equal)-contextLines-1:] {
for _, line := range c.Equal[len(c.Equal)-contextLines:] {
currentHunkBody = append(currentHunkBody, " "+line)
}

Expand Down
59 changes: 38 additions & 21 deletions patchutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ var mixedModePathFileTests = []struct {
resultFile: "mi_f7_od_nd.diff",
wantErr: false,
},
{
oldSource: "mixed_old_source/f6_no_changes_to_olddiff.txt",
oldDiffFile: "mi_f6_os_od.diff",
newSource: "mixed_new_source/f6_no_changes_to_olddiff.txt",
newDiffFile: "mi_f6_ns_nd.diff",
resultFile: "mi_f6_od_nd.diff",
wantErr: false,
},
{
oldSource: "mixed_old_source",
oldDiffFile: "mi_os_od.diff",
Expand Down Expand Up @@ -176,19 +184,17 @@ func init() {
func TestInterDiffMode(t *testing.T) {
for _, tt := range interDiffFileTests {
t.Run(tt.resultFile, func(t *testing.T) {
var fileA, errA = os.Open(tt.diffAFile)
var fileB, errB = os.Open(tt.diffBFile)

if errA != nil {
fileA, err := os.Open(tt.diffAFile)
if err != nil {
t.Errorf("Error in opening %s file.", tt.diffAFile)
}

if errB != nil {

fileB, err := os.Open(tt.diffBFile)
if err != nil {
t.Errorf("Error in opening %s file.", tt.diffBFile)
}

correctResult, err := ioutil.ReadFile(tt.resultFile)

if err != nil {
t.Error(err)
}
Expand All @@ -197,11 +203,13 @@ func TestInterDiffMode(t *testing.T) {
var readerB io.Reader = fileB

currentResult, err := InterDiff(readerA, readerB)


want := normalizeNewlines(correctResult)
got := normalizeNewlines([]byte(currentResult))
if (tt.wantErr == nil) && (err == nil) {
if d := cmp.Diff(normalizeNewlines(correctResult), normalizeNewlines([]byte(currentResult))); d != "" {
if !cmp.Equal(want, got){
t.Errorf("File contents mismatch for %s (-want +got):\n%s",
tt.resultFile, d)
tt.resultFile, cmp.Diff(want, got))
}
} else if !errors.Is(err, tt.wantErr) {
t.Errorf("Interdiff mode for %q: got error %v; want error %v", tt.resultFile, err, tt.wantErr)
Expand Down Expand Up @@ -234,10 +242,13 @@ func TestApplyDiff(t *testing.T) {
}

currentResult, err := applyDiff(string(source), d)

want := normalizeNewlines(correctResult)
got := normalizeNewlines([]byte(currentResult))
if (tt.wantErr == nil) && (err == nil) {
if d := cmp.Diff(normalizeNewlines(correctResult), normalizeNewlines([]byte(currentResult))); d != "" {
if !cmp.Equal(want, got) {
t.Errorf("File contents mismatch for %s (-want +got):\n%s",
tt.resultFile, d)
tt.resultFile, cmp.Diff(want, got))
}
} else if !errors.Is(err, tt.wantErr) {
t.Errorf("Applying diff for %q: got error %v; want error %v", tt.resultFile, err, tt.wantErr)
Expand Down Expand Up @@ -293,10 +304,12 @@ func TestMixedMode(t *testing.T) {
t.Errorf("printing result diff for file %q: %v",
tt.resultFile, err)
}

if d := cmp.Diff(normalizeNewlines(correctResult), normalizeNewlines([]byte(currentResult))); d != "" {

want := normalizeNewlines(correctResult)
got := normalizeNewlines([]byte(currentResult))
if !cmp.Equal(want, got) {
t.Errorf("File contents mismatch for %s (-want +got):\n%s",
tt.resultFile, d)
tt.resultFile, cmp.Diff(want, got))
}
})
}
Expand Down Expand Up @@ -335,10 +348,12 @@ func TestMixedModeFile(t *testing.T) {
if err != nil {
t.Errorf("Mixed mode for %q: got error %v; want error nil", tt.resultFile, err)
}

if d := cmp.Diff(normalizeNewlines(correctResult), normalizeNewlines([]byte(currentResult))); d != "" {

want := normalizeNewlines(correctResult)
got := normalizeNewlines([]byte(currentResult))
if !cmp.Equal(want, got) {
t.Errorf("File contents mismatch for %s (-want +got):\n%s",
tt.resultFile, d)
tt.resultFile, cmp.Diff(want, got))
}
})
}
Expand Down Expand Up @@ -370,10 +385,12 @@ func TestMixedModePath(t *testing.T) {
if err != nil {
t.Errorf("MixedModePath for %q: got error %v; want error nil", tt.resultFile, err)
}

if d := cmp.Diff(normalizeNewlines(correctResult), normalizeNewlines([]byte(currentResult))); d != "" {

want := normalizeNewlines(correctResult)
got := normalizeNewlines([]byte(currentResult))
if !cmp.Equal(want, got) {
t.Errorf("File contents mismatch for %s (-want +got):\n%s",
tt.resultFile, d)
tt.resultFile, cmp.Diff(want, got))
}
}
})
Expand Down
2 changes: 0 additions & 2 deletions test_examples/mi_f6_ns_nd.diff
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,5 @@
Do-do-do-do ah-ah-ah, do-do-do-do, Cities of Gold.
Do-do-do-do, Cities of Gold. Ah-ah-ah-ah-ah…
-some day we will find The Cities of Gold.
\ No newline at end of file
+some day we will find The Cities of Gold.
+some day we will find The Cities of Silver.
\ No newline at end of file
14 changes: 4 additions & 10 deletions test_examples/mi_f6_od_nd.diff
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
--- mixed_updated_old_source/f6_no_changes_to_olddiff.txt 2020-10-30 11:12:55.910740594 +0000
--- mixed_old_source/f6_no_changes_to_olddiff.txt
+++ mixed_updated_new_source/f6_no_changes_to_olddiff.txt 2020-10-30 11:32:22.322300642 +0000
@@ -1,5 +1,6 @@
@@ -1,4 +1,5 @@
Text is generated in https://vole.wtf/text-generator/
Children of the sun,
+Children of the sun,
see your time has just begun,
searching for your ways,
through adventures every day.
@@ -11,4 +12,5 @@
Ah-ah-ah-ah-ah… some day we will find The Cities of Gold.
Do-do-do-do ah-ah-ah, do-do-do-do, Cities of Gold.
@@ -13,2 +14,3 @@
Do-do-do-do, Cities of Gold. Ah-ah-ah-ah-ah…
-some day we will find The Cities of Gold.
\ No newline at end of file
+some day we will find The Cities of Gold.
some day we will find The Cities of Gold.
+some day we will find The Cities of Silver.
\ No newline at end of file
4 changes: 2 additions & 2 deletions test_examples/mi_nd_od.diff
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ Only in mixed_updated_new_source: f3_deleted_in_olddiff.txt
+++ mixed_old_source/f5_same_in_both_sources.txt
--- mixed_updated_new_source/f6_no_changes_to_olddiff.txt
+++ mixed_old_source/f6_no_changes_to_olddiff.txt
@@ -1,6 +1,5 @@
@@ -1,5 +1,4 @@
Text is generated in https://vole.wtf/text-generator/
Children of the sun,
-Children of the sun,
see your time has just begun,
searching for your ways,
@@ -14,3 +13,2 @@
Do-do-do-do ah-ah-ah, do-do-do-do, Cities of Gold.
Do-do-do-do, Cities of Gold. Ah-ah-ah-ah-ah…
some day we will find The Cities of Gold.
-some day we will find The Cities of Silver.
Expand Down Expand Up @@ -42,3 +41,4 @@ Only in mixed_updated_new_source: f3_deleted_in_olddiff.txt
-flowers, rivers, sand and sea,
snowflakes and the stars are free.
Every day and night,
Only in mixed_updated_old_source: f2_added_in_old_diff.txt
4 changes: 1 addition & 3 deletions test_examples/mi_od_nd.diff
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ Only in mixed_updated_new_source: f3_deleted_in_olddiff.txt
+++ mixed_new_source/f5_same_in_both_sources.txt
--- mixed_old_source/f6_no_changes_to_olddiff.txt
+++ mixed_updated_new_source/f6_no_changes_to_olddiff.txt
@@ -1,5 +1,6 @@
@@ -1,4 +1,5 @@
Text is generated in https://vole.wtf/text-generator/
Children of the sun,
+Children of the sun,
see your time has just begun,
searching for your ways,
@@ -13,2 +14,3 @@
Do-do-do-do ah-ah-ah, do-do-do-do, Cities of Gold.
Do-do-do-do, Cities of Gold. Ah-ah-ah-ah-ah…
some day we will find The Cities of Gold.
+some day we will find The Cities of Silver.
Expand Down Expand Up @@ -43,4 +42,3 @@ Only in mixed_updated_new_source: f3_deleted_in_olddiff.txt
snowflakes and the stars are free.
Every day and night,
Only in mixed_updated_old_source: f2_added_in_old_diff.txt
Only in mixed_updated_old_source: f2_added_in_old_diff.txt