[svn.haxx.se] · SVN Dev · SVN Users · SVN Org · TSVN Dev · TSVN Users · Subclipse Dev · Subclipse Users · this month's index

Re: Detecting CR eol

From: Csaba Raduly <rcsaba_at_gmail.com>
Date: Wed, 8 Sep 2010 13:22:40 +0200

Hi Giulio,

On Wed, Sep 8, 2010 at 10:25 AM, Giulio Troccoli wrote:
> I am writing a pre-commit hook script in perl. One of the requirement is that all files (luckily they are all text files) have the svn:eol-style property set to LF and the actual eol is indeed LF. If that's not the case I will reject the commit and direct the user to a page on our intranet to explain what to do to fix it.
>
> My problem is how to detect whether the eol is LF and nothing else. I'm developing on Linux (Centos 5) and Perl 5.10. Subversion is 1.6.9, if it matters.
>
> I thought about using the dos2unix utility (we only use Windows or Linux) and then check that the file hasn't changed, but it seems a lot of processing.
>
> My second idea was to use a regular expression to check each line of each file. This way at least I would stop as soon as I find an eol that is not LF, saving some processing. I still need to svn cat each file into an array I think.
>

You need to use svnlook cat, but there is no need to read all its
output into memory. You can process it line-by-line.
Here's an outline (completely untested)

#!/usr/bin/perl -w
use strict;

my ($REPOS, $TXN) = @ARGV;

my $crlf = 0;

... determine the list of files
my @files = `svnlook changed -t $TXN $REPOS`;
chomp @files; # remove the newline at the end
s/^U\s+// for @files; # remove the leading U

FILE:
foreach my $file (@files) {
  open (SVN, "svnlook cat $file |") or die "open pipe failed: $!"
  while (<SVN>) # read from the pipe, one line at a time
  {
    chomp; # cut the platform-specific line end. On Unix, this drops
the \n but keeps the \r
    if ( /^M$/ ) { # last character is a \r (a.k.a. Control-M)
      $crlf = 1; last FILE;
    }
  }
  close(SVN) or die "close pipe failed: $!" # it is very important to
check the close on pipes
}

if ($crlf)
{
  die "$file contains DOS line endings";
}

-- 
Life is complex, with real and imaginary parts.
"Ok, it boots. Which means it must be bug-free and perfect. " -- Linus Torvalds
"People disagree with me. I just ignore them." -- Linus Torvalds
Received on 2010-09-08 13:23:21 CEST

This is an archived mail posted to the Subversion Users mailing list.

This site is subject to the Apache Privacy Policy and the Apache Public Forum Archive Policy.