1

I am trying to get all the committed files on an Azure Build Pipeline. This is the yaml script I'm using:

trigger:
  branches:
    include:
      - swagger_dev
  paths:
    include:
      - swagger_dev/swaggers/*.yaml

variables:
  CLIENT_CREDENTIALS: $(ClientCredentials)

steps:
  - powershell: |
      echo "$(Build.SourceVersion)"
      echo "$(git diff-tree --no-commit-id --diff-filter=d --name-only -r $(Build.SourceVersion))"

When I commit one or more files, the Pipeline correctly echoes the Build.SourceVersion but then echoes an empty output for the git command: pipeline log

How is that possible? I am currently on a branch called swagger_dev and the committed files are in the directory swagger_dev/swaggers. Maybe I should add those informations to the diff-tree command?

5
  • Try: $(git diff HEAD HEAD~ --name-only) - This works for me, but I am not using any build version. Commented Nov 16, 2022 at 12:46
  • Thank you for the answer @DillyB Unfortunately it does not work, I get this error: fatal: ambiguous argument 'HEAD~': unknown revision or path not in the working tree. Use '--' to separate paths from revisions, like this: 'git <command> [<revision>...] -- [<file>...]' Commented Nov 17, 2022 at 7:48
  • The fact that there's no previous commit (HEAD~ is an unknown revision means "there is no previous commit") explains the problem: you have a shallow clone, as directed by your CI system's default settings. Adjust those defaults, if that's possible, to make sure you have at least a depth-2 clone. Commented Nov 22, 2022 at 4:48
  • Once you've fixed that, remember that git diff-tree should generally get two commits or trees (as in @DillyB's answer). Given just one commit ID, Git will use its parent(s). Commented Nov 22, 2022 at 4:51
  • Thank you so much torek, I think this can be the right solution! I am trying to understand how I can avoid the shallow clone on Azure Devops, do you have any advice? Commented Nov 22, 2022 at 5:38

2 Answers 2

2

git diff-tree command requires at least depth 2 to get the changed files.

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

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

You can try to set the fetchDepth to 0 or >2 in YAML Pipeline.

For example:

steps:
    - checkout: self
      fetchDepth: 0

Or you can navigate to YAML Pipeline -> ... -> Triggers -> YAML -> Get sources -> Shallow fetch. You can unselect the option.

enter image description here

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

The bash task which I am using is to check the name of the tfvars file.

- bash: |
              PATH_FILTER="folder1/**/*"
              CHANGED_FILES=$(git diff HEAD HEAD~ --name-only -- `find . -name "*.auto.tfvars"`)
              MATCH_COUNT=0
              MATCH_FILE=""
              FILE_PATH=""
              FILE_NAME=""

              echo "Checking for file changes..."
              for FILE in $CHANGED_FILES
              do
                if [[ $FILE == *$PATH_FILTER* ]]; then
                  MATCH_FILE=${FILE}
                  MATCH_PATH="$(dirname ${FILE})"
                  FILE_NAME="$(basename ${FILE})"
                  echo "MATCH:  ${FILE} changed"
                  MATCH_COUNT=$(($MATCH_COUNT+1))
                else
                  echo "IGNORE: ${FILE} changed"
                fi
              done

1 Comment

Using bash I get a similar message: fatal: bad revision 'HEAD~'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.