git server setup and code check

Keywords: Javascript git Docker Vue GitLab

For local git service, gitlab is usually selected. I first chose gitlab and installed it on centos7 according to the steps on the official website. The download speed was unbearable and I had no choice but to give up. Finally, choose to install gogs image in docker to build git service.

 

I. installation of gogs

1. Pull image

docker pull gogs/gogs

2. Create data directory

mkdir -p /var/gogs

3. Create a window and run it

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

4. Configure gogs

Browser input url: http://ip:13000

...

II. Submit code check

Commit code check mainly uses git hooks to run scripts and check the code before it is submitted. If the check fails, it is forbidden to submit.

The client hook is used in this handover, and the project is created with Vue cli.

1. Install pre Git

yarn add pre-git@3.17.0 --dev

2. Configure pre Git

Insert the following code into 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. Write custom code check script

Create verify/commit-check.js in the root directory of the project. The main implementation of this check is to force the use of eslint and add comments to the header of the file. The content of commit-check.js is as follows:

 1 const fs = require('fs')
 2 const path = require('path')
 3 const config = require('../config')
 4  5 // Color output error message
 6 // Start with chalk 
 7 // windows Invalid under
 8 // There's a better way to leave a message
 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 // inspect eslint Is it open?
23 if (!config.dev.useEslint) {
24     consoleLog.green('###########################')
25     consoleLog.red('ERROR: ' + 'Set config.dev.useEslint = true.')
26     consoleLog.red('Please set up config.dev.useEslint = true.')
27     consoleLog.white('\n')
28     process.exit(1)
29 } else {
30     readDirSync(path.join(__dirname, '../src'))
31 }
32 33 // Check if the file header contains comments
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('Please add a file header comment.')
44             consoleLog.white('\n')
45             process.exit(1)
46         }
47     }
48 }
49 // traverse folder 
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 }

 

III. test

git add .
git commit -m "test"

To some extent, a simple commit code check script is complete.

Posted by glennn3 on Sat, 07 Dec 2019 17:30:54 -0800