Skip to content

Instantly share code, notes, and snippets.

@tkrotoff
Last active September 30, 2015 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkrotoff/1771088 to your computer and use it in GitHub Desktop.
Save tkrotoff/1771088 to your computer and use it in GitHub Desktop.
A simple regexp to manipulate svn dump files
#!/usr/bin/env ruby
# A simple regexp to manipulate svn dump files.
#
# Example of a svn dump file:
#
# Node-path: MySourceCode.cpp
# Node-kind: file
# Node-action: add
# Prop-content-length: 10
# Text-content-length: 1203
# Text-content-md5: fda28ccb2404bfc206aa24fefec342d4
# Content-length: 1213
# <File content>
#
# Node-path: MySourceCode.cs
# Node-kind: file
# Node-action: change
# Text-content-length: 1305
# Text-content-md5: fda28ccb2404bfc206aa24fefec342d4
# Content-length: 1305
# <File content>
#
# Examples of tools and commands that I have used:
# svndumpfilter --drop-empty-revs --renumber-revs include dir < original.dump > dir.dump
# svndumptool.py merge -i dir1.dump -i dir2.dump -o merge.dump
# svndumptool.py merge -i dir.dump -r "olddirectory" "newdirectory" -o dir.dump
# rm -rf svn_repo && svnadmin create svn_repo && svnadmin load svn_repo < dir.dump
# Edit dump file using VIM, other editors might fail with embedded binary
# Use this script and others around
# git svn clone file:///svn_repo git_repo --no-metadata -A authors.txt
#
# How to generate a dump file from a remote repository:
# svnadmin create svn_repo
# svnsync init file:///svn_repo http://svn_remote_repo
# If you get the following error:
# svnsync: E165006: Repository has not been enabled to accept revision propchanges;
# ask the administrator to create a pre-revprop-change hook
# then create an empty file hooks/pre-revprop-change.bat (if under Windows)
# svnsync sync file:///svn_repo
# svnadmin dump svn_repo > svn_repo.dump
#
# Check svn2git: https://github.com/nirvdrum/svn2git
file = File.open('svn.dump', 'r+b')
content = file.read
# Remove MD5 for all nodes.
# When modifying a node you need to update its MD5
# or simply remove it :)
content.gsub!(/Text-content-md5:[ ][a-z0-9]+\n/, '')
regexp = Regexp.new(
'Node-path:[ ](\S+)Tests.cs\n' \
'Node-kind:[ ]file\n' \
'Node-action:[ ](add|change)\n' \
'(Prop-content-length:[ ]\d+\n|)' \
'Text-content-length:[ ](\d+)\n' \
'Content-length:[ ](\d+)\n',
Regexp::EXTENDED | Regexp::MULTILINE
)
# Change text length and content length.
# When modifying a node you need to update its text and content length.
# You cannot remove this information.
new_length_diff = -6
content.gsub!(regexp) do
new_text_length = $4.to_i + new_length_diff
new_content_length = $5.to_i + new_length_diff
"Node-path: #{$1}Tests.cs\n" \
"Node-kind: file\n" \
"Node-action: #{$2}\n" \
"#{$3}" \
"Text-content-length: #{new_text_length}\n" \
"Content-length: #{new_content_length}\n"
end
file.truncate(0) # Empty the existing file
file.seek(0) # Seek to the start of the file before writing again
file.write(content)
file.close
begin
Node-path: MySourceCode.cpp
Node-kind: file
Node-action: add
Prop-content-length: 10
Text-content-length: 1203
Text-content-md5: fda28ccb2404bfc206aa24fefec342d4
Content-length: 1213
end
begin
Node-path: MySourceCode.cpp
Node-kind: file
Node-action: change
Text-content-length: 1305
Text-content-md5: fda28ccb2404bfc206aa24fefec342d4
Content-length: 1305
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment