rubygems/rubygems — pull request #381 by strzibny (2012-10-02 07:31:56+00:00)
Title: Add Gem::Commands::PatchCommand and Gem::Patcher

# `patch` command

This adds the patch command to RubyGems. The patch command helps to patch gems without manually opening and rebuilding them. It opens a given .gem file, extracts it, patches it with system `patch` command, clones its spec, updates the file list and builds the patched gem.

## Usage

`gem patch [options] name-version.gem PATCH [PATCH ...]`

Optionally with `-pNUMBER` or `--strip=NUMBER` option that sets the file name strip count to NUMBER (same options as for `patch` command on Linux machines).

## Example

Instead of extracting gem, patching the gem files with `patch`, manually changing file list in spec and packing it again:

```ruby
irb(main):001:0> require 'rubygems/package'
=> true
irb(main):002:0> package = Gem::Package.new 'idn-0.0.2.gem'
=> #
irb(main):008:0> package.extract_files '/home/strzibny/unpacked'
=> nil
irb(main):009:0> patched_gem = package.spec.file_name
=> "idn-0.0.2.gem"
```
```
patch -p0 < mypatch.patch
```
```ruby

irb(main):012:0> patched_package = Gem::Package.new patched_gem
=> #
irb(main):013:0> patched_package.spec = package.spec.clone
=> #
irb(main):014:0> patched_package.spec.rubygems_version = '2.0.a'
=> "2.0.a"
irb(main):015:0> Dir.chdir '/home/strzibny/unpacked' do
irb(main):016:1* patched_package.build
irb(main):017:1> end
```
will be enough to run:
```ruby
[strzibny@dhcp-25-242 gem-patch]$ gem patch idn-0.0.2.gem rubygem-idn-0.0.2-Fix-for-ruby-1.9.x.patch
```
Note: To reproduce this you have to add `patched_package.spec.rubygems_version = 2.0.a` in lib/rubygems/patcher.rb on line 74. This is because idn gem file uses RubyGems 1.8.