| 
									
										
										
										
											2020-07-13 03:34:34 -04:00
										 |  |  | #!/usr/bin/env bash
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # This script ensures proper POSIX text file formatting and a few other things. | 
					
						
							| 
									
										
										
										
											2020-07-25 21:38:34 +02:00
										 |  |  | # This is supplementary to clang_format.sh and black_format.sh, but should be | 
					
						
							|  |  |  | # run before them. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # We need dos2unix and recode. | 
					
						
							|  |  |  | if [ ! -x "$(command -v dos2unix)" -o ! -x "$(command -v recode)" ]; then | 
					
						
							|  |  |  |     printf "Install 'dos2unix' and 'recode' to use this script.\n" | 
					
						
							|  |  |  | fi | 
					
						
							| 
									
										
										
										
											2020-07-13 03:34:34 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | set -uo pipefail | 
					
						
							|  |  |  | IFS=$'\n\t' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Loops through all text files tracked by Git. | 
					
						
							|  |  |  | git grep -zIl '' | | 
					
						
							|  |  |  | while IFS= read -rd '' f; do | 
					
						
							|  |  |  |     # Exclude some types of files. | 
					
						
							|  |  |  |     if [[ "$f" == *"csproj" ]]; then | 
					
						
							|  |  |  |         continue | 
					
						
							|  |  |  |     elif [[ "$f" == *"sln" ]]; then | 
					
						
							|  |  |  |         continue | 
					
						
							| 
									
										
										
										
											2022-01-05 17:53:08 -08:00
										 |  |  |     elif [[ "$f" == *".bat" ]]; then | 
					
						
							|  |  |  |         continue | 
					
						
							| 
									
										
										
										
											2021-04-19 20:50:52 +02:00
										 |  |  |     elif [[ "$f" == *".out" ]]; then | 
					
						
							|  |  |  |         # GDScript integration testing files. | 
					
						
							|  |  |  |         continue | 
					
						
							| 
									
										
										
										
											2020-07-13 03:34:34 -04:00
										 |  |  |     elif [[ "$f" == *"patch" ]]; then | 
					
						
							|  |  |  |         continue | 
					
						
							|  |  |  |     elif [[ "$f" == *"pot" ]]; then | 
					
						
							|  |  |  |         continue | 
					
						
							|  |  |  |     elif [[ "$f" == *"po" ]]; then | 
					
						
							|  |  |  |         continue | 
					
						
							|  |  |  |     elif [[ "$f" == "thirdparty"* ]]; then | 
					
						
							|  |  |  |         continue | 
					
						
							|  |  |  |     elif [[ "$f" == "platform/android/java/lib/src/com/google"* ]]; then | 
					
						
							|  |  |  |         continue | 
					
						
							| 
									
										
										
										
											2021-02-17 11:28:27 +01:00
										 |  |  |     elif [[ "$f" == *"-so_wrap."* ]]; then | 
					
						
							|  |  |  |         continue | 
					
						
							| 
									
										
										
										
											2020-07-13 03:34:34 -04:00
										 |  |  |     fi | 
					
						
							| 
									
										
										
										
											2020-07-28 02:56:05 -04:00
										 |  |  |     # Ensure that files are UTF-8 formatted. | 
					
						
							| 
									
										
										
										
											2020-07-13 03:34:34 -04:00
										 |  |  |     recode UTF-8 "$f" 2> /dev/null | 
					
						
							| 
									
										
										
										
											2020-07-28 02:56:05 -04:00
										 |  |  |     # Ensure that files have LF line endings and do not contain a BOM. | 
					
						
							| 
									
										
										
										
											2020-07-13 03:34:34 -04:00
										 |  |  |     dos2unix "$f" 2> /dev/null | 
					
						
							| 
									
										
										
										
											2020-07-28 02:56:05 -04:00
										 |  |  |     # Remove trailing space characters and ensures that files end | 
					
						
							|  |  |  |     # with newline characters. -l option handles newlines conveniently. | 
					
						
							| 
									
										
										
										
											2020-07-25 21:38:34 +02:00
										 |  |  |     perl -i -ple 's/\s*$//g' "$f" | 
					
						
							| 
									
										
										
										
											2020-07-13 03:34:34 -04:00
										 |  |  | done | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-27 10:34:33 -06:00
										 |  |  | diff=$(git diff --color) | 
					
						
							| 
									
										
										
										
											2020-07-13 03:34:34 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-22 17:13:08 -05:00
										 |  |  | # If no diff has been generated all is OK, clean up, and exit. | 
					
						
							| 
									
										
										
										
											2022-01-27 10:34:33 -06:00
										 |  |  | if [ -z "$diff" ] ; then | 
					
						
							| 
									
										
										
										
											2020-07-13 03:34:34 -04:00
										 |  |  |     printf "Files in this commit comply with the formatting rules.\n" | 
					
						
							|  |  |  |     exit 0 | 
					
						
							|  |  |  | fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-22 17:13:08 -05:00
										 |  |  | # A diff has been created, notify the user, clean up, and exit. | 
					
						
							| 
									
										
										
										
											2020-07-13 03:34:34 -04:00
										 |  |  | printf "\n*** The following differences were found between the code " | 
					
						
							|  |  |  | printf "and the formatting rules:\n\n" | 
					
						
							| 
									
										
										
										
											2022-01-27 10:34:33 -06:00
										 |  |  | echo "$diff" | 
					
						
							| 
									
										
										
										
											2020-07-13 03:34:34 -04:00
										 |  |  | printf "\n*** Aborting, please fix your commit(s) with 'git commit --amend' or 'git rebase -i <hash>'\n" | 
					
						
							|  |  |  | exit 1 |