Skip to content

Commit d443f84

Browse files
committed
Convert build/lib/git.js to TypeScript
1 parent a2477d9 commit d443f84

2 files changed

Lines changed: 112 additions & 57 deletions

File tree

build/lib/git.js

Lines changed: 51 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,51 @@
1-
/*---------------------------------------------------------------------------------------------
2-
* Copyright (c) Microsoft Corporation. All rights reserved.
3-
* Licensed under the MIT License. See License.txt in the project root for license information.
4-
*--------------------------------------------------------------------------------------------*/
5-
6-
var path = require('path');
7-
var fs = require('fs');
8-
9-
exports.getVersion = function (repo) {
10-
var git = path.join(repo, '.git');
11-
var headPath = path.join(git, 'HEAD');
12-
var head;
13-
14-
try {
15-
head = fs.readFileSync(headPath, 'utf8').trim();
16-
} catch (e) {
17-
return void 0;
18-
}
19-
20-
if (/^[0-9a-f]{40}$/i.test(head)) {
21-
return head;
22-
}
23-
24-
var refMatch = /^ref: (.*)$/.exec(head);
25-
26-
if (!refMatch) {
27-
return void 0;
28-
}
29-
30-
var ref = refMatch[1];
31-
var refPath = path.join(git, ref);
32-
33-
try {
34-
return fs.readFileSync(refPath, 'utf8').trim();
35-
} catch (e) {
36-
// noop
37-
}
38-
39-
var packedRefsPath = path.join(git, 'packed-refs');
40-
var refsRaw;
41-
42-
try {
43-
refsRaw = fs.readFileSync(packedRefsPath, 'utf8').trim();
44-
} catch (e) {
45-
return void 0;
46-
}
47-
48-
var refsRegex = /^([0-9a-f]{40})\s+(.+)$/gm;
49-
var refsMatch;
50-
var refs = {};
51-
52-
while (refsMatch = refsRegex.exec(refsRaw)) {
53-
refs[refsMatch[2]] = refsMatch[1];
54-
}
55-
56-
return refs[ref];
57-
};
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
"use strict";
6+
var path = require('path');
7+
var fs = require('fs');
8+
/**
9+
* Returns the sha1 commit version of a repository or undefined in case of failure.
10+
*/
11+
function getVersion(repo) {
12+
var git = path.join(repo, '.git');
13+
var headPath = path.join(git, 'HEAD');
14+
var head;
15+
try {
16+
head = fs.readFileSync(headPath, 'utf8').trim();
17+
}
18+
catch (e) {
19+
return void 0;
20+
}
21+
if (/^[0-9a-f]{40}$/i.test(head)) {
22+
return head;
23+
}
24+
var refMatch = /^ref: (.*)$/.exec(head);
25+
if (!refMatch) {
26+
return void 0;
27+
}
28+
var ref = refMatch[1];
29+
var refPath = path.join(git, ref);
30+
try {
31+
return fs.readFileSync(refPath, 'utf8').trim();
32+
}
33+
catch (e) {
34+
}
35+
var packedRefsPath = path.join(git, 'packed-refs');
36+
var refsRaw;
37+
try {
38+
refsRaw = fs.readFileSync(packedRefsPath, 'utf8').trim();
39+
}
40+
catch (e) {
41+
return void 0;
42+
}
43+
var refsRegex = /^([0-9a-f]{40})\s+(.+)$/gm;
44+
var refsMatch;
45+
var refs = {};
46+
while (refsMatch = refsRegex.exec(refsRaw)) {
47+
refs[refsMatch[2]] = refsMatch[1];
48+
}
49+
return refs[ref];
50+
}
51+
exports.getVersion = getVersion;

build/lib/git.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
"use strict";
6+
7+
import * as path from 'path';
8+
import * as fs from 'fs';
9+
10+
/**
11+
* Returns the sha1 commit version of a repository or undefined in case of failure.
12+
*/
13+
export function getVersion(repo:string): string {
14+
const git = path.join(repo, '.git');
15+
const headPath = path.join(git, 'HEAD');
16+
let head: string;
17+
18+
try {
19+
head = fs.readFileSync(headPath, 'utf8').trim();
20+
} catch (e) {
21+
return void 0;
22+
}
23+
24+
if (/^[0-9a-f]{40}$/i.test(head)) {
25+
return head;
26+
}
27+
28+
const refMatch = /^ref: (.*)$/.exec(head);
29+
30+
if (!refMatch) {
31+
return void 0;
32+
}
33+
34+
const ref = refMatch[1];
35+
const refPath = path.join(git, ref);
36+
37+
try {
38+
return fs.readFileSync(refPath, 'utf8').trim();
39+
} catch (e) {
40+
// noop
41+
}
42+
43+
const packedRefsPath = path.join(git, 'packed-refs');
44+
let refsRaw:string;
45+
46+
try {
47+
refsRaw = fs.readFileSync(packedRefsPath, 'utf8').trim();
48+
} catch (e) {
49+
return void 0;
50+
}
51+
52+
const refsRegex = /^([0-9a-f]{40})\s+(.+)$/gm;
53+
let refsMatch: RegExpExecArray;
54+
let refs:{[ref:string]:string} = {};
55+
56+
while (refsMatch = refsRegex.exec(refsRaw)) {
57+
refs[refsMatch[2]] = refsMatch[1];
58+
}
59+
60+
return refs[ref];
61+
}

0 commit comments

Comments
 (0)