Skip to content

Commit

Permalink
feat(hooks): use git cli to get the correct git dir
Browse files Browse the repository at this point in the history
  • Loading branch information
umbopepato committed Oct 30, 2020
1 parent 0a14727 commit 26ed86c
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions src/git_hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,46 @@ vr run-hook ${hook} "$@"
`;
}

function areGitHooksInstalled(cwd: string): boolean {
return existsSync(path.join(cwd, ".git", "hooks", ".velociraptor"));
function areGitHooksInstalled(gitDir: string): boolean {
return existsSync(path.join(gitDir, "hooks", ".velociraptor"));
}

function installGitHooks(cwd: string) {
const hooksFolder = path.join(cwd, ".git", "hooks");
function installGitHooks(gitDir: string) {
const hooksDir = path.join(gitDir, "hooks");
hooks.forEach((hook) => {
const hookFile = path.join(hooksFolder, hook);
writeFileStrSync(hookFile, hookScript(hook));
const hookFile = path.join(hooksDir, hook);
Deno.writeTextFileSync(hookFile, hookScript(hook));
try {
Deno.chmodSync(hookFile, 0o0755);
} catch (e) {
// silently fail
// Windows
}
});
writeFileStrSync(path.join(hooksFolder, ".velociraptor"), "");
Deno.writeTextFileSync(path.join(hooksDir, ".velociraptor"), "");
console.log(`
${blue("Git hooks installed successfully")}
`);
}

export function checkGitHooks(config: ScriptsConfiguration, cwd: string) {
if (
!areGitHooksInstalled(cwd) &&
Object.values(config.scripts)
.filter((s) => s instanceof Object && isScriptObject(s)) // TODO move instanceof in isScriptObject
.some((s: any) => {
return "githook" in s && hooks.includes(s.githook);
})
) {
installGitHooks(cwd);
export async function checkGitHooks(configData: ConfigData) {
if (Deno.env.get(VR_HOOKS) === "false") return;
try {
const gitDir = await spawn(
["git", "rev-parse", "--git-common-dir"],
configData.cwd,
);
const absGitDir = path.join(configData.cwd, gitDir.trim());
if (
!areGitHooksInstalled(absGitDir) &&
Object.values(configData.config.scripts)
.filter(isScriptObject)
.some((s: any) => {
return "githook" in s && hooks.includes(s.githook);
})
) {
installGitHooks(absGitDir);
}
} catch (e) {
// Not a git repo
}
}

0 comments on commit 26ed86c

Please sign in to comment.