Skip to content

Commit f2fc5c9

Browse files
committedApr 9, 2022
Override local configuration for git.commit to show full commit hashes when committing files.
1 parent c4a2a13 commit f2fc5c9

File tree

6 files changed

+81
-49
lines changed

6 files changed

+81
-49
lines changed
 

‎.changeset/long-squids-shop.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"simple-git": minor
3+
---
4+
5+
Show full commit hash in a CommitResult

‎simple-git/src/git.js

-29
Original file line numberDiff line numberDiff line change
@@ -140,35 +140,6 @@ Git.prototype.checkoutLatestTag = function (then) {
140140
});
141141
};
142142

143-
/**
144-
* Commits changes in the current working directory - when specific file paths are supplied, only changes on those
145-
* files will be committed.
146-
*
147-
* @param {string|string[]} message
148-
* @param {string|string[]} [files]
149-
* @param {Object} [options]
150-
* @param {Function} [then]
151-
*/
152-
Git.prototype.commit = function (message, files, options, then) {
153-
const next = trailingFunctionArgument(arguments);
154-
155-
if (!filterStringOrStringArray(message)) {
156-
return this._runTask(
157-
configurationErrorTask('git.commit: requires the commit message to be supplied as a string/string[]'),
158-
next,
159-
);
160-
}
161-
162-
return this._runTask(
163-
commitTask(
164-
asArray(message),
165-
asArray(filterType(files, filterStringOrStringArray, [])),
166-
[...filterType(options, filterArray, []), ...getTrailingOptions(arguments, 0, true)]
167-
),
168-
next
169-
);
170-
};
171-
172143
/**
173144
* Pull the updated contents of the current repo
174145
*/

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { SimpleGitBase } from '../../typings';
22
import { taskCallback } from './task-callback';
33
import { changeWorkingDirectoryTask } from './tasks/change-working-directory';
4+
import commit from './tasks/commit';
45
import config from './tasks/config';
56
import grep from './tasks/grep';
67
import { hashObjectTask } from './tasks/hash-object';
@@ -123,4 +124,4 @@ export class SimpleGitApi implements SimpleGitBase {
123124
}
124125
}
125126

126-
Object.assign(SimpleGitApi.prototype, config(), grep(), log());
127+
Object.assign(SimpleGitApi.prototype, commit(), config(), grep(), log());

‎simple-git/src/lib/tasks/commit.ts

+41-8
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,53 @@
1-
import { CommitResult } from '../../../typings';
1+
import type { CommitResult, SimpleGit } from '../../../typings';
2+
import type { SimpleGitApi } from '../simple-git-api';
3+
import type { StringTask } from '../types';
24
import { parseCommitResult } from '../parsers/parse-commit';
3-
import { StringTask } from '../types';
5+
import {
6+
asArray,
7+
filterArray,
8+
filterStringOrStringArray,
9+
filterType,
10+
getTrailingOptions, prefixedArray,
11+
trailingFunctionArgument
12+
} from '../utils';
13+
import { configurationErrorTask } from './task';
414

515
export function commitTask(message: string[], files: string[], customArgs: string[]): StringTask<CommitResult> {
6-
const commands: string[] = ['commit'];
7-
8-
message.forEach((m) => commands.push('-m', m));
9-
10-
commands.push(
16+
const commands: string[] = [
17+
'-c',
18+
'core.abbrev=40',
19+
'commit',
20+
...prefixedArray(message, '-m'),
1121
...files,
1222
...customArgs,
13-
);
23+
];
1424

1525
return {
1626
commands,
1727
format: 'utf-8',
1828
parser: parseCommitResult,
29+
};
30+
}
31+
32+
export default function (): Pick<SimpleGit, 'commit'> {
33+
return {
34+
commit(this: SimpleGitApi, message: string | string[], ...rest: unknown[]) {
35+
const next = trailingFunctionArgument(arguments);
36+
const task = rejectDeprecatedSignatures(message) ||
37+
commitTask(
38+
asArray(message),
39+
asArray(filterType(rest[0], filterStringOrStringArray, [])),
40+
[...filterType(rest[1], filterArray, []), ...getTrailingOptions(arguments, 0, true)]
41+
);
42+
43+
return this._runTask(task, next);
44+
},
45+
};
46+
47+
function rejectDeprecatedSignatures(message?: unknown) {
48+
return (
49+
!filterStringOrStringArray(message) &&
50+
configurationErrorTask(`git.commit: requires the commit message to be supplied as a string/string[]`)
51+
);
1952
}
2053
}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { createTestContext, newSimpleGit, setUpInit, SimpleGitTestContext } from '../__fixtures__';
2+
3+
describe('commit', () => {
4+
let context: SimpleGitTestContext;
5+
6+
beforeEach(async () => context = await createTestContext());
7+
beforeEach(async () => {
8+
await setUpInit(context);
9+
await context.files('hello', 'world');
10+
await context.git.add('.');
11+
});
12+
13+
it('details full commit hashes', async () => {
14+
const result = await newSimpleGit(context.root).commit('commit message');
15+
16+
expect(result.commit.length).toBeGreaterThan(10);
17+
expect(result.commit).toEqual(
18+
await context.git.revparse('HEAD'),
19+
);
20+
});
21+
22+
});

‎simple-git/test/unit/commit.spec.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { promiseError } from '@kwsites/promise-result';
12
import {
23
assertExecutedCommands,
34
assertGitError,
@@ -11,7 +12,6 @@ import {
1112
} from './__fixtures__';
1213
import { SimpleGit, TaskConfigurationError } from '../..';
1314
import { parseCommitResult } from '../../src/lib/parsers/parse-commit';
14-
import { promiseError } from '@kwsites/promise-result';
1515

1616
describe('commit', () => {
1717
let git: SimpleGit;
@@ -26,62 +26,62 @@ describe('commit', () => {
2626
it('empty commit', async () => {
2727
git.commit([], {'--amend': null, '--no-edit': null});
2828
await closeWithSuccess();
29-
assertExecutedCommands('commit', '--amend', '--no-edit');
29+
assertExecutedCommands('-c', 'core.abbrev=40', 'commit', '--amend', '--no-edit');
3030
});
3131

3232
it('single message, no files, no options and callback', async () => {
3333
const task = git.commit('message', callback);
3434
await closeWithSuccess();
35-
assertExecutedCommands('commit', '-m', 'message');
35+
assertExecutedCommands('-c', 'core.abbrev=40', 'commit', '-m', 'message');
3636
expect(callback).toHaveBeenCalledWith(null, await task);
3737
});
3838

3939
it('multi message, no files, no options and callback', async () => {
4040
const task = git.commit(['aaa', 'bbb'], callback);
4141
await closeWithSuccess();
42-
assertExecutedCommands('commit', '-m', 'aaa', '-m', 'bbb');
42+
assertExecutedCommands('-c', 'core.abbrev=40', 'commit', '-m', 'aaa', '-m', 'bbb');
4343
expect(callback).toHaveBeenCalledWith(null, await task);
4444
});
4545

4646
it('single message, no files, with options object and callback', async () => {
4747
const task = git.commit('message', {'--opt': null}, callback);
4848
await closeWithSuccess();
49-
assertExecutedCommands('commit', '-m', 'message', '--opt');
49+
assertExecutedCommands('-c', 'core.abbrev=40', 'commit', '-m', 'message', '--opt');
5050
expect(callback).toHaveBeenCalledWith(null, await task);
5151
});
5252

5353
it('single message, single file, options object and callback', async () => {
5454
const task = git.commit('msg', 'aaa.txt', {'--opt': null}, callback);
5555
await closeWithSuccess();
56-
assertExecutedCommands('commit', '-m', 'msg', 'aaa.txt', '--opt');
56+
assertExecutedCommands('-c', 'core.abbrev=40', 'commit', '-m', 'msg', 'aaa.txt', '--opt');
5757
expect(callback).toHaveBeenCalledWith(null, await task);
5858
});
5959

6060
it('single message, single file, no options with callback', async () => {
6161
const task = git.commit('msg', 'aaa.txt', callback);
6262
await closeWithSuccess();
63-
assertExecutedCommands('commit', '-m', 'msg', 'aaa.txt',);
63+
assertExecutedCommands('-c', 'core.abbrev=40', 'commit', '-m', 'msg', 'aaa.txt',);
6464
expect(callback).toHaveBeenCalledWith(null, await task);
6565
});
6666

6767
it('multi message, single file, no options with callback', async () => {
6868
const task = git.commit(['aaa', 'bbb'], 'aaa.txt', callback);
6969
await closeWithSuccess();
70-
assertExecutedCommands('commit', '-m', 'aaa', '-m', 'bbb', 'aaa.txt');
70+
assertExecutedCommands('-c', 'core.abbrev=40', 'commit', '-m', 'aaa', '-m', 'bbb', 'aaa.txt');
7171
expect(callback).toHaveBeenCalledWith(null, await task);
7272
});
7373

7474
it('multi message, multi file, no options with callback', async () => {
7575
const task = git.commit(['aaa', 'bbb'], ['a.txt', 'b.txt'], callback);
7676
await closeWithSuccess();
77-
assertExecutedCommands('commit', '-m', 'aaa', '-m', 'bbb', 'a.txt', 'b.txt');
77+
assertExecutedCommands('-c', 'core.abbrev=40', 'commit', '-m', 'aaa', '-m', 'bbb', 'a.txt', 'b.txt');
7878
expect(callback).toHaveBeenCalledWith(null, await task);
7979
});
8080

8181
it('multi message, multi file, options object with callback', async () => {
8282
const task = git.commit(['aaa', 'bbb'], ['a.txt', 'b.txt'], {'--foo': null}, callback);
8383
await closeWithSuccess();
84-
assertExecutedCommands('commit', '-m', 'aaa', '-m', 'bbb', 'a.txt', 'b.txt', '--foo');
84+
assertExecutedCommands('-c', 'core.abbrev=40', 'commit', '-m', 'aaa', '-m', 'bbb', 'a.txt', 'b.txt', '--foo');
8585
expect(callback).toHaveBeenCalledWith(null, await task);
8686
});
8787

@@ -158,7 +158,7 @@ describe('commit', () => {
158158
branch: 'alpha',
159159
root: false
160160
}));
161-
})
161+
});
162162

163163
});
164164

0 commit comments

Comments
 (0)
Please sign in to comment.