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

const gitcmd        = 'git log --pretty=full --since="{{sincecmd}}" --until="{{untilcmd}}"'
    , commitdatecmd = '$(git show -s --format=%ad `{{refcmd}}`)'
    , untilcmd      = ''
    , refcmd        = 'git rev-list --max-count=1 {{ref}}'
    , defaultRef    = '--tags'


const spawn    = require('child_process').spawn
    , bl       = require('bl')
    , split2   = require('split2')
    , list     = require('list-stream')
    , after    = require('after')
    , ghauth   = require('ghauth')
    , ghissues = require('ghissues')
    , chalk    = require('chalk')
    , argv     = require('minimist')(process.argv.slice(2))

    , commitStream = require('./commit-stream')

    , ghUser        = argv._[0] || 'iojs'
    , ghProject     = argv._[1] || 'io.js'
    , authOptions   = {
          configName : 'changelog-maker'
        , scopes     : []
      }


function replace (s, m) {
  Object.keys(m).forEach(function (k) {
    s = s.replace(new RegExp('\\{\\{' + k + '\\}\\}', 'g'), m[k])
  })
  return s
}


function organiseCommits (list) {
  if (argv['start-ref'])
    return list

  // filter commits to those _before_ 'working on ...'
  var started = false
  return list.filter(function (commit) {
    if (started)
      return false

    if ((/working on v?[\d\.]+/i).test(commit.summary))
      started = true
    else if ((/^v?[\d\.]+$/).test(commit.summary))
      started = true

    return !started
  })
}


function commitTags (list, callback) {
  var sublist = list.filter(function (commit) {
    return typeof commit.ghIssue == 'number' && commit.ghUser && commit.ghProject
  })

  if (!sublist.length)
    return setImmediate(callback)

  ghauth(authOptions, function (err, authData) {
    if (err)
      return callback(err)

    var done = after(sublist.length, callback)

    sublist.forEach(function (commit) {
      function onFetch (err, issue) {
        if (err) {
          console.error(`Error fetching issue #${commit.ghIssue}: ${err.message}`)
          return done()
        }

        if (issue.labels)
          commit.labels = issue.labels.map(function (label) { return label.name })
        done()
      }

      ghissues.get(authData, commit.ghUser, commit.ghProject, commit.ghIssue, onFetch)
    })
  })
}


function commitToGroup (commit) {
  return (/^[\w,]+:/.test(commit.summary) && commit.summary.split(':')[0]) || null
}


function commitToOutput (commit) {
  var semver     = commit.labels && commit.labels.filter(function (l) { return l.indexOf('semver') > -1 }) || false
    , group      = commitToGroup(commit) || ''
    , summaryOut = !group ? commit.summary : commit.summary.substr(group.length + 2)
    , shaOut     = `[\`${commit.sha.substr(0,10)}\`](https://github.com/${ghUser}/${ghProject}/commit/${commit.sha.substr(0,10)})`
    , labelOut   = (semver.length ? '(' + semver.join(', ').toUpperCase() + ') ' : '') + group
    , prUrlMatch = commit.prUrl && commit.prUrl.match(/^https?:\/\/.+\/([^\/]+\/[^\/]+)\/\w+\/\d+$/i)
    , out

  if (argv.simple) { 
    if (labelOut.length)
      summaryOut = `${labelOut}: ${summaryOut}`
    out = `* [${commit.sha.substr(0,10)}] - ${summaryOut} (${commit.author.name})`

    if (prUrlMatch)
      out += ` ${prUrlMatch[1] != ghUser + '/' + ghProject ? prUrlMatch[1] : ''}#${commit.ghIssue || commit.prUrl}`
  } else {
    if (labelOut.length)
      summaryOut = `**${labelOut}**: ${summaryOut}`
    out = `* [${shaOut}] - ${summaryOut} (${commit.author.name})`

    if (prUrlMatch)<commit_msg>escape '[' and ']' in commit summaries<commit_after>#!/usr/bin/env node

const gitcmd        = 'git log --pretty=full --since="{{sincecmd}}" --until="{{untilcmd}}"'
    , commitdatecmd = '$(git show -s --format=%ad `{{refcmd}}`)'
    , untilcmd      = ''
    , refcmd        = 'git rev-list --max-count=1 {{ref}}'
    , defaultRef    = '--tags'


const spawn    = require('child_process').spawn
    , bl       = require('bl')
    , split2   = require('split2')
    , list     = require('list-stream')
    , after    = require('after')
    , ghauth   = require('ghauth')
    , ghissues = require('ghissues')
    , chalk    = require('chalk')
    , argv     = require('minimist')(process.argv.slice(2))

    , commitStream = require('./commit-stream')

    , ghUser        = argv._[0] || 'iojs'
    , ghProject     = argv._[1] || 'io.js'
    , authOptions   = {
          configName : 'changelog-maker'
        , scopes     : []
      }


function replace (s, m) {
  Object.keys(m).forEach(function (k) {
    s = s.replace(new RegExp('\\{\\{' + k + '\\}\\}', 'g'), m[k])
  })
  return s
}


function organiseCommits (list) {
  if (argv['start-ref'])
    return list

  // filter commits to those _before_ 'working on ...'
  var started = false
  return list.filter(function (commit) {
    if (started)
      return false

    if ((/working on v?[\d\.]+/i).test(commit.summary))
      started = true
    else if ((/^v?[\d\.]+$/).test(commit.summary))
      started = true

    return !started
  })
}


function commitTags (list, callback) {
  var sublist = list.filter(function (commit) {
    return typeof commit.ghIssue == 'number' && commit.ghUser && commit.ghProject
  })

  if (!sublist.length)
    return setImmediate(callback)

  ghauth(authOptions, function (err, authData) {
    if (err)
      return callback(err)

    var done = after(sublist.length, callback)

    sublist.forEach(function (commit) {
      function onFetch (err, issue) {
        if (err) {
          console.error(`Error fetching issue #${commit.ghIssue}: ${err.message}`)
          return done()
        }

        if (issue.labels)
          commit.labels = issue.labels.map(function (label) { return label.name })
        done()
      }

      ghissues.get(authData, commit.ghUser, commit.ghProject, commit.ghIssue, onFetch)
    })
  })
}


function commitToGroup (commit) {
  return (/^[\w,]+:/.test(commit.summary) && commit.summary.split(':')[0]) || null
}


function cleanMarkdown (txt) {
  // just escape '[' & ']'
  return txt.replace(/([\[\]])/g, '\\$1')
}


function commitToOutput (commit) {
  var semver     = commit.labels && commit.labels.filter(function (l) { return l.indexOf('semver') > -1 }) || false
    , group      = commitToGroup(commit) || ''
    , summaryOut = !group ? commit.summary : commit.summary.substr(group.length + 2)
    , shaOut     = `[\`${commit.sha.substr(0,10)}\`](https://github.com/${ghUser}/${ghProject}/commit/${commit.sha.substr(0,10)})`
    , labelOut   = (semver.length ? '(' + semver.join(', ').toUpperCase() + ') ' : '') + group
    , prUrlMatch = commit.prUrl && commit.prUrl.match(/^https?:\/\/.+\/([^\/]+\/[^\/]+)\/\w+\/\d+$/i)
    , out

  if (argv.simple) { 
    if (labelOut.length)
      summaryOut = `${labelOut}: ${summaryOut}`
    out = `* [${commit.sha.substr(0,10)}] - ${summaryOut} (${commit.author.name})`

    if (prUrlMatch)
      out += ` ${prUrlMatch[1] != ghUser + '/' + ghProject ? prUrlMatch[1] : ''}#${commit.ghIssue || commit.prUrl}`
  } else {
    if (labelOut.length)
      summaryOut = `**${labelOut}**: ${summaryOut}`
    out = `* [${shaOut}] - ${cleanMarkdown(summaryOut)} (${commit.author.name})`

    if (prUrlMatch)
{'fraction_non_alphanumeric': 0.2, 'fraction_numerical': 0.005694444444444445, 'mean_word_length': 2.655329949238579, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '3554453', 'n_tokens_mistral': 2522, 'n_tokens_neox': 2436, 'n_words': 475}