Can CVS and Subversion be set to ignore whitespace

2019-03-17 15:26发布

问题:

CVS and Subversion both have a handy merge feature so that when you update a source file that you have modified, it merges in changes that others have made on the same file.

However, if your changes and the other ones are incompatible - generally if you have both changed the same parts of the code - it will create a conflict. Both stretches of source code will be included into the merged file and you need to manually sort out which changes to keep. All fine so far.

My problem is that some of us use different development environments (Netbeans versus vi if you must know) and Netbeans has an auto-indenting feature which re-indents the code. Therefore, when we merge changes, we sometimes get huge conflicts which are mostly caused by simple changes in indentation and are not genuine changes to code. Often these create hundreds of lines of apparent conflicts which have to be manually resolved, but usually they come down to just a few lines of real changes. A similar situation occurs when someone's editor changes unix to Windows newlines or vice versa.

So - can I set merge to ignore these "conflicts" when comparing the two versions? Diff has the --ignore-space-change or -b option and I would like to have essentially the same feature available in cvs or svn. We use each tool on different projects so I would be happy to have the answer for either or both.

Two final notes:

  • clearly the merge process would have to make an arbitrary choice as to which version of the whitespace to use in the merged file. I'm fine with that - we can always reformat it again later.
  • I could avoid some of this by being more disciplined and checking in more often - acknowledged and understood. But I am not perfect.

回答1:

For SVN: In commandline tool, there is the option -x which you can set to "b" or "w" to ignore space changes resp. all spaces.
You can also supply a third party tool for doing the merges. So if you have a merger which ignores whitespaces, you can use this one. TortoiseSVN, as always, is a frontend to all parameters, so it will support for ignoring whitespaces as well.

The svn merge command is described here. The option you need is --diff3-cmd



回答2:

For Windows users, you can use TortoiseSVN (a Windows Explorer shell extension for Subversion) which comes with merge features that support what you are describing:

Ignore line endings excludes changes which are due solely to difference in line-end style.

Compare whitespaces includes all changes in indentation and inline whitespace as added/removed lines.

Ignore whitespace changes excludes changes which are due solely to a change in the amount or type of whitespace, eg. changing the indentation or changing tabs to spaces. Adding whitespace where there was none before, or removing a whitespace completely is still shown as a change.

Ignore all whitespaces excludes all whitespace-only changes.



回答3:

TortoiseMerge doesn't have any CLA (Command Line Arguments) to ignore the whitespace and ignore the case. After searching a lot it seems that it can be achieved still by tweaking the registry values.

/* DisableWhitespaceDifferences and DisableCaseDifferences. 
* The settings for TortoiseMerge is stored in Registry in CurrentUser\Software\TortoiseMerge\
* DWORDS stored the property values.
* 
* IgnoreWS         :   Set to 1 to ignore the whitespace differences. 
*                      Set to 0 to allow the whitespace differences.             
* IgnoreEOL        :   Set to 1 to ignore the End of Line differences. 
*                      Set to 0 to allow the End of Line differences.             
* CaseInsensitive  :   Set to 1 to ignore the Case differences. 
*                      Set to 0 to allow the Case differences.             
*/

// Get the key from the registry
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\TortoiseMerge", true))
{
   if (key != null)
   {
        // Set the IgnoreWS and IgnoreEOL DWORDs based on DisableWhitespaceDifferences is set or not
        key.SetValue("IgnoreWS", DisableWhitespaceDifferences ? 1 : 0, RegistryValueKind.DWord);
        key.SetValue("IgnoreEOL", DisableWhitespaceDifferences ? 1 : 0, RegistryValueKind.DWord);

        // Set the CaseInsensitive DWORD based on DisableCaseDifferences is set or not
        key.SetValue("CaseInsensitive", DisableCaseDifferences ? 1 : 0, RegistryValueKind.DWord);

        // close key
        key.Close();
    }
}