Skip to content

Instantly share code, notes, and snippets.

@rdavila
Created May 17, 2010 15:57
Show Gist options
  • Save rdavila/403908 to your computer and use it in GitHub Desktop.
Save rdavila/403908 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'fileutils'
module GitScripts
class Branch
attr_reader :name
def initialize(path)
@path = path
@name = get_name
end
def remote?
File.exists?(File.join(@path, ".git/refs/remotes/origin/", get_name))
end
private
def get_name
output = `cd #{@path} && git branch`
branch = output.select { |e| e =~ /\A\*.+/ }.first
branch[/\*\s(.+)/, 1]
end
end
class WorkingCopy
def initialize(path)
@path = path
@original_branch = GitScripts::Branch.new(path)
end
def integrate(remote_branch)
system("cd #{@path} && git checkout #{remote_branch}")
system("git pull origin #{remote_branch}")
system("git checkout #{@original_branch.name}")
@original_branch.remote? ? system("git merge #{remote_branch}") : system("git rebase #{remote_branch}")
end
def ship(remote_branch)
system("cd #{@path} && git checkout #{remote_branch}")
system("git pull origin #{remote_branch}")
system("git merge --no-ff #{@original_branch.name}")
system("git push origin #{remote_branch}")
system("git checkout #{@original_branch.name}")
end
def finish
system("cd #{@path} && git checkout master")
system("git branch -D #{@original_branch.name}")
end
def execute(command, *args)
send(command.to_sym, *args)
end
end
end
wc = GitScripts::WorkingCopy.new(FileUtils.pwd)
wc.execute(ARGV.shift, *ARGV)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment