|
1 | 1 | const { createMacro } = require('babel-plugin-macros'); |
2 | 2 | const { execSync } = require('child_process'); |
3 | 3 |
|
4 | | -const parseGitLog = (() => { |
5 | | - let message = ''; |
6 | | - let refs = ''; |
| 4 | +const parsedGitLog = (() => { |
| 5 | + let message; |
| 6 | + let refs; |
7 | 7 | const commit = {}; |
8 | 8 | // 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 |
9 | 10 | const logResult = execSync('git log --format="%D%n%h%n%H%n%cI%n%B" -n 1 HEAD') |
10 | 11 | .toString() |
11 | 12 | .trim() |
12 | 13 | .split(/\r?\n/); |
13 | 14 | [refs, commit.shortHash, commit.hash, commit.date, ...message] = logResult; |
14 | | - commit.message = message.join("\n"); |
| 15 | + commit.message = message.join('\n'); |
15 | 16 | return {refs, commit}; |
16 | 17 | })(); |
17 | 18 |
|
18 | 19 | const parseRefs = (refs) => { |
19 | | - let branch; |
| 20 | + let branch = undefined; |
20 | 21 | const tags = []; |
21 | | - refs.split(", ").map((item) => { |
22 | | - const isBranch = item.match(/HEAD -> (.*)/); |
23 | | - const isTag = item.match(/tag: (.*)/); |
| 22 | + refs.split(', ').map((item) => { |
| 23 | + // if HEAD is not detached, the branch is printed out as `HEAD -> branch_name`. |
| 24 | + // if HEAD is detached, the output becomes `HEAD`. |
| 25 | + const isBranch = item.match(/^HEAD -> (.*)$/); |
| 26 | + const isTag = item.match(/^tag: (.*)$/); |
24 | 27 |
|
25 | 28 | if (isTag && isTag.length > 1) { |
26 | 29 | tags.push(isTag[1]); |
27 | 30 | } else { |
28 | 31 | branch = isBranch ? isBranch[1] : branch; |
29 | 32 | } |
30 | 33 | }); |
| 34 | + |
31 | 35 | return [branch, tags]; |
32 | 36 | }; |
33 | 37 |
|
34 | 38 | const gitInfo = (() => { |
35 | 39 | const ret = {}; |
36 | 40 | try { |
37 | | - const logResult = parseGitLog; |
| 41 | + const logResult = parsedGitLog; |
38 | 42 | [ret.branch, ret.tags] = parseRefs(logResult.refs); |
39 | 43 | ret.commit = logResult.commit; |
40 | 44 | } catch (e) { |
|
0 commit comments