Skip to content

Commit

Permalink
Don't rely on git if .git isn't there during build (#4498)
Browse files Browse the repository at this point in the history
If we don't have a `.git` directory during build, then don't try and run `git` commands to get e.g. history and last changed for policy files and changelog.

Closes #4497
  • Loading branch information
mattgodbolt committed Dec 27, 2022
1 parent f05b4b6 commit 2348a32
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
21 changes: 13 additions & 8 deletions etc/scripts/parsed_pug_file.js
Expand Up @@ -12,33 +12,38 @@ const expectedHashes = {
privacy: '014c73c485dd3625',
};

function execGit(command) {
function _execGit(command) {
const gitResult = execSync(command);
if (!gitResult) {
throw new Error(`Failed to execute ${command}`);
}
return gitResult.toString();
}

const gitChanges = execGit('git log --date=local --after="3 months ago" "--grep=(#[0-9]*)" --oneline')
.split('\n')
.map(line => line.match(/(?<hash>\w+) (?<description>.*)/))
.filter(x => x)
.map(match => match.groups);

export default function(content) {
const filePath = this.resourcePath;
const filename = path.basename(filePath, '.pug');
const options = this.getOptions();
if (!options.useGit) {
this.emitWarning(new Error(`Running without git: file contents for ${filePath} will be wrong`));
}
const execGit = options.useGit ? _execGit : () => 'no-git-available';
const lastTime = execGit(`git log -1 --format=%cd "${filePath}"`).trimEnd();
const lastCommit = execGit(`git log -1 --format=%h "${filePath}"`).trimEnd();
const gitChanges = execGit('git log --date=local --after="3 months ago" "--grep=(#[0-9]*)" --oneline')
.split('\n')
.map(line => line.match(/(?<hash>\w+) (?<description>.*)/))
.filter(x => x)
.map(match => match.groups);

const compiled = pug.compile(content.toString(), {filename: filePath});

// When calculating the hash we ignore the hard-to-predict values like lastTime and lastCommit, else every time
// we merge changes in policies to main we get a new hash after checking in, and that breaks the build.
const htmlTextForHash = compiled({gitChanges, lastTime:'some-last-time', lastCommit:'some-last-commit'});
const hashDigest = getHashDigest(htmlTextForHash, 'sha256', 'hex', 16);
const expectedHash = expectedHashes[filename];
if (expectedHash !== undefined && expectedHash !== hashDigest) {
if (options.useGit && expectedHash !== undefined && expectedHash !== hashDigest) {
this.emitError(
new Error(
`Hash for file '${filePath}' changed from '${expectedHash}' to '${hashDigest}'` +
Expand Down
5 changes: 5 additions & 0 deletions webpack.config.esm.js
Expand Up @@ -22,6 +22,7 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

import fs from 'fs';
import path from 'path';
import {fileURLToPath} from 'url';

Expand All @@ -41,6 +42,7 @@ console.log(`webpack config for ${isDev ? 'development' : 'production'}.`);

const distPath = path.resolve(__dirname, 'out', 'dist');
const staticPath = path.resolve(__dirname, 'out', 'webpack', 'static');
const hasGit = fs.existsSync(path.resolve(__dirname, '.git'));

// Hack alert: due to a variety of issues, sometimes we need to change
// the name here. Mostly it's things like webpack changes that affect
Expand Down Expand Up @@ -164,6 +166,9 @@ export default {
{
test: /\.pug$/,
loader: './etc/scripts/parsed_pug_file.js',
options: {
useGit: hasGit,
},
},
{
test: /\.ts$/,
Expand Down

0 comments on commit 2348a32

Please sign in to comment.