diff options
| -rwxr-xr-x | .hooks/pre-commit | 34 | ||||
| -rw-r--r-- | pom.xml | 22 | 
2 files changed, 56 insertions, 0 deletions
diff --git a/.hooks/pre-commit b/.hooks/pre-commit new file mode 100755 index 0000000..0f4c49a --- /dev/null +++ b/.hooks/pre-commit @@ -0,0 +1,34 @@ +#!/bin/sh + +trap 'rm -rf "$temp_dir"' EXIT +temp_dir=".tmp" + +IFS=' +' +for staged_file in $(git diff --cached --name-only); do +    [ -f "${staged_file}" ] || continue +    [ "${staged_file##*.}" = "java" ] || continue + +    check_files=y +    mkdir -p "$temp_dir/$(dirname "$staged_file")" +    git show :"$staged_file" > "$temp_dir/$staged_file" +done +unset IFS + +[ -z "$check_files" ] && exit 0 + +printf "%s\n" "-a -n --set-exit-if-changed" > "$temp_dir/gf_config" + +find "$temp_dir" -name '*.java' >> "$temp_dir/gf_config" + +output=$(./mvnw -q exec:java \ +                -Dexec.mainClass="com.google.googlejavaformat.java.Main" \ +                -Dexec.args="@$temp_dir/gf_config") + +if [ $? != 0 ]; then +    echo 'Run "./mvnw -q com.coveo:fmt-maven-plugin:format" first.' >&2 +    echo "Following files were rejected by the formatter:" >&2 +    escaped=$(printf '%s\n' "$temp_dir/" | sed 's/[[\.*^$/]/\\&/g') +    printf "%s\n" "$output" | sed "s/$escaped/    /" >&2 +    exit 1 +fi @@ -23,9 +23,11 @@          <!-- test dependencies -->          <junit.version>4.12</junit.version>          <mockito.version>2.18.0</mockito.version> +        <google-java-format.version>1.5</google-java-format.version>          <!-- plugins -->          <maven-surefire-plugin.version>2.21.0</maven-surefire-plugin.version>          <maven-shade-plugin.version>3.1.1</maven-shade-plugin.version> +        <fmt-maven-plugin.version>2.4.0</fmt-maven-plugin.version>      </properties>      <dependencies> @@ -83,6 +85,11 @@              <version>${mockito.version}</version>              <scope>test</scope>          </dependency> +        <dependency> +            <groupId>com.google.googlejavaformat</groupId> +            <artifactId>google-java-format</artifactId> +            <version>${google-java-format.version}</version> +        </dependency>      </dependencies>      <build> @@ -131,6 +138,21 @@                      </transformers>                  </configuration>              </plugin> +            <plugin> +                <groupId>com.coveo</groupId> +                <artifactId>fmt-maven-plugin</artifactId> +                <version>${fmt-maven-plugin.version}</version> +                <configuration> +                    <style>aosp</style> +                </configuration> +                <executions> +                    <execution> +                        <goals> +                            <goal>format</goal> +                        </goals> +                    </execution> +                </executions> +            </plugin>          </plugins>      </build>  | 
