How do you apply a patch to ruby on rails?

2019-03-11 05:19发布

问题:

I'd like to apply the action_mailer patch mentioned in this thread but I have never applied a patch before and I'm not sure how these work: https://rails.lighthouseapp.com/projects/8994/tickets/2263

My action mailer gem is here: /opt/ruby-enterprise-1.8.6-20090421/lib/ruby/gems/1.8/gems/actionmailer-2.3.2

I assume I need to go to that directory and run the patch command...something like this?

cd /opt/ruby-enterprise-1.8.6-20090421/lib/ruby/gems/1.8/gems/
wget https://rails.lighthouseapp.com/attachments/108548/0001-Fix-implicit-multipart-mailer-views-when-RAILS_ROOT.patch
patch < 0001-Fix-implicit-multipart-mailer-views-when-RAILS_ROOT.patch

One thing I'm not really clear on also is that the patch file refers to the "actionmailer" directory but mine is called "actionmailer-2.3.2"

回答1:

You typically don't want to patch the gem source itself. You probably will want to freeze the gems into ${RAILS_ROOT}/vendor/rails, and then apply the patch locally.

From your ${RAILS_ROOT} dir, dump all of your rails gems into vendor/rails

rake rails:freeze:gems

Apply the patch

  cd vendor/rails/ 
  patch -p1 < 0001-Fix-implicit-multipart-mailer-views-when-RAILS_ROOT.patch


回答2:

Micholson's answer above works. But to patch the actual gem for all rails apps (see my comment on his answer) this worked:

cd /opt/ruby-enterprise-1.8.6-20090421/lib/ruby/gems/1.8/gems/actionmailer-2.3.2
wget sudo wget https://rails.lighthouseapp.com/attachments/108548/0001-Fix-implicit-multipart-mailer-views-when-RAILS_ROOT.patch
sudo patch -p2 < 0001-Fix-implicit-multipart-mailer-views-when-RAILS_ROOT.patch
sudo rm 0001-Fix-implicit-multipart-mailer-views-when-RAILS_ROOT.patch

I found out you can also reverse a patch with -R if anything goes wrong. I'm surprised this process wasn't better documented somewhere. Hopefully this will turn up in Google searches for people new to patching like me.



回答3:

Here is a shell one-liner for patching a gem:

patch -d "$(gem env gemdir)"/gems/actionmailer-* -p1 < <(curl -s https://rails.lighthouseapp.com/attachments/108548/0001-Fix-implicit-multipart-mailer-views-when-RAILS_ROOT.patch)

Another example using gist gem:

patch -d "$(gem env gemdir)"/gems/gist-* -p1 < <(curl -s https://github.com/defunkt/gist/commit/5843e9827f529cba020d08ac764d70c8db8fbd71.patch)