Skip to content

Commit

Permalink
rayon rather than threads
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisDavison committed Jan 23, 2024
1 parent 6eaa76e commit 7a8fdbe
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 22 deletions.
54 changes: 53 additions & 1 deletion Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "repoutil"
version = "0.23.0"
version = "0.24.0"
authors = ["Chris Davison <c.jr.davison@gmail.com>"]
edition = "2018"
license = "MIT"
Expand All @@ -13,3 +13,4 @@ description = "Wrapper around my common git commands"
shellexpand = "1.0"
anyhow = "1.0"
structopt = "0.3.26"
rayon = "1.8.1"
4 changes: 2 additions & 2 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,13 @@ pub fn branchstat(p: &Path, as_json: bool) -> Result<Option<String>> {
Ok(Some(if as_json {
format!(
"{{\"title\": \"{}\", \"subtitle\": \"{}\"}}",
p.display().to_string(),
p.display(),
outputs
)
} else {
format!(
"{:40} | {}",
p.display().to_string(),
p.display(),
outputs
)
}))
Expand Down
24 changes: 6 additions & 18 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use anyhow::{anyhow, Result};
use std::fs::read_dir;
use std::path::{Path, PathBuf};
use std::thread;
use structopt::StructOpt;
use rayon::prelude::*;

use shellexpand::tilde;

Expand Down Expand Up @@ -95,31 +95,19 @@ fn main() {

let all_repos = get_repos_from_config().expect("Couldn't get repos");

let handles = all_repos.clone().into_iter().map(|repo| {
// for repo in all_repos {
// Spawn a thread for each repo
// and run the chosen command.
// The handle must 'move' to take ownership of `cmd`
thread::spawn(move || match cmd(&repo, json) {
let mut messages: Vec<_> = all_repos.par_iter().map(|repo| {
match cmd(repo, json) {
Ok(Some(out)) => out,
Err(e) => format!("ERR Repo {}: {}", repo.display(), e),
_ => String::new(),
})
});

let mut messages = Vec::new();
for h in handles {
match h.join() {
Ok(msg) => messages.push(msg),
Err(e) => eprintln!("A child git command panic'd: {:?}", e),
}
}
}).collect();

messages.sort();
let messages = messages.iter().filter(|msg| !msg.is_empty());
if json {
println!(
"{{\"items\": [{}]}}",
messages
messages.iter()
.map(|x| x.to_string())
.collect::<Vec<String>>()
.join(",")
Expand Down

0 comments on commit 7a8fdbe

Please sign in to comment.