解决Azure DevOps管道中的git diff错误。

huangapple go评论81阅读模式
英文:

Resolving git diff error in Azure DevOps pipeline

问题

我目前正在构建流水线的CI部分。我创建了一个功能分支:

git branch feature/123
git checkout feature/123

对一些文件进行了一些更改...

git add .
git commit -m "changes"

现在我可以使用以下命令查看差异:

git diff HEAD^1 HEAD --name-only

在我的Visual Studio中,它会返回已更改的文件。这正是我想要的。

但是,无论我从Azure托管的机器上运行相同的git diff命令多少次(在检出存储库之后),我都会收到以下错误:

fatal: ambiguous argument 'HEAD^1': unknown revision or path not in the working tree.

这是我的CI流水线的样子:

stages:
  - stage: code_checks
    jobs:         
      - job: artifacts_validation_and_requirements
        steps:
          - checkout: 'self'
            submodules: 'true'
            persistCredentials: true
          - script: |
              git diff --name-only --diff-filter=AMR HEAD^1 HEAD 
            displayName: 'Get Changes'

我不知道为什么这在我的CI流水线中不起作用,但在我的本地机器上起作用。

是否有人可以指导我正确的方向?

提前感谢!

英文:

I'm currently building the CI part of the pipeline. I've created a feature branch:

git branch feature/123
git checkout feature/123

Made some changes to some files...

git add .
git commit -m "changes"

Now I can see the differences using the following command:

git diff HEAD^1 HEAD --name-only

Locally, in my visual studio, it returns the files changed. This is exactly what I want

But, whenever I run the exact same git diff command from my hosted machine in Azure (after checking out the repo, ofcourse). I get the following error:

fatal: ambiguous argument 'HEAD^1': unknown revision or path not in the working tree.

This is what my CI pipeline looks like:

stages:
  - stage: code_checks
    jobs:         
      - job: artifacts_validation_and_requirements
        steps:
          - checkout: 'self'
            submodules: 'true'
            persistCredentials: true
          - script: |
              git diff --name-only --diff-filter=AMR HEAD^1 HEAD 
            displayName: 'Get Changes'

I have no idea why this doesn't work in my CI pipeline but does work in on my local machine.

Could anyone point me in the right direction?

Thanks in advance!

答案1

得分: 3

>fatal: ambiguous argument 'HEAD^1': unknown revision or path not in the working tree.

我可以在我的流水线中重现这个问题。

[![enter image description here][1]][1]

这个问题的原因可能与流水线存储库的获取深度有关。

默认情况下,流水线存储库的浅获取深度为1。

要解决这个问题,您可以尝试在 YAML 流水线中将 **fetchDepth** 设置为 **0**。

    stages:
      - stage: code_checks
        jobs:         
          - job: artifacts_validation_and_requirements
            steps:
              - checkout: 'self'
                submodules: 'true'
                fetchDepth: 0
                persistCredentials: true
              - script: |
                  git diff --name-only --diff-filter=AMR HEAD^1 HEAD 
                displayName: 'Get Changes'


  [1]: https://i.stack.imgur.com/s4Uyv.png
英文:

>fatal: ambiguous argument 'HEAD^1': unknown revision or path not in the working tree.

I can reproduce this issue in my pipeline.

解决Azure DevOps管道中的git diff错误。

The cause of the issue could be related to the fetch depth of the Pipeline repo.

By default, the Shallow fetch of the pipeline repo is 1 by default.

To solve this issue, you can try to set the fetchDepth to 0 in YAML Pipeline.

stages:
  - stage: code_checks
    jobs:         
      - job: artifacts_validation_and_requirements
        steps:
          - checkout: 'self'
            submodules: 'true'
            fetchDepth: 0
            persistCredentials: true
          - script: |
              git diff --name-only --diff-filter=AMR HEAD^1 HEAD 
            displayName: 'Get Changes'

答案2

得分: 0

我正在尝试使用此命令来获取当前拉取请求(PR)与主分支之间的所有更改,这将在流水线中运行。

基本上是想要获取当前PR中添加的文件列表。

这在命令行上确实有效,但即使在更改提取深度后,我仍然收到以下错误:

致命错误:模糊参数 'main':未知的修订或不在工作树中的路径。
使用 '--' 将路径与修订分开,就像这样:
'git [...] -- [...]'
##[error]Cmd.exe 退出代码 '128'。

英文:

I am trying to use this command to get all changes between the current PR which would be run in a pipeline and the main branch.

Essentially trying to get a list of files added in the current PR.

This does work on command line but even after changing the fetch depth I still get the error:

fatal: ambiguous argument 'main': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
##[error]Cmd.exe exited with code '128'.

huangapple
  • 本文由 发表于 2023年1月9日 17:13:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75055130.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定