Skip to content

Commit 709d80e

Browse files
authored
Create utility git.firstCommit() to fetch the commit hash of the fi… (#958)
* Create utility `git.firstCommit()` to fetch the commit hash of the first commit in the repo
1 parent 8a31c45 commit 709d80e

File tree

6 files changed

+41
-0
lines changed

6 files changed

+41
-0
lines changed

.changeset/nervous-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': minor
3+
---
4+
5+
Add firstCommit utility interface

simple-git/readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,8 @@ in v2 (deprecation notices were logged to `stdout` as `console.warn` in v2).
375375
- `.checkIsRepo('bare')` gets whether the current working directory is within a bare git repo (see either [git clone --bare](https://git-scm.com/docs/git-clone#Documentation/git-clone.txt---bare) or [git init --bare](https://git-scm.com/docs/git-init#Documentation/git-init.txt---bare)).
376376
- `.checkIsRepo('root')` gets whether the current working directory is the root directory for a repo (sub-directories will return false).
377377

378+
- `.firstCommit()` gets the commit hash of the first commit made to the current repo.
379+
378380
## git show
379381

380382
- `.show(options)` show various types of objects for example the file content at a certain commit. `options` is the single value string or any [options](#how-to-specify-options) supported by the [git show](https://git-scm.com/docs/git-show) command.

simple-git/src/lib/simple-git-api.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { changeWorkingDirectoryTask } from './tasks/change-working-directory';
44
import checkout from './tasks/checkout';
55
import commit from './tasks/commit';
66
import config from './tasks/config';
7+
import firstCommit from './tasks/first-commit';
78
import grep from './tasks/grep';
89
import { hashObjectTask } from './tasks/hash-object';
910
import { initTask } from './tasks/init';
@@ -144,6 +145,7 @@ Object.assign(
144145
checkout(),
145146
commit(),
146147
config(),
148+
firstCommit(),
147149
grep(),
148150
log(),
149151
show(),
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Response, SimpleGit } from '../../../typings';
2+
import { SimpleGitApi } from '../simple-git-api';
3+
4+
export default function (): Pick<SimpleGit, 'firstCommit'> {
5+
return {
6+
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+
});
14+
},
15+
};
16+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { assertExecutedCommands, closeWithSuccess, newSimpleGit } from './__fixtures__';
2+
3+
describe('firstCommit', () => {
4+
it('gets the first commit in a repo', async () => {
5+
const task = newSimpleGit().firstCommit();
6+
await closeWithSuccess('a-commit-hash\n');
7+
8+
expect(await task).toBe('a-commit-hash');
9+
assertExecutedCommands('rev-list', '--max-parents=0', 'HEAD');
10+
});
11+
});

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,11 @@ export interface SimpleGit extends SimpleGitBase {
563563

564564
fetch(callback?: types.SimpleGitTaskCallback<resp.FetchResult>): Response<resp.FetchResult>;
565565

566+
/**
567+
* Gets the commit hash of the first commit in the repo
568+
*/
569+
firstCommit(): Response<string>;
570+
566571
/**
567572
* Gets the current value of a configuration property by it key, optionally specify the scope in which
568573
* to run the command (omit / set to `undefined` to check in the complete overlaid configuration visible

0 commit comments

Comments
 (0)