美文网首页
git msg 提交格式校验

git msg 提交格式校验

作者: 桃李不言的蹊 | 来源:发表于2022-12-11 14:23 被阅读0次
1.需要把我们写的钩子脚本代码命名为commit-msg 命令行执行文件,放到项目的.git->hooks下面(需要给文件权限,不然默认执行不了,chmod 700 hooks/*),如下图: 5211670825866_.pic.jpg

钩子脚本代码如下,(用swift写的钩子脚本):

#!/usr/bin/env xcrun swift
import Foundation

let commitMessageFile = CommandLine.arguments[1]
print("LBLog commitMessageFile \(commitMessageFile)")

guard let data = FileManager.default.contents(atPath: commitMessageFile), let commitMessage = String(data: data, encoding: .utf8) else{
    //    返回状态码大于1  终止此次提交
    exit(1)
}


func shell(_ command: String) -> String {
    let task = Process()
    let outputPipe = Pipe()
    let errorPipe = Pipe()
    
    task.standardOutput = outputPipe
    task.standardError = errorPipe
    task.arguments = ["-c", command]
    task.executableURL = URL(fileURLWithPath: "/bin/zsh")
    
    do {
        try task.run()
        task.waitUntilExit()
    } catch {
        print("There was an error running the command: \(command)")
        print(error.localizedDescription)
        exit(1)
    }
    
    guard let outputData = try? outputPipe.fileHandleForReading.readToEnd(),
          let outputString = String(data: outputData, encoding: .utf8) else {
              // Print error if needed
              if let errorData = try? errorPipe.fileHandleForReading.readToEnd(),
                 let errorString = String(data: errorData, encoding: .utf8) {
                  print("Encountered the following error running the command:")
                  print(errorString)
              }
              exit(1)
          }
    
    return outputString
}


func writeAppendString(prefix: String) {
    print("LBLog final commit msg is \(prefix) \(commitMessage)")
    do {
        try "\(prefix) \(commitMessage.trimmingCharacters(in: .newlines))"
            .write(toFile: commitMessageFile, atomically: true, encoding: .utf8)
    } catch {
        print("Could not write to file \(commitMessageFile)")
        exit(1)
    }
}


//校验提交的msg是否格式正确   正确直接提交
//feat:s-15890 对接租约中心
func checkCommitMsgIsRight(){
    guard let regex = try? NSRegularExpression(pattern: #"[:][S s]-\d{1,10} [\u4e00-\u9fa5 a-z A-Z]{3,}"#, options: .anchorsMatchLines),
          let _ = regex.firstMatch(in: commitMessage, range: NSRange.init(location: 0, length: commitMessage.count)) else {
              print("LBLog input commit msg is not valid formatter , will auto change it")
              return
          }
//    输入的格式正确   直接exit掉
    exit(0)
}


checkCommitMsgIsRight()

let gitBranchName = shell("git rev-parse --abbrev-ref HEAD")
    .trimmingCharacters(in: .newlines)

let stringRange = NSRange(location: 0, length: gitBranchName.utf16.count)

//分支不是S-11825-11807-11688-09月28号-上线分支的格式的
guard let regex = try? NSRegularExpression(pattern: #"[S s]-.*-"#, options: .anchorsMatchLines),
      let match = regex.firstMatch(in: gitBranchName, range: stringRange) else {
          //没有匹配到  把分支名拼上
          writeAppendString(prefix: "feat:s-\(gitBranchName)")
          exit(0)
      }

let range = match.range(at: 0)

let ticketNumber = (gitBranchName as NSString)
    .substring(with: range)
    .trimmingCharacters(in: .newlines)


if !commitMessage.contains(ticketNumber) {
    let prefix = (ticketNumber as NSString).substring(to: ticketNumber.count - 1)
    writeAppendString(prefix: "feat:\(prefix)")
}

相关文章

  • git msg 提交格式校验

    1.需要把我们写的钩子脚本代码命名为commit-msg 命令行执行文件,放到项目的.git->hooks下面(需...

  • Git 常用命令

    Git Note 提交到版本库 git add . -> git commit -m "msg" git comm...

  • git commit 使用及规范

    git commit 使用说明 1 概述 git提交推荐使用命令行工具,请严格遵循提交格式。 2 提交格式 在您g...

  • git commit message的规范与校验

    git commit message格式 git每次提交代码,都必须写commit message(提交说明),用...

  • SourceTree与Git命令对应

    1.提交推送 gitadd file_1 file_2 //添加暂存文件 git commit -m 'msg...

  • GIT之提交日志中文配置

    GIT之提交日志中文配置 Linux 文件提交编码格式 git config --global i18n.comm...

  • commit

    将暂存区中的文件提交到本地仓库中。注意它只会提交暂存区中的文件。 通过 git commit -m [msg] ,...

  • Git 提交规范

    前言在提交commit信息时,为了统一规范commit信息,可以在Git Hooks中编写修改commit-msg...

  • 去掉git的提交校验

    公司里遇到一个情况,公司在开发机提交代码的时候,会自动的触发代码校验,如果格式不合格或者缺少部分必须的语句就会提示...

  • 2018-12-03 git命令

    git staus git add . git commit -m "msg" //git remote add ...

网友评论

      本文标题:git msg 提交格式校验

      本文链接:https://www.haomeiwen.com/subject/ecufqdtx.html