Skip to content

Commit

Permalink
chore: lint with standard (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed May 11, 2020
1 parent 17d363e commit 603d3d0
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 78 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Lint

on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
build:
runs-on: macOS-latest
steps:
- uses: actions/checkout@master
- name: Use Node.js 12.x
uses: actions/setup-node@v1
with:
version: 12.x
- name: Lint
run: |
npm i
npm run lint
163 changes: 86 additions & 77 deletions branch-diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,31 @@

'use strict'

const fs = require('fs')
, path = require('path')
, commitStream = require('commit-stream')
, split2 = require('split2')
, listStream = require('list-stream')
, pkgtoId = require('pkg-to-id')
, stripAnsi = require('strip-ansi')
, map = require('map-async')
, { commitToOutput } = require('changelog-maker/commit-to-output')
, collectCommitLabels = require('changelog-maker/collect-commit-labels')
, groupCommits = require('changelog-maker/group-commits')
, { isReleaseCommit, toGroups } = require('changelog-maker/groups')
, gitexec = require('gitexec')

, pkgFile = path.join(process.cwd(), 'package.json')
, pkgData = fs.existsSync(pkgFile) ? require(pkgFile) : {}
, pkgId = pkgtoId(pkgData)
, refcmd = 'git rev-list --max-count=1 {{ref}}'
, commitdatecmd = '$(git show -s --format=%cd `{{refcmd}}`)'
, gitcmd = 'git log {{startCommit}}..{{branch}} --until="{{untilcmd}}"'
, ghId = {
user: pkgId.user || 'nodejs'
, repo: pkgId.name || 'node'
}
, defaultCommitUrl = 'https://github.com/{ghUser}/{ghRepo}/commit/{ref}'
const fs = require('fs')
const path = require('path')
const commitStream = require('commit-stream')
const split2 = require('split2')
const listStream = require('list-stream')
const pkgtoId = require('pkg-to-id')
const stripAnsi = require('strip-ansi')
const map = require('map-async')
const { commitToOutput } = require('changelog-maker/commit-to-output')
const collectCommitLabels = require('changelog-maker/collect-commit-labels')
const groupCommits = require('changelog-maker/group-commits')
const { isReleaseCommit, toGroups } = require('changelog-maker/groups')
const gitexec = require('gitexec')

const pkgFile = path.join(process.cwd(), 'package.json')
const pkgData = fs.existsSync(pkgFile) ? require(pkgFile) : {}
const pkgId = pkgtoId(pkgData)
const refcmd = 'git rev-list --max-count=1 {{ref}}'
const commitdatecmd = '$(git show -s --format=%cd `{{refcmd}}`)'
const gitcmd = 'git log {{startCommit}}..{{branch}} --until="{{untilcmd}}"'
const ghId = {
user: pkgId.user || 'nodejs',
repo: pkgId.name || 'node'
}
const defaultCommitUrl = 'https://github.com/{ghUser}/{ghRepo}/commit/{ref}'

const formatType = {
PLAINTEXT: 'plaintext',
Expand All @@ -43,58 +43,56 @@ const getFormat = (argv) => {
}
return formatType.MARKDOWN
}

function replace (s, m) {
Object.keys(m).forEach(function (k) {
s = s.replace(new RegExp('\\{\\{' + k + '\\}\\}', 'g'), m[k])
})
return s
}


function branchDiff (branch1, branch2, options, callback) {
if (!branch1 || !branch2)
if (!branch1 || !branch2) {
return callback(new Error('Must supply two branch names to compare'))
}

let repoPath = options.repoPath || process.cwd()
const repoPath = options.repoPath || process.cwd()

findMergeBase(repoPath, branch1, branch2, (err, commit) => {
if (err)
return callback(err)
if (err) { return callback(err) }
map(
[ branch1, branch2 ], (branch, callback) => {
collect(repoPath, branch, commit, branch == branch2 && options.endRef).pipe(listStream.obj(callback))
}
[branch1, branch2], (branch, callback) => {
collect(repoPath, branch, commit, branch === branch2 && options.endRef).pipe(listStream.obj(callback))
}
, (err, branchCommits) => err ? callback(err) : diffCollected(options, branchCommits, callback)
)
})
}


function findMergeBase (repoPath, branch1, branch2, callback) {
let gitcmd = `git merge-base ${branch1} ${branch2}`
const gitcmd = `git merge-base ${branch1} ${branch2}`

gitexec.execCollect(repoPath, gitcmd, (err, data) => {
if (err)
if (err) {
return callback(err)
}

callback(null, data.substr(0, 10))
})
}


function diffCollected (options, branchCommits, callback) {
function isInList (commit) {
return branchCommits[0].some((c) => {
if (commit.sha === c.sha)
return true
if (commit.sha === c.sha) { return true }
if (commit.summary === c.summary) {
if (commit.prUrl && c.prUrl) {
return commit.prUrl === c.prUrl
} else if (commit.author.name === c.author.name
&& commit.author.email === c.author.email) {
if (process.stderr.isTTY)
return commit.prUrl === c.prUrl
} else if (commit.author.name === c.author.name &&
commit.author.email === c.author.email) {
if (process.stderr.isTTY) {
console.error(`Note: Commit fell back to author checking: "${commit.summary}" -`, commit.author)
}
return true
}
}
Expand All @@ -105,8 +103,9 @@ function diffCollected (options, branchCommits, callback) {
let list = branchCommits[1].filter((commit) => !isInList(commit))

collectCommitLabels(list, (err) => {
if (err)
if (err) {
return callback(err)
}

if (options.excludeLabels.length > 0) {
list = list.filter((commit) => {
Expand All @@ -124,8 +123,9 @@ function diffCollected (options, branchCommits, callback) {
})
}

if (options.group)
if (options.group) {
list = groupCommits(list)
}

callback(null, list)
})
Expand All @@ -152,83 +152,92 @@ function printCommits (list, format, reverse, commitUrl) {
}
list = formatted
} else {
list = list.map((commit) => commitToOutput(commit, formatType.MARKDOWN, ghId, commitUrl))
list = list.map((commit) => {
return commitToOutput(commit, formatType.MARKDOWN, ghId, commitUrl)
})
}

if (reverse)
if (reverse) {
list = list.reverse()
}

let out = list.join('\n') + '\n'

if (!process.stdout.isTTY)
if (!process.stdout.isTTY) {
out = stripAnsi(out)
}

process.stdout.write(out)
}


function collect (repoPath, branch, startCommit, endRef) {
let endrefcmd = endRef && replace(refcmd, { ref: endRef })
, untilcmd = endRef ? replace(commitdatecmd, { refcmd: endrefcmd }) : ''
, _gitcmd = replace(gitcmd, { branch, startCommit, untilcmd })
const endrefcmd = endRef && replace(refcmd, { ref: endRef })
const untilcmd = endRef ? replace(commitdatecmd, { refcmd: endrefcmd }) : ''
const _gitcmd = replace(gitcmd, { branch, startCommit, untilcmd })

return gitexec.exec(repoPath, _gitcmd)
.pipe(split2())
.pipe(commitStream(ghId.user, ghId.repo))
}


module.exports = branchDiff

if (require.main === module) {
let minimistConfig = {
boolean: [ 'version', 'group', 'patch-only', 'simple', 'filter-release', 'reverse' ]
}
, argv = require('minimist')(process.argv.slice(2), minimistConfig)
, branch1 = argv._[0]
, branch2 = argv._[1]
, reverse = argv.reverse
, group = argv.group || argv.g
, endRef = argv['end-ref']
, commitUrl = argv['commit-url'] || defaultCommitUrl
, excludeLabels = []
, requireLabels = []
, options
function main () {
const minimistConfig = {
boolean: ['version', 'group', 'patch-only', 'simple', 'filter-release', 'reverse']
}
const argv = require('minimist')(process.argv.slice(2), minimistConfig)
const branch1 = argv._[0]
const branch2 = argv._[1]
const reverse = argv.reverse
const group = argv.group || argv.g
const endRef = argv['end-ref']
const commitUrl = argv['commit-url'] || defaultCommitUrl
let excludeLabels = []
let requireLabels = []

const format = getFormat(argv)

if (argv.version || argv.v)
if (argv.version || argv.v) {
return console.log(`v ${require('./package.json').version}`)
}

if (argv['patch-only'])
excludeLabels = [ 'semver-minor', 'semver-major' ]
if (argv['patch-only']) {
excludeLabels = ['semver-minor', 'semver-major']
}

if (argv['exclude-label']) {
if (!Array.isArray(argv['exclude-label']))
if (!Array.isArray(argv['exclude-label'])) {
argv['exclude-label'] = argv['exclude-label'].split(',')
}
excludeLabels = excludeLabels.concat(argv['exclude-label'])
}

if (argv['require-label']) {
if (!Array.isArray(argv['require-label']))
if (!Array.isArray(argv['require-label'])) {
argv['require-label'] = argv['require-label'].split(',')
}
requireLabels = requireLabels.concat(argv['require-label'])
}

options = {
const options = {
group,
excludeLabels,
requireLabels,
endRef
}

branchDiff(branch1, branch2, options, (err, list) => {
if (err)
throw err
if (err) { throw err }

if (argv['filter-release'])
if (argv['filter-release']) {
list = list.filter((commit) => !isReleaseCommit(commit.summary))
}

printCommits(list, format, reverse, commitUrl)
})
}

if (require.main === module) {
main()
}
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
"bin": {
"branch-diff": "./branch-diff.js"
},
"scripts": {
"lint": "standard",
"format": "standard --fix"
},
"author": "Rod <rod@vagg.org> (http://r.va.gg/)",
"license": "MIT",
"dependencies": {
Expand All @@ -26,5 +30,8 @@
"type": "git",
"url": "https://github.com/rvagg/branch-diff.git"
},
"preferGlobal": true
"preferGlobal": true,
"devDependencies": {
"standard": "^14.3.3"
}
}

0 comments on commit 603d3d0

Please sign in to comment.