Thread overview
[phobos] Annoying whitespace diffs
Aug 22, 2010
Walter Bright
Aug 22, 2010
David Simcha
Aug 22, 2010
Walter Bright
Aug 22, 2010
Jonathan M Davis
Aug 23, 2010
Leandro Lucarella
August 22, 2010
A lot of checkins to Phobos wind up with a lot of invisible changes, because:

1. random addition and subtraction of tabs
2. random appearance and disappearance of trailing whitespace

I'd like to put an end to this, with the following rules:

1. no tabs are allowed
2. no trailing whitespace is allowed

I use this program to do filter my checkins which takes care of it, feel free to use it or some equivalent.
--------------------------------------------------------
/* Replace tabs with spaces, and remove trailing whitespace from lines.
 */

import std.file;
import std.path;

int main(string[] args)
{
    foreach (f; args[1 .. $])
    {
        auto input = cast(char[]) std.file.read(f);
        auto output = filter(input);
        if (output != input)
            std.file.write(f, output);
    }
    return 0;
}


char[] filter(char[] input)
{
    char[] output;
    size_t j;

    int column;
    for (size_t i = 0; i < input.length; i++)
    {
        auto c = input[i];

        switch (c)
        {
            case '\t':
                while ((column & 7) != 7)
                {   output ~= ' ';
                    j++;
                    column++;
                }
                c = ' ';
                column++;
                break;

            case '\r':
            case '\n':
                while (j && output[j - 1] == ' ')
                    j--;
                output = output[0 .. j];
                column = 0;
                break;

            default:
                column++;
                break;
        }
        output ~= c;
        j++;
    }
    while (j && output[j - 1] == ' ')
        j--;
    return output[0 .. j];
}

August 22, 2010
  I develop using CodeBlocks, and apparently in its default setup it
converts tabs to spaces and snips off trailing whitespace.  On the one
hand, this is what's probably causing a lot of the weird diffs, but on
the other hand, every time I work on a module that has these defects
they automatically get fixed.

Ideally (not sure how well this would work in practice) such simple, mechanical style guidelines would automatically be applied to all code on checkin, i.e. your program or some equivalent would automatically run on the server side on all uploaded code immediately before it's committed to the SVN backend.

On 8/22/2010 5:20 PM, Walter Bright wrote:
> A lot of checkins to Phobos wind up with a lot of invisible changes, because:
>
> 1. random addition and subtraction of tabs
> 2. random appearance and disappearance of trailing whitespace
>
> I'd like to put an end to this, with the following rules:
>
> 1. no tabs are allowed
> 2. no trailing whitespace is allowed
>
> I use this program to do filter my checkins which takes care of it, feel free to use it or some equivalent.
> --------------------------------------------------------
> /* Replace tabs with spaces, and remove trailing whitespace from lines. */
>
> import std.file;
> import std.path;
>
> int main(string[] args)
> {
>    foreach (f; args[1 .. $])
>    {
>        auto input = cast(char[]) std.file.read(f);
>        auto output = filter(input);
>        if (output != input)
>            std.file.write(f, output);
>    }
>    return 0;
> }
>
>
> char[] filter(char[] input)
> {
>    char[] output;
>    size_t j;
>
>    int column;
>    for (size_t i = 0; i < input.length; i++)
>    {
>        auto c = input[i];
>
>        switch (c)
>        {
>            case '\t':
>                while ((column & 7) != 7)
>                {   output ~= ' ';
>                    j++;
>                    column++;
>                }
>                c = ' ';
>                column++;
>                break;
>
>            case '\r':
>            case '\n':
>                while (j && output[j - 1] == ' ')
>                    j--;
>                output = output[0 .. j];
>                column = 0;
>                break;
>
>            default:
>                column++;
>                break;
>        }
>        output ~= c;
>        j++;
>    }
>    while (j && output[j - 1] == ' ')
>        j--;
>    return output[0 .. j];
> }
>
> _______________________________________________
> phobos mailing list
> phobos at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/phobos
>

August 22, 2010

David Simcha wrote:
>  I develop using CodeBlocks, and apparently in its default setup it
> converts tabs to spaces and snips off trailing whitespace.  On the one
> hand, this is what's probably causing a lot of the weird diffs, but on
> the other hand, every time I work on a module that has these defects
> they automatically get fixed.
>
> Ideally (not sure how well this would work in practice) such simple, mechanical style guidelines would automatically be applied to all code on checkin, i.e. your program or some equivalent would automatically run on the server side on all uploaded code immediately before it's committed to the SVN backend.
>

I agree, but I have no idea how to force that with svn.

August 22, 2010
On Sunday 22 August 2010 14:20:41 Walter Bright wrote:
> A lot of checkins to Phobos wind up with a lot of invisible changes, because:
> 
> 1. random addition and subtraction of tabs
> 2. random appearance and disappearance of trailing whitespace
> 
> I'd like to put an end to this, with the following rules:
> 
> 1. no tabs are allowed
> 2. no trailing whitespace is allowed

That would certainly be nice. And some editors should be able to take care of this for you - either because of the settings used when saving or through commands that you can run. For instance, vim has :retab which replaces all tabs with spaces. Personally, I set up vim to highlight all tabs and trailing spaces, so having them becomes a bit of an eyesore.

- Jonathan M Davis
August 22, 2010
Walter Bright wrote:
>
>
> David Simcha wrote:
>>  I develop using CodeBlocks, and apparently in its default setup it
>> converts tabs to spaces and snips off trailing whitespace.  On the
>> one hand, this is what's probably causing a lot of the weird diffs,
>> but on the other hand, every time I work on a module that has these
>> defects they automatically get fixed.
>>
>> Ideally (not sure how well this would work in practice) such simple, mechanical style guidelines would automatically be applied to all code on checkin, i.e. your program or some equivalent would automatically run on the server side on all uploaded code immediately before it's committed to the SVN backend.
>>
>
> I agree, but I have no idea how to force that with svn.
>

SVN has submit triggers. The simplest things to do would be to have it detect and reject anything that fails to fit the style. You might be able to make it fix it but it would be more complex.

> _______________________________________________
> phobos mailing list
> phobos at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/phobos
>

August 22, 2010
sed 's/\s*$//' kills trailing whitespace
Tabs are harder but I'd be shocked if nobody ever solved this before.  There might even be a way with sed.  There is no need for custom tools.

Re svn commits I think you can set up a filter script to reject inappropriate files.  You might even be able to apply filters to the files before they are committed.

Sent from my iPhone

On Aug 22, 2010, at 5:20 PM, Walter Bright <walter at digitalmars.com> wrote:

> A lot of checkins to Phobos wind up with a lot of invisible changes, because:
> 
> 1. random addition and subtraction of tabs
> 2. random appearance and disappearance of trailing whitespace
> 
> I'd like to put an end to this, with the following rules:
> 
> 1. no tabs are allowed
> 2. no trailing whitespace is allowed
> 
> I use this program to do filter my checkins which takes care of it, feel free to use it or some equivalent.
> --------------------------------------------------------
> /* Replace tabs with spaces, and remove trailing whitespace from lines. */
> 
> import std.file;
> import std.path;
> 
> int main(string[] args)
> {
>   foreach (f; args[1 .. $])
>   {
>       auto input = cast(char[]) std.file.read(f);
>       auto output = filter(input);
>       if (output != input)
>           std.file.write(f, output);
>   }
>   return 0;
> }
> 
> 
> char[] filter(char[] input)
> {
>   char[] output;
>   size_t j;
> 
>   int column;
>   for (size_t i = 0; i < input.length; i++)
>   {
>       auto c = input[i];
> 
>       switch (c)
>       {
>           case '\t':
>               while ((column & 7) != 7)
>               {   output ~= ' ';
>                   j++;
>                   column++;
>               }
>               c = ' ';
>               column++;
>               break;
> 
>           case '\r':
>           case '\n':
>               while (j && output[j - 1] == ' ')
>                   j--;
>               output = output[0 .. j];
>               column = 0;
>               break;
> 
>           default:
>               column++;
>               break;
>       }
>       output ~= c;
>       j++;
>   }
>   while (j && output[j - 1] == ' ')
>       j--;
>   return output[0 .. j];
> }
> 
> _______________________________________________
> phobos mailing list
> phobos at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/phobos
August 22, 2010
Walter Bright, el 22 de agosto a las 14:20 me escribiste:
> A lot of checkins to Phobos wind up with a lot of invisible changes, because:
> 
> 1. random addition and subtraction of tabs
> 2. random appearance and disappearance of trailing whitespace
> 
> I'd like to put an end to this, with the following rules:
> 
> 1. no tabs are allowed
> 2. no trailing whitespace is allowed
> 
> I use this program to do filter my checkins which takes care of it, feel free to use it or some equivalent.

Maybe is too extreme, but you can add pre-commit-hook[1] to directly reject commits that have tabs or trailing white spaces.

[1] http://svnbook.red-bean.com/en/1.1/ch05s02.html#svn-ch-5-sect-2.1

-- 
Leandro Lucarella (AKA luca)                     http://llucax.com.ar/
----------------------------------------------------------------------
GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145  104C 949E BFB6 5F5A 8D05)
----------------------------------------------------------------------
Con vos hay pica, patovica!
	-- Sidharta Kiwi