git_wok/cmd/
update.rs

1use anyhow::*;
2use std::io::Write;
3
4use crate::{config, repo};
5
6pub fn update<W: Write>(
7    wok_config: &mut config::Config,
8    umbrella: &repo::Repo,
9    stdout: &mut W,
10    no_commit: bool,
11) -> Result<()> {
12    writeln!(stdout, "Updating submodules...")?;
13
14    let mut saw_updates = false;
15    let mut saw_conflicts = false;
16
17    // Step 1: Update each repo with fetch and merge
18    for config_repo in &wok_config.repos {
19        if config_repo.is_skipped_for("update") {
20            continue;
21        }
22
23        if let Some(subrepo) = umbrella.get_subrepo_by_path(&config_repo.path) {
24            // Switch to configured branch first
25            subrepo.switch(&config_repo.head)?;
26
27            // Attempt to merge with remote changes
28            let merge_result = subrepo.merge(&config_repo.head)?;
29
30            // Get the current commit hash for reporting
31            let current_commit = get_current_commit_hash(&subrepo.git_repo)?;
32
33            // Report the result based on merge outcome
34            match merge_result {
35                repo::MergeResult::UpToDate => {
36                    writeln!(
37                        stdout,
38                        "- '{}': already up to date on '{}' ({})",
39                        config_repo.path.display(),
40                        config_repo.head,
41                        &current_commit[..8]
42                    )?;
43                },
44                repo::MergeResult::FastForward => {
45                    saw_updates = true;
46                    writeln!(
47                        stdout,
48                        "- '{}': fast-forwarded '{}' to {}",
49                        config_repo.path.display(),
50                        config_repo.head,
51                        &current_commit[..8]
52                    )?;
53                },
54                repo::MergeResult::Merged => {
55                    saw_updates = true;
56                    writeln!(
57                        stdout,
58                        "- '{}': merged '{}' to {}",
59                        config_repo.path.display(),
60                        config_repo.head,
61                        &current_commit[..8]
62                    )?;
63                },
64                repo::MergeResult::Conflicts => {
65                    saw_conflicts = true;
66                    writeln!(
67                        stdout,
68                        "- '{}': merge conflicts in '{}' ({}), manual resolution required",
69                        config_repo.path.display(),
70                        config_repo.head,
71                        &current_commit[..8]
72                    )?;
73                },
74            }
75        }
76    }
77
78    // Step 2: Stage all submodule changes in umbrella repo
79    let staged_changes = stage_submodule_changes(&umbrella.git_repo)?;
80
81    if saw_conflicts {
82        writeln!(
83            stdout,
84            "Skipped committing umbrella repo due to merge conflicts in subrepos"
85        )?;
86        return Ok(());
87    }
88
89    if no_commit {
90        if staged_changes || saw_updates {
91            writeln!(
92                stdout,
93                "Changes staged; commit skipped because --no-commit was provided"
94            )?;
95        } else {
96            writeln!(stdout, "No submodule updates detected; nothing to commit")?;
97        }
98        return Ok(());
99    }
100
101    // Step 3: Commit the updated submodule state
102    if !staged_changes {
103        writeln!(stdout, "No submodule updates detected; nothing to commit")?;
104        return Ok(());
105    }
106
107    commit_submodule_updates(&umbrella.git_repo)?;
108
109    writeln!(stdout, "Updated submodule state committed")?;
110    Ok(())
111}
112
113fn get_current_commit_hash(git_repo: &git2::Repository) -> Result<String> {
114    let head = git_repo.head()?;
115    let commit = head.peel_to_commit()?;
116    Ok(commit.id().to_string())
117}
118
119fn stage_submodule_changes(git_repo: &git2::Repository) -> Result<bool> {
120    let head_tree = git_repo
121        .head()
122        .ok()
123        .and_then(|head| head.peel_to_tree().ok());
124    let mut index = git_repo.index()?;
125
126    for submodule in git_repo.submodules()? {
127        let submodule_path = submodule.path();
128
129        // Only stage submodules that have a head (are initialized)
130        if let Some(_submodule_oid) = submodule.head_id() {
131            index.add_path(submodule_path)?;
132        }
133    }
134
135    index.write()?;
136
137    if let Some(tree) = head_tree.as_ref() {
138        let diff = git_repo.diff_tree_to_index(Some(tree), Some(&index), None)?;
139        Ok(diff.deltas().len() > 0)
140    } else {
141        Ok(!index.is_empty())
142    }
143}
144
145fn commit_submodule_updates(git_repo: &git2::Repository) -> Result<()> {
146    let commit_message = "Update submodules to latest";
147    let signature = git_repo.signature()?;
148    let tree_id = git_repo.index()?.write_tree()?;
149    let tree = git_repo.find_tree(tree_id)?;
150
151    let head_ref = git_repo.head()?;
152    let parent_commit = head_ref.peel_to_commit()?;
153
154    git_repo.commit(
155        Some("HEAD"),
156        &signature,
157        &signature,
158        commit_message,
159        &tree,
160        &[&parent_commit],
161    )?;
162
163    Ok(())
164}