-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: detect missing git identity (#83)
- Loading branch information
Showing
5 changed files
with
183 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.checkGitIdentity = exports.gitStatus = void 0; | ||
const child_process_1 = require("child_process"); | ||
function getExecOptions(cwd) { | ||
return { cwd, encoding: "utf8" }; | ||
} | ||
function getUpstream(cwd) { | ||
return child_process_1.execSync("git rev-parse --abbrev-ref --symbolic-full-name @{u}", getExecOptions(cwd)).trim(); | ||
} | ||
function getCommitDifferences(cwd, remote) { | ||
// if upstream hard configured we use it | ||
const output = child_process_1.execSync(`git rev-list --left-right --count HEAD...${remote || getUpstream(cwd)}`, getExecOptions(cwd)).trim(); | ||
// something like "1\t0" | ||
return output.split("\t", 2).map(Number); | ||
} | ||
function hasUncommittedChanges(cwd) { | ||
const output = child_process_1.execSync(`git status --porcelain`, getExecOptions(cwd)).trim(); | ||
return output !== ""; | ||
} | ||
/** Locale-independent check for uncommitted changes and commit differences between local and remote branches */ | ||
function gitStatus(cwd, remote) { | ||
const [localDiff, remoteDiff] = getCommitDifferences(cwd, remote); | ||
if (localDiff > 0 && remoteDiff > 0) { | ||
return "diverged"; | ||
} | ||
else if (localDiff === 0 && remoteDiff > 0) { | ||
return "behind"; | ||
} | ||
else if (hasUncommittedChanges(cwd)) { | ||
return "uncommitted"; | ||
} /* if (remote === 0) */ | ||
else { | ||
return localDiff === 0 ? "up-to-date" : "ahead"; | ||
} | ||
} | ||
exports.gitStatus = gitStatus; | ||
function checkGitIdentity(cwd) { | ||
try { | ||
const username = child_process_1.execSync("git config --get user.name", getExecOptions(cwd)).trim(); | ||
const email = child_process_1.execSync("git config --get user.email", getExecOptions(cwd)).trim(); | ||
return username !== "" && email !== ""; | ||
} | ||
catch (e) { | ||
return false; | ||
} | ||
} | ||
exports.checkGitIdentity = checkGitIdentity; | ||
// // check if there are untracked changes | ||
// const gitStatus = execSync("git status", { cwd: rootDir, encoding: "utf8" }); | ||
// if (/have diverged/.test(gitStatus)) { | ||
// if (!isDryRun) { | ||
// fail( | ||
// colors.red( | ||
// "Cannot continue, the local branch has diverged from the git repo!", | ||
// ), | ||
// ); | ||
// } else { | ||
// console.log( | ||
// colors.red( | ||
// "This is a dry run. The full run would fail due to a diverged branch\n", | ||
// ), | ||
// ); | ||
// } | ||
// } else if (!lerna && !/working tree clean/.test(gitStatus)) { | ||
// if (!isDryRun && !allChanges) { | ||
// fail( | ||
// colors.red( | ||
// `Cannot continue, the local branch has uncommitted changes! Add them to a separate commit first or add the "--all" option to include them in the release commit.`, | ||
// ), | ||
// ); | ||
// } else { | ||
// if (allChanges) { | ||
// console.warn( | ||
// colors.yellow( | ||
// `Your branch has uncommitted changes that will be included in the release commit! | ||
// Consider adding them to a separate commit first. | ||
// `, | ||
// ), | ||
// ); | ||
// } else { | ||
// console.log( | ||
// colors.red( | ||
// `This is a dry run. The full run would fail due to uncommitted changes. | ||
// Add them to a separate commit first or add the "--all" option to include them in the release commit. | ||
// `, | ||
// ), | ||
// ); | ||
// } | ||
// } | ||
// } else if (/Your branch is behind/.test(gitStatus)) { | ||
// if (!isDryRun) { | ||
// fail( | ||
// colors.red( | ||
// "Cannot continue, the local branch is behind the remote changes!", | ||
// ), | ||
// ); | ||
// } else { | ||
// console.log( | ||
// colors.red( | ||
// "This is a dry run. The full run would fail due to the local branch being behind\n", | ||
// ), | ||
// ); | ||
// } | ||
// } else if ( | ||
// /Your branch is up\-to\-date/.test(gitStatus) || | ||
// /Your branch is ahead/.test(gitStatus) | ||
// ) { | ||
// // all good | ||
// if (!lerna) { | ||
// console.log(colors.green("git status is good - I can continue...")); | ||
// } | ||
// } |
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