Skip to content

Commit f4b07e6

Browse files
committed
fix: improve git args parsing
1 parent 138434b commit f4b07e6

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

lib/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,15 @@ import {
99
import path from 'path'
1010
import simpleGit, { Response } from 'simple-git'
1111
import YAML from 'js-yaml'
12-
import { getInput, Input, log, outputs, parseBool, setOutput } from './util'
12+
import {
13+
getInput,
14+
Input,
15+
log,
16+
matchGitArgs,
17+
outputs,
18+
parseBool,
19+
setOutput
20+
} from './util'
1321

1422
const baseDir = path.join(process.cwd(), getInput('cwd') || '')
1523
const git = simpleGit({ baseDir })
@@ -90,7 +98,7 @@ console.log(`Running in ${baseDir}`)
9098
if (getInput('tag')) {
9199
info('> Tagging commit...')
92100
await git
93-
.tag(getInput('tag').split(' '), (err, data?) => {
101+
.tag(matchGitArgs(getInput('tag')), (err, data?) => {
94102
if (data) setOutput('tagged', 'true')
95103
return log(err, data)
96104
})
@@ -122,7 +130,7 @@ console.log(`Running in ${baseDir}`)
122130
await git.push(
123131
undefined,
124132
undefined,
125-
pushOption.split(' '),
133+
matchGitArgs(pushOption),
126134
(err, data?) => {
127135
if (data) setOutput('pushed', 'true')
128136
return log(err, data)
@@ -143,9 +151,9 @@ console.log(`Running in ${baseDir}`)
143151
{
144152
'--delete': null,
145153
origin: null,
146-
[getInput('tag')
147-
.split(' ')
148-
.filter((w) => !w.startsWith('-'))[0]]: null
154+
[matchGitArgs(getInput('tag')).filter(
155+
(w) => !w.startsWith('-')
156+
)[0]]: null
149157
},
150158
log
151159
)
@@ -291,7 +299,7 @@ async function add({ logWarning = true, ignoreErrors = false } = {}): Promise<
291299
// Push the result of every git command (which are executed in order) to the array
292300
// If any of them fails, the whole function will return a Promise rejection
293301
await git
294-
.add(args.split(' '), (err: any, data?: any) =>
302+
.add(matchGitArgs(args), (err: any, data?: any) =>
295303
log(ignoreErrors ? null : err, data)
296304
)
297305
.catch((e: Error) => {
@@ -325,7 +333,7 @@ async function remove({
325333
// Push the result of every git command (which are executed in order) to the array
326334
// If any of them fails, the whole function will return a Promise rejection
327335
await git
328-
.rm(args.split(' '), (e: any, d?: any) =>
336+
.rm(matchGitArgs(args), (e: any, d?: any) =>
329337
log(ignoreErrors ? null : e, d)
330338
)
331339
.catch((e: Error) => {

src/util.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@ export function log(err: any | Error, data?: any) {
2929
if (err) core.error(err)
3030
}
3131

32+
/**
33+
* Matches the different pathspecs and arguments by removing spaces that are not inside quotes
34+
* @example
35+
* ```js
36+
* matchGitArgs(' first second "third" \'fourth\'') => [ 'first', 'second', '"third"', "'fourth'" ]
37+
* matchGitArgs(' ') => [ ]
38+
* ```
39+
* @returns An array, if there's no match it'll be empty
40+
*/
41+
export function matchGitArgs(string: string) {
42+
return string.match(/(?:[^\s"]+|"[^"]*")+/g) || []
43+
}
44+
3245
export function parseBool(value: any) {
3346
try {
3447
const parsed = JSON.parse(value)

0 commit comments

Comments
 (0)