Skip to content

Instantly share code, notes, and snippets.

@jaymcgavren
Last active March 27, 2023 11:41

Revisions

  1. Jay McGavren revised this gist Jun 21, 2013. 2 changed files with 109 additions and 100 deletions.
    100 changes: 0 additions & 100 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,100 +0,0 @@
    $ git diff HEAD subscribers_decorator | cat
    diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb
    index 245393c..6a1639a 100644
    ...
    Big 'ol list of stuff I don't want...
    ...
    Followed by a portion that I do...
    ...
    $ # I manually copy the portion of the diff I want.
    $ # `pbpaste` shows you the clipboard contents.
    $ pbpaste
    diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb
    index 245393c..6a1639a 100644
    --- a/app/controllers/posts_controller.rb
    +++ b/app/controllers/posts_controller.rb
    @@ -22,7 +23,8 @@ class PostsController < ApplicationController
    end

    def create
    - @post = Post.new(post_params)
    + @post = PostDecorator.new(Post.new(post_params))
    + @post.user = User.create
    @post.tag_list = tag_params[:tag_list]
    if @post.save
    redirect_to @post, notice: 'Post was successfully created.'
    diff --git a/app/decorators/post_decorator.rb b/app/decorators/post_decorator.rb
    new file mode 100644
    index 0000000..eea3b86
    --- /dev/null
    +++ b/app/decorators/post_decorator.rb
    @@ -0,0 +1,21 @@
    +class PostDecorator
    + def initialize(post)
    + @post = post
    + end
    + def method_missing(method_name, *args, &block)
    + @post.send(method_name, *args, &block)
    + end
    + def respond_to_missing?(method_name, include_private = false)
    + super || @post.respond_to?(method_name, include_private)
    + end
    + def save
    + if @post.save
    + user.subscribers.each do |subscriber|
    + SubscriberMailer.update(subscriber, self).deliver
    + end
    + end
    + end
    + def formatted_date
    + @post.created_at.strftime('%Y-%m-%d')
    + end
    +end
    $ # Note that it spans multiple files.
    $ # I even took a one change that I wanted from one file and ignored the rest.
    $ # Here's the cool part. `git apply -` takes a patch from STDIN...
    $ pbpaste | git apply -
    $ git add .
    $ # Now, look: from clipboard to working directory!
    $ git diff --staged | cat
    diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb
    index 245393c..0fc053c 100644
    --- a/app/controllers/posts_controller.rb
    +++ b/app/controllers/posts_controller.rb
    @@ -22,7 +22,8 @@ class PostsController < ApplicationController
    end

    def create
    - @post = Post.new(post_params)
    + @post = PostDecorator.new(Post.new(post_params))
    + @post.user = User.create
    @post.tag_list = tag_params[:tag_list]
    if @post.save
    redirect_to @post, notice: 'Post was successfully created.'
    diff --git a/app/decorators/post_decorator.rb b/app/decorators/post_decorator.rb
    new file mode 100644
    index 0000000..eea3b86
    --- /dev/null
    +++ b/app/decorators/post_decorator.rb
    @@ -0,0 +1,21 @@
    +class PostDecorator
    + def initialize(post)
    + @post = post
    + end
    + def method_missing(method_name, *args, &block)
    + @post.send(method_name, *args, &block)
    + end
    + def respond_to_missing?(method_name, include_private = false)
    + super || @post.respond_to?(method_name, include_private)
    + end
    + def save
    + if @post.save
    + user.subscribers.each do |subscriber|
    + SubscriberMailer.update(subscriber, self).deliver
    + end
    + end
    + end
    + def formatted_date
    + @post.created_at.strftime('%Y-%m-%d')
    + end
    +end
    109 changes: 109 additions & 0 deletions monkey_cherry_picking.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,109 @@
    I run a diff of my current branch versus a different one that has some stuff I want:

    $ git diff HEAD subscribers_decorator | cat
    diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb
    index 245393c..6a1639a 100644
    ...

    Big 'ol list of stuff I don't want...

    Followed by a portion that I do want... Which I manually copy.

    `pbpaste` shows you the clipboard contents on OSX. `xclip` should do the same on Linux.

    $ pbpaste
    diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb
    index 245393c..6a1639a 100644
    --- a/app/controllers/posts_controller.rb
    +++ b/app/controllers/posts_controller.rb
    @@ -22,7 +23,8 @@ class PostsController < ApplicationController
    end

    def create
    - @post = Post.new(post_params)
    + @post = PostDecorator.new(Post.new(post_params))
    + @post.user = User.create
    @post.tag_list = tag_params[:tag_list]
    if @post.save
    redirect_to @post, notice: 'Post was successfully created.'
    diff --git a/app/decorators/post_decorator.rb b/app/decorators/post_decorator.rb
    new file mode 100644
    index 0000000..eea3b86
    --- /dev/null
    +++ b/app/decorators/post_decorator.rb
    @@ -0,0 +1,21 @@
    +class PostDecorator
    + def initialize(post)
    + @post = post
    + end
    + def method_missing(method_name, *args, &block)
    + @post.send(method_name, *args, &block)
    + end
    + def respond_to_missing?(method_name, include_private = false)
    + super || @post.respond_to?(method_name, include_private)
    + end
    + def save
    + if @post.save
    + user.subscribers.each do |subscriber|
    + SubscriberMailer.update(subscriber, self).deliver
    + end
    + end
    + end
    + def formatted_date
    + @post.created_at.strftime('%Y-%m-%d')
    + end
    +end

    Note that it spans multiple files. I even took a one change that I wanted from one file and ignored the rest.

    Here's the cool part. `git apply -` takes a patch from STDIN...

    $ pbpaste | git apply -

    Now, look: The files are actually updated based on the patch contents from the clipboard!

    $ git add .
    $ git diff --staged | cat
    diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb
    index 245393c..0fc053c 100644
    --- a/app/controllers/posts_controller.rb
    +++ b/app/controllers/posts_controller.rb
    @@ -22,7 +22,8 @@ class PostsController < ApplicationController
    end

    def create
    - @post = Post.new(post_params)
    + @post = PostDecorator.new(Post.new(post_params))
    + @post.user = User.create
    @post.tag_list = tag_params[:tag_list]
    if @post.save
    redirect_to @post, notice: 'Post was successfully created.'
    diff --git a/app/decorators/post_decorator.rb b/app/decorators/post_decorator.rb
    new file mode 100644
    index 0000000..eea3b86
    --- /dev/null
    +++ b/app/decorators/post_decorator.rb
    @@ -0,0 +1,21 @@
    +class PostDecorator
    + def initialize(post)
    + @post = post
    + end
    + def method_missing(method_name, *args, &block)
    + @post.send(method_name, *args, &block)
    + end
    + def respond_to_missing?(method_name, include_private = false)
    + super || @post.respond_to?(method_name, include_private)
    + end
    + def save
    + if @post.save
    + user.subscribers.each do |subscriber|
    + SubscriberMailer.update(subscriber, self).deliver
    + end
    + end
    + end
    + def formatted_date
    + @post.created_at.strftime('%Y-%m-%d')
    + end
    +end

    From clipboard to working directory!
  2. Jay McGavren revised this gist Jun 21, 2013. No changes.
  3. Jay McGavren created this gist Jun 21, 2013.
    100 changes: 100 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,100 @@
    $ git diff HEAD subscribers_decorator | cat
    diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb
    index 245393c..6a1639a 100644
    ...
    Big 'ol list of stuff I don't want...
    ...
    Followed by a portion that I do...
    ...
    $ # I manually copy the portion of the diff I want.
    $ # `pbpaste` shows you the clipboard contents.
    $ pbpaste
    diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb
    index 245393c..6a1639a 100644
    --- a/app/controllers/posts_controller.rb
    +++ b/app/controllers/posts_controller.rb
    @@ -22,7 +23,8 @@ class PostsController < ApplicationController
    end

    def create
    - @post = Post.new(post_params)
    + @post = PostDecorator.new(Post.new(post_params))
    + @post.user = User.create
    @post.tag_list = tag_params[:tag_list]
    if @post.save
    redirect_to @post, notice: 'Post was successfully created.'
    diff --git a/app/decorators/post_decorator.rb b/app/decorators/post_decorator.rb
    new file mode 100644
    index 0000000..eea3b86
    --- /dev/null
    +++ b/app/decorators/post_decorator.rb
    @@ -0,0 +1,21 @@
    +class PostDecorator
    + def initialize(post)
    + @post = post
    + end
    + def method_missing(method_name, *args, &block)
    + @post.send(method_name, *args, &block)
    + end
    + def respond_to_missing?(method_name, include_private = false)
    + super || @post.respond_to?(method_name, include_private)
    + end
    + def save
    + if @post.save
    + user.subscribers.each do |subscriber|
    + SubscriberMailer.update(subscriber, self).deliver
    + end
    + end
    + end
    + def formatted_date
    + @post.created_at.strftime('%Y-%m-%d')
    + end
    +end
    $ # Note that it spans multiple files.
    $ # I even took a one change that I wanted from one file and ignored the rest.
    $ # Here's the cool part. `git apply -` takes a patch from STDIN...
    $ pbpaste | git apply -
    $ git add .
    $ # Now, look: from clipboard to working directory!
    $ git diff --staged | cat
    diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb
    index 245393c..0fc053c 100644
    --- a/app/controllers/posts_controller.rb
    +++ b/app/controllers/posts_controller.rb
    @@ -22,7 +22,8 @@ class PostsController < ApplicationController
    end

    def create
    - @post = Post.new(post_params)
    + @post = PostDecorator.new(Post.new(post_params))
    + @post.user = User.create
    @post.tag_list = tag_params[:tag_list]
    if @post.save
    redirect_to @post, notice: 'Post was successfully created.'
    diff --git a/app/decorators/post_decorator.rb b/app/decorators/post_decorator.rb
    new file mode 100644
    index 0000000..eea3b86
    --- /dev/null
    +++ b/app/decorators/post_decorator.rb
    @@ -0,0 +1,21 @@
    +class PostDecorator
    + def initialize(post)
    + @post = post
    + end
    + def method_missing(method_name, *args, &block)
    + @post.send(method_name, *args, &block)
    + end
    + def respond_to_missing?(method_name, include_private = false)
    + super || @post.respond_to?(method_name, include_private)
    + end
    + def save
    + if @post.save
    + user.subscribers.each do |subscriber|
    + SubscriberMailer.update(subscriber, self).deliver
    + end
    + end
    + end
    + def formatted_date
    + @post.created_at.strftime('%Y-%m-%d')
    + end
    +end