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?

Edoardo
  • 13
  • 5
  • Try: $(git diff HEAD HEAD~ --name-only) - This works for me, but I am not using any build version. – Dilly B Nov 16 '22 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 [...] -- [...]' – Edoardo Nov 17 '22 at 07: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. – torek Nov 22 '22 at 04: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). – torek Nov 22 '22 at 04: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? – Edoardo Nov 22 '22 at 05:38

2 Answers2

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

Kevin Lu-MSFT
  • 20,786
  • 3
  • 19
  • 28
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
Dilly B
  • 1,280
  • 2
  • 11
  • 15