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

**URL:** <https://travis-ci.community/t/find-the-latest-commit-from-master-that-is-present-in-the-pr-codebase/4189>\
**Category:** Travis CI Discussions & Feedback\
**Tags:** build-env\
**Created:** [July 12, 2019, 5:11pm UTC](https://travis-ci.community/t/find-the-latest-commit-from-master-that-is-present-in-the-pr-codebase/4189 "2019-07-12T17:11:34Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rsimha](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/rsimha/32/1875_2.png) [@rsimha](https://travis-ci.community/u/rsimha)\
**Post date:** [July 12, 2019, 5:11pm UTC](https://travis-ci.community/t/find-the-latest-commit-from-master-that-is-present-in-the-pr-codebase/4189/1 "2019-07-12T17:11:34Z")

</div>

Reposting this as a new thread from [New Build Stages](https://travis-ci.community/t/new-build-stages/65/13)

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 [https://github.com/ampproject/amphtml/issues/21527#issuecomment-496717388](https://github.com/ampproject/amphtml/issues/21527#issuecomment-496717388)

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.

---

<div class="post-metadata">

**Author:** ![native-api](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/native-api/32/430_2.png) [@native-api](https://travis-ci.community/u/native-api)\
**Post date:** [July 12, 2019, 9:57pm UTC](https://travis-ci.community/t/find-the-latest-commit-from-master-that-is-present-in-the-pr-codebase/4189/2 "2019-07-12T21:57:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![native-api](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/native-api/32/430_2.png) [@native-api](https://travis-ci.community/u/native-api)\
**Post date:** [July 12, 2019, 10:02pm UTC](https://travis-ci.community/t/find-the-latest-commit-from-master-that-is-present-in-the-pr-codebase/4189/3 "2019-07-12T22:02:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rsimha](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/rsimha/32/1875_2.png) [@rsimha](https://travis-ci.community/u/rsimha)\
**Post date:** [July 22, 2019, 9:30pm UTC](https://travis-ci.community/t/find-the-latest-commit-from-master-that-is-present-in-the-pr-codebase/4189/4 "2019-07-22T21:30:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rsimha](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/rsimha/32/1875_2.png) [@rsimha](https://travis-ci.community/u/rsimha)\
**Post date:** [July 22, 2019, 9:58pm UTC](https://travis-ci.community/t/find-the-latest-commit-from-master-that-is-present-in-the-pr-codebase/4189/5 "2019-07-22T21:58:47Z")

</div>

@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.

 ![image](https://us1.discourse-cdn.com/flex015/uploads/travis_ci/original/2X/6/62c4e78799a3407089682b2915c5d814e9a081df.png)

In the screenshot above (from [this](https://travis-ci.org/ampproject/amphtml/jobs/562306912#L330) 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)

---

<div class="post-metadata">

**Author:** ![native-api](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/native-api/32/430_2.png) [@native-api](https://travis-ci.community/u/native-api)\
**Post date:** [July 22, 2019, 11:35pm UTC](https://travis-ci.community/t/find-the-latest-commit-from-master-that-is-present-in-the-pr-codebase/4189/6 "2019-07-22T23:35:02Z")

</div>

> [@rsimha](#):
>
> 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?

[You should have started with that!](http://xyproblem.info/)

[`git merge-base`](https://git-scm.com/docs/git-merge-base) is for this.

```bash
## (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
d2f9bae288c2015715b70ce4090c635e18dd063f
$ git rev-parse HEAD
957d35bfc4bb2edb4bcdf71980b11935bac4eac1
$ git merge-base origin/master HEAD
d2f9bae288c2015715b70ce4090c635e18dd063f

```

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

---

<div class="post-metadata">

**Author:** ![rsimha](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/rsimha/32/1875_2.png) [@rsimha](https://travis-ci.community/u/rsimha)\
**Post date:** [July 23, 2019, 3:49pm UTC](https://travis-ci.community/t/find-the-latest-commit-from-master-that-is-present-in-the-pr-codebase/4189/7 "2019-07-23T15:49:42Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![native-api](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/native-api/32/430_2.png) [@native-api](https://travis-ci.community/u/native-api)\
**Post date:** [September 24, 2020, 6:25pm UTC](https://travis-ci.community/t/find-the-latest-commit-from-master-that-is-present-in-the-pr-codebase/4189/8 "2020-09-24T18:25:33Z")

</div>

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](https://travis-ci.community/t/build-stages-get-different-codebase-if-the-pr-branch-or-the-base-branch-changes-in-the-meantime/9995)
