List files modified in a pull request within Travis
You are almost there.
There is no reason to use the environment variable $TRAVIS_PULL_REQUEST_BRANCH
in this scenario.
The source tree within travis will be your pull request merged back into the most recent target branch ($TRAVIS_BRANCH
, likely master
). That is, HEAD
is your pull request merged into $TRAVIS_BRANCH
.
What you want are only the changes between your pull request and master
which can be retrieved with
$(git diff --name-only HEAD $(git merge-base HEAD $TRAVIS_BRANCH))
or shorter with the dot-dot-dot notation
git diff --name-only HEAD...$TRAVIS_BRANCH
Thanks @ostrokach
It's also interesting to only take the modified and added files into account (and ignore deleted files) if you want to check the content
git diff --name-only --diff-filter=AM HEAD...$TRAVIS_BRANCH
You should use $TRAVIS_BRANCH
as in the following command:
git diff --name-only HEAD...$TRAVIS_BRANCH
(as suggested by @Matthias Kuhn). But first, execute the following
$ git remote set-branches --add origin $TRAVIS_BRNACH
$ git fetch
$ git diff origin/$TRAVIS_BRANCH # now works as normal
This is needed to fix the following error:
fatal: ambiguous argument 'HEAD...master': unknown revision or path not in the working tree.
(as described in https://github.com/travis-ci/travis-ci/issues/6069).
Using $TRAVIS_COMMIT_RANGE
is also possible, but in case that
there changes in the destination branch of the pull request, they will be included as well. If this case is not likely in your case (for example, when only a single pull request to this branch can be created at a time), so this solution is easier.
The command is:
git diff --name-only $TRAVIS_COMMIT_RANGE