From 70301370f932f37415a4e8acee862789a2c89b35 Mon Sep 17 00:00:00 2001 From: Daniel Rozenberg Date: Mon, 9 Jul 2018 15:25:27 -0400 Subject: [PATCH] Generate a consistent version number when building/disting --- build-system/exec.js | 1 - build-system/git.js | 19 ++++++++++++++++++ build-system/internal-version.js | 33 ++++++++++++++++++++++++++++---- 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/build-system/exec.js b/build-system/exec.js index c900570b69c7..873218f538e9 100644 --- a/build-system/exec.js +++ b/build-system/exec.js @@ -73,7 +73,6 @@ exports.execOrDie = function(cmd, options) { /** * Executes the provided command, returning the process object. - * This will throw an exception if something goes wrong. * @param {string} cmd * @return {!Object} */ diff --git a/build-system/git.js b/build-system/git.js index bdb5e525b1a1..7aca74d1a173 100644 --- a/build-system/git.js +++ b/build-system/git.js @@ -83,3 +83,22 @@ exports.gitBranchName = function() { exports.gitCommitterEmail = function() { return getStdout('git log -1 --pretty=format:"%ae"').trim(); }; + +/** + * Returns the timestamp of the latest commit on the local branch. + * @return {int} + */ +exports.gitCommitFormattedTime = function() { + return getStdout( + 'TZ=UTC git log -1 --pretty="%cd" --date=format-local:%y%m%d%H%M%S') + .trim(); +}; + +/** + * Returns machine parsable list of uncommitted changed files, or an empty + * string if no files were changed. + * @return {string} + */ +exports.gitStatusPorcelain = function() { + return getStdout('git status --porcelain').trim(); +}; diff --git a/build-system/internal-version.js b/build-system/internal-version.js index 518676ccb02d..789626fe26a0 100644 --- a/build-system/internal-version.js +++ b/build-system/internal-version.js @@ -16,8 +16,33 @@ 'use strict'; const argv = require('minimist')(process.argv.slice(2)); +const {gitCommitFormattedTime, gitStatusPorcelain} = require('./git'); -// Used to e.g. references the ads binary from the runtime to get -// version lock. -exports.VERSION = argv.version ? - String(argv.version) : String(Date.now()); +function getVersion() { + if (argv.version) { + return String(argv.version); + } else { + // Generate a consistent version number by using the commit* time of the + // latest commit on the active branch as the twelve digits, and use the + // state of the working directory as the last digit: 0 for a "clean" tree, 1 + // if there are uncommited changes in the working directory. + // + // e.g., the version number of a clean (no uncommited changes) tree that was + // commited on August 1, 2018 at 14:31:11 EDT would be `1808011831110` + // (notice that due to timezone shift, the hour value changes from EDT's 14 + // to UTC's 18. The last digit denotes that this is a clean tree.) + // + // *Commit time is different from author time! Commit time is the time that + // the PR was merged into master; author time is when the author ran the + // "git commit" command. + const lastCommitFormattedTime = gitCommitFormattedTime(); + if (gitStatusPorcelain()) { + return `${lastCommitFormattedTime}1`; + } else { + return `${lastCommitFormattedTime}0`; + } + } +} + +// Used to e.g. references the ads binary from the runtime to get version lock. +exports.VERSION = getVersion();