From d0f2a773d9d78ada82da571dd20c31b6ced06b5c Mon Sep 17 00:00:00 2001
From: Florian Uhlig <f.uhlig@gsi.de>
Date: Mon, 13 Jul 2020 16:16:46 +0200
Subject: [PATCH] Fix the code checker

Only run the piplines when there is a MR to the master branch.
Properly connect the official CbmRoot repository for the format checker
such that the differing source files can be found.
Add a shell script to do this.
---
 .gitlab-ci.yml                   |  9 +++++++--
 cmake/scripts/check-format.sh    |  1 -
 scripts/connect_upstream_repo.sh | 20 ++++++++++++++++++++
 3 files changed, 27 insertions(+), 3 deletions(-)
 create mode 100755 scripts/connect_upstream_repo.sh

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 5837df7b0a..19ca9411e0 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -65,8 +65,12 @@ FormatCheck:
     refs:
       - merge_requests
     variables:
-      - $CI_MERGE_REQUEST_PROJECT_PATH == "computing/cbmroot"
+      - $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
+    # comparisson
+    - scripts/connect_upstream_repo.sh $CI_MERGE_REQUEST_PROJECT_URL
+    - git fetch upstream
     - echo "export FAIRSOFT_VERSION=jun19p1" > env.sh
     - echo "export FAIRROOT_VERSION=v18.2.0" >> env.sh
     - echo "export SIMPATH=/cvmfs/fairroot.gsi.de/fairsoft/\${FAIRSOFT_VERSION}" >> env.sh
@@ -75,6 +79,7 @@ FormatCheck:
     - echo "export SOURCEDIR=$PWD" >> env.sh
     - echo "export PATH=/cvmfs/fairroot.gsi.de/clang-format-8.0.1:\$SIMPATH/bin:$PATH:/cvmfs/it.gsi.de/compiler/llvm/6.0.1/bin/" >> env.sh
     - echo "export LABEL=format-check_MR-\${CI_MERGE_REQUEST_IID}" >> env.sh
+    - echo "export FAIRROOT_FORMAT_BASE=upstream/\${CI_MERGE_REQUEST_TARGET_BRANCH_NAME}" >> env.sh
     - . ./env.sh && ctest -S cmake/scripts/checkformat.cmake -VV
 
 CbmRoot_Merge:
@@ -85,7 +90,7 @@ CbmRoot_Merge:
     refs:
       - merge_requests
     variables:
-      - $CI_MERGE_REQUEST_PROJECT_PATH == "computing/cbmroot"
+      - $CI_MERGE_REQUEST_PROJECT_PATH == "computing/cbmroot" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "master"
   script:
     - echo $CI_MERGE_REQUEST_SOURCE_PROJECT_PATH
     - echo $CI_MERGE_REQUEST_PROJECT_PATH
diff --git a/cmake/scripts/check-format.sh b/cmake/scripts/check-format.sh
index 73b8a16352..ffec2cf57d 100755
--- a/cmake/scripts/check-format.sh
+++ b/cmake/scripts/check-format.sh
@@ -14,4 +14,3 @@ else
     echo "$RESULT"
     exit 1
 fi
-
diff --git a/scripts/connect_upstream_repo.sh b/scripts/connect_upstream_repo.sh
new file mode 100755
index 0000000000..c4781edcca
--- /dev/null
+++ b/scripts/connect_upstream_repo.sh
@@ -0,0 +1,20 @@
+#!/bin/bash
+
+upstream_repo=$1
+
+bla=$(git remote -v | grep upstream)
+if [ $? -eq 0 ]; then
+  echo "Remote link upstream already exist"
+  bla=$(git remote -v | grep upstream | grep $upstream_repo)
+  if [ $? -eq 0 ]; then
+    echo "Remote link upstream already exist and points to the correct repo"
+    # dont do anything
+  else
+    echo "Remote link upstream already exist and points to the wrong repo"
+    git remote rm upstream
+    git remote add upstream $upstream_repo
+  fi
+else
+  echo "Remote repo has to be connected"
+  git remote add upstream $upstream_repo
+fi
-- 
GitLab