Skip to content

Commit

Permalink
fix: allow raw commits to be filtered by path and date range (#893)
Browse files Browse the repository at this point in the history
Co-authored-by: Benjamin E. Coe <bencoe@google.com>
  • Loading branch information
action-hong and bcoe committed Dec 29, 2021
1 parent 1fe2f29 commit b2245a7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
7 changes: 4 additions & 3 deletions packages/git-raw-commits/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,17 @@ function getGitArgs (gitOpts) {
const gitFromTo = [gitOpts.from, gitOpts.to].filter(Boolean).join('..')

const gitArgs = ['log', gitFormat, gitFromTo]
.concat(dargs(gitOpts, {
excludes: ['debug', 'from', 'to', 'format', 'path']
}))

// allow commits to focus on a single directory
// this is useful for monorepos.
if (gitOpts.path) {
gitArgs.push('--', gitOpts.path)
}

return gitArgs.concat(dargs(gitOpts, {
excludes: ['debug', 'from', 'to', 'format', 'path']
}))
return gitArgs
}

function gitRawCommits (rawGitOpts, rawExecOpts) {
Expand Down
34 changes: 34 additions & 0 deletions packages/git-raw-commits/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const shell = require('shelljs')
const through = require('through2')
const writeFileSync = require('fs').writeFileSync
const mkdirp = require('mkdirp')
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms))

describe('git-raw-commits', function () {
before(function () {
Expand Down Expand Up @@ -192,4 +193,37 @@ describe('git-raw-commits', function () {
done()
}))
})

it('should allow commits to be scoped to a specific directory and specific date range', function (done) {
let i = 0

// Since milliseconds are ignored(https://git-scm.com/docs/git-commit#Documentation/git-commit.txt-ISO8601),
// A one-second delay ensures that new commits are filtered(https://www.git-scm.com/docs/git-log#Documentation/git-log.txt---sinceltdategt)
delay(1000)
.then(_ => {
const now = new Date().toISOString()
writeFileSync('./packages/foo/test1', 'hello')
shell.exec('git add --all && git commit -m"Fourth commit"')
writeFileSync('test2', 'hello')
shell.exec('git add --all && git commit -m"Fifth commit"')

gitRawCommits({
path: './packages/foo',
since: now
})
.pipe(through(function (chunk, enc, cb) {
chunk = chunk.toString()

if (i === 0) {
expect(chunk).to.equal('Fourth commit\n\n')
}

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

0 comments on commit b2245a7

Please sign in to comment.