forked from notwaldorf/tiny-care-terminal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitbot.js
110 lines (104 loc) · 3.17 KB
/
gitbot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const resolve = require('resolve-dir');
const subdirs = require('subdirs');
const isGit = require('is-git');
const gitlog = require('gitlog');
const path = require('path');
const async = require("async");
const git = require('git-utils');
try {
const gitUsername = require('git-user-name')();
} catch(err) {
console.error(`ERROR reading git-config.
Use e.g. 'git config --global user.name "Mona Lisa"'.
See 'man git config' for further information.
`);
return process.exit(0);
}
/**
* Go through all `repos` and look for subdirectories up to a given `depth`
* and look for repositories.
* Calls `callback` with array of repositories.
*/
function findGitRepos(repos, depth, callback) {
let allRepos = [];
async.each(repos, (repo, repoDone) => {
repo = resolve(repo);
subdirs(repo, depth, (err, dirs) => {
if (err) {
switch (err.code) {
case 'ENOENT':
return callback(`Could not open directory directory: ${err.path}\n`, null);
case 'EACCES':
return; //ignore if no access
default:
return callback(`Error "${err.code}" doing "${err.syscall}" on directory: ${err.path}\n`, null);
}
}
if (dirs) dirs.push(repo);
async.each(dirs, (dir, dirDone) => {
isGit(dir, (err, isGit) => {
if (err) {
return callback(err, null);
}
if (!dir.includes('.git') && isGit) {
allRepos.push(dir);
}
dirDone();
});
}, repoDone);
});
}, err => {
callback(err, allRepos.sort().reverse());
});
}
/**
* returns all commits of the last given `days`.
* Calls `callback` with line-seperated-strings of the formatted commits.
*/
function getCommitsFromRepos(repos, days, callback) {
let cmts = [];
async.each(repos, (repo, repoDone) => {
let localGitUsername = '';
try {
const gitUtilsRepo = git.open(repo);
localGitUsername = gitUtilsRepo.getConfigValue('user.name') || gitUsername;
} catch (err) {
localGitUsername = gitUsername;
}
try {
gitlog({
repo: repo,
all: true,
number: 100, //max commit count
since: `${days} days ago`,
fields: ['abbrevHash', 'subject', 'authorDateRel', 'authorName'],
author: localGitUsername
}, (err, logs) => {
// Error
if (err) {
callback(`Oh noes😱\nThe repo ${repo} has failed:\n${err}`, null);
}
// Find user commits
let commits = [];
logs.forEach(c => {
// filter simple merge commits
if (c.status && c.status.length)
commits.push(`${c.abbrevHash} - ${c.subject} (${c.authorDateRel}) <${c.authorName.replace('@end@\n','')}>`);
});
// Add repo name and commits
if (commits.length >= 1) {
// Repo name
cmts.push(repo);
cmts.push(...commits);
}
repoDone();
});
} catch(err) {
callback(err, null);
}
}, err => {
callback(err, cmts.length > 0 ? cmts.join('\n') : "Nothing yet. Start small!");
});
}
module.exports.findGitRepos = findGitRepos;
module.exports.getCommitsFromRepos = getCommitsFromRepos;