From 30b5d01e080d79e8bb0c5e03aaa6cb27e6d06638 Mon Sep 17 00:00:00 2001 From: Andrew Diamond Date: Sun, 17 Nov 2019 01:47:52 -0800 Subject: [PATCH 1/7] Add changes line --- src/info.rs | 40 ++++++++++++++++++++++++++++++++++++++++ src/main.rs | 3 +++ 2 files changed, 43 insertions(+) diff --git a/src/info.rs b/src/info.rs index 0320aef07..70872a101 100644 --- a/src/info.rs +++ b/src/info.rs @@ -27,6 +27,7 @@ pub struct Info { last_change: String, repo: String, commits: String, + changes: String, repo_size: String, number_of_lines: usize, license: String, @@ -191,6 +192,14 @@ impl std::fmt::Display for Info { )?; } + if !self.disable_fields.changes && self.changes != "" { + write_buf( + &mut buf, + &self.get_formatted_info_label("Changes: ", color), + &self.changes, + )?; + } + if !self.disable_fields.lines_of_code { write_buf( &mut buf, @@ -304,6 +313,7 @@ impl Info { let (git_v, git_user) = Info::get_git_info(workdir_str); let version = Info::get_version(workdir_str)?; let commits = Info::get_commits(workdir_str, no_merges)?; + let changes = Info::get_pending_changes(workdir_str)?; let repo_size = Info::get_packed_size(workdir_str)?; let last_change = Info::get_last_change(workdir_str)?; let creation_date = Info::get_creation_time(workdir_str)?; @@ -324,6 +334,7 @@ impl Info { last_change, repo: config.repository_url, commits, + changes, repo_size, number_of_lines, license: project_license, @@ -502,6 +513,35 @@ impl Info { } } + fn get_pending_changes(dir: &str) -> Result { + let output = Command::new("git") + .arg("-C") + .arg(dir) + .arg("diff") + .arg("--shortstat") + .arg("HEAD") + .output() + .expect("Failed to execute git."); + + let output = String::from_utf8_lossy(&output.stdout); + + if output == "" { + Ok("".into()) + } else { + let result = String::from(output) + .replace(",", &"") + .replace("\n", &"") + .replace(" files changed", &"+-") + .replace(" file changed", &"+-") + .replace(" insertions(+)", &"+") + .replace(" insertion(+)", &"+") + .replace(" deletions(-)", &"-") + .replace(" deletion(-)", &"-"); + + Ok(result.trim().into()) + } + } + fn get_packed_size(dir: &str) -> Result { let output = Command::new("git") .arg("-C") diff --git a/src/main.rs b/src/main.rs index a8803dc3c..6c2e39890 100644 --- a/src/main.rs +++ b/src/main.rs @@ -57,6 +57,7 @@ pub struct InfoFieldOn { last_change: bool, repo: bool, commits: bool, + changes: bool, lines_of_code: bool, size: bool, license: bool, @@ -75,6 +76,7 @@ enum InfoFields { LastChange, Repo, Commits, + Changes, LinesOfCode, Size, License, @@ -258,6 +260,7 @@ Possible values: [{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}]", InfoFields::Authors => disable_fields.authors = true, InfoFields::LastChange => disable_fields.last_change = true, InfoFields::Repo => disable_fields.repo = true, + InfoFields::Changes => disable_fields.changes = true, InfoFields::Commits => disable_fields.commits = true, InfoFields::LinesOfCode => disable_fields.lines_of_code = true, InfoFields::Size => disable_fields.size = true, From 0cd4e3518d415d8d534fb9923e7ad0f869d96685 Mon Sep 17 00:00:00 2001 From: Andrew Diamond Date: Sun, 17 Nov 2019 14:05:54 -0800 Subject: [PATCH 2/7] Change 'changes' to pending --- src/info.rs | 14 +++++++------- src/main.rs | 6 +++--- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/info.rs b/src/info.rs index 70872a101..5c6b73272 100644 --- a/src/info.rs +++ b/src/info.rs @@ -27,7 +27,7 @@ pub struct Info { last_change: String, repo: String, commits: String, - changes: String, + pending: String, repo_size: String, number_of_lines: usize, license: String, @@ -192,11 +192,11 @@ impl std::fmt::Display for Info { )?; } - if !self.disable_fields.changes && self.changes != "" { + if !self.disable_fields.pending && self.pending != "" { write_buf( &mut buf, - &self.get_formatted_info_label("Changes: ", color), - &self.changes, + &self.get_formatted_info_label("Pending: ", color), + &self.pending, )?; } @@ -313,7 +313,7 @@ impl Info { let (git_v, git_user) = Info::get_git_info(workdir_str); let version = Info::get_version(workdir_str)?; let commits = Info::get_commits(workdir_str, no_merges)?; - let changes = Info::get_pending_changes(workdir_str)?; + let pending = Info::get_pending_pending(workdir_str)?; let repo_size = Info::get_packed_size(workdir_str)?; let last_change = Info::get_last_change(workdir_str)?; let creation_date = Info::get_creation_time(workdir_str)?; @@ -334,7 +334,7 @@ impl Info { last_change, repo: config.repository_url, commits, - changes, + pending, repo_size, number_of_lines, license: project_license, @@ -513,7 +513,7 @@ impl Info { } } - fn get_pending_changes(dir: &str) -> Result { + fn get_pending_pending(dir: &str) -> Result { let output = Command::new("git") .arg("-C") .arg(dir) diff --git a/src/main.rs b/src/main.rs index 6c2e39890..1f1cc2aab 100644 --- a/src/main.rs +++ b/src/main.rs @@ -57,7 +57,7 @@ pub struct InfoFieldOn { last_change: bool, repo: bool, commits: bool, - changes: bool, + pending: bool, lines_of_code: bool, size: bool, license: bool, @@ -76,7 +76,7 @@ enum InfoFields { LastChange, Repo, Commits, - Changes, + Pending, LinesOfCode, Size, License, @@ -260,7 +260,7 @@ Possible values: [{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}]", InfoFields::Authors => disable_fields.authors = true, InfoFields::LastChange => disable_fields.last_change = true, InfoFields::Repo => disable_fields.repo = true, - InfoFields::Changes => disable_fields.changes = true, + InfoFields::Pending => disable_fields.pending = true, InfoFields::Commits => disable_fields.commits = true, InfoFields::LinesOfCode => disable_fields.lines_of_code = true, InfoFields::Size => disable_fields.size = true, From 887b65da959226748075e34431a9e04226324ff6 Mon Sep 17 00:00:00 2001 From: Andrew Diamond Date: Sun, 17 Nov 2019 14:07:29 -0800 Subject: [PATCH 3/7] Move pending under Head in output --- src/info.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/info.rs b/src/info.rs index 5c6b73272..c11b5b247 100644 --- a/src/info.rs +++ b/src/info.rs @@ -87,6 +87,14 @@ impl std::fmt::Display for Info { )?; } + if !self.disable_fields.pending && self.pending != "" { + write_buf( + &mut buf, + &self.get_formatted_info_label("Pending: ", color), + &self.pending, + )?; + } + if !self.disable_fields.version { write_buf( &mut buf, @@ -192,14 +200,6 @@ impl std::fmt::Display for Info { )?; } - if !self.disable_fields.pending && self.pending != "" { - write_buf( - &mut buf, - &self.get_formatted_info_label("Pending: ", color), - &self.pending, - )?; - } - if !self.disable_fields.lines_of_code { write_buf( &mut buf, From ffabfdb33f6ee7bef7fa7c1c09a21f18a6fef346 Mon Sep 17 00:00:00 2001 From: Andrew Diamond Date: Sun, 17 Nov 2019 15:33:02 -0800 Subject: [PATCH 4/7] Show file level changes --- src/info.rs | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/src/info.rs b/src/info.rs index c11b5b247..c6aae4072 100644 --- a/src/info.rs +++ b/src/info.rs @@ -517,9 +517,8 @@ impl Info { let output = Command::new("git") .arg("-C") .arg(dir) - .arg("diff") - .arg("--shortstat") - .arg("HEAD") + .arg("status") + .arg("--porcelain") .output() .expect("Failed to execute git."); @@ -528,17 +527,26 @@ impl Info { if output == "" { Ok("".into()) } else { - let result = String::from(output) - .replace(",", &"") - .replace("\n", &"") - .replace(" files changed", &"+-") - .replace(" file changed", &"+-") - .replace(" insertions(+)", &"+") - .replace(" insertion(+)", &"+") - .replace(" deletions(-)", &"-") - .replace(" deletion(-)", &"-"); - - Ok(result.trim().into()) + let lines = output.lines(); + + let mut deleted = 0; + let mut added = 0; + let mut modified = 0; + + for line in lines { + let prefix = &line[..2]; + + match prefix.trim() { + "D" => deleted += 1, + "A" | "??" => added += 1, + "M" | "MM" => modified += 1, + _ => {} + } + } + + let result = format!("{}+- {}+ {}-", modified, added, deleted); + + Ok(result.into()) } } From 6c74e8fa86eca44b85a9798902c89972225a3426 Mon Sep 17 00:00:00 2001 From: Andrew Diamond Date: Sun, 17 Nov 2019 20:03:43 -0800 Subject: [PATCH 5/7] Add missing match --- src/info.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/info.rs b/src/info.rs index c6aae4072..e6fc647d1 100644 --- a/src/info.rs +++ b/src/info.rs @@ -538,7 +538,7 @@ impl Info { match prefix.trim() { "D" => deleted += 1, - "A" | "??" => added += 1, + "A" | "AM" | "??" => added += 1, "M" | "MM" => modified += 1, _ => {} } From 876181e665680d3af0da178085c97647d40524d2 Mon Sep 17 00:00:00 2001 From: Andrew Diamond Date: Sun, 17 Nov 2019 20:18:34 -0800 Subject: [PATCH 6/7] Add another missing case for renamed files --- src/info.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/info.rs b/src/info.rs index e6fc647d1..27649f08d 100644 --- a/src/info.rs +++ b/src/info.rs @@ -539,7 +539,7 @@ impl Info { match prefix.trim() { "D" => deleted += 1, "A" | "AM" | "??" => added += 1, - "M" | "MM" => modified += 1, + "M" | "MM" | "R" => modified += 1, _ => {} } } From 4a3c136f3d80e83255d2c1455e7c9a05d58a7ced Mon Sep 17 00:00:00 2001 From: Andrew Diamond Date: Sun, 17 Nov 2019 20:19:33 -0800 Subject: [PATCH 7/7] Dont show zeros --- src/info.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/info.rs b/src/info.rs index 27649f08d..bfeb5fde6 100644 --- a/src/info.rs +++ b/src/info.rs @@ -544,7 +544,18 @@ impl Info { } } - let result = format!("{}+- {}+ {}-", modified, added, deleted); + let mut result = String::from(""); + if modified > 0 { + result = format!("{}+-", modified) + } + + if added > 0 { + result = format!("{} {}+", result, added); + } + + if deleted > 0 { + result = format!("{} {}-", result, deleted); + } Ok(result.into()) }