Skip to content

Instantly share code, notes, and snippets.

@sebald
Last active April 24, 2017 11:22
Show Gist options
  • Save sebald/6f9cc8be436e73ecc121777fb12d8ffc to your computer and use it in GitHub Desktop.
Save sebald/6f9cc8be436e73ecc121777fb12d8ffc to your computer and use it in GitHub Desktop.

Revisions

  1. sebald revised this gist Apr 24, 2017. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions prepare-commit-msg
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@ const { exec } = require('child_process');
    const { readFile, writeFile } = require('fs');
    const { EOL } = require('os');

    const CURRENT_BRANCH_JIRA_ISSUE_EXP = /\*[^/]*\/([a-zA-Z]+([-][0-9]+)+)/;
    const CURRENT_BRANCH_JIRA_ISSUE_EXP = /[a-zA-Z]+[-][0-9]+/;
    const messageFile = process.argv[2];

    /**
    @@ -14,12 +14,12 @@ if(!/COMMIT_EDITMSG/i.test(messageFile)) {
    return process.exit(0);
    }

    exec('git branch', (err, stdout) => {
    exec('git rev-parse --abbrev-ref HEAD', (err, stdout) => {
    // Command will fail if there are no commits yet.
    if (err) { return process.exit(0); }

    // Extract Jira issue number from current branch.
    const issue = (CURRENT_BRANCH_JIRA_ISSUE_EXP.exec(stdout)||[])[1];
    const issue = (CURRENT_BRANCH_JIRA_ISSUE_EXP.exec(stdout)||[])[0];

    // Could not find an issue number.
    if (!issue) { return process.exit(0); }
  2. sebald revised this gist Jan 27, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions prepare-commit-msg
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,7 @@ exec('git branch', (err, stdout) => {
    if (err) { return process.exit(0); }

    // Extract Jira issue number from current branch.
    const issue = (CURRENT_BRANCH_JIRA_ISSUE_EXP.exec(stdout)||[])[1].toUpperCase();
    const issue = (CURRENT_BRANCH_JIRA_ISSUE_EXP.exec(stdout)||[])[1];

    // Could not find an issue number.
    if (!issue) { return process.exit(0); }
    @@ -29,7 +29,7 @@ exec('git branch', (err, stdout) => {
    if (err) { return process.exit(0) };

    // Append issue number to file content and write back.
    writeFile(messageFile, `${data}${EOL}${EOL}Issue: ${issue}`, () => {
    writeFile(messageFile, `${data}${EOL}${EOL}Issue: ${issue.toUpperCase()}`, () => {
    process.exit(0);
    });
    });
  3. sebald created this gist Jan 26, 2017.
    36 changes: 36 additions & 0 deletions prepare-commit-msg
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    #!/usr/bin/env node
    const { exec } = require('child_process');
    const { readFile, writeFile } = require('fs');
    const { EOL } = require('os');

    const CURRENT_BRANCH_JIRA_ISSUE_EXP = /\*[^/]*\/([a-zA-Z]+([-][0-9]+)+)/;
    const messageFile = process.argv[2];

    /**
    * We need the message file to append.
    * If we can not find it -> abort.
    */
    if(!/COMMIT_EDITMSG/i.test(messageFile)) {
    return process.exit(0);
    }

    exec('git branch', (err, stdout) => {
    // Command will fail if there are no commits yet.
    if (err) { return process.exit(0); }

    // Extract Jira issue number from current branch.
    const issue = (CURRENT_BRANCH_JIRA_ISSUE_EXP.exec(stdout)||[])[1].toUpperCase();

    // Could not find an issue number.
    if (!issue) { return process.exit(0); }

    // Read commit message file
    readFile(messageFile, (err, data) => {
    if (err) { return process.exit(0) };

    // Append issue number to file content and write back.
    writeFile(messageFile, `${data}${EOL}${EOL}Issue: ${issue}`, () => {
    process.exit(0);
    });
    });
    });