Class: SvnTransform::Transform::Newline

Inherits:
Object
  • Object
show all
Defined in:
lib/svn-transform/transform/newline.rb

Overview

Transform all newlines to use a given sequence. For example, a repository some files using CRLF and some using LF could be transformed so that all files through all revisions use LF.

Constant Summary collapse

LF =
"\n"
CRLF =
"\r\n"
CR =
"\r"

Instance Method Summary collapse

Constructor Details

#initialize(file, newline = LF) ⇒ Newline

Initialize Newline Transform.

Parameters

file<SvnTransform::File>

File at a given revision

newline<String>

What to replace newlines with (defaults to n)



16
17
18
19
# File 'lib/svn-transform/transform/newline.rb', line 16

def initialize(file, newline = LF)
  @file = file
  @newline = newline
end

Instance Method Details

#runObject

Run the transform. It first converts all newlines (LF, CRLF and CR) to LF, then replaces LF if needed.

Returns

True, False

indicating whether a change was made.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/svn-transform/transform/newline.rb', line 26

def run
  # Skip binary files; this is probably not the best way to determine.
  return false if 'application/octet-stream' == @file.properties['svn:mime-type']
  body = @file.body.dup
  # Replace CR and CRLF
  body = all_to_lf(body)
  # Replace LFs with newline if needed
  body.gsub!(LF, @newline) unless LF == @newline
  
  if body != @file.body
    @file.body = body
    return true
  else
    return false
  end
end