利用git 的鉤子攔截有問(wèn)題的代碼
通過(guò)git 提供的鉤子功能,保證有問(wèn)題的代碼絕對(duì)不會(huì)被提交
Git 提供多種鉤子橘荠,在項(xiàng)目目錄的.git/hooks 目錄下面,名稱格式一般是pre-xxx。一個(gè)正常的git 的hooks 下面有多個(gè)示例文件彰亥,名稱格式為pre-xxx.sample。
在這里我們需要的是pre-commit衰齐。
修改他的內(nèi)容為./gradlew assembleDebug
chmod 777 pre-commit
然后修改文件屬性任斋。由于mac 的安全機(jī)制,我們的shell 腳本不會(huì)被執(zhí)行耻涛,需要去除相應(yīng)的屬性废酷。
命令為
xattr -c pre-commit
然后提交代碼測(cè)試吧
改進(jìn):
每次構(gòu)建需要花費(fèi)很多的時(shí)間,可選擇的方案有:
通過(guò)gradle 構(gòu)建時(shí)保存時(shí)間抹缕,如果當(dāng)前修改的文件的時(shí)間大于gradlew 時(shí)間的話澈蟆,需要重新來(lái)一次構(gòu)建
如果當(dāng)前修改文件的 時(shí)間小于gradle 上次的構(gòu)建時(shí)間,不需要重新進(jìn)行構(gòu)建
# coding=utf-8
import os
import sys
import time
from git import Repo
path = "."
if len(sys.argv) > 1:
path = sys.argv[1]
print(path)
last_time = 0
repo = Repo(path)
staged = repo.index.diff(None)
for s in staged:
print(s.a_path)
mTime = os.stat(os.path.join(path, s.a_path)).st_mtime
if mTime > last_time:
last_time = mTime
# headCommit = repo.head.commit
# print(headCommit)
# tree = headCommit.tree
# for t in tree: # intuitive iteration of tree members
# print(t.type)
# if t.type == "tree":
# if len(t.blobs) > 0:
# mTime = os.stat(t.blobs[0].abspath).st_mtime
# else:
# mTime = os.stat(t.abspath).st_mtime
# print(type(mTime))
# if mTime > last_time:
# last_time = mTime
print(last_time)
if last_time == 0:
exit(0)
file_modify_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(last_time))
print("修改時(shí)間是: {0}".format(file_modify_time))
gradle_build_time_file = os.path.join(path, "build_gradle_time")
if not os.path.exists(gradle_build_time_file):
print("沒(méi)有時(shí)間文件")
exit(1)
with open(gradle_build_time_file) as file_obj:
content = file_obj.read()
if int(content) > last_time:
print("無(wú)需重新構(gòu)建")
exit(0)
else:
print("需要重新構(gòu)建")
exit(1)
task saveTime() {
try(FileWriter writer = new FileWriter(new File(rootDir,"build_gradle_time"))) {
writer.write((System.currentTimeMillis()/1000).toInteger().toString())
}
}
preBuild.dependsOn(saveTime)
#!/bin/sh
python main.py
echo $?
if (($? == 0));then
exit 0
fi
./gradlew assembleDebug