Skip to content

Commit b4ab430

Browse files
authored
Add callback support to git.firstCommit (#960)
* Add callback support to `git.firstCommit`
1 parent d3f9320 commit b4ab430

File tree

5 files changed

+24
-9
lines changed

5 files changed

+24
-9
lines changed

.changeset/all-humans-wash.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'simple-git': patch
3+
---
4+
5+
Add trailing callback support to git.firstCommit

simple-git/readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,8 @@ in v2 (deprecation notices were logged to `stdout` as `console.warn` in v2).
308308
- `options.strictDate` - switches the authored date value from an ISO 8601-like format to be strict ISO 8601 format
309309
- `options.symmetric` - defaults to true, enables [symmetric revision range](https://git-scm.com/docs/gitrevisions#_dotted_range_notations) rather than a two-dot range
310310
- `options.to` - sets the newset commit in the range to return, use along with `options.from` to set a bounded range
311+
312+
When only one of `options.from` and `options.to` is supplied, the default value of the omitted option is equivalent to `HEAD`. For any other commit, explicitly supply both from and to commits (for example use `await git.firstCommit()` as the default value of `from` to log since the first commit of the repo).
311313

312314
## git merge
313315

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import { Response, SimpleGit } from '../../../typings';
22
import { SimpleGitApi } from '../simple-git-api';
3+
import { trailingFunctionArgument } from '../utils';
4+
import { straightThroughStringTask } from './task';
35

46
export default function (): Pick<SimpleGit, 'firstCommit'> {
57
return {
68
firstCommit(this: SimpleGitApi): Response<string> {
7-
return this._runTask({
8-
commands: ['rev-list', '--max-parents=0', 'HEAD'],
9-
format: 'utf-8',
10-
parser(stdOut) {
11-
return String(stdOut).trim();
12-
},
13-
});
9+
return this._runTask(
10+
straightThroughStringTask(['rev-list', '--max-parents=0', 'HEAD'], true),
11+
trailingFunctionArgument(arguments)
12+
);
1413
},
1514
};
1615
}
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
import { assertExecutedCommands, closeWithSuccess, newSimpleGit } from './__fixtures__';
22

33
describe('firstCommit', () => {
4-
it('gets the first commit in a repo', async () => {
4+
it('gets the first commit in a repo async', async () => {
55
const task = newSimpleGit().firstCommit();
66
await closeWithSuccess('a-commit-hash\n');
77

88
expect(await task).toBe('a-commit-hash');
99
assertExecutedCommands('rev-list', '--max-parents=0', 'HEAD');
1010
});
11+
12+
it('gets the first commit in a repo callback', async () => {
13+
const callback = jest.fn();
14+
const task = newSimpleGit().firstCommit(callback);
15+
await closeWithSuccess('a-commit-hash\n');
16+
17+
expect(callback).toHaveBeenCalledWith(null, await task);
18+
assertExecutedCommands('rev-list', '--max-parents=0', 'HEAD');
19+
});
1120
});

simple-git/typings/simple-git.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ export interface SimpleGit extends SimpleGitBase {
566566
/**
567567
* Gets the commit hash of the first commit in the repo
568568
*/
569-
firstCommit(): Response<string>;
569+
firstCommit(callback?: types.SimpleGitTaskCallback<string>): Response<string>;
570570

571571
/**
572572
* Gets the current value of a configuration property by it key, optionally specify the scope in which

0 commit comments

Comments
 (0)