[PATCH] eol: fix missing trailing newlines in comitted files

Stepan Koltsov stepancheg at yandex-team.ru
Wed Jun 29 16:45:31 CDT 2011


# HG changeset patch
# User Stepan Koltsov <stepancheg at yandex-team.ru>
# Date 1309383716 -14400
# Node ID 222e553fc1f59d83c01123c63dda918fd76f66ce
# Parent  c64bd320e4f0a0bf36f64292900822e6f1f09a56
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.

diff -r c64bd320e4f0 -r 222e553fc1f5 hgext/eol.py
--- a/hgext/eol.py	Mon Jun 27 16:39:02 2011 -0500
+++ b/hgext/eol.py	Thu Jun 30 01:41:56 2011 +0400
@@ -106,6 +106,8 @@
         return s
     if ui.configbool('eol', 'only-consistent', True) and inconsistenteol(s):
         return s
+    if 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 s and s[-1] != '\n':
+        s = s + '\n'
     return eolre.sub('\r\n', s)
 
 def isbinary(s, params):
diff -r c64bd320e4f0 -r 222e553fc1f5 tests/test-eol-trailing-newline.t
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-eol-trailing-newline.t	Thu Jun 30 01:41:56 2011 +0400
@@ -0,0 +1,39 @@
+Test trailing newline
+
+  $ cat >> $HGRCPATH <<EOF
+  > [extensions]
+  > eol=
+  > EOF
+
+setup repository
+
+  $ hg init repo
+  $ cd repo
+  $ cat > .hgeol <<EOF
+  > [patterns]
+  > **.txt = native
+  > EOF
+
+add text without trailing newline
+
+  $ printf "first\nsecond" > a.txt
+  $ hg commit --addremove -m 'checkin'
+  adding .hgeol
+  adding a.txt
+  $ rm a.txt
+  $ hg update -C -q
+  $ cat a.txt
+  first
+  second
+
+append a line without trailing newline
+
+  $ printf "third" >> a.txt
+  $ hg commit -m 'adding third line'
+  $ rm a.txt
+  $ hg update -C -q
+  $ cat a.txt
+  first
+  second
+  third
+


More information about the Mercurial-devel mailing list