Skip to content

Commit e9f4f8d

Browse files
committed
Add git info for user and version
1 parent d74351d commit e9f4f8d

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

Diff for: src/info.rs

+31-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,
@@ -39,7 +41,13 @@ impl std::fmt::Display for Info {
3941
Some(&c) => c,
4042
None => Color::White,
4143
};
42-
44+
if !self.disable_fields.git_info{
45+
let mut git_info = String::from(&self.git_username);
46+
git_info.push(':');
47+
git_info.push_str(&self.git_version);
48+
write_buf(&mut buf, "", &git_info, color)?;
49+
write_buf(&mut buf, "", "--------------", color)?;
50+
}
4351
if !self.disable_fields.project {
4452
write_buf(&mut buf, "Project: ", &self.project_name, color)?;
4553
}
@@ -187,6 +195,7 @@ impl Info {
187195
disabled: InfoFieldOn,
188196
) -> Result<Info> {
189197
let authors = Info::get_authors(&dir, 3);
198+
let (git_v, git_user) = Info::get_git_info();
190199
let current_commit_info = Info::get_current_commit_info(&dir)?;
191200
let config = Info::get_configuration(&dir)?;
192201
let version = Info::get_version(&dir)?;
@@ -199,6 +208,8 @@ impl Info {
199208
let dominant_language = Language::get_dominant_language(languages_stats.clone());
200209

201210
Ok(Info {
211+
git_version: git_v,
212+
git_username: git_user,
202213
project_name: config.repository_name,
203214
current_commit: current_commit_info,
204215
version,
@@ -262,6 +273,25 @@ impl Info {
262273
authors
263274
}
264275

276+
fn get_git_info() -> (String, String){
277+
let version = Command::new("git")
278+
.arg("--version")
279+
.output()
280+
.expect("Failed to execute git.");
281+
let version = String::from_utf8_lossy(&version.stdout);
282+
let version = version.to_string().replace('\n',"");
283+
284+
let username = Command::new("git")
285+
.arg("config")
286+
.arg("--get")
287+
.arg("user.name")
288+
.output()
289+
.expect("Failed to execute git.");
290+
let username = String::from_utf8_lossy(&username.stdout);
291+
let username = username.to_string().replace('\n',"");
292+
(version, username)
293+
}
294+
265295
fn get_current_commit_info(dir: &str) -> Result<CommitInfo> {
266296
let repo = Repository::open(dir).map_err(|_| Error::NotGitRepo)?;
267297
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,
@@ -188,6 +190,7 @@ Possible values: [{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}]",
188190
.unwrap_or(InfoFields::UnrecognizedField);
189191

190192
match item {
193+
InfoFields::GitInfo => disable_fields.git_info = true,
191194
InfoFields::Project => disable_fields.project = true,
192195
InfoFields::HEAD => disable_fields.head = true,
193196
InfoFields::Version => disable_fields.version = true,

0 commit comments

Comments
 (0)