You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ant.apache.org by ja...@apache.org on 2019/10/20 13:00:26 UTC

[ant] branch master updated: bz-62617 tstamp task - Honor SOURCE_DATE_EPOCH to allow reproducible builds

This is an automated email from the ASF dual-hosted git repository.

jaikiran pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ant.git


The following commit(s) were added to refs/heads/master by this push:
     new 1bfa788  bz-62617 tstamp task - Honor SOURCE_DATE_EPOCH to allow reproducible builds
1bfa788 is described below

commit 1bfa7880afc7cbbc1e3d73be5cfa77dc2fd216d2
Author: Jaikiran Pai <ja...@apache.org>
AuthorDate: Sun Oct 20 18:30:01 2019 +0530

    bz-62617 tstamp task - Honor SOURCE_DATE_EPOCH to allow reproducible builds
---
 CONTRIBUTORS                                       |  1 +
 WHATSNEW                                           |  4 ++++
 contributors.xml                                   |  4 ++++
 manual/Tasks/tstamp.html                           |  7 +++++++
 src/main/org/apache/tools/ant/taskdefs/Tstamp.java | 19 +++++++++++++++++--
 5 files changed, 33 insertions(+), 2 deletions(-)

diff --git a/CONTRIBUTORS b/CONTRIBUTORS
index 4d7a8cf..80171e7 100644
--- a/CONTRIBUTORS
+++ b/CONTRIBUTORS
@@ -221,6 +221,7 @@ Joseph Walton
 Josh Lucas
 Juerg Wanner
 Julian Simpson
+Julien Lepiller
 Justin Vallon
 Justyna Horwat
 Karl Jansen
diff --git a/WHATSNEW b/WHATSNEW
index 1eaf764..32424e4 100644
--- a/WHATSNEW
+++ b/WHATSNEW
@@ -20,6 +20,10 @@ Other changes:
  * The runant.py script should now work with Python 3.
    Github Pull Request #96
 
+ * tstamp task now honors SOURCE_DATE_EPOCH environment variable for
+   reproducible builds (https://reproducible-builds.org/specs/source-date-epoch/#idm55)
+   Bugzilla Report 62617
+
 Changes from Ant 1.10.6 TO Ant 1.10.7
 =====================================
 
diff --git a/contributors.xml b/contributors.xml
index e73660b..61040c2 100644
--- a/contributors.xml
+++ b/contributors.xml
@@ -924,6 +924,10 @@
     <last>Simpson</last>
   </name>
   <name>
+    <first>Julien</first>
+    <last>Lepiller</last>
+  </name>
+  <name>
     <first>Justin</first>
     <last>Vallon</last>
   </name>
diff --git a/manual/Tasks/tstamp.html b/manual/Tasks/tstamp.html
index 4360959..1661392 100644
--- a/manual/Tasks/tstamp.html
+++ b/manual/Tasks/tstamp.html
@@ -44,6 +44,13 @@ you could also specify that value in ISO-8601 format (<code>1972-04-17T08:07:00Z
 specify a value in an invalid format an INFO message will be logged and the value will be
 ignored.</p>
 
+<p>
+  <em>Since Ant 1.10.8</em> the <code>SOURCE_DATE_EPOCH</code> environment variable value (if set)
+  will be honoured for <a href="https://reproducible-builds.org/specs/source-date-epoch/#idm55">reproducible builds</a>.
+  Ant will log a DEBUG message if an invalid value (value that cannot be parsed to an integer), is specified
+  for that environment variable and will instead use the "current" date.
+</p>
+
 <h3>Parameters</h3>
 <table class="attr">
   <tr>
diff --git a/src/main/org/apache/tools/ant/taskdefs/Tstamp.java b/src/main/org/apache/tools/ant/taskdefs/Tstamp.java
index 226419c..077052d 100644
--- a/src/main/org/apache/tools/ant/taskdefs/Tstamp.java
+++ b/src/main/org/apache/tools/ant/taskdefs/Tstamp.java
@@ -50,6 +50,8 @@ import org.apache.tools.ant.types.EnumeratedAttribute;
  */
 public class Tstamp extends Task {
 
+    private static final String ENV_SOURCE_DATE_EPOCH = "SOURCE_DATE_EPOCH";
+
     private List<CustomFormat> customFormats = new Vector<>();
     private String prefix = "";
 
@@ -75,8 +77,21 @@ public class Tstamp extends Task {
     public void execute() throws BuildException {
         try {
             Date d = getNow();
-
-            customFormats.forEach(cts -> cts.execute(getProject(), d, getLocation()));
+            // Honour reproducible builds https://reproducible-builds.org/specs/source-date-epoch/#idm55
+            final String epoch = System.getenv(ENV_SOURCE_DATE_EPOCH);
+            try {
+                if (epoch != null) {
+                    // Value of SOURCE_DATE_EPOCH will be an integer, representing seconds.
+                    d = new Date(Integer.parseInt(epoch) * 1000);
+                }
+                log("Honouring environment variable " + ENV_SOURCE_DATE_EPOCH + " which has been set to " + epoch);
+            } catch(NumberFormatException e) {
+                // ignore
+                log("Ignoring invalid value '" + epoch + "' for " + ENV_SOURCE_DATE_EPOCH
+                        + " environment variable", Project.MSG_DEBUG);
+            }
+            final Date date = d;
+            customFormats.forEach(cts -> cts.execute(getProject(), date, getLocation()));
 
             SimpleDateFormat dstamp = new SimpleDateFormat("yyyyMMdd");
             setProperty("DSTAMP", dstamp.format(d));