eol: fix missing trailing newlines in comitted files
authorStepan Koltsov <stepancheg@yandex-team.ru>
Fri, 01 Jul 2011 23:12:52 +0400
changeset 14855 f33579435378
parent 14854 23c2d7d25329
child 14856 9f5cd6b6d758
eol: fix missing trailing newlines in comitted files Some text editors (Eclipse, for example) do not add trailing newlines, so diffs often contain annoying "\ No newline at the end of file". This patch to eol extension simply adds trailing newline on commit.
hgext/eol.py
tests/test-eol.t
--- a/hgext/eol.py	Fri Jul 01 22:53:58 2011 +0400
+++ b/hgext/eol.py	Fri Jul 01 23:12:52 2011 +0400
@@ -106,6 +106,8 @@
         return s
     if ui.configbool('eol', 'only-consistent', True) and inconsistenteol(s):
         return s
+    if ui.configbool('eol', 'fix-trailing-newline', False) and s and s[-1] != '\n':
+        s = s + '\n'
     return eolre.sub('\n', s)
 
 def tocrlf(s, params, ui, **kwargs):
@@ -114,6 +116,8 @@
         return s
     if ui.configbool('eol', 'only-consistent', True) and inconsistenteol(s):
         return s
+    if ui.configbool('eol', 'fix-trailing-newline', False) and s and s[-1] != '\n':
+        s = s + '\n'
     return eolre.sub('\r\n', s)
 
 def isbinary(s, params):
--- a/tests/test-eol.t	Fri Jul 01 22:53:58 2011 +0400
+++ b/tests/test-eol.t	Fri Jul 01 23:12:52 2011 +0400
@@ -463,3 +463,60 @@
   > EOF
   $ hg commit -m 'consistent'
 
+
+Test trailing newline
+
+  $ cat >> $HGRCPATH <<EOF
+  > [extensions]
+  > eol=
+  > EOF
+
+setup repository
+
+  $ cd $TESTTMP
+  $ hg init trailing
+  $ cd trailing
+  $ cat > .hgeol <<EOF
+  > [patterns]
+  > **.txt = native
+  > [eol]
+  > fix-trailing-newline = False
+  > EOF
+
+add text without trailing newline
+
+  $ printf "first\nsecond" > a.txt
+  $ hg commit --addremove -m 'checking in'
+  adding .hgeol
+  adding a.txt
+  $ rm a.txt
+  $ hg update -C -q
+  $ cat a.txt
+  first
+  second (no-eol)
+
+  $ cat > .hgeol <<EOF
+  > [patterns]
+  > **.txt = native
+  > [eol]
+  > fix-trailing-newline = True
+  > EOF
+  $ printf "third\nfourth" > a.txt
+  $ hg commit -m 'checking in with newline fix'
+  $ rm a.txt
+  $ hg update -C -q
+  $ cat a.txt
+  third
+  fourth
+
+append a line without trailing newline
+
+  $ printf "fifth" >> a.txt
+  $ hg commit -m 'adding another line line'
+  $ rm a.txt
+  $ hg update -C -q
+  $ cat a.txt
+  third
+  fourth
+  fifth
+