{"size": 1545, "ext": "js", "max_stars_repo_path": "src/diff-tree.js", "max_stars_repo_name": "ivanmartos/semantic-release-sentry-releases", "max_stars_repo_stars_event_min_datetime": "2019-11-27T20:24:33.000Z", "max_stars_repo_stars_event_max_datetime": "2022-01-20T21:16:14.000Z", "max_issues_repo_path": "src/diff-tree.js", "max_issues_repo_name": "ivanmartos/semantic-release-sentry-releases", "max_issues_repo_issues_event_min_datetime": "2019-11-05T21:26:33.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-29T13:35:03.000Z", "max_forks_repo_path": "src/diff-tree.js", "max_forks_repo_name": "Waracle/semantic-release-sentry-releases", "max_forks_repo_forks_event_min_datetime": "2020-03-07T23:31:49.000Z", "max_forks_repo_forks_event_max_datetime": "2022-01-05T14:08:29.000Z"}
{"max_stars_count": 11, "max_issues_count": 635, "max_forks_count": 15, "avg_line_length": 25.75, "max_line_length": 75, "alphanum_fraction": 0.5896440129}
const gitDiffTree = require('git-diff-tree')
const path = require('path')

/**
 * @typedef {import('./types').SentryReleasePatchSet} SentryReleasePatchSet
 * @typedef {import('./types').GitDiffTreeData} GitDiffTreeData
 * @typedef {import('./types').GIT_DIFF_TREE_TYPES} GIT_DIFF_TREE_TYPES
 */

const GIT_DIFF_TREE_TYPES = {
  RAW_DATA: 'raw',
  PATCH_DATA: 'patch',
  FILE_STATS: 'stats',
  NO_SHOW: 'noshow'
}

const PATCH_SET_TYPES = {
  ADD: 'A',
  MODIFY: 'M',
  DELETE: 'D',
  RENAME: 'R'
}
/**
 * @param {string} basedir -
 * @param {string} rev -
 * @returns {Promise<Array<SentryReleasePatchSet>>} -
 * @example
 * const patchSet = await getCommitPatchSet('abcdef')
 */
const getCommitPatchSet = (basedir, rev = 'HEAD') => {
  return new Promise((resolve, reject) => {
    /** @type {Array<SentryReleasePatchSet>} */
    const patchSet = []
    const repoPath = path.join(basedir, '.git')
    gitDiffTree(repoPath, { rev })
      .on(
        'data',
        // eslint-disable-next-line valid-jsdoc, jsdoc/require-example
        /**
         * @param {GIT_DIFF_TREE_TYPES} type -
         * @param {GitDiffTreeData} data -
         */
        (type, data) => {
          if (
            type === GIT_DIFF_TREE_TYPES.RAW_DATA &&
            data.status !== PATCH_SET_TYPES.RENAME
          ) {
            patchSet.push({ path: data.toFile, type: data.status })
          }
        }
      )
      .on('error', err => reject(err))
      .on('end', () => {
        resolve(patchSet)
      })
  })
}

module.exports = getCommitPatchSet
