Skip to content

Commit 7ba9d0d

Browse files
committed
Create the git.emptyTreeCommit() method to get the commit hash of the repo in its initial state prior to the first commit being applied.
More future-proof than relying on a hard-coded value for the hash should `git` start using the SHA256 value in place of SHA1.
1 parent 9d45685 commit 7ba9d0d

File tree

6 files changed

+41
-1
lines changed

6 files changed

+41
-1
lines changed

.changeset/blank-club-mauve.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
'simple-git': minor
3+
---
4+
Create new interface `git.emptyTreeCommit` to obtain the commit hash for the repo prior to the first commit.

simple-git/readme.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,10 +285,11 @@ in v2 (deprecation notices were logged to `stdout` as `console.warn` in v2).
285285
- `.grep(searchTerm)` searches for a single search term across all files in the working tree, optionally passing a standard [options](#how-to-specify-options) object of additional arguments
286286
- `.grep(grepQueryBuilder(...))` use the `grepQueryBuilder` to create a complex query to search for, optionally passing a standard [options](#how-to-specify-options) object of additional arguments
287287

288-
## git hash-object
288+
## git hash-object / hash properties
289289

290290
- `.hashObject(filePath, write = false)` computes the object ID value for the contents of the named file (which can be
291291
outside of the work tree), optionally writing the resulting value to the object database.
292+
- `.emptyTreeCommit()` gets the commit hash for the repo in its initial empty state before the `git.firstCommit` was applied, useful for computing the insert-only initial commit with `git.diffSummary`.
292293

293294
## git init
294295

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import checkout from './tasks/checkout';
55
import countObjects from './tasks/count-objects';
66
import commit from './tasks/commit';
77
import config from './tasks/config';
8+
import emptyTreeCommit from './tasks/empty-tree-commit';
89
import firstCommit from './tasks/first-commit';
910
import grep from './tasks/grep';
1011
import { hashObjectTask } from './tasks/hash-object';
@@ -147,6 +148,7 @@ Object.assign(
147148
commit(),
148149
config(),
149150
countObjects(),
151+
emptyTreeCommit(),
150152
firstCommit(),
151153
grep(),
152154
log(),
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Response, SimpleGit } from '../../../typings';
2+
import { SimpleGitApi } from '../simple-git-api';
3+
import { trailingFunctionArgument } from '../utils';
4+
import { straightThroughStringTask } from './task';
5+
6+
export default function (): Pick<SimpleGit, 'emptyTreeCommit'> {
7+
return {
8+
emptyTreeCommit(this: SimpleGitApi): Response<string> {
9+
return this._runTask(
10+
straightThroughStringTask(['hash-object', '-t', 'tree', '/dev/null'], true),
11+
trailingFunctionArgument(arguments)
12+
);
13+
},
14+
};
15+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { createTestContext } from '@simple-git/test-utils';
2+
3+
const EMPTY_SHA = '4b825dc642cb6eb9a060e54bf8d69288fbee4904';
4+
const EMPTY_SHA_256 = '6ef19b41225c5369f1c104d45d8d85efa9b057b53b14b4b9b939dd74decc5321';
5+
6+
describe('empty-tree-commit', () => {
7+
it('gets the empty tree commit', async () => {
8+
const context = await createTestContext();
9+
const commit = await context.git.emptyTreeCommit();
10+
11+
expect(commit === EMPTY_SHA || commit === EMPTY_SHA_256).toBe(true);
12+
});
13+
});

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

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

525525
diffSummary(callback?: types.SimpleGitTaskCallback<resp.DiffResult>): Response<resp.DiffResult>;
526526

527+
/**
528+
* Gets the commit hash of the initial empty repo commit, immediately prior to the first commit in the repo
529+
*/
530+
emptyTreeCommit(callback?: types.SimpleGitTaskCallback<string>): Response<string>;
531+
527532
/**
528533
* Sets an environment variable for the spawned child process, either supply both a name and value as strings or
529534
* a single object to entirely replace the current environment variables.

0 commit comments

Comments
 (0)