git_global/subcommands/
ahead.rs

1//! The `ahead` subcommand: shows repositories that have commits not pushed to a remote
2
3use std::sync::{mpsc, Arc};
4use std::thread;
5
6use crate::config::Config;
7use crate::errors::Result;
8use crate::repo::Repo;
9use crate::report::Report;
10
11/// Runs the `ahead` subcommand.
12pub fn execute(mut config: Config) -> Result<Report> {
13    let repos = config.get_repos();
14    let n_repos = repos.len();
15    let mut report = Report::new(&repos);
16    // TODO: limit number of threads, perhaps with mpsc::sync_channel(n)?
17    let (tx, rx) = mpsc::channel();
18    for repo in repos {
19        let tx = tx.clone();
20        let repo = Arc::new(repo);
21        thread::spawn(move || {
22            let path = repo.path();
23            let ahead = repo.is_ahead();
24            tx.send((path, ahead)).unwrap();
25        });
26    }
27    for _ in 0..n_repos {
28        let (path, ahead) = rx.recv().unwrap();
29        let repo = Repo::new(path.to_string());
30        if ahead {
31            report.add_repo_message(&repo, String::new());
32        }
33    }
34    Ok(report)
35}