Skip to content

Instantly share code, notes, and snippets.

@rcook
Last active March 30, 2020 05:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rcook/6064683 to your computer and use it in GitHub Desktop.
Save rcook/6064683 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'pathname'
require 'tempfile'
THIS_PATH = Pathname.new(__FILE__).realpath
THIS_DIR = THIS_PATH.dirname
THIS_BASE_NAME = THIS_PATH.basename
module HGHelper
def self.run_hg(command)
temp_file = Tempfile.new('forward-port')
status = nil
lines = nil
begin
temp_file.close
status = system("(#{command}) > #{temp_file.path} 2>&1")
lines = File.readlines(temp_file.path).collect(&:chomp)
ensure
temp_file.unlink
end
[status, lines]
end
end
class Version
include Comparable
attr_reader :major, :minor
def self.parse(str)
return nil if str !~ /^v([0-9]+)\.([0-9]+)$/
new(Integer($1), Integer($2))
end
def <=>(other)
delta = major - other.major
return 1 if delta > 0
return -1 if delta < 0
delta = minor - other.minor
return 1 if delta > 0
return -1 if delta < 0
0
end
def to_s
"v#{major}.#{minor}"
end
private
def initialize(major, minor)
@major = major
@minor = minor
end
end
class Options
attr_reader :merge_version
def self.parse(args)
merge_version = nil
while arg = args.shift
case arg
when '--merge-version' then
temp = args.shift or raise RuntimeError.new('Value required for --merge-version')
merge_version = Version.parse(temp) or raise RuntimeError.new("Invalid value \"#{temp}\" for --merge-version")
else
raise RuntimeError.new("Unsupported switch \"#{arg}\"")
end
end
raise RuntimeError.new('Must specify switch --merge-version') unless merge_version
new(merge_version)
end
private
def initialize(merge_version)
@merge_version = merge_version
end
end
def display_output(lines)
lines.each do |line|
puts "> #{line}"
end
end
def fail(message, lines)
display_output(lines)
raise RuntimeError.new(message)
end
def get_hg_branches(dir)
status, lines = HGHelper.run_hg("cd #{dir}; hg branches")
fail('hg branches failed', lines) unless status
lines.collect { |line| line.split.first }
end
def get_versions(branches)
branches.collect { |branch| Version.parse(branch)}.compact.sort
end
def forward_port(dir, merge_version, version)
status, lines = HGHelper.run_hg("cd #{dir}; hg update #{version}; hg merge #{merge_version}")
if status
status, lines = HGHelper.run_hg("hg commit -m \"Automatic forward port from #{merge_version} to #{version}\"")
fail('hg commit failed', lines) unless status
puts "Changes ported from #{merge_version} to #{version}"
true
elsif lines.include?('abort: merging with a working directory ancestor has no effect')
puts "No changes required porting from #{merge_version} to #{version}"
true
else
display_output(lines)
puts <<-EOS
Automatic forward port abandoned due to merge conflict.
Please resolve conflict manually and commit your changes.
Suggested commit command:
hg commit -m "Manual forward port form #{merge_version} to #{version}"
Resume the automatic forward port with the following command:
#{THIS_BASE_NAME} --merge-version #{version}
EOS
false
end
end
options = Options.parse(ARGV.clone)
versions = get_versions(get_hg_branches('.'))
raise RuntimeError.new("Version \"#{options.merge_version}\" not available") unless versions.include?(options.merge_version)
merge_and_later_versions = versions.select { |version| version >= options.merge_version }
merge_and_later_versions.each_with_index do |version, index|
next if index == 0
unless forward_port('.', merge_and_later_versions[index - 1], version)
exit 1
end
end
puts 'Done'
The MIT License (MIT)
Copyright (c) 2013 Richard Cook
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@rcook
Copy link
Author

rcook commented Jul 24, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment