Skip to content

Commit

Permalink
More fixes for git baseline selection in pr-check (#20216)
Browse files Browse the repository at this point in the history
* More fixes for git baseline selection in pr-check

* Fix typo

* Use `master`, not `origin/master`, when choosing merge-base

* Also print TRAVIS_COMMIT

* Use TRAVIS_PULL_REQUEST_SHA instead of TRAVIS_COMMIT

* Do not print TRAVIS_COMMIT

* Address Raghu's comments
  • Loading branch information
danielrozenberg committed Jan 10, 2019
1 parent 5bc4085 commit b03c6fd
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 23 deletions.
56 changes: 33 additions & 23 deletions build-system/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,55 +24,45 @@ const {getStdout} = require('./exec');

const commitLogMaxCount = 100;

/**
* Returns the merge base of the current branch off of master when running on
* a local workspace.
* @return {string}
*/
exports.gitMergeBaseLocalMaster = function() {
return getStdout('git merge-base master HEAD').trim();
};

/**
* Returns the merge base of the current branch off of master, regardless of
* the running environment.
* @return {string}
*/
exports.gitMergeBaseMaster = function() {
if (process.env.TRAVIS) {
const commitRange = process.env.TRAVIS_COMMIT_RANGE.split('...');
return getStdout(`git merge-base ${commitRange[0]} ${commitRange[1]}`)
.trim();
return getStdout(
`git merge-base master ${process.env.TRAVIS_PULL_REQUEST_SHA}`).trim();
}
return exports.gitMergeBaseLocalMaster();
return gitMergeBaseLocalMaster();
};

/**
* Returns the `master` parent of the merge commit (current HEAD) on Travis.
* @return {string}
*/
exports.gitTravisMasterBaseline = function() {
return process.env.TRAVIS_COMMIT_RANGE.split('...')[0];
return getStdout('git rev-parse origin/master').trim();
};

/**
* Returns the list of files changed on the local branch relative to the branch
* point off of master, one on each line.
* Returns the list of files changed relative to the branch point off of master,
* one on each line.
* @return {!Array<string>}
*/
exports.gitDiffNameOnlyMaster = function() {
const branchPoint = exports.gitMergeBaseLocalMaster();
return getStdout(`git diff --name-only ${branchPoint}`).trim().split('\n');
const masterBaseline = gitMasterBaseline();
return getStdout(`git diff --name-only ${masterBaseline}`).trim().split('\n');
};

/**
* Returns the list of files changed on the local branch relative to the branch
* point off of master, in diffstat format.
* Returns the list of files changed relative to the branch point off of master,
* in diffstat format.
* @return {string}
*/
exports.gitDiffStatMaster = function() {
const branchPoint = exports.gitMergeBaseLocalMaster();
return getStdout(`git -c color.ui=always diff --stat ${branchPoint}`);
const masterBaseline = gitMasterBaseline();
return getStdout(`git -c color.ui=always diff --stat ${masterBaseline}`);
};

/**
Expand Down Expand Up @@ -107,7 +97,7 @@ for how to fix this.`;
* @return {!Array<string>}
*/
exports.gitDiffAddedNameOnlyMaster = function() {
const branchPoint = exports.gitMergeBaseLocalMaster();
const branchPoint = gitMergeBaseLocalMaster();
return getStdout(`git diff --name-only --diff-filter=ARC ${branchPoint}`)
.trim().split('\n');
};
Expand Down Expand Up @@ -168,3 +158,23 @@ exports.gitCommitFormattedTime = function() {
exports.gitStatusPorcelain = function() {
return getStdout('git status --porcelain').trim();
};

/**
* Returns the merge base of the current branch off of master when running on
* a local workspace.
* @return {string}
*/
function gitMergeBaseLocalMaster() {
return getStdout('git merge-base master HEAD').trim();
}

/**
* Returns the master baseline commit, regardless of running environment.
* @return {string}
*/
function gitMasterBaseline() {
if (process.env.TRAVIS) {
return exports.gitTravisMasterBaseline();
}
return gitMergeBaseLocalMaster();
}
6 changes: 6 additions & 0 deletions build-system/pr-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const {
gitDiffNameOnlyMaster,
gitDiffStatMaster,
gitMergeBaseMaster,
gitTravisMasterBaseline,
} = require('./git');
const {execOrDie, exec, getStderr, getStdout} = require('./exec');

Expand Down Expand Up @@ -97,6 +98,11 @@ function timedExecOrDie(cmd) {
* Prints a summary of files changed by, and commits included in the PR.
*/
function printChangeSummary() {
if (process.env.TRAVIS) {
console.log(fileLogPrefix, colors.cyan('origin/master'),
'is currently at commit', colors.cyan(gitTravisMasterBaseline()));
}

const filesChanged = gitDiffStatMaster();
console.log(fileLogPrefix,
'Testing the following changes at commit',
Expand Down

0 comments on commit b03c6fd

Please sign in to comment.