In an Azure Pipeline I want to run git diff-tree
on the commits in the push that triggered the pipeline. In a Powershell task and using the Azure DevOps Services REST API I am able to get all the commit IDs for the current push.
Then, in that task I run & git diff-tree --no-commit-id --name-only $commitId -r
, but I get no output from this command.
I have tried to call it in various ways:
$files = @()
foreach ($commitId in $commitIds)
{
Write-Host "Commit id: $commitId"
$files += @(& git diff-tree --no-commit-id --name-only $commitId -r)
Write-Host "Last exit code: $LASTEXITCODE"
}
$files = @()
foreach ($commitId in $commitIds)
{
Write-Host "Commit id: $commitId"
& git diff-tree --no-commit-id --name-only $commitId -r > "$commitId.txt"
Write-Host "Last exit code: $LASTEXITCODE"
$files += @(Get-Content "$commitId.txt")
}
$files = @()
foreach ($commitId in $commitIds)
{
Write-Host "Commit id: $commitId"
$files += @(Invoke-Expression "git diff-tree --no-commit-id --name-only $commitId -r")
Write-Host "Last exit code: $LASTEXITCODE"
}
All of these work locally, but when run in the Azure Pipeline it seems like nothing happens. $LASTEXITCODE
is always 0. Why does this not work?
git diff-tree command requires at least depth 2 to get the changed files.
The cause of the issue is related to the fetch depth of the Pipeline repo.
By default, the Shallow fetch of the pipeline repo is 1 by default. Refer to this doc: Shallow fetch
New pipelines created after the September 2022 Azure DevOps sprint 209 update have Shallow fetch enabled by default and configured with a depth of 1. Previously the default was not to shallow fetch.
You can try to set the fetchDepth to 0 or >2 in YAML Pipeline.
For example:
YAML Pipeline:
steps:
- checkout: self
fetchDepth: 0
Classic Pipeline: