Find the latest commit from `master` that is present in the PR codebase

Reposting this as a new thread from New Build Stages

We’re hitting a problem on the AMP project, where Travis jobs in multiple stages appear to be using the same merge commit (and same commit log) even though origin/master has moved forward.

For details, see the comment at

Would it be possible to expose the commit SHA of origin/master at the start of the first stage of a Travis build via an environment variable, so that it can be used by subsequent stages to establish which files have changed by a pull request? This will allow us to not have to rely on the real origin/master , which is a moving target.

I don’t see what the problem is. What are you trying to achieve?

origin/master is a live repository, nothing prevents it from being altered at any moment – including between the times different jobs run. Travis makes sure as best it can that the same commit is being checked out at every job of the same build – that’s all that matters for the purpose of building stuff.

If you wish to use origin/master commit to label the build or something – too bad for you, that’s not a guarantee you can get. Find a better indicator – like the current commit (available as $TRAVIS_COMMIT) for example which is the standard way to mark non-tag builds for Git-based projects.

In fact, since origin/master lives its own life from the current commit, it’s completely useless as a label. There’s no telling where it will be by the time a build runs and how it will be related to the current commit by that time.

Thanks for the detailed explanation, @native-api! Of course, you’re right about origin/master. I’ll switch our usage to $TRAVIS_COMMIT.

@native-api I tried using $TRAVIS_COMMIT, but it returns the latest commit being tested (which includes the unmerged commits added by the PR author).

The question I’m trying to answer is:

What is the latest commit that was merged to master that is also part of the Travis build?

As you rightly point out, it is not guaranteed to be origin/master, which is a moving target.

In the screenshot above (from this build), I’m looking for a way to determine that 247d7e2 is the latest commit merged to master that is part of the PR build. If origin/master moves forward by the time a subsequent stage begins, I still want to be able to figure out that 247d7e2 was the latest merged commit that’s part of the Travis build. (We use this info to determine which files were changed by the pull request, and therefore, which tests need to be run during the Travis build)

You should have started with that!

git merge-base is for this.

## (showcasing locally)
## same commands as Travis uses to fetch a "merge preview" commit,
##   to emulate a PR build
$ git fetch origin +refs/pull/23467/merge
$ git checkout -qf FETCH_HEAD

$ git rev-parse origin/master
$ git rev-parse HEAD
$ git merge-base origin/master HEAD

Here, the common ancestor is equal to origin/master, which means everything from there is present in the PR.

@native-api, thanks for sticking with me in spite of the roundabout question :slight_smile:

I will try using git merge-base origin/master HEAD and let you know how it goes.

4 posts were split to a new topic: Build stages get different codebase if the PR branch or the base branch changes in the meantime