-
-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
136 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,108 +1,65 @@ | ||
import {execSync} from "node:child_process"; | ||
|
||
/** | ||
* This file is created in the build step and is distributed through NPM | ||
* MUST be in sync with `-/gitDataPath.ts` and `package.json` files. | ||
*/ | ||
// This file is created in the build step and is distributed through NPM | ||
// MUST be in sync with `-/gitDataPath.ts` and `package.json` files. | ||
import {readGitDataFile, GitData} from "./gitDataPath"; | ||
|
||
/** Silent shell that won't pollute stdout, or stderr */ | ||
function shell(cmd: string): string { | ||
return execSync(cmd, {stdio: ["ignore", "pipe", "ignore"]}) | ||
.toString() | ||
.trim(); | ||
} | ||
|
||
/** Tries to get branch from git CLI. */ | ||
function getBranch(): string | undefined { | ||
try { | ||
return shell("git rev-parse --abbrev-ref HEAD"); | ||
} catch (e) { | ||
return undefined; | ||
} | ||
} | ||
|
||
/** Tries to get commit from git from git CLI. */ | ||
function getCommit(): string | undefined { | ||
try { | ||
return shell("git rev-parse --verify HEAD"); | ||
} catch (e) { | ||
return undefined; | ||
} | ||
} | ||
|
||
/** Tries to get the latest tag from git CLI. */ | ||
function getLatestTag(): string | undefined { | ||
try { | ||
return shell("git describe --abbrev=0"); | ||
} catch (e) { | ||
return undefined; | ||
} | ||
} | ||
|
||
/** Gets number of commits since latest tag/release. */ | ||
function getCommitsSinceRelease(): number | undefined { | ||
let numCommits = 0; | ||
const latestTag: string | undefined = getLatestTag(); | ||
try { | ||
numCommits = +shell(`git rev-list ${latestTag}..HEAD --count`); | ||
} catch (e) { | ||
return undefined; | ||
} | ||
return numCommits; | ||
} | ||
|
||
/** Reads git data from a persisted file or local git data at build time. */ | ||
export function readLodestarGitData(): GitData { | ||
export function readAndGetGitData(): GitData { | ||
try { | ||
const currentGitData = getGitData(); | ||
const persistedGitData = getPersistedGitData(); | ||
|
||
// If the CLI is run from source, prioritze current git data | ||
// over `.git-data.json` file, which might be stale here. | ||
let gitData = {...persistedGitData, ...currentGitData}; | ||
|
||
// If the CLI is not run from the git repository, fall back to persistent | ||
if (!gitData.semver || !gitData.branch || !gitData.commit) { | ||
gitData = persistedGitData; | ||
// Gets git data containing current branch and commit info from persistent file. | ||
let persistedGitData: Partial<GitData>; | ||
try { | ||
persistedGitData = readGitDataFile(); | ||
} catch (e) { | ||
persistedGitData = {}; | ||
} | ||
|
||
const currentGitData = getGitData(); | ||
|
||
return { | ||
semver: gitData?.semver, | ||
branch: gitData?.branch || "N/A", | ||
commit: gitData?.commit || "N/A", | ||
numCommits: gitData?.numCommits || "", | ||
// If the CLI is run from source, prioritze current git data | ||
// over `.git-data.json` file, which might be stale here. | ||
branch: currentGitData.branch ?? persistedGitData.branch ?? "", | ||
commit: currentGitData.commit ?? persistedGitData.commit ?? "", | ||
}; | ||
} catch (e) { | ||
return {semver: "", branch: "", commit: "", numCommits: ""}; | ||
return { | ||
branch: "", | ||
commit: "", | ||
}; | ||
} | ||
} | ||
|
||
/** Wrapper for updating git data. ONLY to be used with build scripts! */ | ||
export function forceUpdateGitData(): Partial<GitData> { | ||
return getGitData(); | ||
} | ||
|
||
/** Gets git data containing current branch and commit info from CLI. */ | ||
function getGitData(): Partial<GitData> { | ||
const numCommits: number | undefined = getCommitsSinceRelease(); | ||
let strCommits = ""; | ||
if (numCommits !== undefined && numCommits > 0) { | ||
strCommits = `+${numCommits}`; | ||
} | ||
export function getGitData(): GitData { | ||
return { | ||
branch: getBranch(), | ||
commit: getCommit(), | ||
semver: getLatestTag(), | ||
numCommits: strCommits, | ||
}; | ||
} | ||
|
||
/** Gets git data containing current branch and commit info from persistent file. */ | ||
function getPersistedGitData(): Partial<GitData> { | ||
/** Tries to get branch from git CLI. */ | ||
function getBranch(): string { | ||
try { | ||
return readGitDataFile(); | ||
return shellSilent("git rev-parse --abbrev-ref HEAD"); | ||
} catch (e) { | ||
return {}; | ||
return ""; | ||
} | ||
} | ||
|
||
/** Tries to get commit from git from git CLI. */ | ||
function getCommit(): string { | ||
try { | ||
return shellSilent("git rev-parse --verify HEAD"); | ||
} catch (e) { | ||
return ""; | ||
} | ||
} | ||
|
||
/** Silent shell that won't pollute stdout, or stderr */ | ||
function shellSilent(cmd: string): string { | ||
return execSync(cmd, {stdio: ["ignore", "pipe", "ignore"]}) | ||
.toString() | ||
.trim(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,10 @@ | ||
#!/usr/bin/env node | ||
|
||
/** | ||
* Persist git data and distribute through NPM so CLI consumers can know exactly | ||
* at what commit was this source build. This is also used in the metrics and to log initially. | ||
*/ | ||
// For RATIONALE of this file, check packages/cli/src/util/gitData/gitDataPath.ts | ||
// Persist exact commit in NPM distributions for easier tracking of the build | ||
|
||
import {getGitData} from "./index"; | ||
import {writeGitDataFile} from "./gitDataPath"; | ||
import {forceUpdateGitData} from "./index"; | ||
|
||
/** Script to write the git data file (json) used by the build procedures to persist git data. */ | ||
writeGitDataFile(forceUpdateGitData()); | ||
// Script to write the git data file (json) used by the build procedures to persist git data. | ||
writeGitDataFile(getGitData()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.