Skip to content

Commit

Permalink
Use a more generic approach for line ending handling
Browse files Browse the repository at this point in the history
For the compare-with-baseline we usually try our best to detect if a
real change is there but we can't get 100% confidence, therefore this
adds support for detecting line-ending-only changes in a wide rage of
file using a common heuristic (e.g. git / grep) by check if the file
contains a NULL-byte.

If we found that both files are probably text, we compare them ignoring
the line endings, otherwise they are assumed binary an assumed to be
different.
  • Loading branch information
laeubi committed Apr 18, 2023
1 parent 5fb874c commit bdaf851
Show file tree
Hide file tree
Showing 8 changed files with 665 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.eclipse.tycho.zipcomparator.internal;

import java.io.IOException;
import java.io.InputStream;

import org.codehaus.plexus.component.annotations.Component;
import org.eclipse.tycho.artifactcomparator.ArtifactComparator.ComparisonData;
Expand All @@ -27,7 +28,32 @@ public class DefaultContentsComparator implements ContentsComparator {
@Override
public ArtifactDelta getDelta(ComparatorInputStream baseline, ComparatorInputStream reactor, ComparisonData data)
throws IOException {
return baseline.compare(reactor);
if (isTextFile(baseline) && isTextFile(reactor)) {
//If both items a certainly a text file, we compare them ignoring line endings
return TextComparator.compareText(baseline, reactor);
}
return ArtifactDelta.DEFAULT;
}

/**
* This works like the git-diff tool that determine if a file is binary to look for any NULL
* byte in the file
*
* @param stream
* @return
* @throws IOException
*/
private static boolean isTextFile(ComparatorInputStream stream) throws IOException {
try (InputStream is = stream.asNewStream()) {
int i;
while ((i = is.read()) > -1) {
if (i == 0) {
//seems to be a binary file...
return false;
}
}
}
return true;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.io.IOUtils;
Expand All @@ -37,11 +36,14 @@ public class TextComparator implements ContentsComparator {

static final String HINT = "txt";

private static final Set<String> ALIAS = Set.of(HINT, "java", "javajet", "javajetinc", "html", "htm");

@Override
public ArtifactDelta getDelta(ComparatorInputStream baseline, ComparatorInputStream reactor, ComparisonData data)
throws IOException {
return compareText(baseline, reactor);
}

public static ArtifactDelta compareText(ComparatorInputStream baseline, ComparatorInputStream reactor)
throws IOException {
ByteIterator baselineIterator = new ByteIterator(baseline.asBytes());
ByteIterator reactorIterator = new ByteIterator(reactor.asBytes());
while (baselineIterator.hasNext() && reactorIterator.hasNext()) {
Expand Down Expand Up @@ -94,15 +96,7 @@ private void skipNewLines() {

@Override
public boolean matches(String nameOrExtension) {
if (ALIAS.contains(nameOrExtension.toLowerCase())) {
//a simple extension match
return true;
}
if (nameOrExtension.toLowerCase().endsWith("javax.inject.named")) {
//META-INF/sisu/javax.inject.Named ...
return true;
}
return false;
return HINT.equalsIgnoreCase(nameOrExtension);
}

public static ArtifactDelta createDelta(String message, ComparatorInputStream baseline,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ private ContentsComparator getContentsComparator(String name) {
}
return comparators.values().stream() //
.filter(c -> c.matches(name) || c.matches(extension)) //
.findFirst().orElse(null);
.findFirst().orElseGet(() -> comparators.get(DefaultContentsComparator.TYPE));
}

private static Map<String, ZipEntry> toEntryMap(ZipFile zip, MatchPatterns ignored) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.eclipse.tycho.artifactcomparator.ComparatorInputStream;
import org.eclipse.tycho.p2maven.repository.P2RepositoryManager;
import org.eclipse.tycho.zipcomparator.internal.ContentsComparator;
import org.eclipse.tycho.zipcomparator.internal.DefaultContentsComparator;
import org.eclipse.tycho.zipcomparator.internal.ManifestComparator;
import org.osgi.framework.BundleException;
import org.osgi.framework.Constants;
Expand Down Expand Up @@ -455,6 +456,7 @@ private ContentsComparator getComparator(String name) {
return alternative;
}
}
return contentComparators.get(DefaultContentsComparator.TYPE);
}
return comparator;
}
Expand Down
470 changes: 470 additions & 0 deletions tycho-its/projects/baseline/api-bundle/MPL-1.1.txt

Large diffs are not rendered by default.

128 changes: 128 additions & 0 deletions tycho-its/projects/baseline/api-bundle/about.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>About</title>
</head>
<body lang="EN-US">
<h2>About This Content</h2>

<p>November 30, 2017</p>
<h3>License</h3>

<p>
The Eclipse Foundation makes available all content in this plug-in
(&quot;Content&quot;). Unless otherwise indicated below, the Content
is provided to you under the terms and conditions of the Eclipse
Public License Version 2.0 (&quot;EPL&quot;). A copy of the EPL is
available at <a href="http://www.eclipse.org/legal/epl-2.0">http://www.eclipse.org/legal/epl-2.0</a>.
For purposes of the EPL, &quot;Program&quot; will mean the Content.
</p>

<p>
If you did not receive this Content directly from the Eclipse
Foundation, the Content is being redistributed by another party
(&quot;Redistributor&quot;) and different terms and conditions may
apply to your use of any object code in the Content. Check the
Redistributor's license that was provided with the Content. If no such
license exists, contact the Redistributor. Unless otherwise indicated
below, the terms and conditions of the EPL still apply to any source
code in the Content and such source code may be obtained at <a
href="http://www.eclipse.org/">http://www.eclipse.org</a>.
</p>

<h3>Third Party Content</h3>

<p>
The Content includes items that have been sourced from third parties as set out below. If you
did not receive this Content directly from the Eclipse Foundation, the following is provided
for informational purposes only, and you should look to the Redistributor&rsquo;s license for
terms and conditions of use.
</p>

<h4>Archetype 2.0-alpha-4</h4>
<p>
The plug-in includes software developed by The Apache Software Foundation as part of the Maven project.
Your use of Archetype 2.0-alpha-4 in binary code form contained in the plug-in is subject to the terms and conditions of the
The Apache Software License, Version 2.0 (&quot;ASL&quot;).
A copy of the ASL is available at <a href="http://maven.apache.org/license.html">http://maven.apache.org/license.html</a>.
(a local copy can be found <a href="about_files/LICENSE-2.0.txt">here</a>)
</p>
<p>The original binaries are available at the <a href="http://maven.org">Maven Central Repository</a>.</p>


<h4>Apache Commons Collections 3.2, Apache Commons I/O 1.3.2, Apache Commons Lang 2.1</h4>
<p>
The plug-in includes software developed by The Apache Software Foundation as part of the Apache Commons project.
Your use of Apache Commons Collections 3.2, Apache Commons I/O 1.3.2, Apache Commons Lang 2.1 in binary code form contained in the plug-in is subject to the terms and conditions of the
The Apache Software License, Version 2.0 (&quot;ASL&quot;).
A copy of the ASL is available at <a href="http://www.apache.org/licenses/LICENSE-2.0">http://www.apache.org/licenses/LICENSE-2.0</a>.
(a local copy can be found <a href="about_files/LICENSE-2.0.txt">here</a>)
</p>
<p>The original binaries are available at the <a href="http://maven.org">Maven Central Repository</a>.</p>



<h4>dom4j 2.1.3</h4>
<p>
Your use of dom4j 2.1.3 in binary code form contained in the plug-in is subject to the terms and conditions of
BSD style license (&quot;BSD&quot;).
A local copy of the license can be found <a href="about_files/dom4j-LICENSE.txt ">here</a>.
</p>
<p>The original binaries are available at the <a href="http://maven.org">Maven Central Repository</a>.</p>


<h4>jchardet 1.0</h4>
<p>
Your use of jchardet 1.0 in binary code form contained in the plug-in is subject to the terms and conditions of the
Mozilla Public License 1.1 (&quot;MPL&quot;).
A copy of the ASL is available at <a href="http://www.mozilla.org/MPL/MPL-1.1.html">http://www.mozilla.org/MPL/MPL-1.1.html</a>.
(a local copy can be found <a href="about_files/MPL-1.1.txt">here</a>)
</p>
<p>The original binaries are available at the <a href="http://maven.org">Maven Central Repository</a>.</p>


<h4>JDOM 1.0</h4>
<p>JDOM is available under an Apache-style open source license, with the acknowledgment clause removed..
This license is among the least restrictive license available, enabling developers to use JDOM in.
creating new products without requiring them to release their own products as open source..
This is the license model used by the Apache Project, which created the Apache server..
A copy of the license is contained in the file <a href="about_files/LICENSE.txt">jdom-LICENSE.txt</a>.
</p>



<h4>Jakarta-Oro 2.0.8</h4>
<p>The plug-in includes Jakarta-Oro 2.0.8 (&quot;Jakarta-Oro&quot;) developed by the Apache Software Foundation as part of the Jakarta project.
Your use of Jakarta-Oro 2.0.8 in binary code form contained in the plug-in is subject to the terms and conditions of the
The Apache Software License, Version 1.1 (&quot;ASL-1.1&quot;).
A copy of the ASL 1.1 is available at <a href="http://www.apache.org/licenses/LICENSE-1.1">http://www.apache.org/licenses/LICENSE-1.1</a>.
(a local copy can be found <a href="about_files/LICENSE-1.1.txt">here</a>)
</p>



<h4>Apache Velocity 1.5</h4>
<p>The plug-in includes Apache Velocity 1.5 developed by the Apache Software Foundation.
Your use of Apache Velocity is subject to the terms and conditions of the
Apache Software License 2.0. A copy of the license
is contained in the file <a href="about_files/LICENSE.txt">LICENSE-2.0.txt</a>
and is also available at <a href="http://www.apache.org/licenses/LICENSE-2.0.txt">
http://www.apache.org/licenses/LICENSE-2.0.txt</a>.</p>



<h4>Plexus Velocity 1.1.3</h4>
<p>
Your use of Plexus Velocity 1.1.3 in binary code form contained in the plug-in is subject to the terms and conditions of the
The Apache Software License, Version 2.0 (&quot;ASL&quot;).
A copy of the ASL is available at <a href="http://www.apache.org/licenses/LICENSE-2.0">http://www.apache.org/licenses/LICENSE-2.0</a>.
(a local copy can be found <a href="about_files/LICENSE-2.0.txt">here</a>)
</p>
<p>The original binaries are available at the <a href="http://maven.org">Maven Central Repository</a>.</p>



</body>
</html>
4 changes: 3 additions & 1 deletion tycho-its/projects/baseline/api-bundle/build.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
source.. = src/
output.. = bin/
bin.includes = META-INF/,\
.
.,\
about.html,\
MPL-1.1.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2022 Christoph Läubrich and others.
* Copyright (c) 2022, 2023 Christoph Läubrich and others.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -75,18 +75,41 @@ public void buildBaselineRepository() throws Exception {
/**
* Compares the baseline against itself...
*
* @throws Exception
* @throws Throwable
*/
@Test
public void testUnchangedApi() throws Throwable {
buildBaselineProject(false, r -> {
});
}

/**
* Compares the baseline against itself... but modify the line endings!
*
* @throws Throwable
*/
@Test
public void testChangedLineEndings() throws Throwable {
buildBaselineProject(false, projectPath -> {
for (String file : new String[] { "about.html", "MPL-1.1.txt" }) {
Path about = projectPath.resolve(file);
String string = Files.readString(about);
if (string.contains("\r\n")) {
string = string.replace("\r\n", "\n");
} else if (string.contains("\r")) {
string = string.replace("\r", "\n");
} else if (string.contains("\n")) {
string = string.replace("\n", "\r");
}
Files.writeString(about, string);
}
});
}

/**
* This adds a method to the interface
*
* @throws Exception
* @throws Throwable
*/
@Test
public void testAddMethod() throws Throwable {
Expand All @@ -110,7 +133,7 @@ public void testAddMethod() throws Throwable {
/**
* This adds a internal method to the interface
*
* @throws Exception
* @throws Throwable
*/
@Test
public void testAddInternalMethod() throws Throwable {
Expand All @@ -135,7 +158,7 @@ public void testAddInternalMethod() throws Throwable {
/**
* This adds a resource to the bundle
*
* @throws Exception
* @throws Throwable
*/
@Test
public void testAddResource() throws Throwable {
Expand Down

0 comments on commit bdaf851

Please sign in to comment.