Skip to content

Commit 042e209

Browse files
Fix warnings
* Use `'` instead of `"`. * Throw error if branch was not parsed. * Rename `parseGitLog` to `parsedGitLog`.
1 parent 006e043 commit 042e209

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/GitInfo.macro.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
const { createMacro } = require('babel-plugin-macros');
22
const { execSync } = require('child_process');
33

4-
const parseGitLog = (() => {
5-
let message = '';
6-
let refs = '';
4+
const parsedGitLog = (() => {
5+
let message;
6+
let refs;
77
const commit = {};
88
// only the commit message can have multiple lines. Make sure to always add at the end:
9+
// The format is specified in https://git-scm.com/docs/git-log#_pretty_formats
910
const logResult = execSync('git log --format="%D%n%h%n%H%n%cI%n%B" -n 1 HEAD')
1011
.toString()
1112
.trim()
1213
.split(/\r?\n/);
1314
[refs, commit.shortHash, commit.hash, commit.date, ...message] = logResult;
14-
commit.message = message.join("\n");
15+
commit.message = message.join('\n');
1516
return {refs, commit};
1617
})();
1718

1819
const parseRefs = (refs) => {
19-
let branch;
20+
let branch = undefined;
2021
const tags = [];
21-
refs.split(", ").map((item) => {
22+
refs.split(', ').map((item) => {
2223
const isBranch = item.match(/HEAD -> (.*)/);
2324
const isTag = item.match(/tag: (.*)/);
2425

@@ -28,13 +29,18 @@ const parseRefs = (refs) => {
2829
branch = isBranch ? isBranch[1] : branch;
2930
}
3031
});
32+
33+
if (!branch) {
34+
throw Error('Unable to get parse branch from the log result.');
35+
}
36+
3137
return [branch, tags];
3238
};
3339

3440
const gitInfo = (() => {
3541
const ret = {};
3642
try {
37-
const logResult = parseGitLog;
43+
const logResult = parsedGitLog;
3844
[ret.branch, ret.tags] = parseRefs(logResult.refs);
3945
ret.commit = logResult.commit;
4046
} catch (e) {

0 commit comments

Comments
 (0)