From d58dc7799be7f11dd0551a18cb681d7fb3002a71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakob=20Andr=C3=A9n?= Date: Wed, 12 Oct 2022 14:33:41 +0200 Subject: [PATCH 1/2] Update tests for latest create-react-app --- test/01.test.js | 3 +-- test/02.test.js | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/test/01.test.js b/test/01.test.js index 8ebb8ad..5f77d4d 100644 --- a/test/01.test.js +++ b/test/01.test.js @@ -1,11 +1,10 @@ import GitInfo from 'react-git-info/macro'; -const fs = require('fs'); const gitInfo = GitInfo(); describe('Git information', () => { test('gets correct branch', () => { - expect(gitInfo.branch).toBe('master'); + expect(gitInfo.branch).toBe('main'); }); test('gets correct tags', () => { diff --git a/test/02.test.js b/test/02.test.js index b213fc7..a52234a 100644 --- a/test/02.test.js +++ b/test/02.test.js @@ -4,7 +4,7 @@ const gitInfo = GitInfo(); describe('Git information', () => { test('gets correct branch', () => { - expect(gitInfo.branch).toBe('master'); + expect(gitInfo.branch).toBe('main'); }); test('gets correct tags', () => { From 0478fb7e98110d0a0a097a8f589f28023817aa62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakob=20Andr=C3=A9n?= Date: Wed, 12 Oct 2022 17:35:24 +0200 Subject: [PATCH 2/2] Add GitVersion macro --- macro.d.ts | 24 +++++++++++++++ macro.js | 2 +- package.json | 2 +- src/GitVersion.macro.js | 41 +++++++++++++++++++++++++ src/index.js | 2 +- test/01.test.js | 2 +- test/04.test.js | 21 +++++++++++++ test/05.test.js | 21 +++++++++++++ test/06.test.js | 21 +++++++++++++ test/GitInfoTest.ps1 | 67 ++++++++++++++++++++++++++++++++++++++++- 10 files changed, 198 insertions(+), 5 deletions(-) create mode 100644 src/GitVersion.macro.js create mode 100644 test/04.test.js create mode 100644 test/05.test.js create mode 100644 test/06.test.js diff --git a/macro.d.ts b/macro.d.ts index ffc1c7a..ad43b0e 100644 --- a/macro.d.ts +++ b/macro.d.ts @@ -40,8 +40,32 @@ declare module "react-git-info/macro" { readonly commit: GitCommitInformation; } + export interface GitVersion { + /** + * Most recent tag from the current commit. + */ + readonly tag: string; + /** + * Number of additional commits since the most recent tag. + */ + readonly distance: number; + /** + * Abbreviated commit hash. + */ + readonly shortHash: string; + /** + * True if the working tree has local modifications. + */ + readonly isDirty: boolean; + } + /** * Returns information about the current Git state. */ export default function GitInfo(): GitInformation; + + /** + * Returns information about the current Git version. + */ + export function GitVersion(): GitVersion; } diff --git a/macro.js b/macro.js index 44d86d4..c48890a 100644 --- a/macro.js +++ b/macro.js @@ -1,2 +1,2 @@ // Inspired by https://github.com/kentcdodds/babel-plugin-preval/blob/master/macro.js -module.exports = require('./src/GitInfo.macro') +module.exports = require('./src/GitVersion.macro') diff --git a/package.json b/package.json index 628ae34..4eb6a46 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-git-info", - "version": "2.0.1", + "version": "2.1.0", "description": "Git commit information for your react app", "main": "src/index.js", "scripts": { diff --git a/src/GitVersion.macro.js b/src/GitVersion.macro.js new file mode 100644 index 0000000..c73d0d7 --- /dev/null +++ b/src/GitVersion.macro.js @@ -0,0 +1,41 @@ +const { createMacro } = require('babel-plugin-macros'); +const { execSync } = require('child_process'); + +const parsedGitDescribe = (() => { + + const gitCommand = 'git describe --tags --long --dirty'; + + const logResult = execSync(gitCommand) + .toString() + .trim() + .split("-"); + const lastElement = logResult.pop(); + let shortHash; + if (lastElement === "dirty") { + isDirty = true; + shortHash = logResult.pop(); + } else { + isDirty = false; + shortHash = lastElement; + } + const distance = Number(logResult.pop()); + const tag = logResult.join("-"); + return {tag, distance, shortHash, isDirty}; +})(); + +const gitVersion = (() => { + try { + return parsedGitDescribe; + } catch (e) { + throw Error(`Unable to parse the git version, is it tagged?: ${e}`); + } +})(); + +const getGitVersion = ({ references }) => { + const sourceString = `(function() { return ${JSON.stringify(gitVersion)}; })`; + references.default.forEach(referencePath => { + referencePath.replaceWithSourceString(sourceString); + }); +}; + +module.exports = createMacro(getGitVersion); diff --git a/src/index.js b/src/index.js index 1d8bf66..2665636 100644 --- a/src/index.js +++ b/src/index.js @@ -1 +1 @@ -module.exports = require('./GitInfo.macro'); +module.exports = require('./GitVersion.macro'); diff --git a/test/01.test.js b/test/01.test.js index 5f77d4d..22221a0 100644 --- a/test/01.test.js +++ b/test/01.test.js @@ -1,4 +1,4 @@ -import GitInfo from 'react-git-info/macro'; +import GitInfo from 'react-git-info/macro'; const gitInfo = GitInfo(); diff --git a/test/04.test.js b/test/04.test.js new file mode 100644 index 0000000..ca293dc --- /dev/null +++ b/test/04.test.js @@ -0,0 +1,21 @@ +import GitVersion from 'react-git-info/macro'; +const { execSync } = require('child_process'); + +const shortCommitHash = execSync('git rev-parse --short HEAD').toString().trim(); + +const gitVersion = GitVersion(); + +describe('Git version tag on commit', () => { + test('gets correct tag', () => { + expect(gitVersion.tag).toEqual('hello-version'); + }); + test('gets correct distance', () => { + expect(gitVersion.distance).toEqual(0); + }); + test('gets correct shortHash', () => { + expect(gitVersion.shortHash).toEqual("g" + shortCommitHash); + }); + test('gets correct dirty state', () => { + expect(gitVersion.isDirty).toEqual(true); + }); +}); diff --git a/test/05.test.js b/test/05.test.js new file mode 100644 index 0000000..bade0c0 --- /dev/null +++ b/test/05.test.js @@ -0,0 +1,21 @@ +import GitVersion from 'react-git-info/macro'; +const { execSync } = require('child_process'); + +const shortCommitHash = execSync('git rev-parse --short HEAD').toString().trim(); + +const gitVersion = GitVersion(); + +describe('Git version one commit ahead from tag', () => { + test('gets correct tag', () => { + expect(gitVersion.tag).toEqual('hello-version'); + }); + test('gets correct distance', () => { + expect(gitVersion.distance).toEqual(1); + }); + test('gets correct shortHash', () => { + expect(gitVersion.shortHash).toEqual("g" + shortCommitHash); + }); + test('gets correct dirty state', () => { + expect(gitVersion.isDirty).toEqual(false); + }); +}); diff --git a/test/06.test.js b/test/06.test.js new file mode 100644 index 0000000..8adb262 --- /dev/null +++ b/test/06.test.js @@ -0,0 +1,21 @@ +import GitVersion from 'react-git-info/macro'; +const { execSync } = require('child_process'); + +const shortCommitHash = execSync('git rev-parse --short HEAD').toString().trim(); + +const gitVersion = GitVersion(); + +describe('Git version dirty', () => { + test('gets correct tag', () => { + expect(gitVersion.tag).toEqual('hello-version'); + }); + test('gets correct distance', () => { + expect(gitVersion.distance).toEqual(2); + }); + test('gets correct shortHash', () => { + expect(gitVersion.shortHash).toEqual("g" + shortCommitHash); + }); + test('gets correct dirty state', () => { + expect(gitVersion.isDirty).toEqual(true); + }); +}); diff --git a/test/GitInfoTest.ps1 b/test/GitInfoTest.ps1 index 12b8ddd..bed34ca 100644 --- a/test/GitInfoTest.ps1 +++ b/test/GitInfoTest.ps1 @@ -35,7 +35,7 @@ try { throw 'Local package installation failed.' } - +<# Write-Output '------------------- Test 1 -------------------' $testFile = '01.test.js' @@ -104,6 +104,71 @@ try { throw "One or more tests failed. Exit code = $LASTEXITCODE" } + Remove-Item -Force $testScript #> + + Write-Output '------------------- Test 4 -------------------' + + $testFile = '04.test.js' + $testScript = Join-Path $testRepoPath "src/$testFile" + Copy-Item (Join-Path $scriptsPath $testFile) $testScript + + $commandError = $false + git tag 'hello-version' + $commandError = $commandError -or -not $? + + if ($commandError) { + throw 'Unable to run git commands.' + } + + yarn test + if (-not $?) { + throw "One or more tests failed. Exit code = $LASTEXITCODE" + } + + Remove-Item -Force $testScript + + Write-Output '------------------- Test 5 -------------------' + + $testFile = '05.test.js' + $testScript = Join-Path $testRepoPath "src/$testFile" + Copy-Item (Join-Path $scriptsPath $testFile) $testScript + + $commandError = $false + git add . + git commit --allow-empty -m 'Git commit message' + $commandError = $commandError -or -not $? + + if ($commandError) { + throw 'Unable to run git commands.' + } + + yarn test + if (-not $?) { + throw "One or more tests failed. Exit code = $LASTEXITCODE" + } + + Remove-Item -Force $testScript + + Write-Output '------------------- Test 6 -------------------' + + $testFile = '06.test.js' + $testScript = Join-Path $testRepoPath "src/$testFile" + Copy-Item (Join-Path $scriptsPath $testFile) $testScript + + $commandError = $false + git commit --allow-empty -m 'Another Git commit message' + touch randomfile.txt + $commandError = $commandError -or -not $? + + if ($commandError) { + throw 'Unable to run git commands.' + } + + yarn test + if (-not $?) { + throw "One or more tests failed. Exit code = $LASTEXITCODE" + } + Remove-Item -Force $testScript } finally { Write-Output 'Cleaning up...'