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


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

     

    一、安装 gogs

    1、拉取镜像

    docker pull gogs/gogs

    2、创建数据目录

    mkdir -p /var/gogs

    3、创建窗口并运行

    docker run --privileged=true -d --name=git-gogs -p 10022:22 -p 13000:3000 -v /var/gogs:/data gogs/gogs

    4、配置 gogs

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

    ...

    配置保存路径:

    /var/gogs/gogs/conf/app.ini

    二、提交代码检查

    提交代码检查主要是利用 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 内容如下:

     1 const fs = require('fs')
     2 const path = require('path')
     3 const config = require('../config')
     4  5 // 彩色输出错误信息
     6 // 开始时使用 chalk 
     7 // windows 下无效
     8 // 有更好的方法欢迎留言
     9 function ConsoleLog () {}
    10 ConsoleLog.prototype.white = function (info) {
    11     console.log('x1B[37m', info)
    12 }
    13 ConsoleLog.prototype.green = function (info) {
    14     console.log('x1B[32m', info)
    15 }
    16 ConsoleLog.prototype.red = function (info) {
    17     console.log('x1B[31m', info)
    18 }
    19 20 const consoleLog = new ConsoleLog()
    21 22 // 检查 eslint 是否打开
    23 if (!config.dev.useEslint) {
    24     consoleLog.green('###########################')
    25     consoleLog.red('ERROR: ' + 'Set config.dev.useEslint = true.')
    26     consoleLog.red('请设置 config.dev.useEslint = true.')
    27     consoleLog.white('
    ')
    28     process.exit(1)
    29 } else {
    30     readDirSync(path.join(__dirname, '../src'))
    31 }
    32 33 // 检查文件头是否含有注释
    34 function checkComments (file) {
    35     const extname = path.extname(file)
    36     if (extname === '.vue' || extname === '.js') {
    37         const lines = fs.readFileSync(file).toString().replace(/(^s*)|(s*$)/g, '')
    38         if (lines.startsWith('<!--') || lines.startsWith('/*')) {
    39 40         } else {
    41             consoleLog.green('###########################')
    42             consoleLog.red('ERROR: ' + 'Add file header comments.')
    43             consoleLog.red('请添加文件头部注释.')
    44             consoleLog.white('
    ')
    45             process.exit(1)
    46         }
    47     }
    48 }
    49 // 遍历文件夹
    50 function readDirSync (path) {
    51     let pa = fs.readdirSync(path)
    52     pa.forEach(function (ele) {
    53         let info = fs.statSync(path + '/' + ele)
    54         if (info.isDirectory()) {
    55             readDirSync(path + '/' + ele)
    56         } else {
    57             checkComments(path + '/' + ele)
    58         }
    59     })
    60 }

     

    三、测试下

    git add .
    git commit -m "test"

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

  • 相关阅读:
    树的直径的两种求法
    2018CCPC吉林赛区(重现赛)部分题解
    2019中国大学生程序设计竞赛-女生专场(重现赛)部分题解C-Function(贪心+优先队列) H-clock(模拟)
    HDU-1693 Eat the Trees(插头DP)
    【巷子】---redux---【react】
    【巷子】---flux---【react】
    【JavaScript算法】---希尔排序
    【JavaScript算法】---快速排序法
    【JavaScript算法】---插入排序
    【深拷贝VS浅拷贝】------【巷子】
  • 原文地址:https://www.cnblogs.com/lifefriend/p/10246459.html
Copyright © 2020-2023  润新知