-
Notifications
You must be signed in to change notification settings - Fork 8
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the name |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
module.exports = require('./GitInfo.macro'); | ||
module.exports = require('./GitVersion.macro'); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ const gitInfo = GitInfo(); | |
|
||
describe('Git information', () => { | ||
test('gets correct branch', () => { | ||
expect(gitInfo.branch).toBe('master'); | ||
expect(gitInfo.branch).toBe('main'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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', () => { | ||
|
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); | ||
}); | ||
}); |
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); | ||
}); | ||
}); |
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); | ||
}); | ||
}); |
There was a problem hiding this comment.
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?