Skip to content

Commit 6982b80

Browse files
duckinatorhsbt
authored andcommitted
[rubygems/rubygems] Default to a SOURCE_DATE_EPOCH of 315619200, to simplify reproducible builds.
ruby/rubygems@1d5a627398
1 parent 4fe882e commit 6982b80

File tree

3 files changed

+21
-18
lines changed

3 files changed

+21
-18
lines changed

lib/rubygems.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,13 @@ module Gem
156156
specifications/default
157157
].freeze
158158

159+
##
160+
# The default value for SOURCE_DATE_EPOCH if not specified.
161+
# We want a date after 1980-01-01, to prevent issues with Zip files.
162+
# This particular timestamp is for 1980-01-02 00:00:00 GMT.
163+
164+
DEFAULT_SOURCE_DATE_EPOCH = 315_619_200
165+
159166
@@win_platform = nil
160167

161168
@configuration = nil
@@ -1155,8 +1162,7 @@ def self.use_gemdeps(path = nil)
11551162

11561163
##
11571164
# If the SOURCE_DATE_EPOCH environment variable is set, returns it's value.
1158-
# Otherwise, returns the time that +Gem.source_date_epoch_string+ was
1159-
# first called in the same format as SOURCE_DATE_EPOCH.
1165+
# Otherwise, returns DEFAULT_SOURCE_DATE_EPOCH as a string.
11601166
#
11611167
# NOTE(@duckinator): The implementation is a tad weird because we want to:
11621168
# 1. Make builds reproducible by default, by having this function always
@@ -1171,15 +1177,12 @@ def self.use_gemdeps(path = nil)
11711177
# https://reproducible-builds.org/specs/source-date-epoch/
11721178

11731179
def self.source_date_epoch_string
1174-
# The value used if $SOURCE_DATE_EPOCH is not set.
1175-
@default_source_date_epoch ||= Time.now.to_i.to_s
1176-
11771180
specified_epoch = ENV["SOURCE_DATE_EPOCH"]
11781181

11791182
# If it's empty or just whitespace, treat it like it wasn't set at all.
11801183
specified_epoch = nil if !specified_epoch.nil? && specified_epoch.strip.empty?
11811184

1182-
epoch = specified_epoch || @default_source_date_epoch
1185+
epoch = specified_epoch || DEFAULT_SOURCE_DATE_EPOCH.to_s
11831186

11841187
epoch.strip
11851188
end

test/rubygems/test_gem_package_tar_writer.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def test_add_file
3333
f.write "a" * 10
3434
end
3535

36-
assert_headers_equal(tar_file_header("x", "", 0o644, 10, Time.now),
36+
assert_headers_equal(tar_file_header("x", "", 0o644, 10, Time.at(Gem::DEFAULT_SOURCE_DATE_EPOCH).utc),
3737
@io.string[0, 512])
3838
end
3939
assert_equal "aaaaaaaaaa#{"\0" * 502}", @io.string[512, 512]
@@ -54,7 +54,7 @@ def test_add_symlink
5454
Time.stub :now, Time.at(1_458_518_157) do
5555
@tar_writer.add_symlink "x", "y", 0o644
5656

57-
assert_headers_equal(tar_symlink_header("x", "", 0o644, Time.now, "y"),
57+
assert_headers_equal(tar_symlink_header("x", "", 0o644, Time.at(Gem::DEFAULT_SOURCE_DATE_EPOCH).utc, "y"),
5858
@io.string[0, 512])
5959
end
6060
assert_equal 512, @io.pos
@@ -86,7 +86,7 @@ def test_add_file_digest
8686
"e1cf14b0",
8787
digests["SHA512"].hexdigest
8888

89-
assert_headers_equal(tar_file_header("x", "", 0o644, 10, Time.now),
89+
assert_headers_equal(tar_file_header("x", "", 0o644, 10, Time.at(Gem::DEFAULT_SOURCE_DATE_EPOCH).utc),
9090
@io.string[0, 512])
9191
end
9292
assert_equal "aaaaaaaaaa#{"\0" * 502}", @io.string[512, 512]
@@ -109,7 +109,7 @@ def test_add_file_digest_multiple
109109
"e1cf14b0",
110110
digests["SHA512"].hexdigest
111111

112-
assert_headers_equal(tar_file_header("x", "", 0o644, 10, Time.now),
112+
assert_headers_equal(tar_file_header("x", "", 0o644, 10, Time.at(Gem::DEFAULT_SOURCE_DATE_EPOCH).utc),
113113
@io.string[0, 512])
114114
end
115115
assert_equal "aaaaaaaaaa#{"\0" * 502}", @io.string[512, 512]
@@ -126,7 +126,7 @@ def test_add_file_signer
126126
io.write "a" * 10
127127
end
128128

129-
assert_headers_equal(tar_file_header("x", "", 0o644, 10, Time.now),
129+
assert_headers_equal(tar_file_header("x", "", 0o644, 10, Time.at(Gem::DEFAULT_SOURCE_DATE_EPOCH).utc),
130130
@io.string[0, 512])
131131

132132
assert_equal "aaaaaaaaaa#{"\0" * 502}", @io.string[512, 512]
@@ -137,7 +137,7 @@ def test_add_file_signer
137137
signature = signer.sign digest.digest
138138

139139
assert_headers_equal(tar_file_header("x.sig", "", 0o444, signature.length,
140-
Time.now),
140+
Time.at(Gem::DEFAULT_SOURCE_DATE_EPOCH).utc),
141141
@io.string[1024, 512])
142142
assert_equal "#{signature}#{"\0" * (512 - signature.length)}",
143143
@io.string[1536, 512]
@@ -154,7 +154,7 @@ def test_add_file_signer_empty
154154
io.write "a" * 10
155155
end
156156

157-
assert_headers_equal(tar_file_header("x", "", 0o644, 10, Time.now),
157+
assert_headers_equal(tar_file_header("x", "", 0o644, 10, Time.at(Gem::DEFAULT_SOURCE_DATE_EPOCH).utc),
158158
@io.string[0, 512])
159159
end
160160
assert_equal "aaaaaaaaaa#{"\0" * 502}", @io.string[512, 512]
@@ -168,7 +168,7 @@ def test_add_file_simple
168168
io.write "a" * 10
169169
end
170170

171-
assert_headers_equal(tar_file_header("x", "", 0o644, 10, Time.now),
171+
assert_headers_equal(tar_file_header("x", "", 0o644, 10, Time.at(Gem::DEFAULT_SOURCE_DATE_EPOCH).utc),
172172
@io.string[0, 512])
173173

174174
assert_equal "aaaaaaaaaa#{"\0" * 502}", @io.string[512, 512]
@@ -192,7 +192,7 @@ def test_add_file_simple_padding
192192
Time.stub :now, Time.at(1_458_518_157) do
193193
@tar_writer.add_file_simple "x", 0, 100
194194

195-
assert_headers_equal tar_file_header("x", "", 0, 100, Time.now),
195+
assert_headers_equal tar_file_header("x", "", 0, 100, Time.at(Gem::DEFAULT_SOURCE_DATE_EPOCH).utc),
196196
@io.string[0, 512]
197197
end
198198

@@ -250,7 +250,7 @@ def test_mkdir
250250
Time.stub :now, Time.at(1_458_518_157) do
251251
@tar_writer.mkdir "foo", 0o644
252252

253-
assert_headers_equal tar_dir_header("foo", "", 0o644, Time.now),
253+
assert_headers_equal tar_dir_header("foo", "", 0o644, Time.at(Gem::DEFAULT_SOURCE_DATE_EPOCH).utc),
254254
@io.string[0, 512]
255255

256256
assert_equal 512, @io.pos

test/rubygems/test_gem_specification.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class TestGemSpecification < Gem::TestCase
1616
name: keyedlist
1717
version: !ruby/object:Gem::Version
1818
version: 0.4.0
19-
date: 2004-03-28 15:37:49.828000 +02:00
19+
date: 1980-01-02 00:00:00 UTC
2020
platform:
2121
summary: A Hash which automatically computes keys.
2222
require_paths:
@@ -75,7 +75,7 @@ def ext_spec(platform: Gem::Platform::RUBY)
7575
def assert_date(date)
7676
assert_kind_of Time, date
7777
assert_equal [0, 0, 0], [date.hour, date.min, date.sec]
78-
assert_operator (Gem::Specification::TODAY..Time.now), :cover?, date
78+
assert_equal Time.at(Gem::DEFAULT_SOURCE_DATE_EPOCH).utc, date
7979
end
8080

8181
def setup

0 commit comments

Comments
 (0)