Skip to main content

git_workty/commands/
fetch.rs

1use crate::git::GitRepo;
2use crate::ui;
3use anyhow::{Context, Result};
4use std::process::Command;
5
6pub fn execute(repo: &GitRepo, all: bool) -> Result<()> {
7    ui::print_info("Fetching from remotes...");
8
9    let git_repo = repo.repo.lock().unwrap();
10    let remotes = git_repo.remotes().context("Failed to list remotes")?;
11
12    let remote_names: Vec<String> = if all {
13        remotes.iter().flatten().map(|s| s.to_string()).collect()
14    } else {
15        // Just fetch origin by default
16        vec!["origin".to_string()]
17    };
18
19    drop(git_repo); // Release the lock before running commands
20
21    for remote in &remote_names {
22        ui::print_info(&format!("  Fetching {}...", remote));
23
24        let output = Command::new("git")
25            .current_dir(&repo.root)
26            .args(["fetch", "--prune", remote])
27            .output()
28            .context("Failed to run git fetch")?;
29
30        if !output.status.success() {
31            let stderr = String::from_utf8_lossy(&output.stderr);
32            ui::print_warning(&format!("Failed to fetch {}: {}", remote, stderr.trim()));
33        }
34    }
35
36    ui::print_success(&format!(
37        "Fetched {} remote{}",
38        remote_names.len(),
39        if remote_names.len() == 1 { "" } else { "s" }
40    ));
41
42    Ok(())
43}