Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List files modified in a pull request within Travis

I would like to list all the files modified in a pull request while on travis.

I tried $(git diff --name-only $TRAVIS_PULL_REQUEST_BRANCH $(git merge-base $TRAVIS_PULL_REQUEST_BRANCH master)) but git doesn't know about TRAVIS_PULL_REQUEST_BRANCH since it's on a forked repo.

I also gave it a try with git rev-list and $TRAVIS_COMMIT_RANGE but it includes also the new commits on the master branch.

Any idea how to get the files that are modified exclusively by the PR?

like image 492
Denis Rouzaud Avatar asked Dec 14 '16 14:12

Denis Rouzaud


2 Answers

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
like image 149
Matthias Kuhn Avatar answered Oct 12 '22 01:10

Matthias Kuhn


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

like image 35
Ronyis Avatar answered Oct 12 '22 02:10

Ronyis