From 20747bf21ec871f543822717629058baccbb771e Mon Sep 17 00:00:00 2001 From: Per Lundberg Date: Sun, 27 Nov 2016 20:13:56 +0200 Subject: [PATCH] Added the changelog URL (#5) ...so users can get a clickable link to see the list of changes between the revisions. This approach makes a few assumptions: - The repo is hosted on GitHub. - The repo has a remote named `origin`, which points to the GitHub repo. --- src/main.rs | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 53b4646..e300b6b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,7 +16,9 @@ impl Changelog { let lines = Changelog::get_lines_from(&output); let mut lines_iterator = lines.iter(); - print!("## {}\n\n", self.to_revision); + println!("## {}", self.to_revision); + print!("[Full Changelog](https://github.com/{}/compare/{}...{})\n\n", + self.get_repo_slug(), self.from_revision, self.to_revision); loop { match lines_iterator.next() { @@ -49,6 +51,35 @@ impl Changelog { .split('\n') .collect() } + + fn get_repo_slug(&self) -> String { + let output = Command::new("git") + .arg("remote") + .arg("get-url") + .arg("origin") + .current_dir(&self.repository_path) + .output() + .unwrap_or_else(|e| panic!("Failed to run 'git log' with error: {}", e)); + + let url = String::from_utf8_lossy(&output.stdout).into_owned(); + + // The command output contains a trailing newline that we want to get rid of. + let trimmed_url = url.trim(); + + self.get_repo_slug_from(trimmed_url) + } + + fn get_repo_slug_from(&self, url: &str) -> String { + // This very simplistic and stupid algorithm works for repos of these forms: + // https://github.com/dekellum/fishwife.git + // git@github.com:chaos4ever/chaos.git + let mangled_url = url.replace(":", "/").replace(".git", ""); + let mut url_parts: Vec<_> = mangled_url.split('/').collect(); + let repo_name = url_parts.pop().unwrap(); + let org_name = url_parts.pop().unwrap(); + + format!("{}/{}", org_name, repo_name) + } } fn main() {