aboutsummaryrefslogtreecommitdiff
path: root/hooked
diff options
context:
space:
mode:
authorMichael Gattozzi <mgattozzi@gmail.com>2019-11-22 09:52:39 -0500
committerMichael Gattozzi <mgattozzi@gmail.com>2019-11-22 09:52:39 -0500
commitb966334bf8c67cc82999d8b0962db15c8121bab7 (patch)
treeb74272358b7cffd4749905b02b16802dd95284af /hooked
parent2cd27b8dd40c7361f6f2b8832e210a58f30f3639 (diff)
Add hooked and empty inited hooks from the tool
This adds the hooked binary to the dev-suite repo as well as a stub for a program to be used in this workflow! Hooked works by adding the hooks into the repo and setting them to executable and linking them into the hooks directory under .git. This means hooks get to travel with the repo and are source controlled. All a dev needs to do is run the init command and hooked will symlink them all for them. No need to remember how ln works. It's all handled for you. Future work will iterate about what hooks that dev-suite supplies as part of the script. This will involve configuration files and per repo settings are something that will need to be thought about. Closes Issue #2
Diffstat (limited to 'hooked')
-rw-r--r--hooked/Cargo.toml13
-rw-r--r--hooked/src/bin/hooked-commit-msg.rs3
-rw-r--r--hooked/src/main.rs85
3 files changed, 101 insertions, 0 deletions
diff --git a/hooked/Cargo.toml b/hooked/Cargo.toml
new file mode 100644
index 0000000..acde074
--- /dev/null
+++ b/hooked/Cargo.toml
@@ -0,0 +1,13 @@
+[package]
+name = "hooked"
+version = "0.1.0"
+authors = ["Michael Gattozzi <mgattozzi@gmail.com>"]
+edition = "2018"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+anyhow = "1.0"
+paw = "1.0"
+shared = { path = "../shared" }
+structopt = { version = "0.3", features = ["paw"] }
diff --git a/hooked/src/bin/hooked-commit-msg.rs b/hooked/src/bin/hooked-commit-msg.rs
new file mode 100644
index 0000000..80a1832
--- /dev/null
+++ b/hooked/src/bin/hooked-commit-msg.rs
@@ -0,0 +1,3 @@
+fn main() {
+ println!("Hello, world!");
+}
diff --git a/hooked/src/main.rs b/hooked/src/main.rs
new file mode 100644
index 0000000..d412469
--- /dev/null
+++ b/hooked/src/main.rs
@@ -0,0 +1,85 @@
+#[cfg(windows)]
+use anyhow::bail;
+use anyhow::Result;
+use shared::find_root;
+#[cfg(not(windows))]
+use std::os::unix::fs::{
+ symlink,
+ PermissionsExt,
+};
+#[cfg(windows)]
+use std::os::windows::fs::symlink_file;
+use std::{
+ fs,
+ io::Write,
+};
+
+const HOOKS: [&str; 18] = [
+ "applypatch-msg",
+ "post-applypatch",
+ "pre-commit",
+ "prepare-commit-msg",
+ "commit-msg",
+ "post-commit",
+ "pre-rebase",
+ "post-checkout",
+ "post-merge",
+ "pre-push",
+ "pre-receive",
+ "update",
+ "post-receive",
+ "post-update",
+ "push-to-checkout",
+ "pre-auto-gc",
+ "post-rewrite",
+ "sendemail-validate",
+];
+
+#[derive(structopt::StructOpt)]
+enum Args {
+ /// Initialize the repo to use ticket
+ Init,
+}
+
+#[paw::main]
+fn main(args: Args) {
+ if let Err(e) = match args {
+ Args::Init => init(),
+ } {
+ eprintln!("{}", e);
+ std::process::exit(1);
+ }
+}
+
+fn init() -> Result<()> {
+ #[cfg(windows)]
+ bail!("Windows is currently unsupported!");
+
+ let root = find_root()?;
+ let git_hooks = &root.join(".git").join("hooks");
+ let root = root.join(".dev-suite").join("hooked");
+ fs::create_dir_all(&root)?;
+
+ for hook in &HOOKS {
+ let path = &root.join(hook);
+ let git_hook = &git_hooks.join(hook);
+
+ if !path.exists() {
+ let mut file = fs::File::create(&path)?;
+ let mut perms = file.metadata()?.permissions();
+ perms.set_mode(0o755);
+ file.set_permissions(perms)?;
+ file.write_all(b"#! /bin/bash")?;
+ }
+ let path = path.canonicalize()?;
+
+ if !git_hook.exists() {
+ #[cfg(not(windows))]
+ symlink(&path, &git_hook)?;
+ #[cfg(windows)]
+ symlink_file(&path, &git_hook)?;
+ }
+ }
+
+ Ok(())
+}