【问题标题】:Azure DevOps YAML pipeline - git diff not working correctlyAzure DevOps YAML 管道 - git diff 无法正常工作
【发布时间】:2022-01-30 21:14:48
【问题描述】:

我正在我的 Azure DevOps YAML 管道中执行一个 git 命令,以通过 PowerShell 脚本获取两个提交之间的更改,例如 git diff commit1 commit2 --name-only - 请参阅下面的完整代码:

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'"
}

这会引发错误,声称未找到任何文件。 但是,如果我在本地执行相同的 git diff 命令,我也同步了存储库,它会按预期返回更改的文件。

我是否需要手动更新已通过管道的签出步骤下载的存储库?

【问题讨论】:

    标签: git azure-devops azure-pipelines azure-pipelines-yaml azure-git-deployment


    【解决方案1】:

    你可以尝试以这种方式运行“git diff”

    $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
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2023-01-09
      • 2022-11-14
      • 2020-05-23
      • 1970-01-01
      • 2020-01-10
      • 2019-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多