[rubygems/rubygems] Add mtime to Gem::Package::TarWriter#add_file argument
authorYusuke Nakamura <[email protected]>
Sat, 3 May 2025 14:59:41 +0000 (3 23:59 +0900)
committerHiroshi SHIBATA <[email protected]>
Thu, 8 May 2025 09:03:04 +0000 (8 18:03 +0900)
Since 9e21dd9, Gem::Package::TarWriter#add_file adds the file to
the tar with Gem.source_date_epoch for its mtime.
This behavior breaks the code depending on the previous add_file
behavior.
Therefore, add_file accepts mtime as an argument, and uses
Gem.source_date_epoch if not specified.

https://github.com/rubygems/rubygems/commit/7020ea98a0

lib/rubygems/package/tar_writer.rb
test/rubygems/test_gem_package_tar_writer.rb

index b24bdb6..7dcb973 100644 (file)
@@ -95,10 +95,11 @@ class Gem::Package::TarWriter
   end
 
   ##
-  # Adds file +name+ with permissions +mode+, and yields an IO for writing the
-  # file to
+  # Adds file +name+ with permissions +mode+ and mtime +mtime+ (sets
+  # Gem.source_date_epoch if not specified), and yields an IO for
+  # writing the file to
 
-  def add_file(name, mode) # :yields: io
+  def add_file(name, mode, mtime=nil) # :yields: io
     check_closed
 
     name, prefix = split_name name
@@ -118,7 +119,7 @@ class Gem::Package::TarWriter
 
     header = Gem::Package::TarHeader.new name: name, mode: mode,
                                          size: size, prefix: prefix,
-                                         mtime: Gem.source_date_epoch
+                                         mtime: mtime || Gem.source_date_epoch
 
     @io.write header
     @io.pos = final_pos
index 67a9d0d..7efe9cb 100644 (file)
@@ -50,6 +50,17 @@ class TestGemPackageTarWriter < Gem::Package::TarTestCase
     end
   end
 
+  def test_add_file_with_mtime
+    Time.stub :now, Time.at(1_458_518_157) do
+      @tar_writer.add_file "x", 0o644, Time.now do |f|
+        f.write "a" * 10
+      end
+
+      assert_headers_equal(tar_file_header("x", "", 0o644, 10, Time.now),
+                         @io.string[0, 512])
+    end
+  end
+
   def test_add_symlink
     Time.stub :now, Time.at(1_458_518_157) do
       @tar_writer.add_symlink "x", "y", 0o644