Skip to content

Commit

Permalink
🏗 Count all cherry picks when setting the version (#32689)
Browse files Browse the repository at this point in the history
  • Loading branch information
estherkim committed Mar 1, 2021
1 parent e67bb96 commit f26666e
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 66 deletions.
31 changes: 1 addition & 30 deletions build-system/common/git.js
Expand Up @@ -179,13 +179,7 @@ function gitCommitterEmail() {
* @return {!Array<{sha: string, isCherryPick: boolean}>}
*/
function gitCherryMaster() {
return getStdout('git cherry master')
.trim()
.split('\n')
.map((line) => ({
isCherryPick: line.substring(0, 2) == '- ',
sha: line.substring(2),
}));
return getStdout('git cherry master').trim().split('\n');
}

/**
Expand All @@ -202,27 +196,6 @@ function gitCommitFormattedTime(ref = 'HEAD') {
).trim();
}

/**
* Returns the commit message of a given ref.
* @param {string} ref a Git reference (commit SHA, branch name, etc.) for the
* commit to get the time of.
* @return {string}
*/
function gitCommitMessage(ref = 'HEAD') {
return getStdout(`git log ${ref} -n 1 --pretty="%B"`);
}

/**
* Checks if a branch contains a specific ref.
* @param {string} ref a Git reference (commit SHA, branch name, etc.) for the
* commit to get the time of.
* @param {string} branch the branch to check.
* @return {boolean}
*/
function gitBranchContains(ref, branch = 'master') {
return Boolean(getStdout(`git branch ${branch} --contains ${ref}`).trim());
}

/**
* Returns the merge base of the current branch off of master when running on
* a local workspace.
Expand Down Expand Up @@ -254,13 +227,11 @@ function gitDiffPath(path, commit) {
}

module.exports = {
gitBranchContains,
gitBranchCreationPoint,
gitBranchName,
gitCherryMaster,
gitCommitFormattedTime,
gitCommitHash,
gitCommitMessage,
gitCommitterEmail,
gitDiffAddedNameOnlyMaster,
gitDiffColor,
Expand Down
38 changes: 5 additions & 33 deletions build-system/compile/internal-version.js
Expand Up @@ -16,12 +16,7 @@
'use strict';

const minimist = require('minimist');
const {
gitBranchContains,
gitCherryMaster,
gitCommitMessage,
gitCommitFormattedTime,
} = require('../common/git');
const {gitCherryMaster, gitCommitFormattedTime} = require('../common/git');

// Allow leading zeros in --version_override, e.g. 0000000000001
const argv = minimist(process.argv.slice(2), {
Expand All @@ -32,11 +27,9 @@ const argv = minimist(process.argv.slice(2), {
* Generates the AMP version number.
*
* Version numbers are determined using the following algorithm:
* - Count the number (<X>) of cherry-picked commits on this branch that came
* from the `master` branch, until reaching `master` or the first commit that
* was added directly on this branch (if the current commit is on `master`'s
* commit history, or only contains new commits that are not cherry-picked
* from `master`, then <X> is 0).
* - Count the number (<X>) of cherry-picked commits on this branch,
* including non `master` commits. If this branch only contains new commits
* that are not cherry-picked, then <X> is 0).
* - Find the commit (<C>) before the last cherry-picked commit from the
* `master` branch (if the current branch is `master`, or otherwise in
* `master`'s commit history, then the current commit is <C>).
Expand Down Expand Up @@ -92,28 +85,7 @@ function getVersion() {
return version;
}

let numberOfCherryPicks = 0;
const commitCherriesInfo = gitCherryMaster().reverse();
for (const {isCherryPick, sha} of commitCherriesInfo) {
if (!isCherryPick) {
// Sometimes cherry-picks are mistaken for new commits. Double-check here
// by looking for the hard-coded message at the end of the commit that
// indicates that it was a cherry-pick. Requires that the cherry-pick was
// performed with the `-x` flag.
const commitMessage = gitCommitMessage(sha);
const cherryPickedMatch = /\(cherry picked from commit ([0-9a-f]{40})\)/.exec(
commitMessage
);
if (!cherryPickedMatch) {
break;
}

if (!gitBranchContains(cherryPickedMatch[1])) {
break;
}
}
numberOfCherryPicks++;
}
const numberOfCherryPicks = gitCherryMaster().length;
if (numberOfCherryPicks > 999) {
throw new Error(
`This branch has ${numberOfCherryPicks} cherry-picks, which is more than 999, the maximum allowed number of cherry-picks!`
Expand Down
3 changes: 0 additions & 3 deletions build-system/tasks/release/index.js
Expand Up @@ -406,9 +406,6 @@ async function cleanup_(tempDir) {
}

async function release() {
// TODO(#27771, danielrozenberg): fail this release quickly if there are
// commits in the tree that are not from the `master` branch.

const outputDir = path.resolve(argv.output_dir || './release');
const tempDir = path.join(outputDir, 'tmp');

Expand Down

0 comments on commit f26666e

Please sign in to comment.