Skip to content

Instantly share code, notes, and snippets.

@zbyhoo
Last active December 23, 2016 10:37
Show Gist options
  • Save zbyhoo/721622 to your computer and use it in GitHub Desktop.
Save zbyhoo/721622 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:utf-8') do |file|
lines = file.readlines
lines.each do |line|
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) }
@bimargulies
Copy link

This doesn't work right for new files with svn 1.7. The new empty file gets created and added to svn, but then the patch to add its contents is rejected.

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