[英]-split ' ' that works locally in Powershell does not work in a Azure pipeline Powershell task
I am using git diff command to get the file that changed between the last 2 commits and based on the action have to do different things with the files.我正在使用 git diff 命令来获取在最后 2 次提交之间更改的文件,并且根据操作必须对文件执行不同的操作。 So I have to extract the action on the file and the filename.所以我必须提取对文件和文件名的操作。
For a renamed file:对于重命名的文件:
The result of git diff command is the below which is a renamed swagger file from swaggerA.json
to swaggerB.json
git diff 命令的结果如下,这是一个从swaggerA.json
到swaggerB.json
的重命名的 swagger 文件
R100 swaggerA.json swaggerB.json R100 swaggerA.json swaggerB.json
This is my code:这是我的代码:
$files=$(git diff HEAD~ HEAD --name-status)
$temp=$files -split ' '
echo $temp
echo $temp.Length
$name=$temp[1]
echo "this is $name file"
write-host $name
$output=$name.Split(' ')
$length=$output.Length
$output[0]
$output[1]
$output[$length-1]
The above script should've ideally split and provided 3 parts as:理想情况下,上面的脚本应该已经拆分并提供了 3 个部分:
1) R100 1) R100
2) swaggerAjson 2)招摇Ajson
3) swaggerB.json 3) swaggerB.json
But it doesn't do that in the Powershell task of Azure pipeline, and gives the whole string each time.但是它在 Azure 管道的 Powershell 任务中并没有这样做,并且每次都给出整个字符串。
git diff [cid]..[cid] --name-status
outputs lines of tab-separated values, so splitting on a normal space won't help you: git diff [cid]..[cid] --name-status
输出制表符分隔值的行,因此在正常空间上拆分对您没有帮助:
$changes = git diff HEAD~ HEAD --name-status |ForEach-Object {
# split on tab "`t"
$change,$orig,$new = $_ -split "`t"
[pscustomobject]@{
Change = $change
Original = $orig
Current = $new
}
}
Because the gid diff
returnes a string with hidden ASCII symbol ( `t
), if we copy the results to Notepad++ (and in the settings we press on 'Show all characters') we can see it:因为gid diff
返回一个带有隐藏 ASCII 符号 ( `t
) 的字符串,如果我们将结果复制到 Notepad++(并在设置中按下“显示所有字符”),我们可以看到它:
So we need to replace it with regular space before the split:所以我们需要在拆分之前用常规空间替换它:
$files = $files -replace '[^\p{L}\p{Nd}/(/./_]', ' '
Now it will work and you will get the string splitted :)现在它将起作用,您将分割字符串:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.