Skip to content

Commit

Permalink
Added check for filesystem related changes (#40)
Browse files Browse the repository at this point in the history
* Added check for filesystem related changes in existing files of the c4t branch like delete/rename of file which then leads to errors for the check. Also cleaned up the output to make the process more clear - with at least 37% more emojis.

---------

Co-authored-by: Noctunus <[email protected]>
  • Loading branch information
2 people authored and mo-c4t committed Sep 20, 2024
1 parent abbd4ba commit b08c1cf
Showing 1 changed file with 38 additions and 29 deletions.
67 changes: 38 additions & 29 deletions scripts/diff_against_branch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,17 @@
declare -a ERROR_FILES
ORIGIN=${1:-c4t}

git fetch origin $ORIGIN
# Color definition for bash output
RED='\033[0;31m'
GREEN='\033[0;32m'
PURPLE='\033[0;35m'
NC='\033[0m' # No Color

function check_filesystem_changes {
FILE=$1
echo -e "${RED}[FAIL] ERROR${NC}: Structural filesystem change for already existing file detected (deleted/renamed/etc)! File: $FILE"
ERROR_FILES+=("$FILE")
}

function check_added_file {
FILE=$1
Expand All @@ -34,71 +44,70 @@ function check_added_file {
OTHER_FILE=$(echo $FILE | sed -e "s#/v$FILE_VERSION/#/v$(( FILE_VERSION - 1))/#")

echo
echo "#############################################################################"
echo "## Detected newly added file: $FILE"
echo "## Version: $FILE_VERSION"
echo "## Comparing against $ORIGIN/$OTHER_FILE"
echo "#############################################################################"
echo
echo -e "🆕 Detected newly added file: ${PURPLE}$FILE${NC}"
echo -e "🔢 Version: ${PURPLE}$FILE_VERSION${NC}"
echo -e "🔃 Comparing against ${PURPLE}$ORIGIN/$OTHER_FILE${NC}"

GIT_PAGER=cat git diff --exit-code origin/$ORIGIN:$OTHER_FILE $FILE
if [[ "$?" == "0" ]] ; then
echo "## No change detected! (weird?)"
echo " No change detected! (weird?)"
fi
echo
}

function check_modified_file {
FILE=$1
OTHER_FILE=$FILE

echo
echo "#############################################################################"
echo "## Detected modified file: $FILE"
echo "## Comparing against $ORIGIN/$OTHER_FILE"
echo "#############################################################################"
echo
echo -e "🔧 Detected modified file: ${PURPLE}$FILE${NC}"
echo -e "🔃 Comparing against ${PURPLE}$ORIGIN/$OTHER_FILE${NC}"

GIT_PAGER=cat git diff --exit-code origin/$ORIGIN:$OTHER_FILE $FILE
if [[ "$?" == "0" ]] ; then
echo "## No change detected! (weird?)"
echo " No change detected! (weird?)"
fi

# Check here if the file has any structure modifications, if yes return
# some value != 0 for an automated script to fail if we detect any modifications
# against the c4t branch
diff -w -B - <(git show origin/$ORIGIN:$OTHER_FILE | sed -e "s#//.*##g") < <(cat $FILE | sed -e "s#//.*##g") > /dev/null
if [[ "$?" != "0" ]] ; then
echo
echo "## ❌ [FAIL] ERROR: Structural change detected in already existing file: $FILE"
echo -e "${RED}[FAIL] ERROR${NC}: Structural change detected in already existing file: ${PURPLE}$FILE${NC}"
ERROR_FILES+=("$FILE")
else
echo
echo "## ✅ [PASS] INFO: No structural change detected. The changes should only be in the comments."
echo -e "${GREEN}[PASS] INFO${NC}: No structural change detected. The changes should only be in the comments."
fi
echo
}

echo -e "⏳ Fetching the latest changes from the origin branch: ${PURPLE}$ORIGIN${NC}"
git fetch origin $ORIGIN > /dev/null 2>&1

echo -e "🔍 Checking for illegal filesystem changes like removing/moving existing files"
while read FILE ; do
check_added_file $FILE
done < <(git diff --name-status origin/$ORIGIN | grep -P "^A.*" | grep -oP "proto/.*")
check_filesystem_changes $FILE
done < <(git diff --diff-filter=am --name-status origin/$ORIGIN | grep -oP "proto/.*")

echo -e "🔍 Checking for added files to print out the diff of the new version against the previous version"
while read FILE ; do
check_added_file $FILE
done < <(git diff --diff-filter=A --name-status origin/$ORIGIN | grep -oP "proto/.*")

echo -e "🔍 Checking for modifications in existing files to catch unwanted structural changes"
while read FILE ; do
check_modified_file $FILE
done < <(git diff --name-status origin/$ORIGIN | grep -P "^M.*" | grep -oP "proto/.*")
done < <(git diff --diff-filter=M --name-status origin/$ORIGIN | grep -oP "proto/.*")

if [ ${#ERROR_FILES[@]} -gt 0 ] ; then
echo
echo "#############################################################################"
echo "## ❌ Found ${#ERROR_FILES[@]} errors while doing the diff"
echo "## Please check the following files: "
echo "❌ Found ${#ERROR_FILES[@]} error(s) while doing the diff"
echo "📂 Please check the following files: "
for file in ${ERROR_FILES[@]} ; do
echo "## -> $file"
echo -e " -> ${RED}$file${NC}"
done
echo "#############################################################################"
exit 1
fi

echo
echo "##########################"
echo "## ✅ No issues found! ##"
echo "##########################"
echo "✅ No issues found!"

0 comments on commit b08c1cf

Please sign in to comment.