Skip to content

Commit aad13ca

Browse files
committed
fix: Fix a bug where the git hash was inappropriately truncated when there were no tags (fixes #3)
1 parent 4322437 commit aad13ca

File tree

2 files changed

+159
-150
lines changed

2 files changed

+159
-150
lines changed

src/gitTagDescriptor.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ export const getBranchString = (clean: (s: string) => string): string =>
3131
const dirtyString = (gitInfo: GitInfo, clean: (s: string) => string) =>
3232
gitInfo.dirty ? `.SNAPSHOT.${clean(getHostnameString())}` : '';
3333

34-
const gitHash = (gitInfo: GitInfo) => gitInfo.hash.substring(1);
34+
const gitHash = (gitInfo: GitInfo) =>
35+
gitInfo.hash.startsWith('g') ? gitInfo.hash.substring(1) : gitInfo.hash;
3536

3637
const tagOrHash = (gitInfo: GitInfo) =>
3738
gitInfo.tag ? gitInfo.tag : gitHash(gitInfo);

src/index.spec.ts

Lines changed: 157 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ jest.mock('os');
99
jest.mock('git-rev-sync');
1010

1111
describe('version-from-git-tag', () => {
12-
const hash = 'g9234bfb';
1312
describe('with no tag', () => {
13+
const hash = '9234bfb';
1414
const gitDescribe = {
1515
hash,
1616
};
@@ -52,95 +52,19 @@ describe('version-from-git-tag', () => {
5252
});
5353
});
5454

55-
describe('with no semver', () => {
56-
const tag = 'sometag';
57-
const gitDescribe = {
58-
tag,
59-
hash,
60-
};
61-
describe('when the repo is not on an exact tag', () => {
62-
const distance = 1;
63-
describe('when the repo is dirty', () => {
64-
const dirty = true;
65-
beforeEach(() => {
66-
(gitDescribeSync as jest.Mock).mockReturnValue({
67-
...gitDescribe,
68-
dirty,
69-
distance,
70-
});
71-
});
72-
it('displays the version number correctly', () => {
73-
expect(versionFromGitTag()).toEqual(
74-
'sometag-featwhatever+1.9234bfb.SNAPSHOT.HOSTNAME'
75-
);
76-
});
77-
});
78-
describe('when the repo is not dirty', () => {
79-
const dirty = false;
80-
beforeEach(() => {
81-
(gitDescribeSync as jest.Mock).mockReturnValue({
82-
...gitDescribe,
83-
dirty,
84-
distance,
85-
});
86-
});
87-
it('displays the version number correctly', () => {
88-
expect(versionFromGitTag()).toEqual('sometag-featwhatever+1.9234bfb');
89-
});
90-
});
91-
});
92-
describe('when the repo is on an exact tag', () => {
93-
const distance = 0;
94-
describe('when the repo is dirty', () => {
95-
const dirty = true;
96-
beforeEach(() => {
97-
(gitDescribeSync as jest.Mock).mockReturnValue({
98-
...gitDescribe,
99-
dirty,
100-
distance,
101-
});
102-
});
103-
it('displays the version number correctly', () => {
104-
expect(versionFromGitTag()).toEqual(
105-
'sometag-featwhatever+0.9234bfb.SNAPSHOT.HOSTNAME'
106-
);
107-
});
108-
});
109-
describe('when the repo is not dirty', () => {
110-
const dirty = false;
111-
beforeEach(() => {
112-
(gitDescribeSync as jest.Mock).mockReturnValue({
113-
...gitDescribe,
114-
dirty,
115-
distance,
116-
});
117-
});
118-
it('displays the version number correctly', () => {
119-
expect(versionFromGitTag()).toEqual('sometag');
120-
});
121-
});
122-
});
123-
});
124-
125-
describe('with a release version', () => {
126-
const version = '1.2.3';
127-
const tag = `v${version}`;
128-
const gitDescribe = {
129-
hash,
130-
tag,
131-
semver: {
132-
version,
133-
},
134-
};
135-
136-
describe('when on a branch named feat/whatever', () => {
137-
(branch as jest.Mock).mockReturnValue('feat/whatever');
55+
describe('with a tag', () => {
56+
const hash = 'g9234bfb';
13857

58+
describe('with no semver', () => {
59+
const tag = 'sometag';
60+
const gitDescribe = {
61+
tag,
62+
hash,
63+
};
13964
describe('when the repo is not on an exact tag', () => {
14065
const distance = 1;
14166
describe('when the repo is dirty', () => {
14267
const dirty = true;
143-
14468
beforeEach(() => {
14569
(gitDescribeSync as jest.Mock).mockReturnValue({
14670
...gitDescribe,
@@ -150,7 +74,7 @@ describe('version-from-git-tag', () => {
15074
});
15175
it('displays the version number correctly', () => {
15276
expect(versionFromGitTag()).toEqual(
153-
'1.2.3-featwhatever+1.9234bfb.SNAPSHOT.HOSTNAME'
77+
'sometag-featwhatever+1.9234bfb.SNAPSHOT.HOSTNAME'
15478
);
15579
});
15680
});
@@ -164,7 +88,9 @@ describe('version-from-git-tag', () => {
16488
});
16589
});
16690
it('displays the version number correctly', () => {
167-
expect(versionFromGitTag()).toEqual('1.2.3-featwhatever+1.9234bfb');
91+
expect(versionFromGitTag()).toEqual(
92+
'sometag-featwhatever+1.9234bfb'
93+
);
16894
});
16995
});
17096
});
@@ -181,7 +107,7 @@ describe('version-from-git-tag', () => {
181107
});
182108
it('displays the version number correctly', () => {
183109
expect(versionFromGitTag()).toEqual(
184-
'1.2.3-featwhatever+0.9234bfb.SNAPSHOT.HOSTNAME'
110+
'sometag-featwhatever+0.9234bfb.SNAPSHOT.HOSTNAME'
185111
);
186112
});
187113
});
@@ -195,84 +121,166 @@ describe('version-from-git-tag', () => {
195121
});
196122
});
197123
it('displays the version number correctly', () => {
198-
expect(versionFromGitTag()).toEqual('1.2.3');
124+
expect(versionFromGitTag()).toEqual('sometag');
199125
});
200126
});
201127
});
202128
});
203-
});
204-
describe('with a prerelease version', () => {
205-
const version = '1.2.3-beta';
206-
const tag = `v${version}`;
207-
const gitDescribe = {
208-
hash,
209-
tag,
210-
semver: {
211-
version,
212-
},
213-
};
214-
describe('when the repo is not on an exact tag', () => {
215-
const distance = 1;
216-
describe('when the repo is dirty', () => {
217-
const dirty = true;
218129

219-
beforeEach(() => {
220-
(gitDescribeSync as jest.Mock).mockReturnValue({
221-
...gitDescribe,
222-
dirty,
223-
distance,
130+
describe('with a release version', () => {
131+
const version = '1.2.3';
132+
const tag = `v${version}`;
133+
const gitDescribe = {
134+
hash,
135+
tag,
136+
semver: {
137+
version,
138+
},
139+
};
140+
141+
describe('when on a branch named feat/whatever', () => {
142+
(branch as jest.Mock).mockReturnValue('feat/whatever');
143+
144+
describe('when the repo is not on an exact tag', () => {
145+
const distance = 1;
146+
describe('when the repo is dirty', () => {
147+
const dirty = true;
148+
149+
beforeEach(() => {
150+
(gitDescribeSync as jest.Mock).mockReturnValue({
151+
...gitDescribe,
152+
dirty,
153+
distance,
154+
});
155+
});
156+
it('displays the version number correctly', () => {
157+
expect(versionFromGitTag()).toEqual(
158+
'1.2.3-featwhatever+1.9234bfb.SNAPSHOT.HOSTNAME'
159+
);
160+
});
224161
});
225-
});
226-
it('displays the version number correctly', () => {
227-
expect(versionFromGitTag()).toEqual(
228-
'1.2.3-beta.featwhatever+1.9234bfb.SNAPSHOT.HOSTNAME'
229-
);
230-
});
231-
});
232-
describe('when the repo is not dirty', () => {
233-
const dirty = false;
234-
beforeEach(() => {
235-
(gitDescribeSync as jest.Mock).mockReturnValue({
236-
...gitDescribe,
237-
dirty,
238-
distance,
162+
describe('when the repo is not dirty', () => {
163+
const dirty = false;
164+
beforeEach(() => {
165+
(gitDescribeSync as jest.Mock).mockReturnValue({
166+
...gitDescribe,
167+
dirty,
168+
distance,
169+
});
170+
});
171+
it('displays the version number correctly', () => {
172+
expect(versionFromGitTag()).toEqual(
173+
'1.2.3-featwhatever+1.9234bfb'
174+
);
175+
});
239176
});
240177
});
241-
it('displays the version number correctly', () => {
242-
expect(versionFromGitTag()).toEqual(
243-
'1.2.3-beta.featwhatever+1.9234bfb'
244-
);
178+
describe('when the repo is on an exact tag', () => {
179+
const distance = 0;
180+
describe('when the repo is dirty', () => {
181+
const dirty = true;
182+
beforeEach(() => {
183+
(gitDescribeSync as jest.Mock).mockReturnValue({
184+
...gitDescribe,
185+
dirty,
186+
distance,
187+
});
188+
});
189+
it('displays the version number correctly', () => {
190+
expect(versionFromGitTag()).toEqual(
191+
'1.2.3-featwhatever+0.9234bfb.SNAPSHOT.HOSTNAME'
192+
);
193+
});
194+
});
195+
describe('when the repo is not dirty', () => {
196+
const dirty = false;
197+
beforeEach(() => {
198+
(gitDescribeSync as jest.Mock).mockReturnValue({
199+
...gitDescribe,
200+
dirty,
201+
distance,
202+
});
203+
});
204+
it('displays the version number correctly', () => {
205+
expect(versionFromGitTag()).toEqual('1.2.3');
206+
});
207+
});
245208
});
246209
});
247210
});
248-
describe('when the repo is on an exact tag', () => {
249-
const distance = 0;
250-
describe('when the repo is dirty', () => {
251-
const dirty = true;
252-
beforeEach(() => {
253-
(gitDescribeSync as jest.Mock).mockReturnValue({
254-
...gitDescribe,
255-
dirty,
256-
distance,
211+
describe('with a prerelease version', () => {
212+
const version = '1.2.3-beta';
213+
const tag = `v${version}`;
214+
const gitDescribe = {
215+
hash,
216+
tag,
217+
semver: {
218+
version,
219+
},
220+
};
221+
describe('when the repo is not on an exact tag', () => {
222+
const distance = 1;
223+
describe('when the repo is dirty', () => {
224+
const dirty = true;
225+
226+
beforeEach(() => {
227+
(gitDescribeSync as jest.Mock).mockReturnValue({
228+
...gitDescribe,
229+
dirty,
230+
distance,
231+
});
232+
});
233+
it('displays the version number correctly', () => {
234+
expect(versionFromGitTag()).toEqual(
235+
'1.2.3-beta.featwhatever+1.9234bfb.SNAPSHOT.HOSTNAME'
236+
);
257237
});
258238
});
259-
it('displays the version number correctly', () => {
260-
expect(versionFromGitTag()).toEqual(
261-
'1.2.3-beta.featwhatever+0.9234bfb.SNAPSHOT.HOSTNAME'
262-
);
239+
describe('when the repo is not dirty', () => {
240+
const dirty = false;
241+
beforeEach(() => {
242+
(gitDescribeSync as jest.Mock).mockReturnValue({
243+
...gitDescribe,
244+
dirty,
245+
distance,
246+
});
247+
});
248+
it('displays the version number correctly', () => {
249+
expect(versionFromGitTag()).toEqual(
250+
'1.2.3-beta.featwhatever+1.9234bfb'
251+
);
252+
});
263253
});
264254
});
265-
describe('when the repo is not dirty', () => {
266-
const dirty = false;
267-
beforeEach(() => {
268-
(gitDescribeSync as jest.Mock).mockReturnValue({
269-
...gitDescribe,
270-
dirty,
271-
distance,
255+
describe('when the repo is on an exact tag', () => {
256+
const distance = 0;
257+
describe('when the repo is dirty', () => {
258+
const dirty = true;
259+
beforeEach(() => {
260+
(gitDescribeSync as jest.Mock).mockReturnValue({
261+
...gitDescribe,
262+
dirty,
263+
distance,
264+
});
265+
});
266+
it('displays the version number correctly', () => {
267+
expect(versionFromGitTag()).toEqual(
268+
'1.2.3-beta.featwhatever+0.9234bfb.SNAPSHOT.HOSTNAME'
269+
);
272270
});
273271
});
274-
it('displays the version number correctly', () => {
275-
expect(versionFromGitTag()).toEqual('1.2.3-beta');
272+
describe('when the repo is not dirty', () => {
273+
const dirty = false;
274+
beforeEach(() => {
275+
(gitDescribeSync as jest.Mock).mockReturnValue({
276+
...gitDescribe,
277+
dirty,
278+
distance,
279+
});
280+
});
281+
it('displays the version number correctly', () => {
282+
expect(versionFromGitTag()).toEqual('1.2.3-beta');
283+
});
276284
});
277285
});
278286
});

0 commit comments

Comments
 (0)