Skip to content

Commit

Permalink
fix(conventional-changelog-core): check if HEAD ref exists before usi…
Browse files Browse the repository at this point in the history
…ng it (#578)
  • Loading branch information
tommywo committed Dec 24, 2019
1 parent d25d7ac commit dcb4b22
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 50 deletions.
126 changes: 122 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

98 changes: 52 additions & 46 deletions packages/conventional-changelog-core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const conventionalChangelogWriter = require('conventional-changelog-writer')
const _ = require('lodash')
const stream = require('stream')
const through = require('through2')
const shell = require('shelljs')

const mergeConfig = require('./lib/merge-config')
function conventionalChangelog (options, context, gitRawCommitsOpts, parserOpts, writerOpts, gitRawExecOpts) {
writerOpts = writerOpts || {}
Expand All @@ -16,6 +18,26 @@ function conventionalChangelog (options, context, gitRawCommitsOpts, parserOpts,
})
readable._read = function () { }

var commitsErrorThrown = false

var commitsStream = new stream.Readable({
objectMode: true
})
commitsStream._read = function () { }

function commitsRange (from, to) {
return gitRawCommits(_.merge({}, gitRawCommitsOpts, {
from: from,
to: to
}))
.on('error', function (err) {
if (!commitsErrorThrown) {
setImmediate(commitsStream.emit.bind(commitsStream), 'error', err)
commitsErrorThrown = true
}
})
}

mergeConfig(options, context, gitRawCommitsOpts, parserOpts, writerOpts, gitRawExecOpts)
.then(function (data) {
options = data.options
Expand All @@ -25,60 +47,44 @@ function conventionalChangelog (options, context, gitRawCommitsOpts, parserOpts,
writerOpts = data.writerOpts
gitRawExecOpts = data.gitRawExecOpts

var reverseTags = context.gitSemverTags.slice(0).reverse()
reverseTags.push('HEAD')
if (shell.exec('git rev-parse --verify HEAD', { silent: true }).code === 0) {
var reverseTags = context.gitSemverTags.slice(0).reverse()
reverseTags.push('HEAD')

if (gitRawCommitsOpts.from) {
if (reverseTags.indexOf(gitRawCommitsOpts.from) !== -1) {
reverseTags = reverseTags.slice(reverseTags.indexOf(gitRawCommitsOpts.from))
} else {
reverseTags = [gitRawCommitsOpts.from, 'HEAD']
if (gitRawCommitsOpts.from) {
if (reverseTags.indexOf(gitRawCommitsOpts.from) !== -1) {
reverseTags = reverseTags.slice(reverseTags.indexOf(gitRawCommitsOpts.from))
} else {
reverseTags = [gitRawCommitsOpts.from, 'HEAD']
}
}
}

var commitsErrorThrown = false

var commitsStream = new stream.Readable({
objectMode: true
})
commitsStream._read = function () { }

function commitsRange (from, to) {
return gitRawCommits(_.merge({}, gitRawCommitsOpts, {
from: from,
to: to
}))
.on('error', function (err) {
if (!commitsErrorThrown) {
setImmediate(commitsStream.emit.bind(commitsStream), 'error', err)
commitsErrorThrown = true
}
})
}
var streams = reverseTags.map((to, i) => {
const from = i > 0
? reverseTags[i - 1]
: ''
return commitsRange(from, to)
})

var streams = reverseTags.map((to, i) => {
const from = i > 0
? reverseTags[i - 1]
: ''
return commitsRange(from, to)
})
if (gitRawCommitsOpts.from) {
streams = streams.splice(1)
}

if (gitRawCommitsOpts.from) {
streams = streams.splice(1)
}
if (gitRawCommitsOpts.reverse) {
streams.reverse()
}

if (gitRawCommitsOpts.reverse) {
streams.reverse()
streams.reduce((prev, next) => next.pipe(addStream(prev)))
.on('data', function (data) {
setImmediate(commitsStream.emit.bind(commitsStream), 'data', data)
})
.on('end', function () {
setImmediate(commitsStream.emit.bind(commitsStream), 'end')
})
} else {
commitsStream = gitRawCommits(gitRawCommitsOpts, gitRawExecOpts)
}

streams.reduce((prev, next) => next.pipe(addStream(prev)))
.on('data', function (data) {
setImmediate(commitsStream.emit.bind(commitsStream), 'data', data)
})
.on('end', function () {
setImmediate(commitsStream.emit.bind(commitsStream), 'end')
})

commitsStream
.on('error', function (err) {
err.message = 'Error in git-raw-commits: ' + err.message
Expand Down
1 change: 1 addition & 0 deletions packages/conventional-changelog-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"q": "^1.5.1",
"read-pkg": "^3.0.0",
"read-pkg-up": "^3.0.0",
"shelljs": "^0.8.3",
"through2": "^3.0.0"
},
"scripts": {
Expand Down
26 changes: 26 additions & 0 deletions packages/conventional-changelog-core/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,32 @@ describe('conventionalChangelogCore', function () {
}))
})

it('should work when there is no `HEAD` ref', function (done) {
preparing(2)
shell.rm('.git/refs/HEAD')
var i = 0

conventionalChangelogCore({
releaseCount: 100
})
.pipe(through(function (chunk, enc, cb) {
chunk = chunk.toString()

if (i === 0) {
expect(chunk).to.include('Second commit')
expect(chunk).to.include('Third commit')
} else if (i === 1) {
expect(chunk).to.include('First commit')
}

i++
cb()
}, function () {
expect(i).to.equal(2)
done()
}))
})

it('should honour `gitRawCommitsOpts.from`', function (done) {
preparing(2)

Expand Down

0 comments on commit dcb4b22

Please sign in to comment.