feat: precommit hook for google java format
This commit is contained in:
		
							
								
								
									
										2
									
								
								hooks/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								hooks/README.md
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,2 @@ | |||||||
|  | # Useful hooks in this project | ||||||
|  | To use, just add the content of the wanted hook in `.git/hook/pre-commit`. You may need to use `chmod +x pre-commit` | ||||||
							
								
								
									
										24
									
								
								hooks/google-java-format
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										24
									
								
								hooks/google-java-format
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,24 @@ | |||||||
|  | #!/bin/bash | ||||||
|  |  | ||||||
|  | # Path to the Google Java Formatter JAR | ||||||
|  | FORMATTER_JAR="$HOME/.local/share/java/google-java-format.jar" | ||||||
|  |  | ||||||
|  | # Download the Google Java Formatter JAR if it doesn't exist | ||||||
|  | if [ ! -f "$FORMATTER_JAR" ]; then | ||||||
|  |   echo "Downloading Google Java Formatter..." | ||||||
|  |   mkdir -p "$(dirname "$FORMATTER_JAR")" | ||||||
|  |   curl -L -o "$FORMATTER_JAR" https://github.com/google/google-java-format/releases/download/v1.20.0/google-java-format-1.20.0-all-deps.jar | ||||||
|  | fi | ||||||
|  |  | ||||||
|  | # Format all staged Java files | ||||||
|  | STAGED_JAVA_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep "\.java$") | ||||||
|  |  | ||||||
|  | if [ -n "$STAGED_JAVA_FILES" ]; then | ||||||
|  |   echo "Formatting Java files..." | ||||||
|  |   java -jar "$FORMATTER_JAR" --skip-sorting-imports --skip-reflowing-long-strings --aosp --replace $STAGED_JAVA_FILES | ||||||
|  |  | ||||||
|  |   # Re-stage the formatted files | ||||||
|  |   git add $STAGED_JAVA_FILES | ||||||
|  | fi | ||||||
|  |  | ||||||
|  | exit 0 | ||||||
		Reference in New Issue
	
	Block a user