git 服务搭建及提交代码检查

本地 git 服务,通常都会选择 gitlab。本人最先也是选择 gitlab,在 centos7 上按照官网的步骤进行安装,下载的速度难以忍受,无奈放弃。最终选择在 docker 中安装 gogs 镜像来自建 git 服务。

一、安装 gogs

1、拉取镜像

docker pull gogs/gogs
复制代码

2、创建数据目录

mkdir -p /var/gogs
复制代码

3、创建窗口并运行

docker run -d --name=git-gogs -p 10022:22 -p 13000:3000 -v /var/gogs:/data gogs/gogs
复制代码

4、配置 gogs

浏览器输入 url : http://ip:13000

...

二、提交代码检查

提交代码检查主要是利用 git hooks 来运行脚本,对代码进行提交前的检查,如果检查不通过,则禁止提交。

本交使用的是客户端钩子,工程是用 vue-cli 创建的。

1、安装 pre-git

yarn add pre-git@3.17.0 --dev
复制代码

2、配置 pre-git

在 package.json 中插入下列代码

"scripts": {
    "lint": "eslint --ext .js,.vue src test/unit test/e2e/specs",
    "pre-check": "node verify/commit-check.js && npm run lint"
},
"config": {
    "pre-git": {
        "enabled": true,
        "commit-msg": "simple",
        "pre-commit": [
            "npm run pre-check"
        ],
        "pre-push": [],
        "post-commit": [],
        "post-checkout": [],
        "post-merge": []
    }
}
复制代码

3、编写自定义代码检查脚本

在项目根目录下创建 verify/commit-check.js,此次检查主要实现:强制使用 eslint ,强制文件头部添加注释说明。commit-check.js 内容如下:

const fs = require('fs')
const path = require('path')
const config = require('../config')

// 彩色输出错误信息
// 开始时使用 chalk 
// windows 下无效
// 有更好的方法欢迎留言
function ConsoleLog () {}
ConsoleLog.prototype.white = function (info) {
    console.log('\x1B[37m', info)
}
ConsoleLog.prototype.green = function (info) {
    console.log('\x1B[32m', info)
}
ConsoleLog.prototype.red = function (info) {
    console.log('\x1B[31m', info)
}

const consoleLog = new ConsoleLog()

// 检查 eslint 是否打开
if (!config.dev.useEslint) {
    consoleLog.green('###########################')
    consoleLog.red('ERROR: ' + 'Set config.dev.useEslint = true.')
    consoleLog.red('请设置 config.dev.useEslint = true.')
    consoleLog.white('\n')
    process.exit(1)
} else {
    readDirSync(path.join(__dirname, '../src'))
}

// 检查文件头是否含有注释
function checkComments (file) {
    const extname = path.extname(file)
    if (extname === '.vue' || extname === '.js') {
        const lines = fs.readFileSync(file).toString().replace(/(^\s*)|(\s*$)/g, '')
        if (lines.startsWith('<!--') || lines.startsWith('/*')) {

        } else {
            consoleLog.green('###########################')
            consoleLog.red('ERROR: ' + 'Add file header comments.')
            consoleLog.red('请添加文件头部注释.')
            consoleLog.white('\n')
            process.exit(1)
        }
    }
}
// 遍历文件夹
function readDirSync (path) {
    let pa = fs.readdirSync(path)
    pa.forEach(function (ele) {
        let info = fs.statSync(path + '/' + ele)
        if (info.isDirectory()) {
            readDirSync(path + '/' + ele)
        } else {
            checkComments(path + '/' + ele)
        }
    })
}
复制代码

三、测试下

git add .
git commit -m "test"
复制代码

至些,一个简单的提交代码检查脚本就完成了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值