summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/rubygems.rb17
-rw-r--r--lib/rubygems/package.rb2
-rw-r--r--lib/rubygems/package/tar_writer.rb8
-rw-r--r--lib/rubygems/specification.rb2
4 files changed, 23 insertions, 6 deletions
diff --git a/lib/rubygems.rb b/lib/rubygems.rb
index 4d06d98fdf7..2676fbd3db8 100644
--- a/lib/rubygems.rb
+++ b/lib/rubygems.rb
@@ -1242,6 +1242,23 @@ An Array (#{env.inspect}) was passed in from #{caller[3]}
end
+ ##
+ # The SOURCE_DATE_EPOCH environment variable (or, if that's not set, the current time), converted to Time object.
+ # This is used throughout RubyGems for enabling reproducible builds.
+ #
+ # If it is not set as an environment variable already, this also sets it.
+ #
+ # Details on SOURCE_DATE_EPOCH:
+ # https://reproducible-builds.org/specs/source-date-epoch/
+
+ def self.source_date_epoch
+ if ENV["SOURCE_DATE_EPOCH"].nil? || ENV["SOURCE_DATE_EPOCH"].empty?
+ ENV["SOURCE_DATE_EPOCH"] = Time.now.to_i.to_s
+ end
+
+ Time.at(ENV["SOURCE_DATE_EPOCH"].to_i).utc.freeze
+ end
+
# FIX: Almost everywhere else we use the `def self.` way of defining class
# methods, and then we switch over to `class << self` here. Pick one or the
# other.
diff --git a/lib/rubygems/package.rb b/lib/rubygems/package.rb
index 16cda5affec..bef37aedc1f 100644
--- a/lib/rubygems/package.rb
+++ b/lib/rubygems/package.rb
@@ -193,7 +193,7 @@ class Gem::Package
def initialize(gem, security_policy) # :notnew:
@gem = gem
- @build_time = ENV["SOURCE_DATE_EPOCH"] ? Time.at(ENV["SOURCE_DATE_EPOCH"].to_i).utc : Time.now
+ @build_time = Gem.source_date_epoch
@checksums = {}
@contents = nil
@digests = Hash.new { |h, algorithm| h[algorithm] = {} }
diff --git a/lib/rubygems/package/tar_writer.rb b/lib/rubygems/package/tar_writer.rb
index 87ee39a9449..96d8184e8ed 100644
--- a/lib/rubygems/package/tar_writer.rb
+++ b/lib/rubygems/package/tar_writer.rb
@@ -123,7 +123,7 @@ class Gem::Package::TarWriter
header = Gem::Package::TarHeader.new :name => name, :mode => mode,
:size => size, :prefix => prefix,
- :mtime => ENV["SOURCE_DATE_EPOCH"] ? Time.at(ENV["SOURCE_DATE_EPOCH"].to_i).utc : Time.now
+ :mtime => Gem.source_date_epoch
@io.write header
@io.pos = final_pos
@@ -217,7 +217,7 @@ class Gem::Package::TarWriter
header = Gem::Package::TarHeader.new(:name => name, :mode => mode,
:size => size, :prefix => prefix,
- :mtime => ENV["SOURCE_DATE_EPOCH"] ? Time.at(ENV["SOURCE_DATE_EPOCH"].to_i).utc : Time.now).to_s
+ :mtime => Gem.source_date_epoch).to_s
@io.write header
os = BoundedStream.new @io, size
@@ -245,7 +245,7 @@ class Gem::Package::TarWriter
:size => 0, :typeflag => "2",
:linkname => target,
:prefix => prefix,
- :mtime => ENV["SOURCE_DATE_EPOCH"] ? Time.at(ENV["SOURCE_DATE_EPOCH"].to_i).utc : Time.now).to_s
+ :mtime => Gem.source_date_epoch).to_s
@io.write header
@@ -298,7 +298,7 @@ class Gem::Package::TarWriter
header = Gem::Package::TarHeader.new :name => name, :mode => mode,
:typeflag => "5", :size => 0,
:prefix => prefix,
- :mtime => ENV["SOURCE_DATE_EPOCH"] ? Time.at(ENV["SOURCE_DATE_EPOCH"].to_i).utc : Time.now
+ :mtime => Gem.source_date_epoch
@io.write header
diff --git a/lib/rubygems/specification.rb b/lib/rubygems/specification.rb
index c023e4f4aa1..b3db311cbf3 100644
--- a/lib/rubygems/specification.rb
+++ b/lib/rubygems/specification.rb
@@ -1667,7 +1667,7 @@ class Gem::Specification < Gem::BasicSpecification
# https://reproducible-builds.org/specs/source-date-epoch/
def date
- @date ||= ENV["SOURCE_DATE_EPOCH"] ? Time.utc(*Time.at(ENV["SOURCE_DATE_EPOCH"].to_i).utc.to_a[3..5].reverse) : TODAY
+ @date ||= Time.utc(*Gem.source_date_epoch.utc.to_a[3..5].reverse)
end
DateLike = Object.new # :nodoc: