Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function parse(stdout, options = {}) {
const minimalShaRegex = /^([0-9a-f]{7,40})(-dirty)?$/;
// when git describe fails to locate tags, it returns only the minimal sha
if (minimalShaRegex.test(stdout)) {
// repo might still be dirty
const [, sha, isDirty] = minimalShaRegex.exec(stdout);
// count number of commits since beginning of time
const refCount = childProcess.execSync("git", ["rev-list", "--count", sha], options);
return { refCount, sha, isDirty: Boolean(isDirty) };
}
const [, lastTagName, lastVersion, refCount, sha, isDirty] =
/^((?:.*@)?(.*))-(\d+)-g([0-9a-f]+)(-dirty)?$/.exec(stdout) || [];
return { lastTagName, lastVersion, refCount, sha, isDirty: Boolean(isDirty) };
}
function sync(options = {}, includeMergedTags) {
const stdout = childProcess.execSync("git", getArgs(options, includeMergedTags), options);
const result = parse(stdout, options);
// only called by collect-updates with no matcher
log.silly("git-describe.sync", "%j => %j", stdout, result);
return result;
}
function updateRemote(opts) {
// git fetch, but for everything
childProcess.execSync("git", ["remote", "update"], opts);
}
function getLastCommit(execOpts) {
if (hasTags(execOpts)) {
log.silly("getLastTagInBranch");
return childProcess.execSync("git", ["describe", "--tags", "--abbrev=0"], execOpts);
}
log.silly("getFirstCommit");
return childProcess.execSync("git", ["rev-list", "--max-parents=0", "HEAD"], execOpts);
}
function currentBranch(opts) {
log.silly("currentBranch");
const branch = childProcess.execSync("git", ["rev-parse", "--abbrev-ref", "HEAD"], opts);
log.verbose("currentBranch", branch);
return branch;
}
function hasTags(opts) {
log.silly("hasTags");
let result = false;
try {
result = !!ChildProcessUtilities.execSync("git", ["tag"], opts);
} catch (err) {
log.warn("ENOTAGS", "No git tags were reachable from this branch!");
log.verbose("hasTags error", err);
}
log.verbose("hasTags", result);
return result;
}
execSync(cmd, args) {
return ChildProcessUtilities.execSync(cmd, args, this.execOpts);
}
function currentBranch(opts) {
log.silly("currentBranch");
const branch = childProcess.execSync("git", ["rev-parse", "--abbrev-ref", "HEAD"], opts);
log.verbose("currentBranch", branch);
return branch;
}
function getLastTag(opts) {
log.silly("getLastTag");
const lastTag = ChildProcessUtilities.execSync("git", ["describe", "--tags", "--abbrev=0"], opts);
log.verbose("getLastTag", lastTag);
return lastTag;
}