Skip to content

Commit

Permalink
feat(git-client): GitClient#getLastTag and ConventionalGitClient#getL…
Browse files Browse the repository at this point in the history
…astSemverTag methods are added. GitClient#getRawCommits ignore param is added. (#1217)
  • Loading branch information
dangreen committed Mar 16, 2024
1 parent 0e4f293 commit 53254b3
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
node-version: 16
cache: 'pnpm'
- run: pnpm install
- run: pnpm test
- run: pnpm test -- --test-timeout=60000
- name: coverage
uses: coverallsapp/github-action@main
with:
Expand Down
21 changes: 14 additions & 7 deletions packages/git-client/src/ConventionalGitClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,16 @@ export class ConventionalGitClient extends GitClient {
params: GetCommitsParams & Params = {},
parserOptions: ParserStreamOptions = {}
): AsyncIterable<Commit> {
const { filterReverts, ...gitLogParams } = params
const [parseCommits, filterRevertedCommits] = await this.loadDeps()

if (params.filterReverts) {
yield* filterRevertedCommits(this.getCommits({
filterReverts: false,
...params
}, parserOptions))
if (filterReverts) {
yield* filterRevertedCommits(this.getCommits(gitLogParams, parserOptions))
return
}

const parse = parseCommits(parserOptions)
const commitsStream = this.getRawCommits(params)
const commitsStream = this.getRawCommits(gitLogParams)

yield* parse(commitsStream)
}
Expand All @@ -85,7 +83,7 @@ export class ConventionalGitClient extends GitClient {
* @param params.clean - Clean version from prefix and trash.
* @yields Semver tags.
*/
async* getSemverTags(params: GetSemverTagsParams = {}) {
async* getSemverTags(params: GetSemverTagsParams & Params = {}) {
const {
prefix,
skipUnstable,
Expand Down Expand Up @@ -131,6 +129,15 @@ export class ConventionalGitClient extends GitClient {
}
}

/**
* Get last semver tag.
* @param params - getSemverTags params.
* @returns Last semver tag, `null` if not found.
*/
async getLastSemverTag(params: GetSemverTagsParams & Params = {}) {
return (await this.getSemverTags(params).next()).value || null
}

/**
* Get current sematic version from git tags.
* @param params - Additional git params.
Expand Down
17 changes: 16 additions & 1 deletion packages/git-client/src/GitClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ export class GitClient {
from = '',
to = 'HEAD',
format = '%B',
ignore,
...restParams
} = params
const shouldNotIgnore = ignore
? (chunk: string) => !ignore.test(chunk)
: () => true
const args = this.formatArgs(
'log',
`--format=${format}%n${SCISSOR}`,
Expand All @@ -64,7 +68,9 @@ export class GitClient {
let chunk: string

for await (chunk of commitsStream) {
yield chunk
if (shouldNotIgnore(chunk)) {
yield chunk
}
}
}

Expand Down Expand Up @@ -98,6 +104,15 @@ export class GitClient {
}
}

/**
* Get last tag.
* @param params - Additional git params.
* @returns Last tag, `null` if not found.
*/
async getLastTag(params: Params = {}) {
return (await this.getTags(params).next()).value || null
}

/**
* Check file is ignored via .gitignore.
* @param file - Path to target file.
Expand Down
4 changes: 4 additions & 0 deletions packages/git-client/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export interface GitLogParams {
* Commits format.
*/
format?: string
/**
* Pattern to filter commits.
*/
ignore?: RegExp
}

export interface GetCommitsParams extends GitLogParams {
Expand Down
2 changes: 1 addition & 1 deletion packages/git-client/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ function formatParams(params: Params) {
for (arrayValue of value) {
args.push(formatKeyValue(key, arrayValue))
}
} else {
} else if (value) {
args.push(formatKeyValue(key, value))
}
}
Expand Down
16 changes: 6 additions & 10 deletions packages/git-raw-commits/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,16 @@ function getFinalOptions (options = {}) {
* @yields {string} - Raw commit.
*/
export async function * getRawCommits (options) {
const { cwd, debug, ignore, ...finalOptions } = getFinalOptions(options)
const ignoreRegex = typeof ignore === 'string'
? new RegExp(ignore)
: ignore
const shouldNotIgnore = ignoreRegex
? chunk => !ignoreRegex.test(chunk.toString())
: () => true
const { cwd, debug, ...finalOptions } = getFinalOptions(options)
const client = new GitClient(cwd, debug)
let commit

if (typeof finalOptions.ignore === 'string') {
finalOptions.ignore = new RegExp(finalOptions.ignore)
}

for await (commit of client.getRawCommits(finalOptions)) {
if (shouldNotIgnore(commit)) {
yield commit
}
yield commit
}
}

Expand Down

0 comments on commit 53254b3

Please sign in to comment.