noosphere_cli/native/commands/sphere/
sync.rs

1use crate::native::{content::Content, workspace::Workspace};
2use anyhow::{anyhow, Result};
3use noosphere_core::context::{SphereSync, SyncExtent, SyncRecovery};
4
5/// Attempt to synchronize the local workspace with a configured gateway,
6/// optionally automatically retrying a fixed number of times in case a rebase
7/// is required, and then re-rendering the workspace as needed up to a given
8/// depth.
9pub async fn sync(auto_retry: u32, render_depth: Option<u32>, workspace: &Workspace) -> Result<()> {
10    workspace.ensure_sphere_initialized()?;
11
12    match Content::read_changes(workspace).await? {
13        Some((_, content_changes, _)) if !content_changes.is_empty() => {
14            return Err(anyhow!(
15                "You have unsaved local changes; save or revert them before syncing!"
16            ));
17        }
18        _ => (),
19    };
20
21    {
22        let mut context = workspace.sphere_context().await?;
23        context
24            .sync_with_options(SyncExtent::FetchAndPush, SyncRecovery::Retry(auto_retry))
25            .await?;
26    }
27
28    info!("Sync complete, rendering updated workspace...");
29
30    workspace.render(render_depth, false).await?;
31
32    info!("Done!");
33
34    Ok(())
35}