Skip to content

Commit ba9361d

Browse files
author
Alex
committed
Check if git is available for version check
1 parent b4b7732 commit ba9361d

1 file changed

Lines changed: 24 additions & 8 deletions

File tree

server/config/VersionCheck.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,18 @@ class VersionCheck {
3131
private gitApiHost: string;
3232
private gitLatestReleaseJSONPath: string;
3333
private redirects: number;
34+
private gitAvailable: boolean;
3435
constructor() {
3536
this.userAgent = 'tagyoureit-nodejs-poolController-dashPanel-app';
3637
this.gitApiHost = 'api.github.com';
3738
this.gitLatestReleaseJSONPath = '/repos/rstrouse/nodejs-poolController-dashPanel/releases/latest';
39+
this.gitAvailable = this.detectGit();
40+
}
41+
private detectGit(): boolean {
42+
try {
43+
execSync('git --version', { stdio: 'ignore' });
44+
return true;
45+
} catch { return false; }
3846
}
3947

4048
public checkGitRemote() {
@@ -53,13 +61,15 @@ class VersionCheck {
5361
let out: string;
5462
try {
5563
if (typeof env.SOURCE_BRANCH !== 'undefined') {
56-
out = env.SOURCE_BRANCH // check for docker variable
64+
out = env.SOURCE_BRANCH; // provided via build/CI
5765
}
58-
else {
66+
else if (this.gitAvailable) {
5967
let res = execSync('git rev-parse --abbrev-ref HEAD');
6068
out = res.toString().trim();
69+
} else {
70+
out = '--';
6171
}
62-
logger.info(`The current git local branch output is ${out}`);
72+
if (out !== '--') logger.info(`The current git local branch output is ${out}`);
6373
switch (out) {
6474
case 'fatal':
6575
case 'command':
@@ -69,16 +79,21 @@ class VersionCheck {
6979
c.gitLocalBranch = out;
7080
}
7181
}
72-
catch (err) { logger.error(`Unable to retrieve local git branch. ${err}`); }
82+
catch (err) {
83+
logger.warn(`Git branch unavailable (git missing or error). Suppressing further branch errors.`);
84+
c.gitLocalBranch = '--';
85+
}
7386
try {
7487
if (typeof env.SOURCE_COMMIT !== 'undefined') {
75-
out = env.SOURCE_COMMIT; // check for docker variable
88+
out = env.SOURCE_COMMIT;
7689
}
77-
else {
90+
else if (this.gitAvailable) {
7891
let res = execSync('git rev-parse HEAD');
7992
out = res.toString().trim();
93+
} else {
94+
out = '--';
8095
}
81-
logger.info(`The current git local commit output is ${out}`);
96+
if (out !== '--') logger.info(`The current git local commit output is ${out}`);
8297
switch (out) {
8398
case 'fatal':
8499
case 'command':
@@ -89,7 +104,8 @@ class VersionCheck {
89104
}
90105
}
91106
catch (err) {
92-
logger.error(`Unable to retrieve local git commit. ${err}`);
107+
logger.warn(`Git commit unavailable (git missing or error). Suppressing further commit errors.`);
108+
c.gitLocalCommit = '--';
93109
}
94110
config.setSection('appVersion', c);
95111

0 commit comments

Comments
 (0)