Skip to content

Commit

Permalink
Display current commit and its references' names
Browse files Browse the repository at this point in the history
Signed-off-by: Nikos Filippakis <nikolaos.filippakis@cern.ch>
  • Loading branch information
nikofil committed Oct 2, 2019
1 parent 34bd253 commit bde2d86
Showing 1 changed file with 63 additions and 1 deletion.
64 changes: 63 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ extern crate clap;

use colored::Color;
use colored::*;
use git2::Repository;
use git2::{Repository, Oid};
use license::License;
use clap::{App, Arg};
use std::{
Expand All @@ -28,6 +28,7 @@ type Result<T> = result::Result<T, Error>;

struct Info {
project_name: String,
current_commit: CommitInfo,
version: String,
dominant_language: Language,
languages: Vec<(Language, f64)>,
Expand All @@ -54,6 +55,13 @@ impl fmt::Display for Info {
self.project_name
)?;

writeln!(
buffer,
"{}{}",
"HEAD: ".color(color).bold(),
self.current_commit
)?;

writeln!(
buffer,
"{}{}",
Expand Down Expand Up @@ -220,6 +228,30 @@ fn true_len(line: &str) -> usize {
.len()
}

struct CommitInfo {
commit: Oid,
refs: Vec<String>,
}

impl CommitInfo {
fn new(commit: Oid, refs: Vec<String>) -> CommitInfo {
CommitInfo { commit, refs }
}
}

impl fmt::Display for CommitInfo {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.refs.len() > 0 {
let refs_str = self.refs.iter().map(|ref_name| {
ref_name.as_str()
}).collect::<Vec<&str>>().join(", ");
write!(f, "{} ({})", self.commit, refs_str)
} else {
write!(f, "{}", self.commit)
}
}
}

#[derive(PartialEq, Eq, Hash, Clone)]
enum Language {
Assembly,
Expand Down Expand Up @@ -303,13 +335,15 @@ fn main() -> Result<()> {
let dominant_language = languages_stat_vec[0].0.clone();

let authors = get_authors(&dir, 3);
let current_commit_info = get_current_commit_info(&dir)?;
let config = get_configuration(&dir)?;
let version = get_version(&dir)?;
let commits = get_commits(&dir)?;
let last_change = get_last_change(&dir)?;

let info = Info {
project_name: config.repository_name,
current_commit: current_commit_info,
version,
dominant_language,
languages: languages_stat_vec,
Expand Down Expand Up @@ -527,6 +561,31 @@ fn get_authors(dir: &str, n: usize) -> Vec<String> {
authors
}

fn get_current_commit_info(dir: &str) -> Result<CommitInfo> {
let repo = Repository::open(dir).map_err(|_| Error::NotGitRepo)?;
let head = repo.head().map_err(|_| Error::ReferenceInfoError)?;
let head_oid = head.target().ok_or(Error::ReferenceInfoError)?;
let refs = repo.references().map_err(|_| Error::ReferenceInfoError)?;
let refs_info = refs.into_iter().filter_map(|reference| {
match reference {
Ok(reference) => {
match (reference.target(), reference.shorthand()) {
(Some(oid), Some(shorthand)) if oid == head_oid => {
Some(if reference.is_tag() {
String::from("tags/") + shorthand
} else {
String::from(shorthand)
})
},
_ => None
}
},
Err(_) => None,
}
}).collect::<Vec<String>>();
Ok(CommitInfo::new(head_oid, refs_info))
}

fn get_total_loc(languages: &tokei::Languages) -> usize {
languages
.values()
Expand Down Expand Up @@ -670,6 +729,8 @@ enum Error {
ReadDirectory,
/// Not in a Git Repo
NotGitRepo,
/// Error while getting branch info
ReferenceInfoError,
}

impl fmt::Debug for Error {
Expand All @@ -680,6 +741,7 @@ impl fmt::Debug for Error {
Error::NoGitData => "Could not retrieve git configuration data",
Error::ReadDirectory => "Could not read directory",
Error::NotGitRepo => "This is not a Git Repo",
Error::ReferenceInfoError => "Error while retrieving reference information",
};
write!(f, "{}", content)
}
Expand Down

0 comments on commit bde2d86

Please sign in to comment.