<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))
    , tsml     = require('tsml')

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

    , ghUser        = argv._[0] || 'nodejs'
    , 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)
    })
  })
}


var revertRe = /^revert\s+"?/i
  , groupRe  = /^((:?\w|\-|,|, )+):\s*/i

function commitToGroup (commit) {
  var summary = commit.summary.replace(revertRe, '')
    , m       = summary.match(groupRe)

  return m && m[1]
}


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


function toStringSimple (data) {
  var s = tsml`
<commit_msg>rewrite "iojs" org to "nodejs" manually<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))
    , tsml     = require('tsml')

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

    , ghUser        = argv._[0] || 'nodejs'
    , 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()
      }

      if (commit.ghUser == 'iojs')
        commit.ghUser = 'nodejs' // forcably rewrite as the GH API doesn't do it for us
      console.log(`ghissues commitUser=${commit.ghUser}, project=${commit.ghProject}, issue=${commit.ghIssue}`)
      ghissues.get(authData, commit.ghUser, commit.ghProject, commit.ghIssue, onFetch)
    })
  })
}


var revertRe = /^revert\s+"?/i
  , groupRe  = /^((:?\w|\-|,|, )+):\s*/i

function commitToGroup (commit) {
  var summary = commit.summary.replace(revertRe, '')
    , m       = summary.match(groupRe)

  return m && m[1]
}


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


function toStringSimple (data) {
  var s = tsml`

{'fraction_non_alphanumeric': 0.1786784140969163, 'fraction_numerical': 0.0028193832599118945, 'mean_word_length': 2.475811390079608, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1310162', 'n_tokens_mistral': 1977, 'n_tokens_neox': 1904, 'n_words': 417}