Create a gist now

Instantly share code, notes, and snippets.

Sync an svn repo to git. A simple script that can be run from a cron job.
require 'Time'
require 'Nokogiri'
#
# svn-to-git
#
# Sync an svn repo to git.
#
# Checks for svn updates. If there are none, does
# nothing. Otherwise, commits & pushes the latest
# changes into git with an appropriate commit message.
#
def commit_and_push_to_git_with(log_messages)
commit_msg = log_messages.join("\n")
system 'git', 'add', '-A'
system 'git', 'commit', '-m', commit_msg
system 'git', 'push'
end
##### Execution starts here #####
# Get the git cut-off date
last_git_commit = Time.parse(`git log -1 --format="%ci"`)
# Get new svn log messages
messages = []
`svn update`
svn_log = Nokogiri::XML(`svn log --xml`)
svn_log.xpath('//logentry').each { |entry|
entry_date = Time.parse(entry.xpath('date').inner_text)
if entry_date > last_git_commit
messages << "Revision #{entry['revision']}: #{entry.xpath('msg').inner_text}"
end
}
# Send to git if there are new updates
unless messages.empty?
puts "Committing and pushing #{messages.size} new svn updates:"
messages.each { |m| puts " #{m}" }
commit_and_push_to_git_with(messages)
else
puts "No new svn updates"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment