Skip to content

Commit

Permalink
Add rayon for parallel iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisDavison committed Mar 14, 2021
1 parent ac111d3 commit c87005d
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 9 deletions.
114 changes: 114 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ description = "Wrapper around my common git commands"
shellexpand = "1.0"
anyhow = "1.0"
structopt = "0.3"

rayon = "1.5.0"
24 changes: 16 additions & 8 deletions src/git.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::*;
use anyhow::{anyhow, Result};
use rayon::prelude::*;
use std::path::Path;
use std::process::Command;

Expand Down Expand Up @@ -36,7 +37,11 @@ pub fn stat(p: &Path) -> Result<Option<String>> {
Ok(Some(format!("{}\n{}\n", p.display(), out_lines.join("\n"))))
} else {
// We aren't ahead or behind etc, but may have local uncommitted changes
let status: Vec<String> = out_lines.iter().skip(1).map(|x| x.to_string()).collect();
let status: Vec<String> = out_lines
.par_iter()
.skip(1)
.map(|x| x.to_string())
.collect();
if status.is_empty() {
Ok(None)
} else {
Expand All @@ -54,7 +59,7 @@ fn ahead_behind(p: &Path) -> Result<Option<String>> {
"refs/heads",
],
)?
.iter()
.par_iter()
.map(|x| x.trim_matches('\'').trim())
.filter(|x| {
let splits: Vec<&str> = x.split(' ').collect();
Expand Down Expand Up @@ -98,24 +103,27 @@ fn untracked(p: &Path) -> Result<Option<String>> {

pub fn branches(p: &Path) -> Result<Option<String>> {
let branches: String = command_output(p, &["branch"])?
.iter()
.par_iter()
.map(|x| x.trim())
.filter(|x| x.starts_with('*'))
.map(|x| &x[2..])
.collect();
let parentpath = p.parent().context("No parent for dir")?;
let parentpath = p.parent().ok_or(anyhow!("No parent for dir"))?;
let parentname = parentpath
.file_stem()
.context("No stem for parent")?
.ok_or(anyhow!("No stem for parent"))?
.to_string_lossy();
let dirname = p
.file_stem()
.ok_or(anyhow!("No stem for dir"))?
.to_string_lossy();
let dirname = p.file_stem().context("No stem for dir")?.to_string_lossy();
let dirstr = format!("{}/{}", parentname, dirname);
Ok(Some(format!("{:40}\t{}", dirstr, branches)))
}

pub fn branchstat(p: &Path) -> Result<Option<String>> {
let outputs = vec![ahead_behind(p)?, modified(p)?, status(p)?, untracked(p)?]
.iter()
.par_iter()
.filter(|&x| x.is_some())
.map(|x| x.as_ref().unwrap().as_str())
.collect::<Vec<&str>>()
Expand Down

0 comments on commit c87005d

Please sign in to comment.