check_file_eol.sh 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #-------------------------------------------------------------------------------------------------------
  2. # Copyright (C) Microsoft. All rights reserved.
  3. # Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. #-------------------------------------------------------------------------------------------------------
  5. # We will run dos2unix on the argument and make sure that it doesn't change.
  6. # If it changes, that means someone introduced a CRLF by not setting core.autocrlf to true.
  7. ERRFILE=check_eol.sh.err
  8. ERRFILETEMP=$ERRFILE.0
  9. # display a helpful message for someone reading the log
  10. echo "Check EOL > Checking $1"
  11. if [ ! -e $1 ]; then # the file wasn't present; not necessarily an error
  12. echo "WARNING: file not found: $1"
  13. exit 0 # don't report an error but don't run the rest of this file
  14. fi
  15. # We can't rely on dos2unix being installed, so simply grep for the CR octet 0x0d via xxd.
  16. # We don't want to simply detect a literal 0d in the file or output so configure xxd to emit
  17. # octets in such a way that we can grep for the CR octet and not accidentally detect
  18. # text of the file or 0d spanning 2 octets in xxd output, e.g., 20d1 (' Ñ').
  19. xxd -i -c 16 $1 | grep '0x0d' > $ERRFILETEMP
  20. if [ $? -eq 0 ]; then # grep found matches ($?==0), so we found CR (0x0d) in the file
  21. echo "ERROR: CR (0x0d) was introduced in $1" >> $ERRFILE
  22. # Display a user-readable hex dump for context of the problem.
  23. # Don't pollute the log with every single matching line, first 10 lines should be enough.
  24. echo "Displaying first 10 lines of hex dump where CR (0x0d) was found:" >> $ERRFILE
  25. xxd -g 1 $1 | grep -n '0d ' > $ERRFILETEMP
  26. head -n 10 $ERRFILETEMP >> $ERRFILE
  27. # To help the user, display how many lines of hex output actually contained CR.
  28. LINECOUNT=`python -c "file=open('$ERRFILETEMP', 'r'); print len(file.readlines())"`
  29. echo "Total hex dump lines containing CR (0x0d): $LINECOUNT" >> $ERRFILE
  30. echo "--------------" >> $ERRFILE # same length as '--- ERRORS ---'
  31. fi
  32. rm -f $ERRFILETEMP