Skip to content

Commit 2adf47d

Browse files
steveukxfelipecrs
andauthored
Pr/1047 (#1049)
* Allow passing repeated flags to git commands * Add changeset * Add options as array example to README * Filter options as array with `filterPrimitives` * Simplify with a for-of * Fix log.ts * Format * Update `@types/node`, remove the `typescript` resolution override, target es2018 to cater for new regexp format. Update numstat and abort integration tests to be more robust --------- Co-authored-by: Felipe Santos <felipecassiors@gmail.com>
1 parent 34c5eba commit 2adf47d

File tree

14 files changed

+90
-36
lines changed

14 files changed

+90
-36
lines changed

.changeset/tame-zoos-search.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+
Allow repeating git options like `{'--opt': ['value1', 'value2']}`

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"simple-git"
88
],
99
"resolutions": {
10-
"typescript": "4.7.4",
1110
"jest": "29.7.0"
1211
},
1312
"scripts": {

packages/test-typescript-consumer/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"directory": "packages/test-typescript-consumer"
2626
},
2727
"devDependencies": {
28-
"jest": "^29.7.0"
28+
"jest": "^29.7.0",
29+
"typescript": "^5.7.3"
2930
}
3031
}

simple-git/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"@simple-git/babel-config": "^1.0.0",
2424
"@types/debug": "^4.1.12",
2525
"@types/jest": "^29.2.2",
26-
"@types/node": "^22.13.5",
26+
"@types/node": "^22.15.30",
2727
"esbuild": "^0.25.0",
2828
"esbuild-node-externals": "^1.18.0",
2929
"jest": "^29.7.0",

simple-git/readme.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,14 @@ git.pull('origin', 'master', { '--no-rebase': null });
436436
git.pull('origin', 'master', { '--rebase': 'true' });
437437
```
438438

439+
When the value of the property is an array of `string`s or `number`s, each element will be
440+
included as separate `name=value` pairs:
441+
442+
```javascript
443+
// results in 'git log --grep=bug --grep=fix --grep=feature'
444+
git.log({ '--grep': ['bug', 'fix', 'feature'] });
445+
```
446+
439447
### Options as an Array
440448

441449
Options can also be supplied as an array of strings to be merged into the task's commands

simple-git/src/lib/tasks/log.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
import {
1212
appendTaskOptions,
1313
filterArray,
14-
filterPrimitives,
14+
filterPlainObject,
1515
filterString,
1616
filterType,
1717
trailingFunctionArgument,
@@ -95,18 +95,17 @@ export function parseLogOptions<T extends Options>(
9595
customArgs: string[] = []
9696
): ParsedLogOptions {
9797
const splitter = filterType(opt.splitter, filterString, SPLITTER);
98-
const format =
99-
!filterPrimitives(opt.format) && opt.format
100-
? opt.format
101-
: {
102-
hash: '%H',
103-
date: opt.strictDate === false ? '%ai' : '%aI',
104-
message: '%s',
105-
refs: '%D',
106-
body: opt.multiLine ? '%B' : '%b',
107-
author_name: opt.mailMap !== false ? '%aN' : '%an',
108-
author_email: opt.mailMap !== false ? '%aE' : '%ae',
109-
};
98+
const format = filterPlainObject(opt.format)
99+
? opt.format
100+
: {
101+
hash: '%H',
102+
date: opt.strictDate === false ? '%ai' : '%aI',
103+
message: '%s',
104+
refs: '%D',
105+
body: opt.multiLine ? '%B' : '%b',
106+
author_name: opt.mailMap !== false ? '%aN' : '%an',
107+
author_email: opt.mailMap !== false ? '%aE' : '%ae',
108+
};
110109

111110
const [fields, formatStr] = prettyFormat(format, splitter);
112111

simple-git/src/lib/types/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export type TaskOptions<O extends Options = Options> = string[] | O;
1717
/**
1818
* Options supplied in most tasks as an optional trailing object
1919
*/
20-
export type OptionsValues = null | string | number;
20+
export type OptionsValues = null | string | number | (string | number)[];
2121
export type Options = Record<string, OptionsValues>;
2222

2323
export type OptionFlags<FLAGS extends string, VALUE = null> = Partial<Record<FLAGS, VALUE>>;

simple-git/src/lib/utils/task-options.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
filterType,
77
} from './argument-filters';
88
import { asFunction, isUserFunction, last } from './util';
9-
import { Maybe, Options, OptionsValues } from '../types';
9+
import { Maybe, Options } from '../types';
1010
import { isPathSpec } from '../args/pathspec';
1111

1212
export function appendTaskOptions<T extends Options = Options>(
@@ -18,12 +18,18 @@ export function appendTaskOptions<T extends Options = Options>(
1818
}
1919

2020
return Object.keys(options).reduce((commands: string[], key: string) => {
21-
const value: OptionsValues = options[key];
21+
const value = options[key];
2222

2323
if (isPathSpec(value)) {
2424
commands.push(value);
2525
} else if (filterPrimitives(value, ['boolean'])) {
2626
commands.push(key + '=' + value);
27+
} else if (Array.isArray(value)) {
28+
for (const v of value) {
29+
if (!filterPrimitives(v, ['string', 'number'])) {
30+
commands.push(key + '=' + v);
31+
}
32+
}
2733
} else {
2834
commands.push(key);
2935
}

simple-git/src/lib/utils/util.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1+
import { Buffer } from 'node:buffer';
12
import { exists, FOLDER } from '@kwsites/file-exists';
23
import { Maybe } from '../types';
34

5+
type Callable = (...args: unknown[]) => unknown;
6+
47
export const NULL = '\0';
58

6-
export const NOOP: (...args: any[]) => void = () => {};
9+
export const NOOP: Callable = () => {};
710

811
/**
912
* Returns either the source argument when it is a `Function`, or the default
1013
* `NOOP` function constant
1114
*/
12-
export function asFunction<T extends () => any>(source: T | any): T {
13-
return typeof source === 'function' ? source : NOOP;
15+
export function asFunction<T>(source: T | unknown): Callable {
16+
if (typeof source !== 'function') {
17+
return NOOP;
18+
}
19+
return source as Callable;
1420
}
1521

1622
/**

simple-git/test/integration/log-numstat.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('log-numstat', function () {
2020
ae: '%ae',
2121
},
2222
'--all': null,
23-
'--since': '2024-02-04',
23+
'--since': '2025-01-01',
2424
'--numstat': null,
2525
});
2626

0 commit comments

Comments
 (0)