-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(git-hooks): draft git hooks integration
- Loading branch information
1 parent
7244a71
commit 210f0b7
Showing
5 changed files
with
102 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { | ||
writeFileStrSync, | ||
existsSync, | ||
path, | ||
} from "../deps.ts"; | ||
import { | ||
ScriptsConfiguration, | ||
isScriptObject, | ||
} from "./scripts_config.ts"; | ||
|
||
const hooks = [ | ||
"applypatch-msg", | ||
"pre-applypatch", | ||
"post-applypatch", | ||
"pre-commit", | ||
"pre-merge-commit", | ||
"prepare-commit-msg", | ||
"commit-msg", | ||
"post-commit", | ||
"pre-rebase", | ||
"post-checkout", | ||
"post-merge", | ||
"pre-push", | ||
"post-update", | ||
"push-to-checkout", | ||
"pre-auto-gc", | ||
"post-rewrite", | ||
"sendemail-validate", | ||
]; | ||
|
||
function hookScript(hook: string): string { | ||
return `!/bin/sh | ||
vr --git-hook=${hook} "$@" | ||
`; | ||
} | ||
|
||
function areGitHooksInstalled(cwd: string): boolean { | ||
return existsSync(path.join(cwd, ".git", "hooks", ".velociraptor")); | ||
} | ||
|
||
function installGitHooks(cwd: string) { | ||
const hooksFolder = path.join(cwd, ".git", "hooks"); | ||
hooks.forEach((hook) => { | ||
const hookFile = path.join(hooksFolder, hook); | ||
writeFileStrSync(hookFile, hookScript(hook)); | ||
try { | ||
Deno.chmodSync(hookFile, 0o0755); | ||
} catch (e) { | ||
// silently fail | ||
} | ||
}); | ||
writeFileStrSync(path.join(hooksFolder, ".velociraptor"), ""); | ||
} | ||
|
||
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); | ||
} | ||
} |
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