From 709d80e0dde3f233dc69eb4b3bbba146a411d503 Mon Sep 17 00:00:00 2001 From: Steve King Date: Sun, 19 Nov 2023 12:52:20 +0000 Subject: [PATCH] =?UTF-8?q?Create=20utility=20`git.firstCommit()`=20to=20f?= =?UTF-8?q?etch=20the=20commit=20hash=20of=20the=20fi=E2=80=A6=20(#958)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Create utility `git.firstCommit()` to fetch the commit hash of the first commit in the repo --- .changeset/nervous-humans-wash.md | 5 +++++ simple-git/readme.md | 2 ++ simple-git/src/lib/simple-git-api.ts | 2 ++ simple-git/src/lib/tasks/first-commit.ts | 16 ++++++++++++++++ simple-git/test/unit/first-commit.spec.ts | 11 +++++++++++ simple-git/typings/simple-git.d.ts | 5 +++++ 6 files changed, 41 insertions(+) create mode 100644 .changeset/nervous-humans-wash.md create mode 100644 simple-git/src/lib/tasks/first-commit.ts create mode 100644 simple-git/test/unit/first-commit.spec.ts diff --git a/.changeset/nervous-humans-wash.md b/.changeset/nervous-humans-wash.md new file mode 100644 index 00000000..d92335eb --- /dev/null +++ b/.changeset/nervous-humans-wash.md @@ -0,0 +1,5 @@ +--- +'simple-git': minor +--- + +Add firstCommit utility interface diff --git a/simple-git/readme.md b/simple-git/readme.md index 06a334cb..13fb5c37 100644 --- a/simple-git/readme.md +++ b/simple-git/readme.md @@ -375,6 +375,8 @@ in v2 (deprecation notices were logged to `stdout` as `console.warn` in v2). - `.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)). - `.checkIsRepo('root')` gets whether the current working directory is the root directory for a repo (sub-directories will return false). +- `.firstCommit()` gets the commit hash of the first commit made to the current repo. + ## git show - `.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. diff --git a/simple-git/src/lib/simple-git-api.ts b/simple-git/src/lib/simple-git-api.ts index 18a1df0f..d49a4132 100644 --- a/simple-git/src/lib/simple-git-api.ts +++ b/simple-git/src/lib/simple-git-api.ts @@ -4,6 +4,7 @@ import { changeWorkingDirectoryTask } from './tasks/change-working-directory'; import checkout from './tasks/checkout'; import commit from './tasks/commit'; import config from './tasks/config'; +import firstCommit from './tasks/first-commit'; import grep from './tasks/grep'; import { hashObjectTask } from './tasks/hash-object'; import { initTask } from './tasks/init'; @@ -144,6 +145,7 @@ Object.assign( checkout(), commit(), config(), + firstCommit(), grep(), log(), show(), diff --git a/simple-git/src/lib/tasks/first-commit.ts b/simple-git/src/lib/tasks/first-commit.ts new file mode 100644 index 00000000..882b5eea --- /dev/null +++ b/simple-git/src/lib/tasks/first-commit.ts @@ -0,0 +1,16 @@ +import { Response, SimpleGit } from '../../../typings'; +import { SimpleGitApi } from '../simple-git-api'; + +export default function (): Pick { + return { + firstCommit(this: SimpleGitApi): Response { + return this._runTask({ + commands: ['rev-list', '--max-parents=0', 'HEAD'], + format: 'utf-8', + parser(stdOut) { + return String(stdOut).trim(); + }, + }); + }, + }; +} diff --git a/simple-git/test/unit/first-commit.spec.ts b/simple-git/test/unit/first-commit.spec.ts new file mode 100644 index 00000000..8cfdf32a --- /dev/null +++ b/simple-git/test/unit/first-commit.spec.ts @@ -0,0 +1,11 @@ +import { assertExecutedCommands, closeWithSuccess, newSimpleGit } from './__fixtures__'; + +describe('firstCommit', () => { + it('gets the first commit in a repo', async () => { + const task = newSimpleGit().firstCommit(); + await closeWithSuccess('a-commit-hash\n'); + + expect(await task).toBe('a-commit-hash'); + assertExecutedCommands('rev-list', '--max-parents=0', 'HEAD'); + }); +}); diff --git a/simple-git/typings/simple-git.d.ts b/simple-git/typings/simple-git.d.ts index c22dab3a..70a55325 100644 --- a/simple-git/typings/simple-git.d.ts +++ b/simple-git/typings/simple-git.d.ts @@ -563,6 +563,11 @@ export interface SimpleGit extends SimpleGitBase { fetch(callback?: types.SimpleGitTaskCallback): Response; + /** + * Gets the commit hash of the first commit in the repo + */ + firstCommit(): Response; + /** * Gets the current value of a configuration property by it key, optionally specify the scope in which * to run the command (omit / set to `undefined` to check in the complete overlaid configuration visible