Skip to content

Instantly share code, notes, and snippets.

@dantewang
Forked from zbyhoo/gitp2svnp.rb
Created December 26, 2011 02:52
Show Gist options
  • Save dantewang/1520442 to your computer and use it in GitHub Desktop.
Save dantewang/1520442 to your computer and use it in GitHub Desktop.
Simple script converting git patch file into tortoise merge compatible format. Usage: gitp2svnp.rb <git_patch_file>
#!/usr/bin/ruby
if ARGV.length != 1
puts "usage: git2svn <file_name>"
puts " file_name - git patch file name"
exit
end
separator = "\n===================================================================\n"
new_file = false
content = ''
git_patch = ARGV[0]
File.open(git_patch, 'r') do |file|
lines = file.readlines
lines.each do |line|
# Delete the additional "function/class defination info" from the hunk header line by simply replace the line with the second "@@" and anything before it.
# So it will not be compatible with 3-way (or more-way) diff.
# Question: considering the two-way diff, is this code safe?
if line =~ /@@.*@@.*/
line.gsub!(/(@@.*@@).*/, '\1')
end
line.gsub!(/^--- a\/(.*)/, 'Index: \1' + separator + '--- \1')
if line =~ /^--- \/.*/
new_file = true
line.gsub!(/.*/,'')
end
if line =~ /^\+\+\+ b\/(.*)/
line.gsub!(/^\+\+\+ b\/(.*)/, '+++ \1')
if new_file == true
new_file = false
line.gsub!(/^\+\+\+ (.*)/,'Index: \1' + separator + '--- \1' + "\n" + '+++ \1')
end
end
content += line
end
end
svn_patch = git_patch.dup
if svn_patch =~ /\.patch$/
index = svn_patch.rindex('.patch')
svn_patch = svn_patch.insert(index, '.svn')
else
svn_patch += '.svn.patch'
end
File.open(svn_patch, 'w') { |f| f.write(content) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment