|
| 1 | +const fs = require('mz/fs'); |
| 2 | +const path = require('path'); |
| 3 | +const exec = require('mz/child_process').exec; |
| 4 | +const run = require('./run'); |
| 5 | +const debug = require("debug")('lib:countAuthors'); |
| 6 | + |
| 7 | +const countFileAuthorStats = require('./countFileAuthorStats'); |
| 8 | +const fetchGithubUserByQuery = require('./fetchGithubUserByQuery'); |
| 9 | +const githubEmailMap = require('../githubEmailMap.json'); |
| 10 | + |
| 11 | +async function countAuthors(repoPath, contributorsCache) { |
| 12 | + |
| 13 | + if (contributorsCache.commit) { |
| 14 | + let diff = await run(`git diff --name-only ${contributorsCache.commit} HEAD`, { |
| 15 | + cwd: repoPath |
| 16 | + }); |
| 17 | + let changedFiles = diff.trim().split('\n'); |
| 18 | + for(let file of changedFiles) { |
| 19 | + delete contributorsCache.files[file]; |
| 20 | + } |
| 21 | + } else { |
| 22 | + contributorsCache.files = Object.create(null); |
| 23 | + } |
| 24 | + |
| 25 | + |
| 26 | + let stdout = await run('git grep -Il ""', { // ls-files returns binary files, this doesn't |
| 27 | + cwd: repoPath |
| 28 | + }); |
| 29 | + |
| 30 | + let files = stdout.split(/\n/).map(l => l.trim()).filter(Boolean); |
| 31 | + |
| 32 | + for (let i = 0; i < files.length; i++) { |
| 33 | + let file = files[i]; |
| 34 | + |
| 35 | + if (file[0] === '.' || file.includes('/.')) { |
| 36 | + delete contributorsCache.files[file]; |
| 37 | + continue; // system file, ignore it |
| 38 | + } |
| 39 | + |
| 40 | + if (file.includes('.view')) { |
| 41 | + // ignore big test files from view |
| 42 | + let fileStat = await fs.stat(path.resolve(repoPath, file)); |
| 43 | + if (fileStat.size > 60e3) { |
| 44 | + delete contributorsCache.files[file]; |
| 45 | + continue; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + debug(file, i, files.length); |
| 50 | + |
| 51 | + if (!contributorsCache.files[file]) { |
| 52 | + contributorsCache.files[file] = await countFileAuthorStats(repoPath, file); |
| 53 | + } |
| 54 | + |
| 55 | + } |
| 56 | + |
| 57 | + debug("stats", contributorsCache.files); |
| 58 | + |
| 59 | + contributorsCache.commit = (await run(`git rev-parse --short HEAD`, { |
| 60 | + cwd: repoPath |
| 61 | + })).trim(); // output ends with \n |
| 62 | + |
| 63 | + let statsByAuthor = Object.create(null); |
| 64 | + |
| 65 | + for(let file in contributorsCache.files) { |
| 66 | + let fileStats = contributorsCache.files[file]; |
| 67 | + for (let author in fileStats) { |
| 68 | + statsByAuthor[author] = (statsByAuthor[author] || 0) + fileStats[author]; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + debug("stats by author", statsByAuthor); |
| 73 | + |
| 74 | + // build email cache |
| 75 | + |
| 76 | + let emailToGithubUser = contributorsCache.emailToGithubUser || Object.create(null); |
| 77 | + |
| 78 | + for (let githubEmail in statsByAuthor) { |
| 79 | + let linesCount = statsByAuthor[githubEmail]; |
| 80 | + |
| 81 | + if (linesCount < 10) { |
| 82 | + // skip contributors who have less than 10 lines |
| 83 | + continue; |
| 84 | + } |
| 85 | + |
| 86 | + let githubUser = emailToGithubUser[githubEmail]; |
| 87 | + |
| 88 | + |
| 89 | + // console.log("GITHUBUSER", githubUser); |
| 90 | + // console.log("EMAILTOGITHUBUSER", emailToGithubUser); |
| 91 | + if (!(githubEmail in emailToGithubUser)) { // may be empty |
| 92 | + |
| 93 | + if (githubEmailMap[githubEmail]) { |
| 94 | + let login = githubEmailMap[githubEmail]; |
| 95 | + githubUser = await fetchGithubUserByQuery(`${login} in:login`); |
| 96 | + } else if (githubEmail.endsWith('@users.noreply.github.com')) { |
| 97 | + let login = githubEmail.slice(0, -'@users.noreply.github.com'.length); |
| 98 | + githubUser = await fetchGithubUserByQuery(`${login} in:login`); |
| 99 | + } else { |
| 100 | + githubUser = await fetchGithubUserByQuery(`${githubEmail} in:email`); |
| 101 | + } |
| 102 | + |
| 103 | + // couldn't find github user =( |
| 104 | + if (!githubUser) { |
| 105 | + debug("Error: No github user for " + githubEmail); |
| 106 | + } |
| 107 | + |
| 108 | + emailToGithubUser[githubEmail] = githubUser || null; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + contributorsCache.emailToGithubUser = emailToGithubUser; |
| 113 | + |
| 114 | + debug(contributorsCache); |
| 115 | + |
| 116 | +} |
| 117 | + |
| 118 | +module.exports = countAuthors; |
| 119 | + |
| 120 | +//countAuthors('/js/javascript-tutorial-zh', {}); |
0 commit comments