Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable reproducible builds by suppressing date comment in properties #502

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,10 @@
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
</dependency>
<dependency>
<groupId>nu.studer</groupId>
<artifactId>java-ordered-properties</artifactId>
<version>1.0.4</version>
</dependency>
</dependencies>
</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import nu.studer.java.util.OrderedProperties;
import org.sonatype.plexus.build.incremental.BuildContext;
import pl.project13.core.log.LoggerBridge;
import pl.project13.core.util.SortedProperties;

import javax.annotation.Nonnull;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
Expand Down Expand Up @@ -86,8 +87,11 @@ public void maybeGeneratePropertiesFile(@Nonnull Properties localProperties, Fil
if (shouldGenerate) {
Files.createDirectories(gitPropsFile.getParentFile().toPath());
try (OutputStream outputStream = new FileOutputStream(gitPropsFile)) {
SortedProperties sortedLocalProperties = new SortedProperties();
sortedLocalProperties.putAll(localProperties);
OrderedProperties sortedLocalProperties = new OrderedProperties.OrderedPropertiesBuilder()
.withSuppressDateInComment(true)
.withOrdering(Comparator.nullsLast(Comparator.naturalOrder()))
.build();
localProperties.forEach((key, value) -> sortedLocalProperties.setProperty((String) key, (String) value));
if (isJsonFormat) {
try (Writer outputWriter = new OutputStreamWriter(outputStream, sourceCharset)) {
log.info("Writing json file to [{}] (for module {})...", gitPropsFile.getAbsolutePath(), projectName);
Expand Down
87 changes: 0 additions & 87 deletions core/src/main/java/pl/project13/core/util/SortedProperties.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* This file is part of git-commit-id-plugin by Konrad 'ktoso' Malawski <[email protected]>
*
* git-commit-id-plugin is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* git-commit-id-plugin is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with git-commit-id-plugin. If not, see <http://www.gnu.org/licenses/>.
*/

package pl.project13.core;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.sonatype.plexus.build.incremental.BuildContext;
import org.sonatype.plexus.build.incremental.DefaultBuildContext;
import pl.project13.core.log.LoggerBridge;
import pl.project13.core.log.StdOutLoggerBridge;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Properties;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertEquals;

public class PropertiesFileGeneratorTest {
@Rule
public final TemporaryFolder temporaryFolder = new TemporaryFolder();

private final LoggerBridge loggerBridge = new StdOutLoggerBridge(false);
private final BuildContext buildContext = new DefaultBuildContext();

private PropertiesFileGenerator propertiesFileGenerator;

@Before
public void setUp() {
propertiesFileGenerator = new PropertiesFileGenerator(loggerBridge, buildContext, "properties", "", "test");
}

@Test
public void generatedPropertiesFileDoesNotContainDateComment() throws GitCommitIdExecutionException, IOException {
Properties properties = new Properties();
properties.put(GitCommitPropertyConstant.COMMIT_ID_FULL, "b5993378ffadd1f84dc8da220b9204d157ec0f29");
properties.put(GitCommitPropertyConstant.BRANCH, "develop");

Path propertiesPath = temporaryFolder.getRoot().toPath().resolve("git.properties");
propertiesFileGenerator.maybeGeneratePropertiesFile(properties, temporaryFolder.getRoot(), propertiesPath.getFileName().toString(), UTF_8);

byte[] bytes = Files.readAllBytes(propertiesPath);
String actualContent = new String(bytes, UTF_8);
String expectedContent = "#Generated by Git-Commit-Id-Plugin\n"
+ "branch=develop\n"
+ "commit.id.full=b5993378ffadd1f84dc8da220b9204d157ec0f29\n";
assertEquals(expectedContent, actualContent);
}
}

This file was deleted.