<commit_before>import git from 'simple-git';

import * as env from './utils/env';


/**
 * Clone Repository
 *
 * @param {string} repoPath repository path
 * @param {string} cloneDir local path for clone
 * @return {Promise}
 */
export function cloneRepo(repoPath, cloneDir) {
  return new Promise((resolve, reject) => {
    git().clone(repoPath, cloneDir, err => {
      if (err) {
        reject(err);
      } else {
        resolve({repo: git(cloneDir)});
      }
    });
  });
}


/**
 * Get Repository name from remote path
 *
 * @param {string} repoPath repository path
 * @return {string}
 */
export function getRepoNameFromRemotePath(repoPath) {
  return repoPath.split('/').slice(-1)[0].split('.git')[0];
}


/**
 * Get Repository
 *
 * @param {string} repoPath repository path
 * @param {string} cloneDir local path for clone
 * @return {Promise}
 */
export function getRepo(repoPath, {cloneDir} = {}) {
  if (/^(?:(?:https?|ftps?|ssh|git|rsync):\/\/)/.test(repoPath)) {
    return cloneRepo(repoPath, cloneDir || getRepoNameFromRemotePath(repoPath));
  }
  return new Promise(resolve => {
    resolve({repo: git(repoPath)});
  });
}


/**
 * Get Diff Strings
 *
 * @param {Object} repo repository object of simple-git
 * @param {String[]} opts diff options for Git command
 * @return {Promise}
 */
export function getDiff(repo, ...opts) {
  return new Promise((resolve, reject) => {
    if (opts.length === 0) {
      opts.push('HEAD~1');
    }
    repo.diff(opts, (err, diff) => {
      if (err) {
        reject(err);
      } else {
        resolve(diff);
      }
    });
  });
}


/**
 * Get Diff Strings for Pages
 *
 * @param {Object} repo repository object of simple-git
 * @param {Object[]} pages pages
 * @return {Promise}
 */
export function getDiffPages(repo, ...pages) {
  return Promise.all(pages.map(page => {
    return getDiff(repo, `${page.sha}~1`).then(diff => ({page, diff}));
  }));
}


/**
 * Get remote repository path from payload object
 *
 * @param {Object} [payload] GitHub Webhook payload
 * @return {string}
 */
export function getRepoPathFromPayload(payload) {
  const repo = payload.repository;

  /**
   * @param {string} url repository path
   * @return {string}
   */
  function replace(url) {
    return url.replace(/\.git$/, '.wiki.git');
  }

  if (!repo.private) {
    return replace(repo.clone_url);
  }
  const token = env.get('GITHUB_TOKEN');
  if (token) {
    return replace(`https://${token}:@${repo.clone_url.split('https://')[0]}`);
  }
  return replace(repo.ssh_url);
}
<commit_msg>fix(repository): Fix diff of specific commit<commit_after>import git from 'simple-git';

import * as env from './utils/env';


/**
 * Clone Repository
 *
 * @param {string} repoPath repository path
 * @param {string} cloneDir local path for clone
 * @return {Promise}
 */
export function cloneRepo(repoPath, cloneDir) {
  return new Promise((resolve, reject) => {
    git().clone(repoPath, cloneDir, err => {
      if (err) {
        reject(err);
      } else {
        resolve({repo: git(cloneDir)});
      }
    });
  });
}


/**
 * Get Repository name from remote path
 *
 * @param {string} repoPath repository path
 * @return {string}
 */
export function getRepoNameFromRemotePath(repoPath) {
  return repoPath.split('/').slice(-1)[0].split('.git')[0];
}


/**
 * Get Repository
 *
 * @param {string} repoPath repository path
 * @param {string} cloneDir local path for clone
 * @return {Promise}
 */
export function getRepo(repoPath, {cloneDir} = {}) {
  if (/^(?:(?:https?|ftps?|ssh|git|rsync):\/\/)/.test(repoPath)) {
    return cloneRepo(repoPath, cloneDir || getRepoNameFromRemotePath(repoPath));
  }
  return new Promise(resolve => {
    resolve({repo: git(repoPath)});
  });
}


/**
 * Get Diff Strings
 *
 * @param {Object} repo repository object of simple-git
 * @param {String[]} opts diff options for Git command
 * @return {Promise}
 */
export function getDiff(repo, ...opts) {
  return new Promise((resolve, reject) => {
    repo.show(opts, (err, diff) => {
      if (err) {
        reject(err);
      } else {
        resolve(diff);
      }
    });
  });
}


/**
 * Get Diff Strings for Pages
 *
 * @param {Object} repo repository object of simple-git
 * @param {Object[]} pages pages
 * @return {Promise}
 */
export function getDiffPages(repo, ...pages) {
  return Promise.all(pages.map(page => {
    return getDiff(repo, `${page.sha}`, '--format=%N').then(diff => ({page, diff}));
  }));
}


/**
 * Get remote repository path from payload object
 *
 * @param {Object} [payload] GitHub Webhook payload
 * @return {string}
 */
export function getRepoPathFromPayload(payload) {
  const repo = payload.repository;

  /**
   * @param {string} url repository path
   * @return {string}
   */
  function replace(url) {
    return url.replace(/\.git$/, '.wiki.git');
  }

  if (!repo.private) {
    return replace(repo.clone_url);
  }
  const token = env.get('GITHUB_TOKEN');
  if (token) {
    return replace(`https://${token}:@${repo.clone_url.split('https://')[0]}`);
  }
  return replace(repo.ssh_url);
}

{'fraction_non_alphanumeric': 0.17516805061289048, 'fraction_numerical': 0.0021747726374060894, 'mean_word_length': 3.3165529010238908, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 4, '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': '7441317', 'n_tokens_mistral': 1651, 'n_tokens_neox': 1646, 'n_words': 414}