|
1 | 1 | import { configurationErrorTask, EmptyTask, straightThroughStringTask } from './task'; |
2 | 2 | import { OptionFlags, Options, StringTask } from '../types'; |
3 | | -import { append, filterString } from '../utils'; |
| 3 | +import { |
| 4 | + append, |
| 5 | + filterString, |
| 6 | + filterType, |
| 7 | + getTrailingOptions, |
| 8 | + trailingFunctionArgument, |
| 9 | +} from '../utils'; |
| 10 | +import { pathspec } from '../args/pathspec'; |
| 11 | +import { SimpleGit } from '../../../typings'; |
| 12 | +import { SimpleGitApi } from '../simple-git-api'; |
4 | 13 |
|
5 | 14 | export type CloneOptions = Options & |
6 | 15 | OptionFlags< |
@@ -29,34 +38,53 @@ export type CloneOptions = Options & |
29 | 38 | string |
30 | 39 | >; |
31 | 40 |
|
32 | | -function disallowedCommand(command: string) { |
33 | | - return /^--upload-pack(=|$)/.test(command); |
34 | | -} |
35 | | - |
36 | | -export function cloneTask( |
| 41 | +type CloneTaskBuilder = ( |
37 | 42 | repo: string | undefined, |
38 | 43 | directory: string | undefined, |
39 | 44 | customArgs: string[] |
40 | | -): StringTask<string> | EmptyTask { |
41 | | - const commands = ['clone', ...customArgs]; |
| 45 | +) => StringTask<string> | EmptyTask; |
42 | 46 |
|
43 | | - filterString(repo) && commands.push(repo); |
44 | | - filterString(directory) && commands.push(directory); |
| 47 | +export const cloneTask: CloneTaskBuilder = (repo, directory, customArgs) => { |
| 48 | + const commands = ['clone', ...customArgs]; |
45 | 49 |
|
46 | | - const banned = commands.find(disallowedCommand); |
47 | | - if (banned) { |
48 | | - return configurationErrorTask(`git.fetch: potential exploit argument blocked.`); |
49 | | - } |
| 50 | + filterString(repo) && commands.push(pathspec(repo)); |
| 51 | + filterString(directory) && commands.push(pathspec(directory)); |
50 | 52 |
|
51 | 53 | return straightThroughStringTask(commands); |
52 | | -} |
| 54 | +}; |
53 | 55 |
|
54 | | -export function cloneMirrorTask( |
55 | | - repo: string | undefined, |
56 | | - directory: string | undefined, |
57 | | - customArgs: string[] |
58 | | -) { |
| 56 | +export const cloneMirrorTask: CloneTaskBuilder = (repo, directory, customArgs) => { |
59 | 57 | append(customArgs, '--mirror'); |
60 | 58 |
|
61 | 59 | return cloneTask(repo, directory, customArgs); |
| 60 | +}; |
| 61 | + |
| 62 | +function createCloneTask( |
| 63 | + api: 'clone' | 'mirror', |
| 64 | + task: CloneTaskBuilder, |
| 65 | + repoPath: string | undefined, |
| 66 | + ...args: unknown[] |
| 67 | +) { |
| 68 | + if (!filterString(repoPath)) { |
| 69 | + return configurationErrorTask(`git.${api}() requires a string 'repoPath'`); |
| 70 | + } |
| 71 | + |
| 72 | + return task(repoPath, filterType(args[0], filterString), getTrailingOptions(arguments)); |
| 73 | +} |
| 74 | + |
| 75 | +export default function (): Pick<SimpleGit, 'clone' | 'mirror'> { |
| 76 | + return { |
| 77 | + clone(this: SimpleGitApi, repo: string | unknown, ...rest: unknown[]) { |
| 78 | + return this._runTask( |
| 79 | + createCloneTask('clone', cloneTask, filterType(repo, filterString), ...rest), |
| 80 | + trailingFunctionArgument(arguments) |
| 81 | + ); |
| 82 | + }, |
| 83 | + mirror(this: SimpleGitApi, repo: string | unknown, ...rest: unknown[]) { |
| 84 | + return this._runTask( |
| 85 | + createCloneTask('mirror', cloneMirrorTask, filterType(repo, filterString), ...rest), |
| 86 | + trailingFunctionArgument(arguments) |
| 87 | + ); |
| 88 | + }, |
| 89 | + }; |
62 | 90 | } |
0 commit comments