'Azure DevOps YAML pipeline - git diff not working correctly
I am executing a git command in my Azure DevOps YAML pipeline to get changes between two commits like git diff commit1 commit2 --name-only via a PowerShell script - see full code below:
Write-Information "git diff --name-only $git_event_before $git_event_after --diff-filter=ACM ""*.txt"""
$txt_files = @($(git diff --name-only $git_event_before $git_event_after --diff-filter=ACM "*.txt"))
$txt_files = $txt_files | ForEach-Object { Join-Path $root_path $_ | Get-Item }
if ($txt_files.Count -eq 0) {
Write-Error "Something went wrong, could not find any changed .txt files using the above 'git diff'"
}
This throws an error claiming no files were found.
However, if I execute the very same git diff command locally where I do also have the repository synced, it returns the changed files as expected.
Do I need to manually update the repository that is already downloaded by the checkout-step of the pipeline ?
Solution 1:[1]
Could you try to run "git diff" this way
$gitDiffCmd = 'diff "origin/{0}" "origin/{0}@{{1}}" --name-only' -f $buildSourceBranch
$gitCommitFiles = Invoke-Git $gitDiffCmd
function Invoke-Git {
[CmdletBinding()]
[OutputType([string])]
param (
[Parameter(Mandatory)]
[string] $Command
)
try {
$old_env = $env:GIT_REDIRECT_STDERR
$env:GIT_REDIRECT_STDERR = '2>&1'
$response = Invoke-Expression "git $Command "
return $response
} catch {
Write-Host $_.ScriptStackTrace
} finally {
$env:GIT_REDIRECT_STDERR = $old_env
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Aaron |
