Skip to content

Commit

Permalink
Fixing implausibly old time stamp 1970-01-01 00:00:00 by using the ti…
Browse files Browse the repository at this point in the history
…mestamp from the Git revision instead of default 0 value (#3883)

* Fixing implausibly old time stamp 1970-01-01 00:00:00 by using the timestamp from the Git revision instead of default 0 value

Signed-off-by: Andriy Redko <[email protected]>

* Address code review comments

Signed-off-by: Andriy Redko <[email protected]>

* Address code review comments (encapsulating the Git origin date inside build script

Signed-off-by: Andriy Redko <[email protected]>
(cherry picked from commit f165845)
  • Loading branch information
reta authored and github-actions[bot] committed Jul 13, 2022
1 parent 9fe6461 commit 953e7e3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
41 changes: 38 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
* under the License.
*/

import java.nio.charset.StandardCharsets;
import java.io.ByteArrayOutputStream;

import com.avast.gradle.dockercompose.tasks.ComposePull
import com.github.jengelman.gradle.plugins.shadow.ShadowPlugin
import de.thetaphi.forbiddenapis.gradle.ForbiddenApisPlugin
Expand All @@ -37,11 +40,15 @@ import org.opensearch.gradle.Version
import org.opensearch.gradle.VersionProperties
import org.opensearch.gradle.info.BuildParams
import org.opensearch.gradle.plugin.PluginBuildPlugin
import org.opensearch.gradle.tar.SymbolicLinkPreservingTar
import org.gradle.plugins.ide.eclipse.model.AccessRule
import org.gradle.plugins.ide.eclipse.model.EclipseJdt
import org.gradle.plugins.ide.eclipse.model.SourceFolder
import org.gradle.api.Project;
import org.gradle.process.ExecResult;
import org.gradle.util.DistributionLocator
import org.gradle.util.GradleVersion

import static org.opensearch.gradle.util.GradleUtils.maybeConfigure

plugins {
Expand Down Expand Up @@ -150,6 +157,30 @@ Map<String, String> buildMetadataMap = buildMetadataValue.tokenize(';').collectE
return [key, value]
}

/**
* Using 'git' command line (if available), tries to fetch the commit date of the current revision
* @return commit date of the current revision or 0 if it is not available
*/
long gitRevisionDate = {
// Try to get last commit date as Unix timestamp
try (ByteArrayOutputStream stdout = new ByteArrayOutputStream()) {
ExecResult result = project.exec(spec -> {
spec.setIgnoreExitValue(true);
spec.setStandardOutput(stdout);
spec.commandLine("git", "log", "-1", "--format=%ct");
});

if (result.getExitValue() == 0) {
return Long.parseLong(stdout.toString(StandardCharsets.UTF_8).replaceAll("\\s", "")) * 1000; /* seconds to millis */
}
} catch (IOException | GradleException | NumberFormatException ex) {
/* fall back to default Unix epoch timestamp */
}

return 0;
}()


// injecting groovy property variables into all projects
allprojects {
project.ext {
Expand Down Expand Up @@ -282,11 +313,15 @@ allprojects {
}

// support for reproducible builds
tasks.withType(AbstractArchiveTask).configureEach {
tasks.withType(AbstractArchiveTask).configureEach { task ->
// ignore file timestamps
// be consistent in archive file order
preserveFileTimestamps = false
reproducibleFileOrder = true
task.preserveFileTimestamps = false
task.reproducibleFileOrder = true
if (task instanceof SymbolicLinkPreservingTar) {
// Replace file timestamps with latest Git revision date (if available)
task.lastModifiedTimestamp = gitRevisionDate
}
}

project.afterEvaluate {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@
* This task is necessary because the built-in task {@link org.gradle.api.tasks.bundling.Tar} does not preserve symbolic links.
*/
public class SymbolicLinkPreservingTar extends Tar {
private long lastModifiedTimestamp = 0;

public void setLastModifiedTimestamp(long lastModifiedTimestamp) {
this.lastModifiedTimestamp = lastModifiedTimestamp;
}

@Override
protected CopyAction createCopyAction() {
Expand All @@ -80,23 +85,26 @@ protected CopyAction createCopyAction() {
compressor = new SimpleCompressor();
break;
}
return new SymbolicLinkPreservingTarCopyAction(getArchiveFile(), compressor, isPreserveFileTimestamps());
return new SymbolicLinkPreservingTarCopyAction(getArchiveFile(), compressor, isPreserveFileTimestamps(), lastModifiedTimestamp);
}

private static class SymbolicLinkPreservingTarCopyAction implements CopyAction {

private final Provider<RegularFile> tarFile;
private final ArchiveOutputStreamFactory compressor;
private final boolean isPreserveFileTimestamps;
private final long lastModifiedTimestamp;

SymbolicLinkPreservingTarCopyAction(
final Provider<RegularFile> tarFile,
final ArchiveOutputStreamFactory compressor,
final boolean isPreserveFileTimestamps
final boolean isPreserveFileTimestamps,
final long lastModifiedTimestamp
) {
this.tarFile = tarFile;
this.compressor = compressor;
this.isPreserveFileTimestamps = isPreserveFileTimestamps;
this.lastModifiedTimestamp = lastModifiedTimestamp;
}

@Override
Expand Down Expand Up @@ -219,7 +227,7 @@ private void handleProcessingException(final FileCopyDetailsInternal details, fi
}

private long getModTime(final FileCopyDetails details) {
return isPreserveFileTimestamps ? details.getLastModified() : 0;
return isPreserveFileTimestamps ? details.getLastModified() : lastModifiedTimestamp;
}

}
Expand Down

0 comments on commit 953e7e3

Please sign in to comment.