Reputation: 445
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.
Upvotes: 2
Views: 717
Reputation: 41585
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 :)
Upvotes: 3
Reputation: 174505
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
}
}
Upvotes: 4