Created
January 25, 2013 23:03
-
-
Save dogweather/4638715 to your computer and use it in GitHub Desktop.
Sync an svn repo to git. A simple script that can be run from a cron job.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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