Skip to content

Commit 465cb6b

Browse files
committed
Implement Travis CI line ending checks
In our repository, we want: * All text files to use only LF ('\n') as their line endings. Not CRLF ('\r\n') * All text files to end with a newline (Newline at EOF). So, ideally, we should have checks that automatically check for these requirements in our repository. However, we can't impose these checks on all developers, namely Windows developers. They could be using either LF or CRLF (e.g. if the developer activated Git's core.autocrlf configuration). Even if we did, we can't guarantee that developers do not bypass these checks. As such, we have to implement these checks on the Travis CI level instead. The next question is how do we implement these checks. Checkstyle is already helping us with that, but it is not completely effective as it does not check *all* text files -- it only checks the source files which are used for compilation. This means that it does not check things like resource files (CSS and FXML files), test data and documentation. While we could probably teach it to check all files, we meet another problem; Checkstyle does not know what a text file is. At best, we can only give it a filter of file extensions to check. On the other hand, Git knows the difference between text files and binary files. As such, let's implement some shell scripts that will use Git's facilities to implement our line ending checks on all text files. These checks will be run on Travis CI, thus guaranteeing that master will not have any more line ending problems. These checks are implemented in POSIX shell scripts so that Travis CI would not need to download any additional dependencies in order to run them. Furthermore, developers running Linux or Macs can also run them locally without downloading additional dependencies as well. Windows devs would be unable to run them, but then again these line ending checks are not useful to them because as mentioned above, Windows devs may be using CRLF line endings anyway.
1 parent 5318247 commit 465cb6b

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

.travis.yml

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ language: java
22
matrix:
33
include:
44
- jdk: oraclejdk8
5-
script: travis_retry ./gradlew clean checkstyleMain checkstyleTest headless allTests coverage coveralls -i
5+
6+
script: >-
7+
./config/travis/run-checks.sh &&
8+
travis_retry ./gradlew clean checkstyleMain checkstyleTest headless allTests coverage coveralls -i
9+
610
before_install:
711
- "export DISPLAY=:99.0"
812
- "sh -e /etc/init.d/xvfb start"

config/travis/check-eof-newline.sh

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/sh
2+
# Checks that all text files end with a newline.
3+
4+
ret=0
5+
6+
for filename in $(git grep --cached -I -l -e '' -- ':/'); do
7+
if [ "$(tail -c 1 "./$filename")" != '' ]; then
8+
line="$(wc -l "./$filename" | cut -d' ' -f1)"
9+
echo "ERROR:$filename:$line: no newline at EOF."
10+
ret=1
11+
fi
12+
done
13+
14+
exit $ret

config/travis/check-line-endings.sh

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/sh
2+
# Checks for prohibited line endings.
3+
# Prohibited line endings: \r\n
4+
5+
git grep --cached -I -n --no-color -P '\r$' -- ':/' |
6+
awk '
7+
BEGIN {
8+
FS = ":"
9+
OFS = ":"
10+
ret = 0
11+
}
12+
{
13+
ret = 1
14+
print "ERROR", $1, $2, " prohibited \\r\\n line ending, use \\n instead."
15+
}
16+
END {
17+
exit ret
18+
}
19+
'

config/travis/run-checks.sh

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/sh
2+
# Runs all check-* scripts, and returns a non-zero exit code if any of them fail.
3+
4+
dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd) &&
5+
ret=0 &&
6+
for checkscript in "$dir"/check-*; do
7+
if ! "$checkscript"; then
8+
ret=1
9+
fi
10+
done
11+
exit $ret

0 commit comments

Comments
 (0)