Is there any api to get all changed file names in a merge request?

I’ve not found any, although I’ve managed to derive the info in the script for a build job. This basically fetches a copy of the target branch from the target repo, computes the fork-point for the branch on which the merge request is anchored, and then captures a list of changed files.

somejob:
  script:
  - if [[ -n "$CI_MERGE_REQUEST_TARGET_BRANCH_NAME" ]]; then git fetch --verbose $CI_MERGE_REQUEST_PROJECT_URL +$CI_MERGE_REQUEST_TARGET_BRANCH_NAME:target_branch; git tag fork_point $(git merge-base target_branch $CI_COMMIT_SHA); fi
  - export CHANGED_FILES="`git diff --name-only fork_point $CI_COMMIT_SHA`"
  - echo $CHANGED_FILES

It’s possible that the list of changed files could exceed the size of a variable, so I would consider using a shell construct that can take a file list on stdin such as while read file; do ...; done or xargs.

somejob:
  script:
  - if [[ -n "$CI_MERGE_REQUEST_TARGET_BRANCH_NAME" ]]; then git fetch --verbose $CI_MERGE_REQUEST_PROJECT_URL +$CI_MERGE_REQUEST_TARGET_BRANCH_NAME:target_branch; git tag fork_point $(git merge-base target_branch $CI_COMMIT_SHA); fi
  - git diff --name-only fork_point $CI_COMMIT_SHA | while read changedfile; do echo $changedfile; done

You might also run into credential issues if the target repository is different or is non-public.

It would be nice if GITLAB could just tell us this information since it already has to compute it.

1 Like