Android git commit之前进行checkStyle检查

背景:开发的人员越来越多,水平习惯参差不齐,这就导致了代码的维护越来越复杂,所以希望有一个规范,可以规范大家的提交,所以出现了我所做的这个通过hook实现在代码commit之前进行检查。


实现效果:针对我所提交的代码进行checkStyle检查。


实现:

1.修改pre-commit文件。

在.git下面的hooks文件夹中增加pre-commit 文件,

修改权限chmod a+x pre-commit

#!/bin/sh
#
# An example hook script to verify what is about to be committed.
# Called by "git commit" with no arguments.  The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# To enable this hook, rename this file to "pre-commit".

if git rev-parse --verify HEAD >/dev/null 2>&1
then
    against=HEAD
else
    # Initial commit: diff against an empty tree object
    against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

SCRIPT_DIR=$(dirname "$0")
SCRIPT_ABS_PATH=`cd "$SCRIPT_DIR"; pwd`
cd $SCRIPT_ABS_PATH/../../UCarNew/
cd ..
FILE_LIST=$(git status)
cd UCarNew
RESULT_INFO=$($SCRIPT_ABS_PATH/../../UCarNew/gradlew -Dorg.gradle.project.checkstyle="$FILE_LIST" checkstyle)
if [ $? -eq 0   ]; then
    echo "checkstyle OK"
else
    [[ $ERROR_INFO =~ "checkstyle" ]] && exit 1
fi
cd ..

2.在build.gradle文件中添加checkstyle的task

    task checkstyle(type :Checkstyle) {
        source 'src'
        source 'biz_mine'
        source 'base'
        source 'sdk'
        source 'thirdparty'
        source 'patch'
        source 'plugin'
        if(project.hasProperty('checkstyle')){
            def ft = fc(checkstyle);
            for(int i =0;i
def filterCommitter(String androidbootClassFiles){
    ArrayList filterList = new ArrayList();
    String [] files = androidbootClassFiles.split("\\n")
    for(String file : files){
        if(file.contains("UCarNew")){
            String[] b = file.split("modified:   ");
            filterList.add(b[1])
        }
    }
    return filterList;
}

ext {
    fc = this.&filterCommitter
}

这样就实现了我们需要的功能。

接下来还需要需改checkstyle的配置文件



    
    
    
    
    
    
    
    
    
    
        
        
        
        
            
            
            
            
            
            
            
        
        
        
        
        
            
        
        
        
            
            
            
            
            
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
            
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
            
        
        
        
            
            
            
        
        
        
            
            
            
        
        
        
            
        
        
        
        
        
        
        
        
            
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
            
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
            
            
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
            
            
        
        
        
        
        
        
        
        
        
        
        
        
            
            
        
        
            
            
        
        
            
            
        
        
        
            
        
        
        
        
        
        
        
        
        
        
        
        
        
            
        
    


到这里就完成了整个配置了,可以根据自己的需求来配置不同的选项了

最后再说一下如何使用

每次git commit 或者通过android studio commit代码的时候,都会先对修改的java文件进行检查,如果有问题,

则会在build/reports/checkstyle/checkstyle.xml中将错误列举出来。


以这一条举例说明:

39行第9列 错误,声明变量顺序错误,进行的是com.puppycrawl.tools.checkstyle.checks.coding.DeclarationOrderCheck检查。

具体错误原因可以参考下面表格

错误提示

错误说明

missing a javadoc comment

缺少类注释

Line longer than X characters

行长度超过X个字符(包括空格)

Return count is X(max allowed 3)

一个方法内的返回数量是X(最大值只能为3)

Nested if-else depth is X(max allowed is 3)

最大的if-else嵌套层数为X(最大只能为3)

Array brackets at illegal position

数组的方括号“[]”的位置不正确(检查数组类型的定义是String[] args,而不是String args[])

Line matchs the illegal pattern 'System\.out\.println'

本行包含System.out.println语句

ctor def modifier at indentation level 8 not at corrent indentation 4

缩进不正确,一般是因为没有在Eclipse中使用4个空格代替tab键引起。

'static' modifier out of order with the JLS suggestions

static修饰符没有按照JLS的建议来排序(eg.写成public final static...应该改成public static final)

Name 'X' must match pattern '^[A-Z][A-Z0-9][_A-Z0-9+]$'(正则表达式)

名称不符合正则表达式'^[A-Z][A-Z0-9][_A-Z0-9+]$'(即为大写字母,数字、下划线等)。

一般在静态变量没有大写时提示,包名不是全部消息时提示,类名不是大写开头时提示,方法名不是小写开头时提示

Variable access definition in wrong order

变量定义顺序不正确(例如在类成员变量定义时,将private类型的变量定义在public类型的变量之前)

Static variable definition in wrong order

静态变量定义顺序不正确(例如在构造函数之后定义静态变量)

Instance variable definition in wrong order

成员变量定义顺序不正确(例如在构造函数之后定义成员变量)

X is a magic number

X是一个魔术数字(非0、1、2的数字)

if construct must use '{}'

if结构必须使用'{}'

Got an exception - Unexpected character 0xfffd in identifier

因为没有设置checkstyle配置文件的charset为UTF-8,而类文件使用UTF-8编码,并且含有中文

“{” should be on the previous line

“{” 应该位于前一行

Methods is missing a javadoc comment

方法前面缺少javadoc注释

Expected @throws tag for “Exception”

在注释中希望有@throws的说明

“.” Is preceeded with whitespace

“.” 前面不能有空格

“.” Is followed by whitespace

“.” 后面不能有空格

“=” is not preceeded with whitespace“=”

前面缺少空格

“=” is not followed with whitespace

“=” 后面缺少空格

“}” should be on the same line

“}” 应该与下条语句位于同一行

Unused @param tag for “unused”

没有参数“unused”,不需注释

Variable “X” missing javadoc

变量“CA”缺少javadoc注释

Line contains a tab character

行含有”tab” 字符

Redundant “Public” modifier

冗余的“public” modifier

final modifier out of order with the JSL suggestion

final修饰符的顺序错误

Avoid using the “.*” form of import

Import格式避免使用“.*”

Redundant import from the same package

从同一个包中Import内容

Unused import-X Import

import的X类没有被使用

Duplicate import to line X

重复Import同一个内容

Import from illegal package

从非法包中 Import内容

“while” construct must use “{}”

“while” 语句缺少“{}”

Variable “X” must be private and have accessor method

变量“X”应该是private的,并且有调用它的方法

Variable “X” must match pattern “^[a-z][a-zA-Z0-9]*$”

变量“X”不符合命名规则“^[a-z][a-zA-Z0-9]*$”

“(” is followed by whitespace

“(” 后面不能有空格

“)” is proceeded by whitespace

“)” 前面不能有空格





你可能感兴趣的:(Android git commit之前进行checkStyle检查)