-
Notifications
You must be signed in to change notification settings - Fork 0
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
3 changed files
with
51 additions
and
1 deletion.
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,39 @@ | ||
use git2::Repository; | ||
use std::{ | ||
fs::{self, File}, | ||
io::Write, | ||
os::unix::fs::PermissionsExt, | ||
}; | ||
|
||
static HOOK: &str = "#!/bin/sh\nexec grawler check"; | ||
|
||
pub fn setup() { | ||
eprintln!("Creating a git pre-commit hook..."); | ||
|
||
let hooks_dir = Repository::discover(".") | ||
.map(|x| x.path().join("hooks")) | ||
.unwrap(); | ||
|
||
fs::create_dir_all(&hooks_dir).expect("Failed to create a dir for hooks"); | ||
|
||
let hook_path = hooks_dir.join("pre-commit"); | ||
|
||
if hook_path.exists() { | ||
log::warn!("The pre-commit hook already exists"); | ||
} | ||
|
||
let hook_bytes = HOOK.as_bytes(); | ||
let mut hook = File::create(&hook_path).expect("Failed to create a hook"); | ||
hook.write_all(hook_bytes) | ||
.expect("Failed to write to a hook"); | ||
|
||
let mut perms = hook | ||
.metadata() | ||
.map(|x| x.permissions()) | ||
.expect("Failed to get permissions from hook"); | ||
perms.set_mode(0o755); | ||
|
||
fs::set_permissions(hook_path, perms).expect("Failed to set new perms"); | ||
|
||
log::info!("Success! :)"); | ||
} |
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