Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/build/lib/git.js
3520 views
1
"use strict";
2
var __importDefault = (this && this.__importDefault) || function (mod) {
3
return (mod && mod.__esModule) ? mod : { "default": mod };
4
};
5
Object.defineProperty(exports, "__esModule", { value: true });
6
exports.getVersion = getVersion;
7
/*---------------------------------------------------------------------------------------------
8
* Copyright (c) Microsoft Corporation. All rights reserved.
9
* Licensed under the MIT License. See License.txt in the project root for license information.
10
*--------------------------------------------------------------------------------------------*/
11
const path_1 = __importDefault(require("path"));
12
const fs_1 = __importDefault(require("fs"));
13
/**
14
* Returns the sha1 commit version of a repository or undefined in case of failure.
15
*/
16
function getVersion(repo) {
17
const git = path_1.default.join(repo, '.git');
18
const headPath = path_1.default.join(git, 'HEAD');
19
let head;
20
try {
21
head = fs_1.default.readFileSync(headPath, 'utf8').trim();
22
}
23
catch (e) {
24
return undefined;
25
}
26
if (/^[0-9a-f]{40}$/i.test(head)) {
27
return head;
28
}
29
const refMatch = /^ref: (.*)$/.exec(head);
30
if (!refMatch) {
31
return undefined;
32
}
33
const ref = refMatch[1];
34
const refPath = path_1.default.join(git, ref);
35
try {
36
return fs_1.default.readFileSync(refPath, 'utf8').trim();
37
}
38
catch (e) {
39
// noop
40
}
41
const packedRefsPath = path_1.default.join(git, 'packed-refs');
42
let refsRaw;
43
try {
44
refsRaw = fs_1.default.readFileSync(packedRefsPath, 'utf8').trim();
45
}
46
catch (e) {
47
return undefined;
48
}
49
const refsRegex = /^([0-9a-f]{40})\s+(.+)$/gm;
50
let refsMatch;
51
const refs = {};
52
while (refsMatch = refsRegex.exec(refsRaw)) {
53
refs[refsMatch[2]] = refsMatch[1];
54
}
55
return refs[ref];
56
}
57
//# sourceMappingURL=git.js.map
58