Skip to content

Instantly share code, notes, and snippets.

@ucarion
Created January 30, 2015 00:39
Show Gist options
  • Save ucarion/ecfaf80a0d122b05e89d to your computer and use it in GitHub Desktop.
Save ucarion/ecfaf80a0d122b05e89d to your computer and use it in GitHub Desktop.
Rust issue
pub fn read_file(path: &str) -> IoResult<String> {
let basepath = Path::new(".git");
File::open(&basepath.join(path)).read_to_string()
}
pub enum HeadState {
Detached(String),
Attached(String)
}
pub fn read_head() -> HeadState {
let head = read_file("HEAD").unwrap();
let head_slice = head.as_slice();
let re = regex!(r"ref: (.+)");
match re.captures(head_slice) {
None => {
HeadState::Detached(head.clone())
},
Some(captures) => {
HeadState::Attached(captures.at(1).unwrap().to_string())
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment