Skip to content

Commit

Permalink
More refactorings. (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
perlun authored Nov 24, 2016
1 parent 27ee48d commit 29899e0
Showing 1 changed file with 37 additions and 24 deletions.
61 changes: 37 additions & 24 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,18 @@
use std::env;
use std::process::Command;
use std::process::exit;
use std::process::Output;

struct Settings<'a> {
repository_path: &'a str,
from_revision: &'a str,
to_revision: &'a str
struct Changelog {
repository_path: String,
from_revision: String,
to_revision: String
}

impl<'a> Settings<'a> {
pub fn get_commits(self) {
let range = format!("{}..{}", self.from_revision, self.to_revision);

let output = Command::new("git")
.arg("log")
.arg("--oneline")
.arg(&range)
.current_dir(self.repository_path)
.output().unwrap_or_else(|e| panic!("Failed to run 'git log' with error: {}", e));

let stdout_output = String::from_utf8_lossy(&output.stdout);
let lines = stdout_output
.split('\n')
.collect::<Vec<_>>();
impl Changelog {
pub fn generate_changelog(self) {
let output = self.get_log_output();
let lines = Changelog::get_lines_from(&output);
let mut lines_iterator = lines.iter();

print!("## {}\n\n", self.to_revision);
Expand All @@ -37,18 +27,41 @@ impl<'a> Settings<'a> {
}
}
}

fn get_log_output(&self) -> String {
let output = Command::new("git")
.arg("log")
.arg("--oneline")
.arg(self.range())
.current_dir(&self.repository_path)
.output()
.unwrap_or_else(|e| panic!("Failed to run 'git log' with error: {}", e));
unsafe {
String::from_utf8_unchecked(output.stdout)
}
}

fn range(&self) -> String {
format!("{}..{}", self.from_revision, self.to_revision)
}

fn get_lines_from(output: &str) -> Vec<&str> {
output
.split('\n')
.collect()
}
}

fn main() {
let args: Vec<_> = env::args().collect();

if args.len() == 4 {
let settings = Settings {
repository_path: &args[1],
from_revision: &args[2],
to_revision: &args[3]
let changelog = Changelog {
repository_path: args[1].clone(),
from_revision: args[2].clone(),
to_revision: args[3].clone()
};
settings.get_commits();
changelog.generate_changelog();
}
else {
println!("Usage: {} <path> <from_revision> <to_revision>\n", args[0]);
Expand Down

0 comments on commit 29899e0

Please sign in to comment.