Skip to content

Commit

Permalink
Make build-system code main-branch-agnostic (#33535)
Browse files Browse the repository at this point in the history
  • Loading branch information
rsimha authored Mar 31, 2021
1 parent 7a3de99 commit 9beb76a
Show file tree
Hide file tree
Showing 16 changed files with 141 additions and 105 deletions.
4 changes: 3 additions & 1 deletion build-system/common/ci.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
'use strict';

const {mainBranch} = require('./main-branch');

/**
* @fileoverview Provides various kinds of CI state.
*
Expand Down Expand Up @@ -70,7 +72,7 @@ const isCircleci = isCircleciBuild();
* @return {boolean}
*/
function isCircleciPushBranch(branchName) {
return branchName == 'master' || /^amp-release-.*$/.test(branchName);
return branchName == mainBranch || /^amp-release-.*$/.test(branchName);
}

/**
Expand Down
97 changes: 51 additions & 46 deletions build-system/common/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,34 @@ const {
ciPullRequestSha,
} = require('./ci');
const {getStdout} = require('./exec');
const {mainBranch} = require('./main-branch');

/**
* Returns the commit at which the current PR branch was forked off of master.
* During CI, there is an additional merge commit, so we must pick the first of
* the boundary commits (prefixed with a -) returned by git rev-list.
* On local branches, this is merge base of the current branch off of master.
* Returns the commit at which the current PR branch was forked off of the main
* branch. During CI, there is an additional merge commit, so we must pick the
* first of the boundary commits (prefixed with a -) returned by git rev-list.
* On local branches, this is merge base of the current branch off of the main
* branch.
* @return {string}
*/
function gitBranchCreationPoint() {
if (isPullRequestBuild()) {
const prSha = ciPullRequestSha();
return getStdout(
`git rev-list --boundary ${prSha}...origin/master | grep "^-" | head -n 1 | cut -c2-`
`git rev-list --boundary ${prSha}...origin/${mainBranch} | grep "^-" | head -n 1 | cut -c2-`
).trim();
}
return gitMergeBaseLocalMaster();
return gitMergeBaseLocalMain();
}

/**
* Returns the `master` parent of the merge commit (current HEAD) during CI
* builds. This is not the same as origin/master (a moving target), since
* Returns the main branch parent of the merge commit (current HEAD) during CI
* builds. This is not the same as origin/<main branch> (a moving target), since
* new commits can be merged while a CI build is in progress.
* @return {string}
*/
function gitCiMasterBaseline() {
return getStdout('git merge-base origin/master HEAD').trim();
function gitCiMainBaseline() {
return getStdout(`git merge-base origin/${mainBranch} HEAD`).trim();
}

/**
Expand All @@ -73,29 +75,29 @@ function gitDiffNameOnly() {
}

/**
* Returns the list of files changed 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 the
* main branch, one on each line.
* @return {!Array<string>}
*/
function gitDiffNameOnlyMaster() {
const masterBaseline = gitMasterBaseline();
return getStdout(`git diff --name-only ${masterBaseline}`).trim().split('\n');
function gitDiffNameOnlyMain() {
const mainBaseline = gitMainBaseline();
return getStdout(`git diff --name-only ${mainBaseline}`).trim().split('\n');
}

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

/**
* Returns a detailed log of commits included in a PR check, starting with (and
* including) the branch point off of master. Limited to commits in the past
* 30 days to keep the output sane.
* including) the branch point off of the main branch. Limited to commits in the
* past 30 days to keep the output length manageable.
*
* @return {string}
*/
Expand All @@ -110,11 +112,11 @@ function gitDiffCommitLog() {

/**
* Returns the list of files added by the local branch relative to the branch
* point off of master, one on each line.
* point off of the main branch, one on each line.
* @return {!Array<string>}
*/
function gitDiffAddedNameOnlyMaster() {
const branchPoint = gitMergeBaseLocalMaster();
function gitDiffAddedNameOnlyMain() {
const branchPoint = gitMergeBaseLocalMain();
return getStdout(`git diff --name-only --diff-filter=ARC ${branchPoint}`)
.trim()
.split('\n');
Expand All @@ -129,13 +131,14 @@ function gitDiffColor() {
}

/**
* Returns the full color diff of the given file relative to the branch point off of master.
* Returns the full color diff of the given file relative to the branch point
* off of the main branch.
* @param {string} file
* @return {string}
*/
function gitDiffFileMaster(file) {
const masterBaseline = gitMasterBaseline();
return getStdout(`git -c color.ui=always diff -U1 ${masterBaseline} ${file}`);
function gitDiffFileMain(file) {
const mainBaseline = gitMainBaseline();
return getStdout(`git -c color.ui=always diff -U1 ${mainBaseline} ${file}`);
}

/**
Expand Down Expand Up @@ -168,7 +171,8 @@ function gitCommitterEmail() {
}

/**
* Returns list of commit SHAs and their cherry-pick status from master.
* Returns list of commit SHAs and their cherry-pick status from the main
* branch.
*
* `git cherry <branch>` returns a list of commit SHAs. While the exact
* mechanism is too complicated for this comment (run `git help cherry` for a
Expand All @@ -178,8 +182,8 @@ function gitCommitterEmail() {
*
* @return {!Array<string>}
*/
function gitCherryMaster() {
const stdout = getStdout('git cherry master').trim();
function gitCherryMain() {
const stdout = getStdout(`git cherry ${mainBranch}`).trim();
return stdout ? stdout.split('\n') : [];
}

Expand All @@ -198,23 +202,24 @@ function gitCommitFormattedTime(ref = 'HEAD') {
}

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

/**
* Returns the master baseline commit, regardless of running environment.
* Returns the baseline commit from the main branch, regardless of running
* environment.
* @return {string}
*/
function gitMasterBaseline() {
function gitMainBaseline() {
if (isCiBuild()) {
return gitCiMasterBaseline();
return gitCiMainBaseline();
}
return gitMergeBaseLocalMaster();
return gitMergeBaseLocalMain();
}

/**
Expand All @@ -230,18 +235,18 @@ function gitDiffPath(path, commit) {
module.exports = {
gitBranchCreationPoint,
gitBranchName,
gitCherryMaster,
gitCherryMain,
gitCommitFormattedTime,
gitCommitHash,
gitCommitterEmail,
gitDiffAddedNameOnlyMaster,
gitDiffAddedNameOnlyMain,
gitDiffColor,
gitDiffCommitLog,
gitDiffFileMaster,
gitDiffFileMain,
gitDiffNameOnly,
gitDiffNameOnlyMaster,
gitDiffNameOnlyMain,
gitDiffPath,
gitDiffStatMaster,
gitCiMasterBaseline,
gitDiffStatMain,
gitCiMainBaseline,
shortSha,
};
25 changes: 25 additions & 0 deletions build-system/common/main-branch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright 2021 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* TODO(rsimha, #32195): Change this to main when branch is renamed, and delete
* this file once the dust settles.
*/
const mainBranch = 'master';

module.exports = {
mainBranch,
};
4 changes: 2 additions & 2 deletions build-system/common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const {clean} = require('../tasks/clean');
const {default: ignore} = require('ignore');
const {doBuild} = require('../tasks/build');
const {doDist} = require('../tasks/dist');
const {gitDiffNameOnlyMaster} = require('./git');
const {gitDiffNameOnlyMain} = require('./git');
const {green, cyan, yellow} = require('kleur/colors');
const {log, logLocalDev} = require('./logging');

Expand Down Expand Up @@ -75,7 +75,7 @@ function getValidExperiments() {
*/
function getFilesChanged(globs) {
const allFiles = globby.sync(globs, {dot: true});
return gitDiffNameOnlyMaster().filter((changedFile) => {
return gitDiffNameOnlyMain().filter((changedFile) => {
return fs.existsSync(changedFile) && allFiles.includes(changedFile);
});
}
Expand Down
41 changes: 21 additions & 20 deletions build-system/compile/internal-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
'use strict';

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

// Allow leading zeros in --version_override, e.g. 0000000000001
const argv = minimist(process.argv.slice(2), {
Expand All @@ -27,15 +27,16 @@ 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,
* including non `master` commits. If this branch only contains new commits
* - Count the number (<X>) of cherry-picked commits on this branch, including
* commits not on the main branch . 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>).
* main branch (if the current branch is the main branch, or otherwise in
* its commit history, then the current commit is <C>).
* - Find the commit time of <C> (<C>.time). Note that commit time might be
* different from author time! e.g., commit time might be the time that a PR
* was merged into `master`, or a commit was cherry-picked onto the branch;
* was merged into the main branch, or a commit was cherry-picked onto the
* branch;
* author time is when the original author of the commit ran the "git commit"
* command.
* - The version number is <C>.time.format("YYmmDDHHMM", "UTC") + <X> (the
Expand All @@ -45,29 +46,29 @@ const argv = minimist(process.argv.slice(2), {
* that will fail to build. This should never happen.
*
* Examples:
* 1. The version number of a release built from the HEAD commit on `master`,
* where that HEAD commit was committed on April 25, 2020 2:31 PM EDT would
* be `2004251831000`.
* 1. The version number of a release built from the HEAD commit on the main
* branch, where that HEAD commit was committed on April 25, 2020 2:31 PM EDT
* would be `2004251831000`.
* - EDT is UTC-4, so the hour value changes from EDT's 14 to UTC's 18.
* - The last three digits are 000 as this commit is on `master`.
* - The last three digits are 000 as this commit is on the main branch.
*
* 2. The version number of a release built from a local working branch (e.g.,
* on a developer's workstation) that was split off from a `master` commit
* on a developer's workstation) that was split off from a main branch commit
* from May 6, 2021 10:40 AM PDT and has multiple commits that exist only on
* local working branch would be `2105061840000`.
* - PDT is UTC-7, so the hour value changes from PDT's 10 to UTC's 17.
* - The last three digits are 000 as this commit is on a branch that was
* split from `master`, and does not have any cherry-picked commits.
* split from the main branch, and does not have any cherry-picked commits.
*
* 3. For a release built from a local working branch that was split off from a
* `master` commit from November 9, 2021 11:48 PM PST, and then:
* - had one commit that was cherry-picked from `master`,
* main branch commit from November 9, 2021 11:48 PM PST, and then:
* - had one commit that was cherry-picked from the main branch,
* - followed by two commits that were created directly on the branch, the
* last of which was commited on November 23, 2021 6:38 PM PST,
* - followed by twelve commits that were cherry-picked from `master`, then
* its version number would be `2111240238012`.
* - The latest twelve commits are cherry-picks from `master`, and the one
* before them is not, so our last three digits are set to 012.
* - followed by twelve commits that were cherry-picked from the main branch,
* then its version number would be `2111240238012`.
* - The latest twelve commits are cherry-picks from the main branch, and the
* one before them is not, so our last three digits are set to 012.
* - PST is UTC-8, so the hour value changes from PST's 18 to UTC's 2, and
* one day is added.
*
Expand All @@ -85,12 +86,12 @@ function getVersion() {
return version;
}

const numberOfCherryPicks = gitCherryMaster().length;
const numberOfCherryPicks = gitCherryMain().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! Please make ' +
'sure your local master branch is up to date.'
'sure your local main branch is up to date.'
);
}

Expand Down
4 changes: 2 additions & 2 deletions build-system/pr-check/build-targets.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const minimatch = require('minimatch');
const path = require('path');
const {cyan} = require('kleur/colors');
const {getLoggingPrefix, logWithoutTimestamp} = require('../common/logging');
const {gitDiffNameOnlyMaster} = require('../common/git');
const {gitDiffNameOnlyMain} = require('../common/git');
const {isCiBuild} = require('../common/ci');

/**
Expand Down Expand Up @@ -306,7 +306,7 @@ function determineBuildTargets() {
lintFiles = globby.sync(config.lintGlobs);
presubmitFiles = globby.sync(config.presubmitGlobs);
prettifyFiles = globby.sync(config.prettifyGlobs);
const filesChanged = gitDiffNameOnlyMaster();
const filesChanged = gitDiffNameOnlyMain();
for (const file of filesChanged) {
let isRuntimeFile = true;
Object.keys(targetMatchers).forEach((target) => {
Expand Down
3 changes: 2 additions & 1 deletion build-system/pr-check/npm-checks.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const {cyan, red} = require('kleur/colors');
const {exec} = require('../common/exec');
const {getLoggingPrefix, logWithoutTimestamp} = require('../common/logging');
const {gitDiffColor, gitDiffNameOnly} = require('../common/git');
const {mainBranch} = require('../common/main-branch');

/**
* Makes sure package.json and package-lock.json are in sync.
Expand Down Expand Up @@ -103,7 +104,7 @@ function isPackageLockFileProperlyUpdated() {
logWithoutTimestamp(
loggingPrefix,
'⤷ To fix this, sync your branch to',
cyan('ampproject/amphtml:master') + ', run',
cyan(`ampproject/amphtml:${mainBranch}`) + ', run',
cyan('npm install') + ', and push a new commit containing the changes.'
);
logWithoutTimestamp(loggingPrefix, 'Expected changes:');
Expand Down
Loading

0 comments on commit 9beb76a

Please sign in to comment.