Skip to content

Commit 0679013

Browse files
Account for detached HEAD and TSDoc for type definitions
1 parent 11cdff8 commit 0679013

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

macro.d.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,47 @@
1+
/**
2+
* A Babel plugin macro giving access to git information at compile time.
3+
*/
14
declare module "react-git-info/macro" {
5+
/**
6+
* Information about a git commit.
7+
*/
28
export interface GitCommitInformation {
9+
/**
10+
* Date of the commit as a string in strict ISO 8601 format.
11+
*/
312
readonly date: string;
13+
/**
14+
* Full hash of the latest commit on the active branch.
15+
*/
416
readonly hash: string;
17+
/**
18+
* Raw commit message.
19+
*/
520
readonly message: string;
21+
/**
22+
* Abbreviated commit hash with length defined as `core.abbrev` in
23+
* {@link https://git-scm.com/docs/git-config | git-config}.
24+
*/
625
readonly shortHash: string;
726
}
827

928
export interface GitInformation {
29+
/**
30+
* Tags pointing to the current commit.
31+
*/
1032
readonly tags: string[];
11-
readonly branch: string;
33+
/**
34+
* The current git branch. `undefined` if the repository is in a detached HEAD state.
35+
*/
36+
readonly branch?: string;
37+
/**
38+
* Information about the commit pointed to by `HEAD`.
39+
*/
1240
readonly commit: GitCommitInformation;
1341
}
1442

43+
/**
44+
* Returns information about the current Git state.
45+
*/
1546
export default function GitInfo(): GitInformation;
1647
}

src/GitInfo.macro.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ const parseRefs = (refs) => {
2020
let branch = undefined;
2121
const tags = [];
2222
refs.split(', ').map((item) => {
23-
const isBranch = item.match(/HEAD -> (.*)/);
24-
const isTag = item.match(/tag: (.*)/);
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: (.*)$/);
2527

2628
if (isTag && isTag.length > 1) {
2729
tags.push(isTag[1]);
@@ -30,10 +32,6 @@ const parseRefs = (refs) => {
3032
}
3133
});
3234

33-
if (!branch) {
34-
throw Error('Unable to get parse branch from the log result.');
35-
}
36-
3735
return [branch, tags];
3836
};
3937

0 commit comments

Comments
 (0)