diff --git a/.gitignore b/.gitignore index 3c3629e..d5f19d8 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +package-lock.json diff --git a/LICENSE.md b/LICENSE.md index 43f7153..fceb2e2 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,8 +1,6 @@ -The MIT License (MIT) -===================== +# The MIT License (MIT) -Copyright (c) 2015 Rod Vagg ---------------------------- +## Copyright (c) 2015 Rod Vagg Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/README.md b/README.md index 29e5571..db10c33 100644 --- a/README.md +++ b/README.md @@ -16,16 +16,15 @@ Intended to be used on a standard log format (not a simplified one using one of Typical usage: ```js -var spawn = require('child_process').spawn - , split2 = require('split2') - , listStream = require('list-stream') +import { spawn } from 'node:child_process' +import split2 from 'split2' +import listStream from 'list-stream' -spawn('bash', [ '-c', 'git log' ]) +spawn('bash', ['-c', 'git log']) .stdout.pipe(split2()) // break up by newline characters .pipe(commitStream('rvagg', 'commit-stream')) .pipe(listStream.obj(onCommitList)) - function onCommitList (err, list) { // list is an array of objects } @@ -33,22 +32,22 @@ function onCommitList (err, list) { Commit object properties: -* `sha`: the full commit sha -* `author`: an object representing the author of the commit +- `sha`: the full commit sha +- `author`: an object representing the author of the commit - `name`: the name of the committer - `email`: the email of the committer -* `authors`: a list of such objects representing the authors of the commit, supporting multiple authors through `Co-authored-by:` -* `authorDate`: a string representing the date of the original commit by the author (never change) -* `commitDate`: a string representing the date of the last change of the commit -* `summary`: the one-line summary of the commit -* `description`: the free-form text content of the summary, minus specific metadata lines -* `reviewers`: an array containing objects with `name` and `email` properties if the commit metadata contains reviewers in a `Reviewed-By: Foo Bar ` format. -* `changes`: if `--stat` data is included in the git log, an object containing change stats: +- `authors`: a list of such objects representing the authors of the commit, supporting multiple authors through `Co-authored-by:` +- `authorDate`: a string representing the date of the original commit by the author (never change) +- `commitDate`: a string representing the date of the last change of the commit +- `summary`: the one-line summary of the commit +- `description`: the free-form text content of the summary, minus specific metadata lines +- `reviewers`: an array containing objects with `name` and `email` properties if the commit metadata contains reviewers in a `Reviewed-By: Foo Bar ` format. +- `changes`: if `--stat` data is included in the git log, an object containing change stats: - `files`: the number of files changed - `insertions`: the number of new lines inserted - `deletions`: the number of old lines removed -* `prUrl`: a URL pointing to a pull-request where this change was made if the commit metadata contains a `PR-URL: https://github.com/user/project/pull/XX` line. Note that whatever proceeds the `PR-URL: ` string will be collected in this property; one exception being that if a shortened `#XX` version is found and you have supplied `defaultGitHubUser` and `defaultGitHubProject` arguments to the constructor then a full GitHub pull-request will be reconstituted in its place. -* `ghUser`, `ghProject, `ghIssue`: if a proper GitHub pull request is found for the `prUrl` property (including shortened `#XX` ones), these properties will be added to point to the original user, project and issue (pull-request) for this change, as extracted from the URL. +- `prUrl`: a URL pointing to a pull-request where this change was made if the commit metadata contains a `PR-URL: https://github.com/user/project/pull/XX` line. Note that whatever proceeds the `PR-URL: ` string will be collected in this property; one exception being that if a shortened `#XX` version is found and you have supplied `defaultGitHubUser` and `defaultGitHubProject` arguments to the constructor then a full GitHub pull-request will be reconstituted in its place. +- `ghUser`, `ghProject`, `ghIssue`: if a proper GitHub pull request is found for the `prUrl` property (including shortened `#XX` ones), these properties will be added to point to the original user, project and issue (pull-request) for this change, as extracted from the URL. ## License diff --git a/commit-stream.js b/commit-stream.js index 62d9d87..583a64b 100644 --- a/commit-stream.js +++ b/commit-stream.js @@ -1,75 +1,91 @@ -const through2 = require('through2') - , stripAnsi = require('strip-ansi') +import through2 from 'through2' +import stripAnsi from 'strip-ansi' - -module.exports = commitStream - - -function commitStream (ghUser, ghProject) { - var commit +export default function commitStream (ghUser, ghProject) { + let commit return through2.obj(onLine, onEnd) function addLine (line) { line = stripAnsi(line) - if (!line) - return + if (line.length === 0) return - var old, m + let old, m if (/^commit \w+$/.test(line)) { old = commit - commit = { - sha: line.split(' ')[1] + commit = { sha: line.split(' ')[1] } + } else if ((m = line.match(/^CommitDate:\s+(.*)$/)) !== null) { + if (commit === undefined || commit === null) { + throw new Error('Something unexpected occurred') } - } else if (m = line.match(/^CommitDate:\s+(.*)$/)) { - if (!commit) - throw new Error('wut?') commit.commitDate = m[1].trim() - } else if (m = line.match(/^\s*(?:Author|Co[- ]?authored[- ]?by):?\s*([^<]+) <([^>]+)>\s*$/i)) { - if (!commit) - throw new Error('wut?') - if (!commit.authors) + } else if ( + (m = line.match( + /^\s*(?:Author|Co[- ]?authored[- ]?by):?\s*([^<]+) <([^>]+)>\s*$/i + )) !== null + ) { + if (commit === undefined || commit === null) { + throw new Error('Something unexpected occurred') + } + if (commit.authors === undefined || commit.authors === null) { commit.authors = [] + } commit.authors.push({ name: m[1], email: m[2] }) - } else if (m = line.match(/^AuthorDate:\s+(.*)$/)) { - if (!commit) - throw new Error('wut?') - commit.authorDate = m[1].trim() - } else if (m = line.match(/^Date:\s+(.*)$/)) { - if (!commit) - throw new Error('wut?') + } else if ((m = line.match(/^(?:AuthorDate|Date):\s+(.*)$/)) !== null) { + if (commit === undefined || commit === null) { + throw new Error('Something unexpected occurred') + } commit.authorDate = m[1].trim() - } else if (m = line.match(/^\s+Reviewed[- ]?By:?\s*([^<]+) <([^>]+)>\s*$/i)) { - if (!commit.reviewers) + } else if ( + (m = line.match(/^\s+Reviewed[- ]?By:?\s*([^<]+) <([^>]+)>\s*$/i)) !== + null + ) { + if (commit.reviewers === undefined || commit.reviewers === null) { commit.reviewers = [] + } commit.reviewers.push({ name: m[1], email: m[2] }) - } else if (m = line.match(/^\s+PR(?:[- ]?URL)?:?\s*(.+)\s*$/)) { + } else if ((m = line.match(/^\s+PR(?:[- ]?URL)?:?\s*(.+)\s*$/)) !== null) { commit.prUrl = m[1] - if (ghUser && ghProject && (m = commit.prUrl.match(/^\s*#?(\d+)\s*$/))) { - commit.prUrl = 'https://github.com/' + ghUser + '/' + ghProject + '/pull/' + m[1] + if ( + typeof ghUser === 'string' && + ghUser.length !== 0 && + typeof ghProject === 'string' && + ghProject.length !== 0 && + (m = commit.prUrl.match(/^\s*#?(\d+)\s*$/)) !== null + ) { + commit.prUrl = `https://github.com/${ghUser}/${ghProject}/pull/${m[1]}` } - if (m = commit.prUrl.match(/^(https?:\/\/.+\/([^\/]+)\/([^\/]+))\/\w+\/(\d+)(\/)?$/i)) { - commit.ghIssue = +m[4] - commit.ghUser = m[2] + if ( + (m = commit.prUrl.match( + /^(https?:\/\/.+\/([^/]+)\/([^/]+))\/\w+\/(\d+)(\/)?$/i + )) !== null + ) { + commit.ghIssue = parseInt(m[4]) + commit.ghUser = m[2] commit.ghProject = m[3] } - } else if (/^ /.test(line) && (line = line.trim()).length) { - if (!commit.summary) { + } else if (/^ {4}/.test(line) && (line = line.trim()).length !== 0) { + if (commit.summary === undefined || commit.summary === null) { commit.summary = line } else { - if (!commit.description) + if (commit.description === undefined || commit.description === null) { commit.description = [] + } commit.description.push(line) } - } else if (/^ [^ ]/.test(line) && (line = line.trim()).length) { - if (m = line.match(/^(\d+) files? changed(?:, (\d+) insertions?\(\+\))?(?:, (\d+) deletions?\(-\))?/)) { - commit.changes = { - files : Number(m[1]) - , insertions : Number(m[2] || 0) - , deletions : Number(m[3] || 0) - } + } else if ( + /^ [^ ]/.test(line) && + (line = line.trim()).length !== 0 && + (m = line.match( + /^(\d+) files? changed(?:, (\d+) insertions?\(\+\))?(?:, (\d+) deletions?\(-\))?/ + )) !== null + ) { + commit.changes = { + files: parseInt(m[1]), + insertions: parseInt(m[2] ?? 0), + deletions: parseInt(m[3] ?? 0) } } @@ -77,19 +93,24 @@ function commitStream (ghUser, ghProject) { } function onLine (line, _, callback) { - var commit = addLine(line) - if (commit && commit.authors && commit.authors.length > 0) + const commit = addLine(line) + + if (commit?.authors?.length > 0) { commit.author = commit.authors[0] - if (commit) + this.push(commit) + } + callback() } function onEnd (callback) { - if (commit && commit.authors && commit.authors.length > 0) + if (commit?.authors?.length > 0) { commit.author = commit.authors[0] - if (commit) + this.push(commit) + } + callback() } } diff --git a/package.json b/package.json index d1b31a5..c56595e 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "1.1.0", "description": "Turn a `git log` into a stream of commit objects", "main": "commit-stream.js", + "type": "module", "scripts": { "test": "node test.js" }, @@ -17,12 +18,12 @@ }, "homepage": "https://github.com/rvagg/commit-stream", "devDependencies": { - "list-stream": "~1.0.0", - "split2": "~1.0.0", - "tape": "~4.0.1" + "list-stream": "^2.1.0", + "split2": "^4.2.0", + "tape": "^5.6.3" }, "dependencies": { - "strip-ansi": "^6.0.1", - "through2": "~2.0.0" + "strip-ansi": "^7.0.1", + "through2": "^4.0.2" } } diff --git a/test.js b/test.js index be1b9bf..e84c786 100644 --- a/test.js +++ b/test.js @@ -1,127 +1,168 @@ -const through2 = require('through2') - , commitStream = require('./') - , spawn = require('child_process').spawn - , test = require('tape') - , split2 = require('split2') - , listStream = require('list-stream') - +import commitStream from './commit-stream.js' +import { spawn } from 'node:child_process' +import test from 'tape' +import split2 from 'split2' +import listStream from 'list-stream' function gitToList (gitCmd, callback) { - var child = spawn('bash', [ '-c', gitCmd ]) - child.stdout.pipe(split2()).pipe(commitStream()).pipe(listStream.obj(callback)) + const child = spawn('bash', ['-c', gitCmd]) + child.stdout + .pipe(split2()) + .pipe(commitStream()) + .pipe(listStream.obj(callback)) } - -test('current plain commit log', function (t) { - gitToList('git log', function (err, list) { +test('current plain commit log', (t) => { + gitToList('git log', (err, list) => { t.error(err, 'no error') - t.ok(list && list.length > 1, 'got a list') - - t.deepEqual(list[list.length - 9], { - author : { email: 'ralphtheninja@riseup.net', name: 'Lars-Magnus Skog' } - , authors : [ { email: 'ralphtheninja@riseup.net', name: 'Lars-Magnus Skog' } ] - , authorDate : 'Tue Feb 9 15:46:46 2016 +0100' - , description : [ - 'Fixes: https://github.com/rvagg/changelog-maker/issues/35' - ] - , ghIssue : 1 - , ghProject : 'commit-stream' - , ghUser : 'rvagg' - , prUrl : 'https://github.com/rvagg/commit-stream/pull/1' - , sha : '1bcfd072fd74399808fbcd5cf31ce5342dd6d70c' - , summary : 'process: this should not match PR-URL' - , reviewers : [ { email: 'rod@vagg.org', name: 'Rod Vagg' } ] - }, 'got correct ninth commit') - - t.deepEqual(list[list.length - 4], { - author: { email: 'rod@vagg.org', name: 'Rod Vagg' } - , authors: [ { email: 'rod@vagg.org', name: 'Rod Vagg' } ] - , authorDate: 'Fri Apr 17 11:16:51 2015 +1000' - , sha: 'f92b93c3c7175b07f847dd45058b121cea6b3a20' - , summary: 'deleted package.json line' - }, 'got correct fourth commit') - - t.deepEqual(list[list.length - 3], { - author: { email: 'rod@vagg.org', name: 'Rod Vagg' } - , authors: [ { email: 'rod@vagg.org', name: 'Rod Vagg' } ] - , authorDate: 'Fri Apr 17 11:13:06 2015 +1000' - , description: [ 'comment', 'Reviewed-By: Nobody' ] - , sha: 'db34ce2af09a6a9fb70241d43965a2bc48b90b4c' - , summary: 'squished package.json' - }, 'got correct third commit') - - t.deepEqual(list[list.length - 2], { - author : { email: 'rod@vagg.org', name: 'Rod Vagg' } - , authors : [ { email: 'rod@vagg.org', name: 'Rod Vagg' } ] - , authorDate: 'Fri Apr 17 10:52:16 2015 +1000' - , description : [ - 'Some extra summary information here' - , 'newline' - , 'Fixes: https://github.com/rvagg/commit-stream/issues/0' - ] - , ghIssue : 0 - , ghProject : 'commit-stream' - , ghUser : 'rvagg' - , prUrl : 'https://github.com/rvagg/commit-stream/pulls/0' - , reviewers : [ { email: 'roger@vagg.org', name: 'Roger Vagg' } ] - , sha : '6275758f597ae579202fbe4eccca1407f2c67ac1' - , summary : 'added test.js' - }, 'got correct second commit') - - t.deepEqual(list[list.length - 1], { - sha : 'd94841274e2979e7758413a0f48fa37560d0dde6' - , authorDate: 'Thu Apr 16 20:49:21 2015 +1000' - , author : { name: 'Rod Vagg', email: 'rod@vagg.org' } - , authors: [ { email: 'rod@vagg.org', name: 'Rod Vagg' } ] - , summary: 'make it so' - }, 'got correct first commit') - - t.deepEqual(list[list.length - 16], { - sha : list[list.length - 16].sha // unknown at time of writing :) - , authorDate: 'Tue Jun 12 23:41:35 2018 +0200' - , author : { name: 'Anna Henningsen', email: 'anna@addaleax.net' } - , authors: [ - { name: 'Anna Henningsen', email: 'anna@addaleax.net' } - , { name: 'nobody', email: 'nobody@nowhere' } - ] - , summary: 'add support for co-authored-by' - }, 'got correct co-authored-by commit') + t.ok(list?.length > 1, 'got a list') + + t.deepEqual( + list[list.length - 9], + { + author: { email: 'ralphtheninja@riseup.net', name: 'Lars-Magnus Skog' }, + authors: [ + { email: 'ralphtheninja@riseup.net', name: 'Lars-Magnus Skog' } + ], + authorDate: 'Tue Feb 9 15:46:46 2016 +0100', + description: [ + 'Fixes: https://github.com/rvagg/changelog-maker/issues/35' + ], + ghIssue: 1, + ghProject: 'commit-stream', + ghUser: 'rvagg', + prUrl: 'https://github.com/rvagg/commit-stream/pull/1', + sha: '1bcfd072fd74399808fbcd5cf31ce5342dd6d70c', + summary: 'process: this should not match PR-URL', + reviewers: [{ email: 'rod@vagg.org', name: 'Rod Vagg' }] + }, + 'got correct ninth commit' + ) + + t.deepEqual( + list[list.length - 4], + { + author: { email: 'rod@vagg.org', name: 'Rod Vagg' }, + authors: [{ email: 'rod@vagg.org', name: 'Rod Vagg' }], + authorDate: 'Fri Apr 17 11:16:51 2015 +1000', + sha: 'f92b93c3c7175b07f847dd45058b121cea6b3a20', + summary: 'deleted package.json line' + }, + 'got correct fourth commit' + ) + + t.deepEqual( + list[list.length - 3], + { + author: { email: 'rod@vagg.org', name: 'Rod Vagg' }, + authors: [{ email: 'rod@vagg.org', name: 'Rod Vagg' }], + authorDate: 'Fri Apr 17 11:13:06 2015 +1000', + description: ['comment', 'Reviewed-By: Nobody'], + sha: 'db34ce2af09a6a9fb70241d43965a2bc48b90b4c', + summary: 'squished package.json' + }, + 'got correct third commit' + ) + + t.deepEqual( + list[list.length - 2], + { + author: { email: 'rod@vagg.org', name: 'Rod Vagg' }, + authors: [{ email: 'rod@vagg.org', name: 'Rod Vagg' }], + authorDate: 'Fri Apr 17 10:52:16 2015 +1000', + description: [ + 'Some extra summary information here', + 'newline', + 'Fixes: https://github.com/rvagg/commit-stream/issues/0' + ], + ghIssue: 0, + ghProject: 'commit-stream', + ghUser: 'rvagg', + prUrl: 'https://github.com/rvagg/commit-stream/pulls/0', + reviewers: [{ email: 'roger@vagg.org', name: 'Roger Vagg' }], + sha: '6275758f597ae579202fbe4eccca1407f2c67ac1', + summary: 'added test.js' + }, + 'got correct second commit' + ) + + t.deepEqual( + list[list.length - 1], + { + sha: 'd94841274e2979e7758413a0f48fa37560d0dde6', + authorDate: 'Thu Apr 16 20:49:21 2015 +1000', + author: { name: 'Rod Vagg', email: 'rod@vagg.org' }, + authors: [{ email: 'rod@vagg.org', name: 'Rod Vagg' }], + summary: 'make it so' + }, + 'got correct first commit' + ) + + t.deepEqual( + list[list.length - 16], + { + sha: list[list.length - 16].sha, // unknown at time of writing :) + authorDate: 'Tue Jun 12 23:41:35 2018 +0200', + author: { name: 'Anna Henningsen', email: 'anna@addaleax.net' }, + authors: [ + { name: 'Anna Henningsen', email: 'anna@addaleax.net' }, + { name: 'nobody', email: 'nobody@nowhere' } + ], + summary: 'add support for co-authored-by' + }, + 'got correct co-authored-by commit' + ) t.end() }) }) - -test('current commit log with changes', function (t) { - gitToList('git log --stat', function (err, list) { +test('current commit log with changes', (t) => { + gitToList('git log --stat', (err, list) => { t.error(err, 'no errors') - t.ok(list && list.length > 0, 'got a list') - - t.deepEqual(list[list.length - 4].changes, { - files : 1 - , insertions : 0 - , deletions : 1 - }, 'got correct second commit changes') - - t.deepEqual(list[list.length - 3].changes, { - files : 1 - , insertions : 1 - , deletions : 28 - }, 'got correct second commit changes') - - t.deepEqual(list[list.length - 2].changes, { - files : 1 - , insertions : 49 - , deletions : 0 - }, 'got correct second commit changes') - - t.deepEqual(list[list.length - 1].changes, { - files : 1 - , insertions : 28 - , deletions : 0 - }, 'got correct first commit changes') + t.ok(list?.length > 0, 'got a list') + + t.deepEqual( + list[list.length - 4].changes, + { + files: 1, + insertions: 0, + deletions: 1 + }, + 'got correct second commit changes' + ) + + t.deepEqual( + list[list.length - 3].changes, + { + files: 1, + insertions: 1, + deletions: 28 + }, + 'got correct second commit changes' + ) + + t.deepEqual( + list[list.length - 2].changes, + { + files: 1, + insertions: 49, + deletions: 0 + }, + 'got correct second commit changes' + ) + + t.deepEqual( + list[list.length - 1].changes, + { + files: 1, + insertions: 28, + deletions: 0 + }, + 'got correct first commit changes' + ) t.end() })