From 325572150d4c12364f76088c1010cddbb4cfd12d Mon Sep 17 00:00:00 2001
From: Florian Uhlig <f.uhlig@gsi.de>
Date: Fri, 22 Jan 2021 14:37:01 +0100
Subject: [PATCH] Add test to check the file format

Only files with unix style file endings should eneter the repository.
Add a CI stage which fails if the file type of a changed file is dos.
Add a script which executes the actaul test.
---
 .gitlab-ci.yml               | 17 +++++++++++++++-
 scripts/check-file-format.sh | 38 ++++++++++++++++++++++++++++++++++++
 2 files changed, 54 insertions(+), 1 deletion(-)
 create mode 100755 scripts/check-file-format.sh

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 59977edc76..1c2516fc71 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -64,7 +64,7 @@ LinearHistCheck:
     -   exit 1
     - fi
 
-FormatCheck:
+CodeFormatCheck:
   stage: checkFormat
   tags:
     - CbmRoot
@@ -89,6 +89,21 @@ FormatCheck:
     - echo "export FAIRROOT_FORMAT_BASE=upstream/\${CI_MERGE_REQUEST_TARGET_BRANCH_NAME}" >> env.sh
     - . ./env.sh && ctest -S cmake/scripts/checkformat.cmake -VV
 
+FileFormatCheck:
+  stage: checkFormat
+  tags:
+    - CbmRoot
+  only:
+    refs:
+      - merge_requests
+    variables:
+      - $CI_MERGE_REQUEST_PROJECT_PATH == "computing/cbmroot" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "master"
+  script:
+    # Get the upstream repository manually. I did not find any other way to have it for
+    # comparison
+    - scripts/connect_upstream_repo.sh $CI_MERGE_REQUEST_PROJECT_URL
+    - git fetch upstream
+    - scripts/check-file-format.sh upstream
 
 CbmRoot_Continuous:
   stage: build
diff --git a/scripts/check-file-format.sh b/scripts/check-file-format.sh
new file mode 100755
index 0000000000..4ba5cb0400
--- /dev/null
+++ b/scripts/check-file-format.sh
@@ -0,0 +1,38 @@
+#!/bin/bash
+
+if [[ $# -eq 1 ]]; then
+  UPSTREAM=$1
+else
+  if [ -z $UPSTREAM ]; then
+    UPSTREAM=$(git remote -v | grep git.cbm.gsi.de[:/]computing/cbmroot | cut -f1 | uniq)
+    if [ -z $UPSTREAM ]; then
+      echo "Error: Name of upstream repository not provided and not found by automatic means"
+      echo 'Please provide if by checking your remotes with "git remote -v" and exporting UPSTREAM'
+      echo "or passing as an argument"
+      exit -1
+    fi
+  fi
+fi  
+echo "Upstream name is :" $UPSTREAM
+
+BASE_COMMIT=$UPSTREAM/master
+CHANGED_FILES=$(git diff --name-only $BASE_COMMIT)
+for file in $CHANGED_FILES; do
+  result=$(file $file | grep CRLF)
+  if [[ "$result" != "" ]]; then
+    echo ""
+    echo "File $file has wrong file format"
+    echo "$result"
+    echo ""
+    okay=false
+  fi
+done
+if [[ "$okay" = "false" ]]; then
+  echo ""
+  echo "Not all files have the correct file format"
+  echo "Test failed"
+  exit 1
+else
+  exit 0
+fi
+
-- 
GitLab