Skip to content

Commit

Permalink
fix(core): import handles argument escaping correctly in Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysoo committed Sep 17, 2024
1 parent 2be7424 commit 7e3588c
Showing 1 changed file with 31 additions and 15 deletions.
46 changes: 31 additions & 15 deletions packages/nx/src/utils/git-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,20 +166,29 @@ export class GitRepository {
const destinationPosixPath = destination.split(sep).join(posix.sep);
// First, if the source is not a root project, then only include commits relevant to the subdirectory.
if (source !== '') {
const indexFilterCommand = this.quoteArg(
`node ${join(__dirname, 'git-utils.index-filter.js')
// Need to keep two slashes for Windows or else the path will be invalid.
// e.g. 'C:\Users\bob\projects\repo' is invalid, but 'C:\\Users\\bob\\projects\\repo' is valid
.replace('\\', '\\\\')} "${sourcePosixPath}"`
);
await this.execAsync(
`git filter-branch -f --index-filter 'node ${join(
__dirname,
'git-utils.index-filter.js'
)} "${sourcePosixPath}"' --prune-empty -- ${branchName}`
`git filter-branch -f --index-filter ${indexFilterCommand} --prune-empty -- ${branchName}`
);
}
// Then, move files to their new location if necessary.
if (source === '' || source !== destination) {
const treeFilterCommand = this.quoteArg(
`node ${join(__dirname, 'git-utils.tree-filter.js')
// Need to keep two slashes for Windows or else the path will be invalid.
// e.g. 'C:\Users\bob\projects\repo' is invalid, but 'C:\\Users\\bob\\projects\\repo' is valid
.replace(
'\\',
'\\\\'
)} "${sourcePosixPath}" "${destinationPosixPath}"`
);
await this.execAsync(
`git filter-branch -f --tree-filter 'node ${join(
__dirname,
'git-utils.tree-filter.js'
)} "${sourcePosixPath}" "${destinationPosixPath}"' -- ${branchName}`
`git filter-branch -f --tree-filter ${treeFilterCommand} -- ${branchName}`
);
}
}
Expand All @@ -191,18 +200,25 @@ export class GitRepository {
});
}

private quotePath(_path: string, ensureTrailingSlash?: true) {
const path =
ensureTrailingSlash && _path !== '' && !_path.endsWith('/')
? `${_path}/`
: _path;
private quotePath(path: string, ensureTrailingSlash?: true) {
return this.quoteArg(
ensureTrailingSlash && path !== '' && !path.endsWith('/')
? `${path}/`
: path
);
}

private quoteArg(arg: string) {
return process.platform === 'win32'
? // Windows/CMD only understands double-quotes, single-quotes are treated as part of the file name
// Bash and other shells will substitute `$` in file names with a variable value.
`"${path}"`
`"${
// For Windows, double-quotes must be escaped by using two double-quotes.
arg.replace('"', '""')
}"`
: // e.g. `git mv "$$file.txt" "libs/a/$$file.txt"` will not work since `$$` is swapped with the PID of the last process.
// Using single-quotes prevents this substitution.
`'${path}'`;
`'${arg}'`;
}
}

Expand Down

0 comments on commit 7e3588c

Please sign in to comment.