Skip to content

Add Git info for user and version #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use crate::{AsciiArt, CommitInfo, Configuration, Error, InfoFieldOn};
type Result<T> = std::result::Result<T, crate::Error>;

pub struct Info {
git_version: String,
git_username: String,
project_name: String,
current_commit: CommitInfo,
version: String,
Expand All @@ -40,7 +42,18 @@ impl std::fmt::Display for Info {
Some(&c) => c,
None => Color::White,
};

if !self.disable_fields.git_info{
let git_info;
if self.git_username != "" {
git_info = format!("{} : {}", self.git_username, self.git_version);
write!(&mut buf, "{}{}", &self.get_formatted_info_label(&self.git_username, color), " : ")?;
} else {
git_info = self.git_version.clone();
}
write_buf(&mut buf, &self.get_formatted_info_label(&self.git_version, color), "")?;
let separator = "-".repeat(git_info.len());
write_buf(&mut buf, &self.get_formatted_info_label("", color), &separator)?;
}
if !self.disable_fields.project {
write_buf(&mut buf, &self.get_formatted_info_label("Project: ", color), &self.project_name)?;
}
Expand Down Expand Up @@ -190,6 +203,7 @@ impl Info {
bold_flag: bool,
) -> Result<Info> {
let authors = Info::get_authors(&dir, 3);
let (git_v, git_user) = Info::get_git_info(&dir);
let current_commit_info = Info::get_current_commit_info(&dir)?;
let config = Info::get_configuration(&dir)?;
let version = Info::get_version(&dir)?;
Expand All @@ -202,6 +216,8 @@ impl Info {
let dominant_language = Language::get_dominant_language(languages_stats.clone());

Ok(Info {
git_version: git_v,
git_username: git_user,
project_name: config.repository_name,
current_commit: current_commit_info,
version,
Expand Down Expand Up @@ -266,6 +282,25 @@ impl Info {
authors
}

fn get_git_info(dir: &str) -> (String, String){
let version = Command::new("git")
.arg("--version")
.output()
.expect("Failed to execute git.");
let version = String::from_utf8_lossy(&version.stdout).replace('\n',"");

let username = Command::new("git")
.arg("-C")
.arg(dir)
.arg("config")
.arg("--get")
.arg("user.name")
.output()
.expect("Failed to execute git.");
let username = String::from_utf8_lossy(&username.stdout).replace('\n',"");
(version, username)
}

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)?;
Expand Down
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Result<T> = result::Result<T, Error>;

#[derive(Default)]
pub struct InfoFieldOn {
git_info:bool,
project: bool,
head: bool,
version: bool,
Expand All @@ -55,6 +56,7 @@ pub struct InfoFieldOn {
#[derive(PartialEq, Eq, EnumString, EnumCount, EnumIter, IntoStaticStr)]
#[strum(serialize_all = "snake_case")]
enum InfoFields {
GitInfo,
Project,
HEAD,
Version,
Expand Down Expand Up @@ -193,6 +195,7 @@ Possible values: [{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}]",
.unwrap_or(InfoFields::UnrecognizedField);

match item {
InfoFields::GitInfo => disable_fields.git_info = true,
InfoFields::Project => disable_fields.project = true,
InfoFields::HEAD => disable_fields.head = true,
InfoFields::Version => disable_fields.version = true,
Expand Down