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

// Heavy inspiration from https://github.com/kentcdodds/validate-commit-msg

'use strict';

var fs = require('fs');
var path = require('path');
var findParentDir = require('find-parent-dir');
var chalk = require('chalk');

// fixup! and squash! are part of Git, commits tagged with them are not intended to be merged, cf. https://git-scm.com/docs/git-commit
// https://regex101.com/r/xg2yHR/1
var PATTERN = /^((fixup! |squash! )?(:\w+:){1}((\s\w+)+),(\s\w+)?\s#\d+)(?:\n|$)/;
var MERGE_COMMIT_PATTERN = /^Merge /;

// declare helper functions
var bufferToString = function (buffer) {
    return hasToString(buffer) && buffer.toString();
};

var hasToString = function (x) {
    return x && typeof x.toString === 'function';
};

var error = function() {
    console.error(chalk.red(arguments[0]));
};

var warn = function () {
    console.warn(chalk.yellow(arguments[0]));
};

// read/parse the commit message
var commitMsgPath = path.resolve("./", process.env.GIT_PARAMS);
fs.readFile(commitMsgPath, function readFile(err, buffer) {
    if(err && err.code !== 'ENOENT') {
        throw err;
    }

    var isFile = !err;
    var message = (
        isFile
        ? bufferToString(buffer)
        : commitMsgPath
    );
    
    // validate that it's in the right format (regex)
    // validate(msg, isFile);
    if (message === '') {
        error('Aborting commit due to empty commit message.');
        process.exit(1);
    }

    if (MERGE_COMMIT_PATTERN.test(message)) {
        warn('Merge commit detected, bypassing commit validation');
        process.exit(0);
    }

    if (!PATTERN.test(message)) {
        error('Commit messages in this repo must be in the following format:\n\n\
  :emoji: short description here, close #45\n\n\
Need some help? Take a look at this guide:\n\
https://git.io/vHvYk');
        process.exit(1);
    }

    // test for valid type
    var emojis = ['boom','sparkles','bug','zap','memo','blue_heart','art','hammer','white_check_mark'];
    var commitEmoji = message.split(":")[1];
    if (emojis.indexOf(commitEmoji) === -1){
        error(`:${commitEmoji}: is not an allowed emoji.\n\n\
Allowed emojis are:\n  :\
${emojis.join(':\n  :')}:\n\n\
Need some help? Take a look at this guide:\n\
https://git.io/vHvYk`);
        process.exit(1);
    }

});
<commit_msg>:blue_heart: update commit validation, #2<commit_after>#!/usr/bin/env node

// Heavy inspiration from https://github.com/kentcdodds/validate-commit-msg

'use strict';

var fs = require('fs');
var path = require('path');
var findParentDir = require('find-parent-dir');
var chalk = require('chalk');

// fixup! and squash! are part of Git, commits tagged with them are not intended to be merged, cf. https://git-scm.com/docs/git-commit
// https://regex101.com/r/xg2yHR/1
var PATTERN = /^((fixup! |squash! )?(:\w+:){1}((\s\w+)+),(\s\w+)?\s#\d+)(?:\n|$)/;
var MERGE_COMMIT_PATTERN = /^Merge /;

// declare helper functions
var bufferToString = function (buffer) {
    return hasToString(buffer) && buffer.toString();
};

var hasToString = function (x) {
    return x && typeof x.toString === 'function';
};

var error = function() {
    console.error(chalk.red.bold(arguments[0]));
};

var warn = function () {
    console.warn(chalk.yellow(arguments[0]));
};

// read/parse the commit message
var commitMsgPath = path.resolve("./", process.env.GIT_PARAMS);
fs.readFile(commitMsgPath, function readFile(err, buffer) {
    if(err && err.code !== 'ENOENT') {
        throw err;
    }

    var isFile = !err;
    var message = (
        isFile
        ? bufferToString(buffer)
        : commitMsgPath
    );
    
    // validate that it's in the right format (regex)
    // validate(msg, isFile);
    if (message === '') {
        error('Aborting commit due to empty commit message.');
        process.exit(1);
    }

    if (MERGE_COMMIT_PATTERN.test(message)) {
        warn('Merge commit detected, bypassing commit validation');
        process.exit(0);
    }

    if (!PATTERN.test(message)) {
        error('Commit messages in this repo must be in the following format:\
            \n\n  :emoji: short description here, close #45\
            \n\nNeed some help? Take a look at this guide:\
            \nhttps://git.io/vHvYk');
        process.exit(1);
    }

    // test for valid type
    var emojis = [
        {
            "name": "boom",
            "tag":":boom:",
            "description":"Breaking change, bumps major version",
            "semver": "major"
        },
        {
            "name": "sparkles",
            "tag":":sparkles:",
            "description":"New feature",
            "semver": "minor"
        },
        {
            "name": "bug",
            "tag":":bug:",
            "description":"Bug fix",
            "semver": "minor"
        },
        {
            "name": "zap",
            "tag":":zap:",
            "description":"Performance improvement",
            "semver": "minor"
        },
        {
            "name": "memo",
            "tag":":memo:",
            "description":"Documentation improvement",
            "semver": "patch"
        },
        {
            "name": "blue_heart",
            "tag":":chore:",
            "description":"Chore task, unrelated to experience or user value",
            "semver": "patch"
        },
        {
            "name": "art",
            "tag":":art:",
            "description":"Code style or formatting improvements",
            "semver": "patch"
        },
        {
            "name": "hammer",
            "tag":":hammer:",
            "description":"Refactored code",
            "semver": "patch"
        },
        {
            "name": "white_check_mark",
            "tag":":white_check_mark:",
            "description":"Adding or improving tests",
            "semver": "patch"
        }
    ];
    var commitEmoji = message.split(":")[1];

    var isValidEmoji = false;
    for (let emoji of emojis) {
        console.log(emoji.name);
        emoji.name === commitEmoji ? isValidEmoji = true : isValidEmoji = false;
    }

    if (isValidEmoji){
        error(`:${commitEmoji}: is not an allowed emoji.\
            \n\nAllowed emojis are:\
            \n  :${emojis.join(':\n  :')}:\
            \n\nNeed some help? Take a look at this guide:\
            \nhttps://git.io/vHvYk`);
        process.exit(1);
    }

});

{'fraction_non_alphanumeric': 0.1690207156308851, 'fraction_numerical': 0.005021971123666039, 'mean_word_length': 2.271560574948665, 'pattern_counts': {'":': 47, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 10, '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': '2943283', 'n_tokens_mistral': 2072, 'n_tokens_neox': 2023, 'n_words': 483}