Skip to content

Commit

Permalink
Generate a consistent version number when building/disting
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Rozenberg committed Aug 29, 2018
1 parent 124eb0d commit 7030137
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
1 change: 0 additions & 1 deletion build-system/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/
Expand Down
19 changes: 19 additions & 0 deletions build-system/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};
33 changes: 29 additions & 4 deletions build-system/internal-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

0 comments on commit 7030137

Please sign in to comment.