check_file_eol.sh 2.3 KB

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