<commit_before>#!/usr/bin/env node

/**
 * GIT COMMIT-MSG hook for validating commit message to fit schema.
 */

'use strict'

let fs = require('fs')

const allowedMaxLength = 70
const allowedPattern = /^(\w*)(\(.*\))?: (.+)$/
const allowedTypes = [
  'feat',
  'fix',
  'docs',
  'style',
  'refactor',
  'test',
  'chore'
]

const error = message => {
  console.error(`INVALID COMMIT MSG: ${message}`)
}

const isFirstLineValid = line => {
  let isValid = true

  if (line.length > allowedMaxLength) {
    error(`is longer than ${allowedMaxLength} characters!`)
    isValid = false
  }

  const match = allowedPattern.exec(line)

  if (!match) {
    error(`does not match pattern "<type>(<scope>): <subject>" ! was: ${line}`)
    return false
  }

  const type = match[1]
  // const scope = match[3]
  // const subject = match[4]

  if (allowedTypes.indexOf(type) < 0) {
    error(`"${type}" is not allowed type !`)
    return false
  }

  return isValid
}

const isSecondLineValid = line => {
  if (line !== '') {
    error('Second line has to be an empty line!')
    return false
  }
  return true
}

let getLineFromBuffer = function(buffer, lineNumber) {
  return buffer.toString().split('\n')[lineNumber]
}

let commitMsgFile = process.argv[2] || getGitFolder() + '/COMMIT_EDITMSG'

fs.readFile(
  commitMsgFile, function(err, buffer) {
    const firstLine = getLineFromBuffer(buffer, 0)
    const secondLine = getLineFromBuffer(buffer, 1)

    if (err) {
      error(`Error reading file: ${err}`)
    }

    if (isFirstLineValid(firstLine) && isSecondLineValid(secondLine)) {
      process.exit(0)
    } else {
      process.exit(1)
    }
  }
)

function getGitFolder() {
  let gitDirLocation = './.git'
  if (!fs.existsSync(gitDirLocation)) {
    error(`Cannot find file ${gitDirLocation}`)
  }

  if (!fs.lstatSync(gitDirLocation).isDirectory()) {
    let unparsedText = '' + fs.readFileSync(gitDirLocation)
    gitDirLocation = unparsedText.substring('gitdir: '.length).trim()
  }

  if (!fs.existsSync(gitDirLocation)) {
    error(`Cannot find file ${gitDirLocation}`)
  }

  return gitDirLocation
}
<commit_msg>chore(tocco-client): refactor msg validation file<commit_after>#!/usr/bin/env node

/**
 * GIT COMMIT-MSG hook for validating commit message to fit schema.
 */

'use strict'

const fs = require('fs')

const allowedMaxLength = 70
const allowedPattern = /^(\w+)(\(.+\))?: (.+)$/
const allowedTypes = [
  'feat',
  'fix',
  'docs',
  'style',
  'refactor',
  'test',
  'chore'
]

const error = message => {
  console.error(`INVALID COMMIT MSG: ${message}`)
}

const isFirstLineValid = line => {
  if (line.length > allowedMaxLength) {
    error(`is longer than ${allowedMaxLength} characters!`)
    return false
  }

  const match = allowedPattern.exec(line)

  if (!match) {
    error(`does not match pattern "<type>(<scope>): <subject>" ! was: ${line}`)
    return false
  }

  const type = match[1]
  // const scope = match[3]
  // const subject = match[4]

  if (!allowedTypes.includes(type)) {
    error(`"${type}" is not allowed type !`)
    return false
  }

  return true
}

const isSecondLineValid = line => {
  if (line !== '') {
    error('Second line has to be an empty line!')
    return false
  }
  return true
}

const getLineFromBuffer = function(buffer, lineNumber) {
  return buffer.toString().split('\n')[lineNumber]
}

const commitMsgFile = process.argv[2] || getGitFolder() + '/COMMIT_EDITMSG'

fs.readFile(
  commitMsgFile, function(err, buffer) {
    const firstLine = getLineFromBuffer(buffer, 0)
    const secondLine = getLineFromBuffer(buffer, 1)

    if (err) {
      error(`Error reading file: ${err}`)
    }

    if (isFirstLineValid(firstLine) && isSecondLineValid(secondLine)) {
      process.exit(0)
    } else {
      process.exit(1)
    }
  }
)

function getGitFolder() {
  let gitDirLocation = './.git'
  if (!fs.existsSync(gitDirLocation)) {
    error(`Cannot find file ${gitDirLocation}`)
  }

  if (!fs.lstatSync(gitDirLocation).isDirectory()) {
    const unparsedText = '' + fs.readFileSync(gitDirLocation)
    gitDirLocation = unparsedText.substring('gitdir: '.length).trim()
  }

  if (!fs.existsSync(gitDirLocation)) {
    error(`Cannot find file ${gitDirLocation}`)
  }

  return gitDirLocation
}

{'fraction_non_alphanumeric': 0.15492957746478872, 'fraction_numerical': 0.004929577464788733, 'mean_word_length': 3.532978723404255, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1439488', 'n_tokens_mistral': 1454, 'n_tokens_neox': 1416, 'n_words': 340}