Wednesday, January 7, 2015

How to find (and fix) files with missing CR (0xd) in Visual Studio

Motivation

On our project the primary source control is TFS. For larger features we have recently started using GIT repository with master branch automatically synchronized from TFS.
When resolving merge conflicts it happens from time to time that line ends get somehow corrupted (there are plenty of settings in GIT client and related diff/merge tools) which results into .cs files which have both/mixed - CR+LF and LF line endings.

How to find files with missing CR (0xd)

It is pretty simple to identify all the corrupted files using a regular expression in the Visual Studio ‘Find in Files’ dialog:

^(.*)[^\r]\n$

Dialog example:

visual_studio_search_for_files_containing_lf.png

How to fix files

Files can be easily fixed by reformatting:

  • Just press CTRL+A to select all the text inside the file
  • Then press CTRL+KF to make it formatted
  • After fixing the issues you can easily verify files using the above search pattern
    • It happens from time to time that the last empty line in the file is not corrected. The easiest way how to fix it is to remove it and perhaps add it again.