Last active
May 18, 2017 07:49
-
-
Save Michael-F-Bryan/4c475ce82eaa7e058a1d586e5d415186 to your computer and use it in GitHub Desktop.
An quick program for parsing `git log` output and capturing each commit's hash and commit message.
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
#[macro_use] | |
extern crate error_chain; | |
extern crate regex; | |
use std::process::Command; | |
use regex::Regex; | |
error_chain!{ | |
foreign_links { | |
Io(std::io::Error); | |
Regex(regex::Error); | |
Utf8(std::string::FromUtf8Error); | |
} | |
} | |
#[derive(PartialEq, Default, Clone, Debug)] | |
struct Commit { | |
hash: String, | |
message: String, | |
} | |
fn run() -> Result<()> { | |
let output = Command::new("git").arg("log").arg("--oneline").output()?; | |
if !output.status.success() { | |
bail!("Command executed with failing error code"); | |
} | |
let pattern = Regex::new(r"(?x) | |
([0-9a-fA-F]+) # commit hash | |
(.*) # The commit message")?; | |
let stdout = String::from_utf8(output.stdout)?; | |
let commits = stdout | |
.lines() | |
.filter_map(|line| pattern.captures(line)) | |
.map(|cap| { | |
Commit { | |
hash: cap[1].to_string(), | |
message: cap[2].trim().to_string(), | |
} | |
}); | |
for commit in commits { | |
println!("{:?}", commit); | |
} | |
Ok(()) | |
} | |
quick_main!(run); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Running this on my fork of
rust-cookbook
gives the following output:And the original command's output: