Skip to main content

dev_prune/commands/
undo.rs

1// Copyright 2026 VKrishna04
2// SPDX-License-Identifier: Apache-2.0
3
4// Handler for `dev-prune undo` command.
5//
6// Reverts the most recent `init` or `link` action by un-registering
7// the repositories added in that pass.
8
9use anyhow::Result;
10
11use crate::config::Registry;
12use crate::output;
13
14pub fn run() -> Result<()> {
15    let mut registry = Registry::load()?;
16
17    if registry.last_added_repos.is_empty() {
18        output::print_warning("No recent repository additions to undo.");
19        return Ok(());
20    }
21
22    let repos_to_undo = registry.last_added_repos.clone();
23    let mut removed_count = 0;
24
25    for path in &repos_to_undo {
26        if registry.remove_repo(path) {
27            removed_count += 1;
28            output::print_info(&format!("Unregistered: {}", output::clean_path(path)));
29        }
30    }
31
32    registry.last_added_repos.clear();
33    registry.save()?;
34
35    output::print_header("Undo Operation Complete");
36    // The list can be stale — `unlink` removes a repository without touching it — so
37    // "unregistered 0 repositories" is a real outcome, and claiming success for it
38    // would leave the user believing something was reverted.
39    if removed_count == 0 {
40        output::print_warning("Those repositories were already unregistered; nothing to undo.");
41    } else {
42        output::print_success(&format!(
43            "Unregistered {removed_count} {}.",
44            output::plural(removed_count, "repository", "repositories")
45        ));
46    }
47
48    Ok(())
49}