Skip to content

Instantly share code, notes, and snippets.

@kstep
Last active March 2, 2016 17:52
Show Gist options
  • Save kstep/86b2460e79d109ffa5b9 to your computer and use it in GitHub Desktop.
Save kstep/86b2460e79d109ffa5b9 to your computer and use it in GitHub Desktop.
use std::env;
use std::ops::Deref;
use std::io::prelude::*;
use std::fs::File;
use std::process::Command;
fn main() {
let args = env::args().skip(1).take(3).collect::<Vec<_>>();
match args.get(1).map_or("", Deref::deref) {
"" | "message" => {
let mut branch = Command::new("git")
.arg("rev-parse")
.arg("--symbolic-full-name")
.arg("--abbrev-ref")
.arg("HEAD")
.output()
.unwrap()
.stdout;
branch.retain(|&b| b != b' ' && b != b'\n');
if !branch.is_empty() {
let message = File::open(&args[0]).and_then(|mut f| {
let mut buf = Vec::new();
f.read_to_end(&mut buf).map(|_| buf)
}).unwrap();
if !message.starts_with(&branch) {
File::create(&args[0]).and_then(|mut f| {
f.write_all(&branch).and_then(|_|
f.write(b" ")).and_then(|_|
f.write_all(&message))
}).unwrap();
}
}
}
_ => ()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment