Skip to content

Commit 908c866

Browse files
authored
Merge pull request #107 from pablodiegoss/master
Add Git info for user and version
2 parents e15894c + ae95e95 commit 908c866

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

Diff for: src/info.rs

+36-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use crate::{AsciiArt, CommitInfo, Configuration, Error, InfoFieldOn};
1414
type Result<T> = std::result::Result<T, crate::Error>;
1515

1616
pub struct Info {
17+
git_version: String,
18+
git_username: String,
1719
project_name: String,
1820
current_commit: CommitInfo,
1921
version: String,
@@ -40,7 +42,18 @@ impl std::fmt::Display for Info {
4042
Some(&c) => c,
4143
None => Color::White,
4244
};
43-
45+
if !self.disable_fields.git_info{
46+
let git_info;
47+
if self.git_username != "" {
48+
git_info = format!("{} : {}", self.git_username, self.git_version);
49+
write!(&mut buf, "{}{}", &self.get_formatted_info_label(&self.git_username, color), " : ")?;
50+
} else {
51+
git_info = self.git_version.clone();
52+
}
53+
write_buf(&mut buf, &self.get_formatted_info_label(&self.git_version, color), "")?;
54+
let separator = "-".repeat(git_info.len());
55+
write_buf(&mut buf, &self.get_formatted_info_label("", color), &separator)?;
56+
}
4457
if !self.disable_fields.project {
4558
write_buf(&mut buf, &self.get_formatted_info_label("Project: ", color), &self.project_name)?;
4659
}
@@ -190,6 +203,7 @@ impl Info {
190203
bold_flag: bool,
191204
) -> Result<Info> {
192205
let authors = Info::get_authors(&dir, 3);
206+
let (git_v, git_user) = Info::get_git_info(&dir);
193207
let current_commit_info = Info::get_current_commit_info(&dir)?;
194208
let config = Info::get_configuration(&dir)?;
195209
let version = Info::get_version(&dir)?;
@@ -202,6 +216,8 @@ impl Info {
202216
let dominant_language = Language::get_dominant_language(languages_stats.clone());
203217

204218
Ok(Info {
219+
git_version: git_v,
220+
git_username: git_user,
205221
project_name: config.repository_name,
206222
current_commit: current_commit_info,
207223
version,
@@ -266,6 +282,25 @@ impl Info {
266282
authors
267283
}
268284

285+
fn get_git_info(dir: &str) -> (String, String){
286+
let version = Command::new("git")
287+
.arg("--version")
288+
.output()
289+
.expect("Failed to execute git.");
290+
let version = String::from_utf8_lossy(&version.stdout).replace('\n',"");
291+
292+
let username = Command::new("git")
293+
.arg("-C")
294+
.arg(dir)
295+
.arg("config")
296+
.arg("--get")
297+
.arg("user.name")
298+
.output()
299+
.expect("Failed to execute git.");
300+
let username = String::from_utf8_lossy(&username.stdout).replace('\n',"");
301+
(version, username)
302+
}
303+
269304
fn get_current_commit_info(dir: &str) -> Result<CommitInfo> {
270305
let repo = Repository::open(dir).map_err(|_| Error::NotGitRepo)?;
271306
let head = repo.head().map_err(|_| Error::ReferenceInfoError)?;

Diff for: src/main.rs

+3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type Result<T> = result::Result<T, Error>;
3838

3939
#[derive(Default)]
4040
pub struct InfoFieldOn {
41+
git_info:bool,
4142
project: bool,
4243
head: bool,
4344
version: bool,
@@ -55,6 +56,7 @@ pub struct InfoFieldOn {
5556
#[derive(PartialEq, Eq, EnumString, EnumCount, EnumIter, IntoStaticStr)]
5657
#[strum(serialize_all = "snake_case")]
5758
enum InfoFields {
59+
GitInfo,
5860
Project,
5961
HEAD,
6062
Version,
@@ -193,6 +195,7 @@ Possible values: [{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}]",
193195
.unwrap_or(InfoFields::UnrecognizedField);
194196

195197
match item {
198+
InfoFields::GitInfo => disable_fields.git_info = true,
196199
InfoFields::Project => disable_fields.project = true,
197200
InfoFields::HEAD => disable_fields.head = true,
198201
InfoFields::Version => disable_fields.version = true,

0 commit comments

Comments
 (0)