Home > Software engineering >  Resolving git diff error in Azure DevOps pipeline
Resolving git diff error in Azure DevOps pipeline

Time:01-09

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!

CodePudding user response:

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

I can reproduce this issue in my pipeline.

enter image description here

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'

CodePudding user response:

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 [...] -- [...]' ##[error]Cmd.exe exited with code '128'.

  • Related