git_workspace/commands/
update.rs

1use super::map_repositories;
2use crate::commands::get_all_repositories_to_archive;
3use crate::lockfile::Lockfile;
4use anyhow::Context;
5use console::style;
6use std::path::Path;
7
8/// Update our workspace. This clones any new repositories and print old repositories to archives.
9pub fn update(workspace: &Path, threads: usize) -> anyhow::Result<()> {
10    // Load our lockfile
11    let lockfile = Lockfile::new(workspace.join("workspace-lock.toml"));
12    let repositories = lockfile.read().with_context(|| "Error reading lockfile")?;
13
14    println!("Updating {} repositories", repositories.len());
15
16    map_repositories(&repositories, threads, |r, progress_bar| {
17        // Only clone repositories that don't exist
18        if !r.exists(workspace) {
19            r.clone(workspace, progress_bar)?;
20            // Maybe this should always be run, but whatever. It's fine for now.
21            r.set_upstream(workspace)?;
22        }
23        Ok(())
24    })?;
25
26    let repos_to_archive = get_all_repositories_to_archive(workspace, repositories)?;
27    if !repos_to_archive.is_empty() {
28        println!(
29            "There are {} repositories that can be archived",
30            repos_to_archive.len()
31        );
32        println!(
33            "Run {} to archive them",
34            style("`git workspace archive`").yellow()
35        );
36    }
37
38    Ok(())
39}