1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
extern crate rust_git_hooks;
use rust_git_hooks::*;
use std::fs::File;
use std::io::Write;
use std::io::Read;
use std::process;
use std::env;
fn main() {
log();
let commit_filename = env::args().nth(1);
let current_branch = get_current_branch();
match (current_branch, commit_filename) {
(Ok(branch), Some(filename)) => {
let write_result = prepend_branch_name(branch, filename);
match write_result {
Ok(_) => {},
Err(e) => {
eprintln!("Failed to prepend message. {}", e);
process::exit(2);
}
};
},
(Err(e), _) => {
eprintln!("Failed to find current branch. {}", e);
process::exit(1);
},
(_, None) => {
eprintln!("Commit file was not provided");
process::exit(2);
}
}
}
fn prepend_branch_name(branch_name: String, commit_filename: String) -> Result<(), std::io::Error> {
// It turns out that prepending a string to a file is not an
// obvious action. You can only write to the end of a file :(
//
// The solution is to read the existing contents, then write a new
// file starting with the branch name, and then writing the rest
// of the file.
let mut read_commit_file = File::open(commit_filename.clone())?;
let mut current_message = String::new();
read_commit_file.read_to_string(&mut current_message)?;
let mut commit_file = File::create(commit_filename)?;
writeln!(commit_file, "{}:", branch_name)?;
write!(commit_file, "{}", current_message)
}
|