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

Stepan Koltsov nga at yandex-team.ru
Wed Jun 29 22:55:14 UTC 2011


On 30.06.11 2:39, Matt Mackall wrote:
> On Thu, 2011-06-30 at 01:45 +0400, Stepan Koltsov wrote:
>> # 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.
> This strikes me as the wrong thing to do: it makes it impossible to make
> a file with no trailing newline.

It is also impossible to make a file with CRLF newlines when LF is 
configured.

> As it happens, some people actually
> don't want your proposed behavior:
>
> http://stackoverflow.com/questions/1050640/vim-disable-automatic-newline-at-end-of-file
>
> I think this issue is rather solidly in the domain of editor
> configuration.

Newline format is also configurable in editor, but eol extension is 
convenient.

Maybe I should add boolean configuration option: eol.fix-trailing-newline?


-- 
Stepan





>> 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
>> +
>> _______________________________________________
>> Mercurial-devel mailing list
>> Mercurial-devel at selenic.com
>> http://selenic.com/mailman/listinfo/mercurial-devel
>




More information about the Mercurial-devel mailing list