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

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

1、安装 gogsnode

一、拉取镜像git

docker pull gogs/gogs
复制代码

二、建立数据目录vue-cli

mkdir -p /var/gogs
复制代码

三、建立窗口并运行docker

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

四、配置 gogsnpm

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

...windows

2、提交代码检查centos

提交代码检查主要是利用 git hooks 来运行脚本,对代码进行提交前的检查,若是检查不经过,则禁止提交。浏览器

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

一、安装 pre-git

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

二、配置 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": []
    }
}
复制代码

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

在项目根目录下建立 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)
        }
    })
}
复制代码

3、测试下

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

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

相关文章
相关标签/搜索