Skip to content

Commit

Permalink
Added the changelog URL (#5)
Browse files Browse the repository at this point in the history
...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.
  • Loading branch information
perlun authored Nov 27, 2016
1 parent 10bf5f4 commit 20747bf
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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
// [email protected]: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() {
Expand Down

0 comments on commit 20747bf

Please sign in to comment.