From 098ece132ce02b59decfca02a592ed60099326b1 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sun, 1 May 2022 15:07:55 -0500 Subject: [PATCH] Code: fix commit hash not appearing --- app/soapbox/utils/code.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/app/soapbox/utils/code.js b/app/soapbox/utils/code.js index 49d6b8352..3316a931f 100644 --- a/app/soapbox/utils/code.js +++ b/app/soapbox/utils/code.js @@ -6,6 +6,14 @@ const pkg = require('../../../package.json'); const shortRepoName = url => new URL(url).pathname.substring(1); const trimHash = hash => hash.substring(0, 7); +const tryGit = cmd => { + try { + return String(execSync(cmd)); + } catch (e) { + return null; + } +}; + const version = pkg => { // Try to discern from GitLab CI first const { CI_COMMIT_TAG, CI_COMMIT_REF_NAME, CI_COMMIT_SHA } = process.env; @@ -19,14 +27,10 @@ const version = pkg => { } // Fall back to git directly - try { - const head = String(execSync('git rev-parse HEAD')); - const tag = String(execSync(`git rev-parse v${pkg.version}`)); + const head = tryGit('git rev-parse HEAD'); + const tag = tryGit(`git rev-parse v${pkg.version}`); - if (head !== tag) return `${pkg.version}-${trimHash(head)}`; - } catch (e) { - // Continue - } + if (head && head !== tag) return `${pkg.version}-${trimHash(head)}`; // Fall back to version in package.json return pkg.version;