Skip to main content

track/cli/handlers/
sync.rs

1use crate::cli::handlers::CommandCtx;
2use crate::models::VcsMode;
3use crate::use_cases::{RepoSyncOutcome, SyncTaskUseCase};
4use crate::utils::{Result, TrackError};
5
6pub fn handle_sync(ctx: &CommandCtx) -> Result<()> {
7    let current_task_id = ctx
8        .db
9        .get_current_task_id()?
10        .ok_or(TrackError::NoActiveTask)?;
11
12    let outcome = SyncTaskUseCase::new(ctx.db).execute(current_task_id)?;
13
14    match outcome.vcs_mode {
15        VcsMode::Jj => {
16            println!("Syncing task bookmark: {}\n", outcome.task_bookmark);
17        }
18        VcsMode::Git => {
19            println!("Syncing git worktree branch: {}\n", outcome.task_bookmark);
20        }
21    }
22
23    for (repo_path, repo_outcome) in &outcome.repos {
24        println!("Repository: {}", repo_path);
25        match repo_outcome {
26            RepoSyncOutcome::Missing => {
27                println!("  ⚠ Repository not found, skipping\n");
28            }
29            RepoSyncOutcome::BookmarkCreated { base_ref, edit_ok } => {
30                println!(
31                    "  ✓ Bookmark {} created from {}",
32                    outcome.task_bookmark, base_ref
33                );
34                print_edit_result(&outcome.task_bookmark, *edit_ok);
35            }
36            RepoSyncOutcome::BookmarkExists { edit_ok } => {
37                println!("  ✓ Bookmark {} already exists", outcome.task_bookmark);
38                print_edit_result(&outcome.task_bookmark, *edit_ok);
39            }
40            RepoSyncOutcome::BookmarkCreateFailed { base_ref, detail } => {
41                println!(
42                    "  ✗ Failed to create bookmark {} from {} ({})",
43                    outcome.task_bookmark, base_ref, detail
44                );
45            }
46            RepoSyncOutcome::WorktreeCreated {
47                base_ref,
48                workspace_path,
49            } => {
50                println!(
51                    "  ✓ Worktree {} created from {} at {}",
52                    outcome.task_bookmark, base_ref, workspace_path
53                );
54            }
55            RepoSyncOutcome::WorktreeExists { workspace_path } => {
56                println!(
57                    "  ✓ Worktree {} already exists at {}",
58                    outcome.task_bookmark, workspace_path
59                );
60            }
61            RepoSyncOutcome::WorktreeCreateFailed { base_ref, detail } => {
62                println!(
63                    "  ✗ Failed to create worktree {} from {} ({})",
64                    outcome.task_bookmark, base_ref, detail
65                );
66            }
67        }
68    }
69
70    if outcome.vcs_mode == VcsMode::Jj {
71        println!("Checking for pending workspaces...");
72    }
73
74    for created in &outcome.workspaces_created {
75        println!(
76            "Creating workspace for TODO #{}: {}",
77            created.todo_index, created.todo_content
78        );
79        println!("  Created {} ({})", created.workspace_path, created.branch);
80    }
81
82    for err in &outcome.workspace_errors {
83        eprintln!(
84            "  Error creating workspace for {}: {}",
85            err.repo_path, err.detail
86        );
87    }
88
89    println!("Sync complete.");
90    Ok(())
91}
92
93fn print_edit_result(task_bookmark: &str, edit_ok: bool) {
94    if edit_ok {
95        println!("  ✓ Moved workspace to {}\n", task_bookmark);
96    } else {
97        println!("  ✗ Failed to move workspace to {}\n", task_bookmark);
98    }
99}