admin管理员组

文章数量:1122846

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?

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?

Share Improve this question asked Nov 22, 2024 at 9:52 NeshNesh 1691 silver badge7 bronze badges 2
  • 1 Probably the repository is a shallow clone that does not contain more than the head commit. Se this question for how unshallow. – hlovdal Commented Nov 22, 2024 at 9:59
  • Yep, that was it. If you add that as an answer I'll mark it. – Nesh Commented Nov 22, 2024 at 10:19
Add a comment  | 

1 Answer 1

Reset to default 1

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:

本文标签: Calling git command in Azure Pipeline Powershell writes no outputStack Overflow