简体   繁体   中英

-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. 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

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:

1) R100

2) swaggerAjson

3) swaggerB.json

But it doesn't do that in the Powershell task of Azure pipeline, and gives the whole string each time.

git diff [cid]..[cid] --name-status outputs lines of tab-separated values, so splitting on a normal space won't help you:

$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:

在此处输入图片说明

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM