lib/commit: respect SOURCE_DATE_EPOCH for commit timestamp

This tweaks `ostree_repo_write_commit` so that it checks for the
envinroment variable `SOURCE_DATE_EPOCH` as a way to override
the current time, which is used as the commit timestamp.

Ref: https://reproducible-builds.org/docs/source-date-epoch/
Ref: https://reproducible-builds.org/specs/source-date-epoch/
This commit is contained in:
Luca BRUNO committed 2021-06-22 12:15:18 +00:00
1 parent 47b7b1efc2
commit 70a8f56ce1
3 files changed
+69 -2

No files matched your search

+23 -2
View File
@@ -3020,6 +3020,10 @@ create_empty_gvariant_dict (void)
*
* Write a commit metadata object, referencing @root_contents_checksum
* and @root_metadata_checksum.
* This uses the current time as the commit timestamp, but it can be
* overridden with an explicit timestamp via the
* [standard](https://reproducible-builds.org/specs/source-date-epoch/)
* `SOURCE_DATE_EPOCH` environment flag.
*/
gboolean
ostree_repo_write_commit (OstreeRepo *self,
@@ -3032,9 +3036,26 @@ ostree_repo_write_commit (OstreeRepo *self,
GCancellable *cancellable,
GError **error)
{
g_autoptr(GDateTime) now = g_date_time_new_now_utc ();
gint64 timestamp = 0;
const gchar *env_timestamp = g_getenv ("SOURCE_DATE_EPOCH");
if (env_timestamp == NULL)
{
g_autoptr(GDateTime) now = g_date_time_new_now_utc ();
timestamp = g_date_time_to_unix (now);
}
else
{
gchar *ret = NULL;
errno = 0;
timestamp = g_ascii_strtoll (env_timestamp, &ret, 10);
if (errno != 0)
return glnx_throw_errno_prefix (error, "Parsing SOURCE_DATE_EPOCH");
if (ret == env_timestamp)
return glnx_throw (error, "Failed to convert SOURCE_DATE_EPOCH");
}
return ostree_repo_write_commit_with_time (self, parent, subject, body,
metadata, root, g_date_time_to_unix (now),
metadata, root, timestamp,
out_commit, cancellable, error);
}