Every now and then we get issues from customers with corrupt files, especially DTM files. The issues in these cases is mostly the version control system Git and the line ending configuration.
Background: Line endings
When you write a new line in a code file, you just see the text written on another line. In the background, there are invisible control characters, which tell the editor, to display the text on another line.
Some text editors, such as e.g. Notepad++, have an option to show these hidden characters, which can be helpful in some cases.
Here’s an example of a file with displayed control characters and without.
Historically, there are various kinds of line ending styles.
Windows uses a combination of carriage return CR
and line feed LF
.
Linux uses only line feed LF
Mor info can be found on the internet, e.g. on Newline - Wikipedia
Line End Configuration (autocrlf)
Git has a configuration option to automatically adjust line endings on commit. If this configuration is set, all line endings will be converted to LF internally in the repository. Files with Windows style CRLF line endings will be converted during commit. When cloning the repository on a Windows client, Git will also automatically convert all LF line endings to CRLF even if the original file which was committed had LF line endings.
In some cases, it is possible that files get corrupted by this setting, because e.g. checksums are not valid anymore.
This is a client setting, which must be set on each git client. Most git clients have a checkbox in the installer to deactivate this configuration, but unfortunately the default is mostly set to on.
Effects
Issues with the settings can lead to strange behavior, where things work well on the working copy of the person who initially committed the file, but people who clone the project have issues with build errors, invalid configurations, checksum errors… In B&R PLC projects, specifically DTM files may get corrupted.
How to prevent
Generally, this is a client setting, which must be disabled for each client. This can be done either in the installer, or later on in the git command line, by using the command
git config --global core.autocrlf false
Sadly, this cannot be stored in the main repository and therefore must be set on all Git clients.
Another, more safe option, is to add a Git attributes file. In a Git attributes file, it is possible to define rules for various file types and Git operations. I would strongly recommend this option.
The only thing you have to do, is placing a file with the name .gitattributes
in the root of your git working copy.
The file contents could look like this:
# Use gitattributes to deactivate line ending conversion, as this may corrupt some files
# See:
# https://community.br-automation.com/t/git-line-ending-configuration/4073
# https://adaptivepatchwork.com/2012/03/01/mind-the-end-of-your-line/
# https://git-scm.com/docs/gitattributes
# Set all files to not make automatic conversion
* -text
IMHO I would recommend to not use any automatic end of line conversion, as there is always a risk of corrupting something. Git should be responsible for version control, but not for formatting files.