Skip to content

Commit

Permalink
feat(git-raw-commits)!: refactored to use @conventional-changelog/git…
Browse files Browse the repository at this point in the history
…-client (#1199)

BREAKING CHANGES

new `getRawCommits` and `getRawCommitsStream` named exports
  • Loading branch information
dangreen authored Jan 6, 2024
1 parent e74b309 commit ba03ffc
Show file tree
Hide file tree
Showing 12 changed files with 240 additions and 289 deletions.
14 changes: 9 additions & 5 deletions packages/conventional-changelog-core/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Readable, Transform } from 'stream'
import { execFileSync } from 'child_process'
import addStream from 'add-stream'
import gitRawCommits from 'git-raw-commits'
import { getRawCommitsStream } from 'git-raw-commits'
import { parseCommitsStream } from 'conventional-commits-parser'
import { writeChangelogStream } from 'conventional-changelog-writer'
import mergeConfig from './lib/merge-config.js'
Expand All @@ -24,11 +24,12 @@ export default function conventionalChangelog (options, context, gitRawCommitsOp
commitsStream._read = function () { }

function commitsRange (from, to) {
return gitRawCommits({
return getRawCommitsStream({
...gitRawCommitsOpts,
from,
to
}, { cwd })
to,
cwd
})
.on('error', function (err) {
if (!commitsErrorThrown) {
setImmediate(commitsStream.emit.bind(commitsStream), 'error', err)
Expand Down Expand Up @@ -85,7 +86,10 @@ export default function conventionalChangelog (options, context, gitRawCommitsOp
setImmediate(commitsStream.emit.bind(commitsStream), 'end')
})
} catch (_e) {
commitsStream = gitRawCommits(gitRawCommitsOpts, gitRawExecOpts)
commitsStream = getRawCommitsStream({
...gitRawCommitsOpts,
...gitRawExecOpts
})
}

commitsStream
Expand Down
7 changes: 3 additions & 4 deletions packages/conventional-recommended-bump/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { filterRevertedCommitsSync } from 'conventional-commits-filter'
import { parseCommitsStream } from 'conventional-commits-parser'
import { loadPreset } from 'conventional-changelog-preset-loader'
import gitSemverTags from 'git-semver-tags'
import gitRawCommits from 'git-raw-commits'
import { getRawCommitsStream } from 'git-raw-commits'

const VERSIONS = ['major', 'minor', 'patch']

Expand Down Expand Up @@ -51,12 +51,11 @@ export default async function conventionalRecommendedBump (optionsArgument, pars
skipUnstable: options.skipUnstable,
cwd: options.cwd
})
const commitsStream = gitRawCommits({
const commitsStream = getRawCommitsStream({
format: '%B%n-hash-%n%H',
from: tags[0] || '',
path: options.path,
...options.gitRawCommitsOpts
}, {
...options.gitRawCommitsOpts,
cwd: options.cwd
})
.pipe(parseCommitsStream(parserOpts))
Expand Down
54 changes: 34 additions & 20 deletions packages/git-client/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
import type { Readable } from 'stream'
/* eslint-disable @typescript-eslint/no-misused-promises */
import {
type SpawnOptionsWithoutStdio,
spawn as spawnChild
} from 'child_process'
import type { Arg } from './types.js'

/**
* Read output stream to string.
* @param stream
* @returns Stream output string.
*/
async function readOutput(stream: AsyncIterable<string | Buffer>) {
let chunk: string | Buffer
let buffer = ''

for await (chunk of stream) {
buffer += chunk.toString()
}

return buffer
}

/**
* Spawn child process.
* @param cmd
Expand All @@ -15,22 +31,18 @@ import type { Arg } from './types.js'
export function spawn(cmd: string, args: string[], options?: SpawnOptionsWithoutStdio) {
return new Promise<string>((resolve, reject) => {
const child = spawnChild(cmd, args, options)
let output = ''
const onData = (data: Buffer) => {
output += data.toString()
}
const onDone = (codeOrError: Error | number | null) => {
const stdoutPromise = readOutput(child.stdout)
const stderrPromise = readOutput(child.stderr)
const onDone = async (codeOrError: Error | number | null) => {
if (codeOrError === 0) {
resolve(output)
resolve(await stdoutPromise)
} else if (codeOrError instanceof Error) {
reject(codeOrError)
} else {
reject(new Error(output))
reject(new Error(await stderrPromise))
}
}

child.stdout?.on('data', onData)
child.stderr?.on('data', onData)
child.on('close', onDone)
child.on('error', onDone)
})
Expand All @@ -41,18 +53,20 @@ export function spawn(cmd: string, args: string[], options?: SpawnOptionsWithout
* @param cmd
* @param args
* @param options
* @returns Readable stdout stream.
* @yields Stdout chunks.
*/
export function stdoutSpawn(cmd: string, args: string[], options?: SpawnOptionsWithoutStdio) {
export async function* stdoutSpawn(cmd: string, args: string[], options?: SpawnOptionsWithoutStdio) {
const child = spawnChild(cmd, args, options)
const { stdout } = child
const { stdout, stderr } = child
const errorPromise = readOutput(stderr)

child.stderr.on('data', (chunk: Buffer) => {
stdout.emit('error', new Error(chunk.toString()))
stdout.emit('close')
})
yield* stdout

const error = await errorPromise

return stdout
if (error) {
throw new Error(error)
}
}

/**
Expand All @@ -61,8 +75,8 @@ export function stdoutSpawn(cmd: string, args: string[], options?: SpawnOptionsW
* @param separator
* @yields String chunks.
*/
export async function* splitStream(stream: Readable, separator: string) {
let chunk: Buffer
export async function* splitStream(stream: AsyncIterable<string | Buffer>, separator: string) {
let chunk: string | Buffer
let payload: string[]
let buffer = ''

Expand Down
121 changes: 40 additions & 81 deletions packages/git-raw-commits/README.md
Original file line number Diff line number Diff line change
@@ -1,101 +1,60 @@
# [![NPM version][npm-image]][npm-url] [![Build Status: Linux][travis-image]][travis-url] [![Build Status: Windows][appveyor-image]][appveyor-url] [![Dependency Status][daviddm-image]][daviddm-url] [![Coverage Status][coverage-image]][coverage-url]
# git-raw-commits

> Get raw git commits out of your repository using git-log(1)
[![ESM-only package][package]][package-url]
[![NPM version][npm]][npm-url]
[![Node version][node]][node-url]
[![Dependencies status][deps]][deps-url]
[![Install size][size]][size-url]
[![Build status][build]][build-url]
[![Coverage status][coverage]][coverage-url]

[package]: https://img.shields.io/badge/package-ESM--only-ffe536.svg
[package-url]: https://nodejs.org/api/esm.html

## Install

```sh
$ npm install --save git-raw-commits
```


## Usage

```js
import gitRawCommits from 'git-raw-commits';

gitRawCommits(options)
.pipe(...);
```


## API

### gitRawCommits(gitOpts, [execOpts])

Returns a readable stream. Stream is split to break on each commit.

#### gitOpts

Type: `object`

Please check the available options at http://git-scm.com/docs/git-log.
**NOTE:** Single dash arguments are not supported because of https://github.com/sindresorhus/dargs/blob/master/index.js#L5.

*NOTE*: for `<revision range>` we can also use `<from>..<to>` pattern, and this module has the following extra options for shortcut of this pattern:

##### gitOpts.from

Type: `string` Default: `''`
[npm]: https://img.shields.io/npm/v/git-raw-commits.svg
[npm-url]: https://npmjs.com/package/git-raw-commits

##### gitOpts.to
[node]: https://img.shields.io/node/v/git-raw-commits.svg
[node-url]: https://nodejs.org

Type: `string` Default: `'HEAD'`
[deps]: https://img.shields.io/librariesio/release/npm/git-raw-commits
[deps-url]: https://libraries.io/npm/git-raw-commits/tree

This module also have the following additions:
[size]: https://packagephobia.com/badge?p=git-raw-commits
[size-url]: https://packagephobia.com/result?p=git-raw-commits

##### gitOpts.format
[build]: https://img.shields.io/github/actions/workflow/status/conventional-changelog/conventional-changelog/ci.yaml?branch=master
[build-url]: https://github.com/conventional-changelog/conventional-changelog/actions

Type: `string` Default: `'%B'`

Please check http://git-scm.com/docs/git-log for format options.

##### gitOpts.debug

Type: `function`

A function to get debug information.

##### gitOpts.path

Type: `string` or `array`

Filter commits to the path(s) provided.

##### execOpts

Options to pass to `git` `childProcess`

Type: `object`

##### execOpts.cwd
[coverage]: https://coveralls.io/repos/github/conventional-changelog/conventional-changelog/badge.svg?branch=master
[coverage-url]: https://coveralls.io/github/conventional-changelog/conventional-changelog?branch=master

Type: `string`
Get raw git commits out of your repository using git-log(1).

Current working directory to execute git in
## Install

```bash
# pnpm
pnpm add git-raw-commits
# yarn
yarn add git-raw-commits
# npm
npm i git-raw-commits
```

## CLI

```sh
$ npm install --global git-raw-commits
$ git-raw-commits --help # for more details
```bash
# Example
git-raw-commits --from HEAD~2 --to HEAD^
# For more details
git-raw-commits --help
```

## API

For JS API see [@conventional-changelog/git-client](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/git-client).

## License

MIT © [Steve Mao](https://github.com/stevemao)


[npm-image]: https://badge.fury.io/js/git-raw-commits.svg
[npm-url]: https://npmjs.org/package/git-raw-commits
[travis-image]: https://travis-ci.org/conventional-changelog/git-raw-commits.svg?branch=master
[travis-url]: https://travis-ci.org/conventional-changelog/git-raw-commits
[appveyor-image]: https://ci.appveyor.com/api/projects/status/4qm3bjmg41k3dsbv/branch/master?svg=true
[appveyor-url]: https://ci.appveyor.com/project/stevemao/git-raw-commits/branch/master
[daviddm-image]: https://david-dm.org/conventional-changelog/git-raw-commits.svg?theme=shields.io
[daviddm-url]: https://david-dm.org/conventional-changelog/git-raw-commits
[coverage-image]: https://coveralls.io/repos/github/conventional-changelog/conventional-changelog/badge.svg?branch=master
[coverage-url]: https://coveralls.io/github/conventional-changelog/conventional-changelog?branch=master
20 changes: 0 additions & 20 deletions packages/git-raw-commits/cli.js

This file was deleted.

Loading

0 comments on commit ba03ffc

Please sign in to comment.