I’m having a challenge to make git diff
work in a Azure DevOps pipeline environment. Here is the pipeline script to compare the git HEAD with previous commit and only copy the files that have been changed. The git command runs fine locally, but it will fail in Azure pipeline because there is no HEAD^
in a CI run. Looking for suggestions if such comparison is feasible in Azure and how may I achieve that.
- task: PowerShell@2
inputs:
targetType: 'inline'
script: |
$targetfolder = "$(Build.StagingDirectory)" + "/"
function CopyFiles{
param( [string]$source )
$target = $targetfolder + $source
New-Item -Force $target
copy-item $source $target -Force
}
$changes = git diff --name-only --relative --diff-filter AMR HEAD^ HEAD .
if ($changes -is [string]){ CopyFiles $changes }
else
{
if ($changes -is [array])
{
foreach ($change in $changes){ CopyFiles $change }
}
}
The error message looks like this:
I think in the Azure pipeline setup, when it checks out the reposition it only pulls the latest commit without the full commit history. That's why when git tries to get previous commit like HEAD^
, it couldn't find anything.
Thanks a lot in advance for help!
HEAD^
with an actual commit ID. It still failed to find such a reference.