Skip to content

Commit 0712f86

Browse files
committed
feat: move log task to separate task builder
1 parent 7f17ce5 commit 0712f86

File tree

4 files changed

+56
-39
lines changed

4 files changed

+56
-39
lines changed

src/git.js

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ const {cleanWithOptionsTask, isCleanOptionsArray} = require('./lib/tasks/clean')
2424
const {commitTask} = require('./lib/tasks/commit');
2525
const {diffSummaryTask} = require('./lib/tasks/diff');
2626
const {fetchTask} = require('./lib/tasks/fetch');
27-
const {logTask, parseLogOptions} = require('./lib/tasks/log');
2827
const {moveTask} = require("./lib/tasks/move");
2928
const {pullTask} = require('./lib/tasks/pull');
3029
const {pushTagsTask} = require('./lib/tasks/push');
@@ -667,38 +666,6 @@ Git.prototype.exec = function (then) {
667666
return this._runTask(task);
668667
};
669668

670-
/**
671-
* Show commit logs from `HEAD` to the first commit.
672-
* If provided between `options.from` and `options.to` tags or branch.
673-
*
674-
* Additionally you can provide options.file, which is the path to a file in your repository. Then only this file will be considered.
675-
*
676-
* To use a custom splitter in the log format, set `options.splitter` to be the string the log should be split on.
677-
*
678-
* Options can also be supplied as a standard options object for adding custom properties supported by the git log command.
679-
* For any other set of options, supply options as an array of strings to be appended to the git log command.
680-
*/
681-
Git.prototype.log = function (options) {
682-
const next = trailingFunctionArgument(arguments);
683-
684-
if (filterString(arguments[0]) && filterString(arguments[1])) {
685-
return this._runTask(
686-
configurationErrorTask(`git.log(string, string) should be replaced with git.log({ from: string, to: string })`),
687-
next
688-
);
689-
}
690-
691-
const parsedOptions = parseLogOptions(
692-
trailingOptionsArgument(arguments) || {},
693-
filterArray(options) && options || []
694-
);
695-
696-
return this._runTask(
697-
logTask(parsedOptions.splitter, parsedOptions.fields, parsedOptions.commands),
698-
next,
699-
)
700-
};
701-
702669
/**
703670
* Clears the queue of pending commands and returns the wrapper instance for chaining.
704671
*

src/lib/simple-git-api.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { changeWorkingDirectoryTask } from './tasks/change-working-directory';
44
import config from './tasks/config';
55
import { hashObjectTask } from './tasks/hash-object';
66
import { initTask } from './tasks/init';
7+
import log from './tasks/log';
78
import { mergeTask } from './tasks/merge';
89
import { pushTask } from './tasks/push';
910
import { statusTask } from './tasks/status';
@@ -121,4 +122,4 @@ export class SimpleGitApi implements SimpleGitBase {
121122
}
122123
}
123124

124-
Object.assign(SimpleGitApi.prototype, config());
125+
Object.assign(SimpleGitApi.prototype, config(), log());

src/lib/tasks/log.ts

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
import { Options, StringTask } from '../types';
2-
import { LogResult } from '../../../typings';
2+
import { LogResult, SimpleGit } from '../../../typings';
33
import {
44
COMMIT_BOUNDARY,
55
createListLogSummaryParser,
66
SPLITTER,
77
START_BOUNDARY
88
} from '../parsers/parse-list-log-summary';
9-
import { appendTaskOptions } from '../utils';
9+
import {
10+
appendTaskOptions,
11+
filterArray,
12+
filterString,
13+
filterType,
14+
trailingFunctionArgument,
15+
trailingOptionsArgument
16+
} from '../utils';
17+
import { SimpleGitApi } from '../simple-git-api';
18+
import { configurationErrorTask } from './task';
1019

1120
enum excludeOptions {
1221
'--pretty',
@@ -45,7 +54,13 @@ export type LogOptions<T = DefaultLogFields> = {
4554
to?: string;
4655
};
4756

48-
function prettyFormat(format: {[key: string]: string | unknown}, splitter: string): [string[], string] {
57+
interface ParsedLogOptions {
58+
fields: string[];
59+
splitter: string;
60+
commands: string[]
61+
}
62+
63+
function prettyFormat(format: { [key: string]: string | unknown }, splitter: string): [string[], string] {
4964
const fields: string[] = [];
5065
const formatStr: string[] = [];
5166

@@ -69,7 +84,7 @@ function userOptions<T>(input: T): Exclude<Omit<T, keyof typeof excludeOptions>,
6984
return output;
7085
}
7186

72-
export function parseLogOptions<T extends Options>(opt: LogOptions<T> = {}, customArgs: string[] = []) {
87+
export function parseLogOptions<T extends Options>(opt: LogOptions<T> = {}, customArgs: string[] = []): ParsedLogOptions {
7388
const splitter = opt.splitter || SPLITTER;
7489
const format = opt.format || {
7590
hash: '%H',
@@ -91,7 +106,7 @@ export function parseLogOptions<T extends Options>(opt: LogOptions<T> = {}, cust
91106

92107
const maxCount: number | undefined = (opt as any).n || (opt as any)['max-count'] || opt.maxCount;
93108
if (maxCount) {
94-
command.push(`--max-count=${ maxCount }`);
109+
command.push(`--max-count=${maxCount}`);
95110
}
96111

97112
if (opt.from && opt.to) {
@@ -122,3 +137,27 @@ export function logTask<T>(splitter: string, fields: string[], customArgs: strin
122137
parser: createListLogSummaryParser(splitter, fields),
123138
};
124139
}
140+
141+
export default function (): Pick<SimpleGit, 'log'> {
142+
return {
143+
log<T extends Options>(this: SimpleGitApi, ...rest: unknown[]) {
144+
const next = trailingFunctionArgument(arguments);
145+
const task = rejectDeprecatedSignatures(...rest) ||
146+
createLogTask(parseLogOptions<T>(trailingOptionsArgument(arguments), filterType(arguments[0], filterArray)))
147+
148+
return this._runTask(task, next);
149+
}
150+
}
151+
152+
function createLogTask(options: ParsedLogOptions) {
153+
return logTask(options.splitter, options.fields, options.commands);
154+
}
155+
156+
function rejectDeprecatedSignatures(from?: unknown, to?: unknown) {
157+
return (
158+
filterString(from) &&
159+
filterString(to) &&
160+
configurationErrorTask(`git.log(string, string) should be replaced with git.log({ from: string, to: string })`)
161+
);
162+
}
163+
}

test/unit/log.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
import { promiseError } from '@kwsites/promise-result';
12
import { LogResult, SimpleGit } from 'typings';
23
import {
34
assertExecutedCommands,
45
assertExecutedCommandsContains,
6+
assertGitError,
7+
assertNoExecutedTasks,
58
closeWithSuccess,
69
like,
710
newSimpleGit
811
} from './__fixtures__';
12+
import { TaskConfigurationError } from '../..'
913
import {
1014
COMMIT_BOUNDARY,
1115
createListLogSummaryParser,
@@ -522,6 +526,12 @@ ${START_BOUNDARY}207601debebc170830f2921acf2b6b27034c3b1f::2016-01-03 15:50:58 +
522526
});
523527

524528
describe('deprecations', () => {
529+
it('rejects from and to as strings', async () => {
530+
const queue = promiseError((git.log as any)('FROM', 'TO'));
531+
assertGitError(await queue, 'should be replaced with', TaskConfigurationError);
532+
assertNoExecutedTasks();
533+
});
534+
525535
it('supports ListLogSummary without generic type', async () => {
526536
const summary: Promise<LogResult> = git.log({from: 'from', to: 'to'});
527537
await closeWithSuccess();

0 commit comments

Comments
 (0)