From e159030764d28ad4c1e59f772cbc108227bec8f7 Mon Sep 17 00:00:00 2001 From: Alexander Wunschik <alex@wunschik.net> Date: Tue, 18 Apr 2017 23:11:56 +0200 Subject: [PATCH 01/17] replaced git-standup with gitlog --- care.js | 30 ++++++++++++++++++++++++------ config.js | 2 +- package.json | 2 ++ standup-helper.sh | 36 ------------------------------------ 4 files changed, 27 insertions(+), 43 deletions(-) delete mode 100644 standup-helper.sh diff --git a/care.js b/care.js index f5af954..ca99777 100755 --- a/care.js +++ b/care.js @@ -8,6 +8,9 @@ var contrib = require('blessed-contrib'); var chalk = require('chalk'); var parrotSay = require('parrotsay-api'); var weather = require('weather-js'); +var gitUsername = require('git-user-name')(); +var gitlog = require('gitlog'); +var async = require("async"); var screen = blessed.screen( {fullUnicode: true, // emoji or bust @@ -102,23 +105,38 @@ function doTheCodes() { var todayCommits = 0; var weekCommits = 0; - var today = spawn('sh ' + __dirname + '/standup-helper.sh', [config.repos], {shell:true}); - todayBox.content = ''; - today.stdout.on('data', data => { + getGitCommits(config.repos, 1, data => { todayCommits = getCommits(`${data}`, todayBox); updateCommitsGraph(todayCommits, weekCommits); screen.render(); }); - var week = spawn('sh ' + __dirname + '/standup-helper.sh', ['-d 7', config.repos], {shell:true}); - weekBox.content = ''; - week.stdout.on('data', data => { + getGitCommits(config.repos, 7, data => { weekCommits = getCommits(`${data}`, weekBox); updateCommitsGraph(todayCommits, weekCommits); screen.render(); }); } +function getGitCommits(repos, days, callback) { + var cmts = []; + async.each(repos, (repo, done) => { + gitlog({ + repo: repo, + since: `${days} days ago`, + fields: ['abbrevHash', 'subject', 'authorDateRel', 'authorName'], + author: gitUsername + }, (err, logs) => { + logs.forEach(c => { + cmts.push(`${c.abbrevHash} - ${c.subject} (${c.authorDateRel}) <${c.authorName}>`); + }); + done(); + }); + }, err => { + callback(cmts.join('\n')); + }); +} + function makeBox(label) { return { label: label, diff --git a/config.js b/config.js index 6cbfb42..ed2d6e1 100644 --- a/config.js +++ b/config.js @@ -11,7 +11,7 @@ config.apiKeys = config.apiKeys === 'true' ? true : false; // Directories in which to run git-standup on for a list of your recent commits. config.repos = process.env.TTC_REPOS || '~/Code'; -config.repos = config.repos.split(',').join(' '); +config.repos = config.repos.split(','); // Where to check the weather for. This can be a zip code or a location name // So both 90210 and "San Francisco, CA" should be ok. diff --git a/package.json b/package.json index 296d87f..e56044b 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,8 @@ "blessed-contrib": "^4.7.5", "chalk": "^1.1.3", "git-standup": "^2.1.8", + "git-user-name": "^1.2.0", + "gitlog": "^2.4.0", "parrotsay-api": "^0.1.1", "scraperjs": "^1.2.0", "twit": "^2.2.5", diff --git a/standup-helper.sh b/standup-helper.sh deleted file mode 100644 index 0a68457..0000000 --- a/standup-helper.sh +++ /dev/null @@ -1,36 +0,0 @@ -#!/usr/bin/env bash -set -e - -# I couldn't figure out how to cd into a different working directory -# and run git-standup there directly from node (cwd did nothing), so -# here we are. - -progname=$0 - -# I love 2 shell. -DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -standup=$DIR"/node_modules/git-standup/git-standup" - -function usage () { - echo "Usage: " - echo " $progname [-d days] [-h] repo1 repo2 etc." - echo " -d \t - Specify the number of days back to include" - echo " -d \t - Display this help screen" -} - -# get the optional days -days=1 -while getopts "d:h" opt; do - case $opt in - d ) days=$OPTARG;; - h ) usage ;; - \?) usage ;; - esac -done -shift $(($OPTIND - 1)) - -# the repo names -for dir in "$@" -do - (cd $dir; $standup -d $days) -done From a2f32502ce4b68c8d9e8be32f01e81fcc348a99b Mon Sep 17 00:00:00 2001 From: Alexander Wunschik <mail@wunschik.it> Date: Wed, 19 Apr 2017 16:17:59 +0200 Subject: [PATCH 02/17] removed git-standup dependencie --- package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package.json b/package.json index e56044b..e43b939 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,6 @@ "blessed": "^0.1.81", "blessed-contrib": "^4.7.5", "chalk": "^1.1.3", - "git-standup": "^2.1.8", "git-user-name": "^1.2.0", "gitlog": "^2.4.0", "parrotsay-api": "^0.1.1", From 83da29264fd6d3fea290159986e2bbf7a4a8c9a3 Mon Sep 17 00:00:00 2001 From: Alexander Wunschik <mail@wunschik.it> Date: Wed, 19 Apr 2017 16:21:15 +0200 Subject: [PATCH 03/17] added myself as contributor ;-) --- package.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/package.json b/package.json index e43b939..1dc49f0 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,9 @@ "description": "A terminal that tries to take care of you 💖", "main": "care.js", "author": "Monica Dinculescu <noms@google.com>", + "contributors": [ + "Alexander Wunschik <mail@wunschik.net> (http://wunschik.it)" + ], "license": "MIT", "bin": { "tiny-care-terminal": "care.js" From 8cff4ac3b875bf48759c48167eee207d848da00b Mon Sep 17 00:00:00 2001 From: Alexander Wunschik <alex@wunschik.net> Date: Wed, 19 Apr 2017 19:44:06 +0200 Subject: [PATCH 04/17] reverted: added myself as contributor ;-) --- package.json | 3 --- 1 file changed, 3 deletions(-) diff --git a/package.json b/package.json index 1dc49f0..e43b939 100644 --- a/package.json +++ b/package.json @@ -4,9 +4,6 @@ "description": "A terminal that tries to take care of you 💖", "main": "care.js", "author": "Monica Dinculescu <noms@google.com>", - "contributors": [ - "Alexander Wunschik <mail@wunschik.net> (http://wunschik.it)" - ], "license": "MIT", "bin": { "tiny-care-terminal": "care.js" From ed9f20e619a2f89ea37da31819eee0a09c9d0ac0 Mon Sep 17 00:00:00 2001 From: Alexander Wunschik <alex@wunschik.net> Date: Wed, 19 Apr 2017 21:21:51 +0200 Subject: [PATCH 05/17] also get gitlogs also from subdirs up to a given depth --- care.js | 57 ++++++++++++++++++++++++++++++++++++++++++---------- config.js | 3 +++ package.json | 2 ++ sample.env | 3 +++ 4 files changed, 54 insertions(+), 11 deletions(-) diff --git a/care.js b/care.js index ca99777..4ef4ce4 100755 --- a/care.js +++ b/care.js @@ -9,6 +9,8 @@ var chalk = require('chalk'); var parrotSay = require('parrotsay-api'); var weather = require('weather-js'); var gitUsername = require('git-user-name')(); +var subdirs = require('subdirs'); +var isGit = require('is-git'); var gitlog = require('gitlog'); var async = require("async"); @@ -105,22 +107,55 @@ function doTheCodes() { var todayCommits = 0; var weekCommits = 0; - getGitCommits(config.repos, 1, data => { - todayCommits = getCommits(`${data}`, todayBox); - updateCommitsGraph(todayCommits, weekCommits); - screen.render(); + getGitRepos(config.repos, config.reposDepth, allRepos => { + getGitCommits(allRepos, 1, data => { + todayCommits = getCommits(`${data}`, todayBox); + updateCommitsGraph(todayCommits, weekCommits); + screen.render(); + }); + + getGitCommits(allRepos, 7, data => { + weekCommits = getCommits(`${data}`, weekBox); + updateCommitsGraph(todayCommits, weekCommits); + screen.render(); + }); }); +} - getGitCommits(config.repos, 7, data => { - weekCommits = getCommits(`${data}`, weekBox); - updateCommitsGraph(todayCommits, weekCommits); - screen.render(); +/** + * Go through all `repos` and look for subdirectories up to a given `depth` + * and look for repositories. + * Calls `callback` with array of repositories. + */ +function getGitRepos(repos, depth, callback) { + var allRepos = []; + async.each(repos, (repo, repoDone) => { + subdirs(repo, depth, (err, dirs) => { + if (err) return callback([ + `😥 Error "${err.code}" doing "${err.syscall}" on directory: ${err.path}` + ]); + if (dirs) dirs.push(repo); + async.each(dirs, (dir, dirDone) => { + isGit(dir, (err, isGit) => { + if (!dir.includes('.git') && isGit) { + allRepos.push(dir); + } + dirDone(); + }); + }, repoDone); + }); + }, err => { + callback(allRepos.sort()); }); } +/** + * returns all commits of the last given `days`. + * Calls `callback` with line-seperated-strings of the formatted commits. + */ function getGitCommits(repos, days, callback) { var cmts = []; - async.each(repos, (repo, done) => { + async.each(repos, (repo, repoDone) => { gitlog({ repo: repo, since: `${days} days ago`, @@ -130,10 +165,10 @@ function getGitCommits(repos, days, callback) { logs.forEach(c => { cmts.push(`${c.abbrevHash} - ${c.subject} (${c.authorDateRel}) <${c.authorName}>`); }); - done(); + repoDone(); }); }, err => { - callback(cmts.join('\n')); + callback(cmts.length > 0 ? cmts.join('\n') : "Nothing yet. Start small!"); }); } diff --git a/config.js b/config.js index ed2d6e1..55b8df2 100644 --- a/config.js +++ b/config.js @@ -13,6 +13,9 @@ config.apiKeys = config.apiKeys === 'true' ? true : false; config.repos = process.env.TTC_REPOS || '~/Code'; config.repos = config.repos.split(','); +// The directory-depth on how to look for git-repos. Use with care! +config.reposDepth = process.env.TTC_REPOS_DEPTH || 0; + // Where to check the weather for. This can be a zip code or a location name // So both 90210 and "San Francisco, CA" should be ok. // It's using weather.service.msn.com behind the curtains. diff --git a/package.json b/package.json index e43b939..8866cce 100644 --- a/package.json +++ b/package.json @@ -15,8 +15,10 @@ "chalk": "^1.1.3", "git-user-name": "^1.2.0", "gitlog": "^2.4.0", + "is-git": "0.0.1", "parrotsay-api": "^0.1.1", "scraperjs": "^1.2.0", + "subdirs": "github:mojoaxel/subdirs", "twit": "^2.2.5", "weather-js": "^2.0.0" }, diff --git a/sample.env b/sample.env index cce8cd8..3897467 100644 --- a/sample.env +++ b/sample.env @@ -5,6 +5,9 @@ export TTC_BOTS='tinycarebot,selfcare_bot,magicrealismbot' # List of folders to look into for `git` commits, comma separated. export TTC_REPOS='~/Work/Code,~/Fun/Code' +// The directory-depth on how to look for git-repos. Use with care! +export TTC_REPOS_DEPTH=2 + # Location/zip code to check the weather for. Both 90210 and "San Francisco, CA" # should be ok. It's using weather.service.msn.com behind the curtains. export TTC_WEATHER='Paris' From 2146858532079780b47c60c923476b0aee531695 Mon Sep 17 00:00:00 2001 From: Alexander Wunschik <alex@wunschik.net> Date: Thu, 20 Apr 2017 10:01:27 +0200 Subject: [PATCH 06/17] updated README --- README.md | 4 +--- care.js | 2 +- config.js | 2 +- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 3436584..0bd1611 100644 --- a/README.md +++ b/README.md @@ -26,9 +26,7 @@ lists the environment variables that you can copy in your `rc` files: - `TTC_BOTS` are the 3 twitter bots to check, comma separated. The first entry in this list will be displayed in the party parrot. - `TTC_REPOS`, a comma separated list of repos to look at for `git` commits. - This is using [`git-standup`](https://github.com/kamranahmedse/git-standup) under - the hood, and looks one subdirectory deep (so if you have all your code - directories in a `~/Code`, you only need to list that one) + - `TTC_REPOS_DEPTH` is the max directory-depth to look for git repositories in the directories defined with `TTC_REPOS` (by default 1) - `TTC_WEATHER`, the location or zip code to check the weather for (so both `90210` and `Paris` should work) - `TTC_CELSIUS` (by default true) diff --git a/care.js b/care.js index 4ef4ce4..0674721 100755 --- a/care.js +++ b/care.js @@ -107,7 +107,7 @@ function doTheCodes() { var todayCommits = 0; var weekCommits = 0; - getGitRepos(config.repos, config.reposDepth, allRepos => { + getGitRepos(config.repos, config.reposDepth-1, allRepos => { getGitCommits(allRepos, 1, data => { todayCommits = getCommits(`${data}`, todayBox); updateCommitsGraph(todayCommits, weekCommits); diff --git a/config.js b/config.js index 55b8df2..99e0495 100644 --- a/config.js +++ b/config.js @@ -14,7 +14,7 @@ config.repos = process.env.TTC_REPOS || '~/Code'; config.repos = config.repos.split(','); // The directory-depth on how to look for git-repos. Use with care! -config.reposDepth = process.env.TTC_REPOS_DEPTH || 0; +config.reposDepth = process.env.TTC_REPOS_DEPTH || 1; // Where to check the weather for. This can be a zip code or a location name // So both 90210 and "San Francisco, CA" should be ok. From cf3ecc857ce0d4cd80c6e01e08a61a1638771338 Mon Sep 17 00:00:00 2001 From: Alexander Wunschik <mail@wunschik.it> Date: Fri, 21 Apr 2017 09:38:21 +0200 Subject: [PATCH 07/17] updated subdirs release --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 19546a5..f3562ea 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "is-git": "0.0.1", "parrotsay-api": "^0.1.1", "scraperjs": "^1.2.0", - "subdirs": "github:mojoaxel/subdirs", + "subdirs": "^1.0.1", "twit": "^2.2.5", "weather-js": "^2.0.0" }, From f38f9fd5cad78576fc1cb2cc0b68990ba813e88a Mon Sep 17 00:00:00 2001 From: Alexander Wunschik <mail@wunschik.it> Date: Fri, 21 Apr 2017 09:38:38 +0200 Subject: [PATCH 08/17] added error handling --- care.js | 52 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/care.js b/care.js index 0674721..06918f1 100755 --- a/care.js +++ b/care.js @@ -106,15 +106,24 @@ function doTheTweets() { function doTheCodes() { var todayCommits = 0; var weekCommits = 0; + todayBox.content = ''; + weekBox.content = ''; - getGitRepos(config.repos, config.reposDepth-1, allRepos => { - getGitCommits(allRepos, 1, data => { + var showError = function(err) { + getCommits("ERROR: "+err, todayBox); + screen.render(); + } + + getGitRepos(config.repos, config.reposDepth-1, (err, allRepos) => { + if (err) return showError(err); + getGitCommits(allRepos, 1, (err, data) => { + if (err) return showError(err); todayCommits = getCommits(`${data}`, todayBox); updateCommitsGraph(todayCommits, weekCommits); screen.render(); }); - - getGitCommits(allRepos, 7, data => { + getGitCommits(allRepos, 7, (err, data) => { + if (err) return showError(err); weekCommits = getCommits(`${data}`, weekBox); updateCommitsGraph(todayCommits, weekCommits); screen.render(); @@ -131,12 +140,12 @@ function getGitRepos(repos, depth, callback) { var allRepos = []; async.each(repos, (repo, repoDone) => { subdirs(repo, depth, (err, dirs) => { - if (err) return callback([ - `😥 Error "${err.code}" doing "${err.syscall}" on directory: ${err.path}` - ]); + if (err) return callback( + `😥 Error "${err.code}" doing "${err.syscall}" on directory: ${err.path}`, null); if (dirs) dirs.push(repo); async.each(dirs, (dir, dirDone) => { isGit(dir, (err, isGit) => { + if (err) callback(err, null); if (!dir.includes('.git') && isGit) { allRepos.push(dir); } @@ -145,7 +154,7 @@ function getGitRepos(repos, depth, callback) { }, repoDone); }); }, err => { - callback(allRepos.sort()); + callback(err, allRepos.sort()); }); } @@ -156,19 +165,24 @@ function getGitRepos(repos, depth, callback) { function getGitCommits(repos, days, callback) { var cmts = []; async.each(repos, (repo, repoDone) => { - gitlog({ - repo: repo, - since: `${days} days ago`, - fields: ['abbrevHash', 'subject', 'authorDateRel', 'authorName'], - author: gitUsername - }, (err, logs) => { - logs.forEach(c => { - cmts.push(`${c.abbrevHash} - ${c.subject} (${c.authorDateRel}) <${c.authorName}>`); + try { + gitlog({ + repo: repo, + since: `${days} days ago`, + fields: ['abbrevHash', 'subject', 'authorDateRel', 'authorName'], + author: gitUsername + }, (err, logs) => { + if (err) callback(err, null); + logs.forEach(c => { + cmts.push(`${c.abbrevHash} - ${c.subject} (${c.authorDateRel}) <${c.authorName}>`); + }); + repoDone(); }); - repoDone(); - }); + } catch(err) { + callback(err, null); + } }, err => { - callback(cmts.length > 0 ? cmts.join('\n') : "Nothing yet. Start small!"); + callback(err, cmts.length > 0 ? cmts.join('\n') : "Nothing yet. Start small!"); }); } From 878fea1b8ae9b30262ae7ed473a3cb58c35c214d Mon Sep 17 00:00:00 2001 From: Alexander Wunschik <alex@wunschik.net> Date: Wed, 26 Apr 2017 21:46:04 +0200 Subject: [PATCH 09/17] seperated gitbot into own module; fixed resolution of relative paths; improved error handling; --- care.js | 93 ++++++++++------------------------------------------ gitbot.js | 74 +++++++++++++++++++++++++++++++++++++++++ package.json | 1 + 3 files changed, 93 insertions(+), 75 deletions(-) create mode 100644 gitbot.js diff --git a/care.js b/care.js index 2feb33f..7790ccc 100755 --- a/care.js +++ b/care.js @@ -1,18 +1,16 @@ #!/usr/bin/env node var config = require(__dirname + '/config.js'); var twitterbot = require(__dirname + '/twitterbot.js'); +var gitbot = require(__dirname + '/gitbot.js'); -var spawn = require( 'child_process' ).spawn; var blessed = require('blessed'); var contrib = require('blessed-contrib'); var chalk = require('chalk'); var parrotSay = require('parrotsay-api'); var weather = require('weather-js'); -var gitUsername = require('git-user-name')(); -var subdirs = require('subdirs'); -var isGit = require('is-git'); -var gitlog = require('gitlog'); -var async = require("async"); + +console.dir(gitbot); +debugger; var screen = blessed.screen( {fullUnicode: true, // emoji or bust @@ -111,21 +109,28 @@ function doTheCodes() { todayBox.content = ''; weekBox.content = ''; - var showError = function(err) { - getCommits("ERROR: "+err, todayBox); + function getCommits(data, box) { + var commitRegex = /(.......) (- .*)/g; + var content = colorizeLog(data); + box.content += content; + return (box.content.match(commitRegex) || []).length; + } + + function showError(err, box) { + getCommits(`😥 ${err}`, box); screen.render(); } - getGitRepos(config.repos, config.reposDepth-1, (err, allRepos) => { + gitbot.findGitRepos(config.repos, config.reposDepth-1, (err, allRepos) => { if (err) return showError(err); - getGitCommits(allRepos, 1, (err, data) => { - if (err) return showError(err); + gitbot.getCommitsFromRepos(allRepos, 1, (err, data) => { + if (err) return showError(err, todayBox); todayCommits = getCommits(`${data}`, todayBox); updateCommitsGraph(todayCommits, weekCommits); screen.render(); }); - getGitCommits(allRepos, 7, (err, data) => { - if (err) return showError(err); + gitbot.getCommitsFromRepos(allRepos, 7, (err, data) => { + if (err) return showError(err, weekBox); weekCommits = getCommits(`${data}`, weekBox); updateCommitsGraph(todayCommits, weekCommits); screen.render(); @@ -133,61 +138,6 @@ function doTheCodes() { }); } -/** - * Go through all `repos` and look for subdirectories up to a given `depth` - * and look for repositories. - * Calls `callback` with array of repositories. - */ -function getGitRepos(repos, depth, callback) { - var allRepos = []; - async.each(repos, (repo, repoDone) => { - subdirs(repo, depth, (err, dirs) => { - if (err) return callback( - `😥 Error "${err.code}" doing "${err.syscall}" on directory: ${err.path}`, null); - if (dirs) dirs.push(repo); - async.each(dirs, (dir, dirDone) => { - isGit(dir, (err, isGit) => { - if (err) callback(err, null); - if (!dir.includes('.git') && isGit) { - allRepos.push(dir); - } - dirDone(); - }); - }, repoDone); - }); - }, err => { - callback(err, allRepos.sort()); - }); -} - -/** - * returns all commits of the last given `days`. - * Calls `callback` with line-seperated-strings of the formatted commits. - */ -function getGitCommits(repos, days, callback) { - var cmts = []; - async.each(repos, (repo, repoDone) => { - try { - gitlog({ - repo: repo, - since: `${days} days ago`, - fields: ['abbrevHash', 'subject', 'authorDateRel', 'authorName'], - author: gitUsername - }, (err, logs) => { - if (err) callback(err, null); - logs.forEach(c => { - cmts.push(`${c.abbrevHash} - ${c.subject} (${c.authorDateRel}) <${c.authorName.replace('@end@\n','')}>`); - }); - repoDone(); - }); - } catch(err) { - callback(err, null); - } - }, err => { - callback(err, cmts.length > 0 ? cmts.join('\n') : "Nothing yet. Start small!"); - }); -} - function makeBox(label) { return { label: label, @@ -224,13 +174,6 @@ function makeGraphBox(label) { return options; } -var commitRegex = /(.......) (- .*)/g; -function getCommits(data, box) { - var content = colorizeLog(data); - box.content += content; - return (box.content.match(commitRegex) || []).length; -} - function updateCommitsGraph(today, week) { commits.setData({titles: ['today', 'week'], data: [today, week]}) } diff --git a/gitbot.js b/gitbot.js new file mode 100644 index 0000000..1a1b920 --- /dev/null +++ b/gitbot.js @@ -0,0 +1,74 @@ +const gitUsername = require('git-user-name')(); +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"); + +/** + * 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) callback(err, null); + if (!dir.includes('.git') && isGit) { + allRepos.push(dir); + } + dirDone(); + }); + }, repoDone); + }); + }, err => { + callback(err, allRepos.sort()); + }); +} + +/** + * 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) => { + try { + gitlog({ + repo: repo, + since: `${days} days ago`, + fields: ['abbrevHash', 'subject', 'authorDateRel', 'authorName'], + author: gitUsername + }, (err, logs) => { + if (err) callback(err, null); + logs.forEach(c => { + cmts.push(`${c.abbrevHash} - ${c.subject} (${c.authorDateRel}) <${c.authorName.replace('@end@\n','')}>`); + }); + 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; diff --git a/package.json b/package.json index c814ce4..fc27c21 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "gitlog": "^2.4.0", "is-git": "0.0.1", "parrotsay-api": "^0.1.1", + "resolve-dir": "^1.0.0", "scraperjs": "^1.2.0", "subdirs": "^1.0.1", "twit": "^2.2.5", From df56ba551493d9110e6d69decdac20b466ad6ec9 Mon Sep 17 00:00:00 2001 From: Alexander Wunschik <alex@wunschik.net> Date: Wed, 26 Apr 2017 22:10:06 +0200 Subject: [PATCH 10/17] fixed emty commit list --- care.js | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/care.js b/care.js index 347d097..c5a97bd 100755 --- a/care.js +++ b/care.js @@ -10,9 +10,6 @@ var parrotSay = require('parrotsay-api'); var bunnySay = require('sign-bunny'); var weather = require('weather-js'); -console.dir(gitbot); -debugger; - var screen = blessed.screen( {fullUnicode: true, // emoji or bust smartCSR: true, @@ -121,11 +118,11 @@ function doTheCodes() { todayBox.content = ''; weekBox.content = ''; - function getCommits(data, box) { - var commitRegex = /(.......) (- .*)/g; - var content = colorizeLog(data); + function getCommits(box, data) { + var content = colorizeLog(data || ''); box.content += content; - return (box.content.match(commitRegex) || []).length; + var commitRegex = /(.......) (- .*)/g; + return (box && box.content) ? (box.content.match(commitRegex) || []).length : '0'; } function showError(err, box) { @@ -137,13 +134,13 @@ function doTheCodes() { if (err) return showError(err); gitbot.getCommitsFromRepos(allRepos, 1, (err, data) => { if (err) return showError(err, todayBox); - todayCommits = getCommits(`${data}`, todayBox); + todayCommits = getCommits(todayBox, `${data}`); updateCommitsGraph(todayCommits, weekCommits); screen.render(); }); gitbot.getCommitsFromRepos(allRepos, 7, (err, data) => { if (err) return showError(err, weekBox); - weekCommits = getCommits(`${data}`, weekBox); + weekCommits = getCommits(weekBox, `${data}`); updateCommitsGraph(todayCommits, weekCommits); screen.render(); }); From 6922c43cd609d4ebf129739a1d34c8dd43fe0d3b Mon Sep 17 00:00:00 2001 From: Alexander Wunschik <alex@wunschik.net> Date: Wed, 26 Apr 2017 22:20:47 +0200 Subject: [PATCH 11/17] @notwaldorf review comments --- care.js | 16 +++++++--------- config.js | 3 --- gitbot.js | 7 +++++-- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/care.js b/care.js index c5a97bd..7b26f95 100755 --- a/care.js +++ b/care.js @@ -125,21 +125,19 @@ function doTheCodes() { return (box && box.content) ? (box.content.match(commitRegex) || []).length : '0'; } - function showError(err, box) { - getCommits(`😥 ${err}`, box); - screen.render(); - } - - gitbot.findGitRepos(config.repos, config.reposDepth-1, (err, allRepos) => { - if (err) return showError(err); + gitbot.findGitRepos(config.repos, config.depth-1, (err, allRepos) => { + if (err) + return showError(err); gitbot.getCommitsFromRepos(allRepos, 1, (err, data) => { - if (err) return showError(err, todayBox); + if (err) + return todayBox.content = err; todayCommits = getCommits(todayBox, `${data}`); updateCommitsGraph(todayCommits, weekCommits); screen.render(); }); gitbot.getCommitsFromRepos(allRepos, 7, (err, data) => { - if (err) return showError(err, weekBox); + if (err) + return weekBox.content = err; weekCommits = getCommits(weekBox, `${data}`); updateCommitsGraph(todayCommits, weekCommits); screen.render(); diff --git a/config.js b/config.js index eaf6ecc..14d01f2 100644 --- a/config.js +++ b/config.js @@ -13,9 +13,6 @@ var config = { // Directories in which to run git-standup on for a list of your recent commits. repos: (process.env.TTC_REPOS || '~/Code').split(','), - // The directory-depth on how to look for git-repos. Use with care! - reposDepth: process.env.TTC_REPOS_DEPTH || 1, - // Directory-depth to look for git repositories. depth: (process.env.TTC_REPOS_DEPTH || 1), diff --git a/gitbot.js b/gitbot.js index 1a1b920..e67122c 100644 --- a/gitbot.js +++ b/gitbot.js @@ -29,7 +29,9 @@ function findGitRepos(repos, depth, callback) { if (dirs) dirs.push(repo); async.each(dirs, (dir, dirDone) => { isGit(dir, (err, isGit) => { - if (err) callback(err, null); + if (err) { + return callback(err, null); + } if (!dir.includes('.git') && isGit) { allRepos.push(dir); } @@ -56,7 +58,8 @@ function getCommitsFromRepos(repos, days, callback) { fields: ['abbrevHash', 'subject', 'authorDateRel', 'authorName'], author: gitUsername }, (err, logs) => { - if (err) callback(err, null); + if (err) + return callback(err, null); logs.forEach(c => { cmts.push(`${c.abbrevHash} - ${c.subject} (${c.authorDateRel}) <${c.authorName.replace('@end@\n','')}>`); }); From 4bc9a05cdf6194957187e7dcd23e7f1faa59d00a Mon Sep 17 00:00:00 2001 From: Alexander Wunschik <alex@wunschik.net> Date: Thu, 27 Apr 2017 08:55:40 +0200 Subject: [PATCH 12/17] added repo name to commit history --- gitbot.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/gitbot.js b/gitbot.js index e67122c..e33e728 100644 --- a/gitbot.js +++ b/gitbot.js @@ -60,6 +60,8 @@ function getCommitsFromRepos(repos, days, callback) { }, (err, logs) => { if (err) return callback(err, null); + if (logs.length >= 1) + cmts.push(`${repo}`); logs.forEach(c => { cmts.push(`${c.abbrevHash} - ${c.subject} (${c.authorDateRel}) <${c.authorName.replace('@end@\n','')}>`); }); From 1aed41b677bca322723ac89f864f4a7e9e387f42 Mon Sep 17 00:00:00 2001 From: Alexander Wunschik <alex@wunschik.net> Date: Thu, 27 Apr 2017 09:03:15 +0200 Subject: [PATCH 13/17] do not hide commits while reloading --- care.js | 4 ++-- gitbot.js | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/care.js b/care.js index 7b26f95..7f42a21 100755 --- a/care.js +++ b/care.js @@ -115,8 +115,6 @@ function doTheTweets() { function doTheCodes() { var todayCommits = 0; var weekCommits = 0; - todayBox.content = ''; - weekBox.content = ''; function getCommits(box, data) { var content = colorizeLog(data || ''); @@ -131,6 +129,7 @@ function doTheCodes() { gitbot.getCommitsFromRepos(allRepos, 1, (err, data) => { if (err) return todayBox.content = err; + todayBox.content = ''; todayCommits = getCommits(todayBox, `${data}`); updateCommitsGraph(todayCommits, weekCommits); screen.render(); @@ -138,6 +137,7 @@ function doTheCodes() { gitbot.getCommitsFromRepos(allRepos, 7, (err, data) => { if (err) return weekBox.content = err; + weekBox.content = ''; weekCommits = getCommits(weekBox, `${data}`); updateCommitsGraph(todayCommits, weekCommits); screen.render(); diff --git a/gitbot.js b/gitbot.js index e33e728..7b44192 100644 --- a/gitbot.js +++ b/gitbot.js @@ -58,10 +58,15 @@ function getCommitsFromRepos(repos, days, callback) { fields: ['abbrevHash', 'subject', 'authorDateRel', 'authorName'], author: gitUsername }, (err, logs) => { + // Error if (err) return callback(err, null); + + // Repo path if (logs.length >= 1) - cmts.push(`${repo}`); + cmts.push(repo); + + // Commit logs.forEach(c => { cmts.push(`${c.abbrevHash} - ${c.subject} (${c.authorDateRel}) <${c.authorName.replace('@end@\n','')}>`); }); From 3f3886dcc351d4d1dc9c5ca7d25b5702e0972686 Mon Sep 17 00:00:00 2001 From: Alexander Wunschik <alex@wunschik.net> Date: Thu, 27 Apr 2017 09:26:40 +0200 Subject: [PATCH 14/17] make it possible to switch between git-standup and gitlog --- care.js | 56 ++++++++++++++++++++++++++++++++++++++----------------- config.js | 3 +++ 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/care.js b/care.js index 7f42a21..209686b 100755 --- a/care.js +++ b/care.js @@ -3,6 +3,7 @@ var config = require(__dirname + '/config.js'); var twitterbot = require(__dirname + '/twitterbot.js'); var gitbot = require(__dirname + '/gitbot.js'); +var spawn = require('child_process').spawn; var blessed = require('blessed'); var contrib = require('blessed-contrib'); var chalk = require('chalk'); @@ -123,26 +124,47 @@ function doTheCodes() { return (box && box.content) ? (box.content.match(commitRegex) || []).length : '0'; } - gitbot.findGitRepos(config.repos, config.depth-1, (err, allRepos) => { - if (err) - return showError(err); - gitbot.getCommitsFromRepos(allRepos, 1, (err, data) => { - if (err) - return todayBox.content = err; - todayBox.content = ''; - todayCommits = getCommits(todayBox, `${data}`); - updateCommitsGraph(todayCommits, weekCommits); - screen.render(); + if (config.gitbot.toLowerCase() === 'gitstandup') { + todayBox.content = ''; + config.repos.forEach(repo => { + var today = spawn(`cd ${repo} && git-standup`, [`-m ${config.depth}`, '-d 1'], {shell:true}); + today.stdout.on('data', data => { + todayCommits = getCommits(todayBox, `${data}`); + updateCommitsGraph(todayCommits, weekCommits); + screen.render(); + }); + }); + weekBox.content = ''; + config.repos.forEach(repo => { + var today = spawn(`cd ${repo} && git-standup`, [`-m ${config.depth}`, '-d 7'], {shell:true}); + today.stdout.on('data', data => { + weekCommits = getCommits(weekBox, `${data}`); + updateCommitsGraph(todayCommits, weekCommits); + screen.render(); + }); }); - gitbot.getCommitsFromRepos(allRepos, 7, (err, data) => { + } else { + gitbot.findGitRepos(config.repos, config.depth-1, (err, allRepos) => { if (err) - return weekBox.content = err; - weekBox.content = ''; - weekCommits = getCommits(weekBox, `${data}`); - updateCommitsGraph(todayCommits, weekCommits); - screen.render(); + return todayBox.content = err; + gitbot.getCommitsFromRepos(allRepos, 1, (err, data) => { + if (err) + return todayBox.content = err; + todayBox.content = ''; + todayCommits = getCommits(todayBox, `${data}`); + updateCommitsGraph(todayCommits, weekCommits); + screen.render(); + }); + gitbot.getCommitsFromRepos(allRepos, 7, (err, data) => { + if (err) + return weekBox.content = err; + weekBox.content = ''; + weekCommits = getCommits(weekBox, `${data}`); + updateCommitsGraph(todayCommits, weekCommits); + screen.render(); + }); }); - }); + } } function makeBox(label) { diff --git a/config.js b/config.js index 14d01f2..7f248c1 100644 --- a/config.js +++ b/config.js @@ -16,6 +16,9 @@ var config = { // Directory-depth to look for git repositories. depth: (process.env.TTC_REPOS_DEPTH || 1), + // Which method is to be used to read the git commits ('gitstandup' | 'gitlog'). + gitbot: (process.env.TTC_GITBOT || 'gitstandup'), + // Where to check the weather for. // It's using weather.service.msn.com behind the curtains. weather: process.env.TTC_WEATHER || 'San Francisco', From 294657b6a90ec28269120d593f3867e4993fc4cd Mon Sep 17 00:00:00 2001 From: Alexander Wunschik <alex@wunschik.net> Date: Thu, 27 Apr 2017 10:15:42 +0200 Subject: [PATCH 15/17] filter pure administrative commits --- gitbot.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/gitbot.js b/gitbot.js index 7b44192..27cc879 100644 --- a/gitbot.js +++ b/gitbot.js @@ -40,7 +40,7 @@ function findGitRepos(repos, depth, callback) { }, repoDone); }); }, err => { - callback(err, allRepos.sort()); + callback(err, allRepos.sort().reverse()); }); } @@ -54,6 +54,7 @@ function getCommitsFromRepos(repos, days, callback) { try { gitlog({ repo: repo, + number: 100, //max commit count since: `${days} days ago`, fields: ['abbrevHash', 'subject', 'authorDateRel', 'authorName'], author: gitUsername @@ -68,7 +69,8 @@ function getCommitsFromRepos(repos, days, callback) { // Commit logs.forEach(c => { - cmts.push(`${c.abbrevHash} - ${c.subject} (${c.authorDateRel}) <${c.authorName.replace('@end@\n','')}>`); + if (c.status && c.status.length) + cmts.push(`${c.abbrevHash} - ${c.subject} (${c.authorDateRel}) <${c.authorName.replace('@end@\n','')}>`); }); repoDone(); }); From 4cf5e21aaada6d32787e783b65fe17f3d8af872e Mon Sep 17 00:00:00 2001 From: Alexander Wunschik <alex@wunschik.net> Date: Sun, 7 May 2017 10:20:01 +0200 Subject: [PATCH 16/17] readded git-stanup as peerDependency --- package.json | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 3f83677..24d9f46 100644 --- a/package.json +++ b/package.json @@ -13,17 +13,21 @@ "blessed": "^0.1.81", "blessed-contrib": "^4.7.5", "chalk": "^1.1.3", + "git-standup": "^2.1.8", "git-user-name": "^1.2.0", "gitlog": "^2.4.0", "is-git": "0.0.1", "parrotsay-api": "^0.1.1", "resolve-dir": "^1.0.0", - "sign-bunny": "^1.0.0", "scraperjs": "^1.2.0", + "sign-bunny": "^1.0.0", "subdirs": "^1.0.1", "twit": "^2.2.5", "weather-js": "^2.0.0" }, + "peerDependencies": { + "git-standup": "^2.1.8" + }, "scripts": { "start": "node care.js" } From 86caaa0bf71caf788f0d0143db63997d5179298c Mon Sep 17 00:00:00 2001 From: Alexander Wunschik <alex@wunschik.net> Date: Sun, 7 May 2017 10:28:19 +0200 Subject: [PATCH 17/17] use existing git-standup implementation because it just works ;-) --- care.js | 43 +++++++++++++++++++++++-------------------- standup-helper.sh | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 20 deletions(-) create mode 100644 standup-helper.sh diff --git a/care.js b/care.js index 209686b..4cadc0d 100755 --- a/care.js +++ b/care.js @@ -117,7 +117,7 @@ function doTheCodes() { var todayCommits = 0; var weekCommits = 0; - function getCommits(box, data) { + function getCommits(data, box) { var content = colorizeLog(data || ''); box.content += content; var commitRegex = /(.......) (- .*)/g; @@ -125,41 +125,44 @@ function doTheCodes() { } if (config.gitbot.toLowerCase() === 'gitstandup') { + var today = spawn('sh ' + __dirname + '/standup-helper.sh', ['-m ' + config.depth, config.repos], {shell:true}); todayBox.content = ''; - config.repos.forEach(repo => { - var today = spawn(`cd ${repo} && git-standup`, [`-m ${config.depth}`, '-d 1'], {shell:true}); - today.stdout.on('data', data => { - todayCommits = getCommits(todayBox, `${data}`); - updateCommitsGraph(todayCommits, weekCommits); - screen.render(); - }); + today.stdout.on('data', data => { + todayCommits = getCommits(`${data}`, todayBox); + updateCommitsGraph(todayCommits, weekCommits); + screen.render(); }); + + var week = spawn('sh ' + __dirname + '/standup-helper.sh', ['-m ' + config.depth + ' -d 7', config.repos], {shell:true}); weekBox.content = ''; - config.repos.forEach(repo => { - var today = spawn(`cd ${repo} && git-standup`, [`-m ${config.depth}`, '-d 7'], {shell:true}); - today.stdout.on('data', data => { - weekCommits = getCommits(weekBox, `${data}`); - updateCommitsGraph(todayCommits, weekCommits); - screen.render(); - }); + week.stdout.on('data', data => { + weekCommits = getCommits(`${data}`, weekBox); + updateCommitsGraph(todayCommits, weekCommits); + screen.render(); }); } else { gitbot.findGitRepos(config.repos, config.depth-1, (err, allRepos) => { - if (err) + if (err) { return todayBox.content = err; + screen.render(); + } gitbot.getCommitsFromRepos(allRepos, 1, (err, data) => { - if (err) + if (err) { return todayBox.content = err; + screen.render(); + } todayBox.content = ''; - todayCommits = getCommits(todayBox, `${data}`); + todayCommits = getCommits(`${data}`, todayBox); updateCommitsGraph(todayCommits, weekCommits); screen.render(); }); gitbot.getCommitsFromRepos(allRepos, 7, (err, data) => { - if (err) + if (err) { return weekBox.content = err; + screen.render(); + } weekBox.content = ''; - weekCommits = getCommits(weekBox, `${data}`); + weekCommits = getCommits(`${data}`, weekBox); updateCommitsGraph(todayCommits, weekCommits); screen.render(); }); diff --git a/standup-helper.sh b/standup-helper.sh new file mode 100644 index 0000000..edd8c36 --- /dev/null +++ b/standup-helper.sh @@ -0,0 +1,36 @@ +#!/usr/bin/env bash +set -e + +# I couldn't figure out how to cd into a different working directory +# and run git-standup there directly from node (cwd did nothing), so +# here we are. + +progname=$0 +standup=$(npm bin -g)"/git-standup" + +usage () { + echo "Usage: " + echo " $progname [-d days] [-h] repo1 repo2 etc." + echo " -d \t - Specify the number of days back to include" + echo " -m \t - Specify the depth of recursive repository search" + echo " -h \t - Display this help screen" +} + +# get the optional days +days=1 +depth=1 +while getopts "d:m:h" opt; do + case $opt in + d ) days=$OPTARG;; + m ) depth=$OPTARG;; + h ) usage ;; + \?) usage ;; + esac +done +shift $(($OPTIND - 1)) + +# the repo names +for dir in "$@" +do + (cd $dir; $standup -d $days -m $depth) +done