Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add git version #28

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions macro.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this duplicated from the original macro?

/**
* 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;
}
2 changes: 1 addition & 1 deletion macro.js
Original file line number Diff line number Diff line change
@@ -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')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... the export of two macros would be somewhat complicated. I would suggest having it be part of the same macro.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
41 changes: 41 additions & 0 deletions src/GitVersion.macro.js
Original file line number Diff line number Diff line change
@@ -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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the name GitVersion doesn't seem too accurate for what it does.

2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = require('./GitInfo.macro');
module.exports = require('./GitVersion.macro');
5 changes: 2 additions & 3 deletions test/01.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import GitInfo from 'react-git-info/macro';
const fs = require('fs');
import GitInfo from 'react-git-info/macro';

const gitInfo = GitInfo();

describe('Git information', () => {
test('gets correct branch', () => {
expect(gitInfo.branch).toBe('master');
expect(gitInfo.branch).toBe('main');
});

test('gets correct tags', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/02.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const gitInfo = GitInfo();

describe('Git information', () => {
test('gets correct branch', () => {
expect(gitInfo.branch).toBe('master');
expect(gitInfo.branch).toBe('main');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did upstream Git change the default branch name already?

});

test('gets correct tags', () => {
Expand Down
21 changes: 21 additions & 0 deletions test/04.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
21 changes: 21 additions & 0 deletions test/05.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
21 changes: 21 additions & 0 deletions test/06.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
67 changes: 66 additions & 1 deletion test/GitInfoTest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ try {
throw 'Local package installation failed.'
}


<#
Write-Output '------------------- Test 1 -------------------'

$testFile = '01.test.js'
Expand Down Expand Up @@ -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...'
Expand Down