Jun
25
svn import: inconsistent newlines
I encountered this error ad nauseum while importing external libraries into my local/offline Subversion repositories:
File ‘/foo/bar’ has inconsistent newlines
At first I was opening each file in Textmate and then “save as”-ing with Unix newlines. This takes way too much time, so I wrote this little PHP diddy:
<?php
// Usage:
// php smart_svn_import.php . http://repo "Initial import"
$command = 'svn import --quiet %s %s -m %s 2>&1';
$command = vsprintf($command, array_map('escapeshellarg', array(
$argv[1], // path
$argv[2], // url
$argv[3], // message
)));
while (true)
{
ob_start();
passthru($command, $return);
$content = ob_get_contents();
ob_end_clean();
if (0 == $return)
{
break;
}
if (preg_match('/^svn: File \'(.*)\' has inconsistent newlines$/m', $content, $match))
{
// fix this file
file_put_contents($match[1], str_replace(
array("\n", "\r\n"),
array(PHP_EOL, PHP_EOL),
file_get_contents($match[1])
));
}
else
{
throw new Exception($content);
}
}