子嘉的博客 子嘉的博客
首页
bic-bic
技术
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

高子嘉

没有比脚更长的路,没有比人更高的山
首页
bic-bic
技术
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 技术文档

  • GitHub技巧

  • 博客搭建

  • 服务端

  • distributed

  • golang

    • gomod
    • compile
    • code_snippet

    • protobuf
    • 单元测试
    • script

      • auto_build
      • pre-commit
  • db

  • docker

  • linux

  • 技术
  • golang
  • script
子嘉
2022-06-05

pre-commit

#!/usr/bin/python
# -*- coding: utf-8 -*-
# 将此文件置于 .git/hooks/ 目录下
# 修改文件为可执行 chmod u+x pre-push
# 该脚本将在执行git push之前执行,用于检查提交的文件格式
print "pre-commit in"

import os
import subprocess

# 获取变化的文件名列表
diff_file_str=subprocess.check_output(["git", "diff", "--cached", "--name-only", "--diff-filter=ACM"])
diff_file_list = diff_file_str.strip().split("\n")
#print "diff file list = ", diff_file_list

go_file_list = []
go_filename_list = []
for filename in diff_file_list:
    # 只处理 golang 文件
    if not filename.endswith(".go"):
        continue
    go_file_list.append(filename.split("/"))
    go_filename_list.append("./" + filename)

check_dir = {}
for filename in go_file_list:
    if len(filename) == 0:
        continue
    elif len(filename) == 1:
        # 没有目录层级,根目录文件,直接检查这个文件
        check_dir[filename[0]] = filename[0]
    elif filename[0] in ("apps", "robot"):
        # 对于apps, robot 目录,不用全部检查, 只检查对应子目录
        check_dir[filename[0]+filename[1]] = "./"+filename[0]+"/"+filename[1]+"/..."
    else:
        check_dir[filename[0]] = "./"+filename[0]+"/..."

check_dir_set = set(check_dir.values())
#print "check_dir_set = ", check_dir_set
err = 0
for d in check_dir_set:
    a=os.system("go vet " + d)
    err = err + a

if err == 0:
    for filename in go_filename_list:
        # 如果当前文件没有被格式化,就格式化它
        UNFORMATTED=subprocess.check_output(["gofmt", "-l", filename])

        if UNFORMATTED:
            os.system("gofmt -w "+ filename)

        # 上述 gofmt可能会对文件作出改动
        # 所以此处将更改提交至暂存区
        os.system("git add "+ filename)
#print "pre-commit out ", err

if err == 0:
    print "commit success!"
    exit(0)
else:
    print "commit failed!"
    exit(1)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
编辑 (opens new window)
#git
上次更新: 2023/02/24, 10:34:03
auto_build
mysql

← auto_build mysql→

最近更新
01
mongodb restore
03-06
02
pytesseract
02-28
03
consul
02-24
更多文章>
Theme by Vdoing | Copyright © 2022-2025 子嘉 | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式