Skip to content

Instantly share code, notes, and snippets.

@rvalyi
Created November 2, 2012 13:37
Show Gist options
  • Save rvalyi/4001400 to your computer and use it in GitHub Desktop.
Save rvalyi/4001400 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
#########################################################################
#
# Copyright (C) 2011 Akretion (http://www.akretion.com). All Rights Reserved
# Author Sebastien BEAU
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#########################################################################
require "popen4"
STEP = 500
BRANCH = "lp:openobject-addons/extra-trunk"
help_message = "
Command
bzr-super-replay path revno --module module1 module2 module3
mandatory args
path => path to the branch where the commit come from
revno => all commit will be replayed from this revision number
option
--module module1 module2 module3, -m module1 module2 module3
Only the commit for this modules will be replayed
--help
Show this message
--auto
Try to play all commit automagically ;)
--hide-merge
This option will hide all commit of merge
--hide-translation
This option will hide launchpad translation
"
oe_modules=[]
auto=false
hide_merge=false
hide_translation=false
count=0
ARGV.each do |option|
count+=1
if option == '--help'
puts help_message
exit
elsif option == '--module' || option == "-m"
ARGV[count..-1].each do |oe_module|
if oe_module[0..1] == '--'
print 'break'
break
else
oe_modules << oe_module
end
end
elsif option == '--auto'
auto=true
elsif option == '--hide-merge'
hide_merge=true
elsif option == '--hide-translation'
hide_translation=true
end
end
puts oe_modules
from_path = ARGV[0]
init_from_rev_number = ARGV[1]
puts "** SEARCHING COMMITS SINCE REV #{init_from_rev_number} **"
revno={}
current_rev = `cd #{from_path}; bzr revno`.to_i
oe_modules.each do |oe_module|
revno[oe_module] = []
from_rev_number = init_from_rev_number
until_rev = from_rev_number.split('.')[0].to_i + STEP
stop = false
while not stop
if until_rev > current_rev
until_rev = current_rev
stop = true
end
puts "========= searching commits from #{from_rev_number} until #{until_rev} ============="
rev_lines = `bzr log #{from_path}/#{oe_module} -r#{from_rev_number}..#{until_rev} --include-merged`
rev_lines.split("\n------------------------------------------------------------").each do |commit|
if not (hide_merge && commit.include?('merge') || hide_translation && (commit.include?('Launchpad Translations') || commit.include?('automatic translations')))
puts commit
rev = []
commit.scan(/revno.*/)[0].gsub("revno:","").strip().split('.').each do |num|
rev << num.to_i
end
msg = commit.split("message:")[1].strip()
committer = commit.scan(/committer.*/)[0].gsub("committer:","").strip()
time = commit.scan(/timestamp.*/)[0].gsub("timestamp:","").strip()[4..1000]
revno[oe_module] << [rev, msg, committer, time]
end
end
from_rev_number = until_rev
until_rev += STEP
end
end
rev_number = []
revno.each do | oe_module, rev_data |
rev_number += rev_data
end
rev_number.uniq!
rev_number.sort!
puts "** REPLAYING REVISIONS **"
puts "#{rev_number.size} revisions to replay"
puts "hint: usually it's handy to open other terminal in the folder to fix failed replays"
`rm -rf /tmp/bzr-replay`
`mkdir /tmp/bzr-replay`
rev_number.each do |rev, message, committer, time|
revno = rev*'.'
puts "\nreplaying revno #{revno}"
message += "\n(#{BRANCH} rev #{revno})"
puts "commit message : #{message}"
if false#auto && (not (revno[1].include?("Launchpad automatic translations") || revno[1].include?("merge")))
`bzr replay #{from_path} --r=#{revno}`
elsif true#not auto
puts "Replay this revision? Y/N"
if true#$stdin.gets.downcase.include?('y')
#`bzr replay #{from_path} --r=#{revno}`
err = ""
`bzr whoami "#{committer}"`
`bzr revert`
status = POpen4::popen4("bzr replay #{from_path} --r=#{revno}") do |stdout, stderr, stdin|
stderr.each {|line| err << line}; puts line }
end
folders = `ls`.split("\n")
unwanted_modules = folders - oe_modules
`rm -rf #{unwanted_modules.join(" ")}` if unwanted_modules
current_modules = `ls`.split("\n")
`bzr add #{current_modules.join(" ")}`
# exit #FIXME
if unwanted_modules && err.strip() == ""
`bzr commit #{current_modules} -m "cleaning after #{BRANCH} branch extraction" --commit-time="#{time}"`
`bzr commit #{unwanted_modules} -m "cleaning after #{BRANCH} branch extraction" --commit-time="#{time}"`
elsif err.index("not versioned")
puts "ERROR: commit touching files not present in the tree; FIXING IT NOW!"
#`mv -r #{oe_modules.join(' ')} /tmp/bzr-replay`
#`rm -rf *`
#oe_modules.each {|m| `mv -r /tmp/bzr-replay/#{m} .`}
`bzr resolve --all --take-other`
`bzr add #{current_modules.join(" ")}`
`bzr commit . -m "#{message}" --commit-time="#{time}"`
elsif err.index("Working tree has conflicts")
puts "FIXING conflicts"
`bzr resolve --all --take-other`
`bzr add #{current_modules.join(" ")}`
`bzr commit . -m "#{message}" --commit-time="#{time}"`
elsif err.index("No final name for trans_id")
puts "KNOW BZR ISSUE FOUND, likely an insignificant revision, moving to the next commit..."
end
else
puts 'next'
end
end
end
`bzr whoami "BZR extractor <[email protected]>"`
puts "Do you want to check if the diff between the two branch? Y/N"
if $stdin.gets.downcase.index('y')
#check if there is a diff between the two branch
oe_modules.each do |oe_module|
diff = `bzr diff #{oe_module} --new=#{from_path}/#{oe_module}`
if diff.size>0
puts "there is some diff for the module #{oe_module}"
puts 'diff :', diff, "\n"
puts "Launch automatic patch? Y/N"
if $stdin.gets.downcase.include?('y')
`bzr diff #{oe_module} --new=#{from_path}/#{oe_module} >/tmp/#{oe_module}.patch; bzr patch /tmp/#{oe_module}.patch; rm /tmp/#{oe_module}.patch`
# puts 'patch applied, please commit it'
`bzr commit #{oe_module} -m "BZR #{BRANCH} branch extraction fixes"`
puts 'patch applied'
else
puts 'no patch applied'
end
else
puts "the module #{oe_module} was correctly merged"
end
end
end
puts "Type 'enter' to close"
$stdin.gets
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment