git_wok/cmd/
lock.rs

1use anyhow::*;
2use std::io::Write;
3
4use crate::{config, repo};
5
6pub fn lock<W: Write>(
7    wok_config: &mut config::Config,
8    umbrella: &repo::Repo,
9    stdout: &mut W,
10) -> Result<()> {
11    // Ensure each repo is switched to its configured branch
12    for config_repo in &wok_config.repos {
13        if let Some(subrepo) = umbrella.get_subrepo_by_path(&config_repo.path) {
14            // Switch subrepo to its configured branch
15            subrepo.switch(&config_repo.head)?;
16        }
17    }
18
19    // Add all submodule changes to the index
20    let mut index = umbrella.git_repo.index()?;
21    for submodule in umbrella.git_repo.submodules()? {
22        let submodule_path = submodule.path();
23
24        // Only add submodules that have a head (are initialized)
25        if let Some(_submodule_oid) = submodule.head_id() {
26            // Add the submodule entry to the index
27            index.add_path(submodule_path)?;
28        }
29    }
30    index.write()?;
31
32    // Commit the submodule state
33    let commit_message = "Lock submodule state";
34    let signature = umbrella.git_repo.signature()?;
35    let tree_id = umbrella.git_repo.index()?.write_tree()?;
36    let tree = umbrella.git_repo.find_tree(tree_id)?;
37
38    let head_ref = umbrella.git_repo.head()?;
39    let parent_commit = head_ref.peel_to_commit()?;
40
41    umbrella.git_repo.commit(
42        Some("HEAD"),
43        &signature,
44        &signature,
45        commit_message,
46        &tree,
47        &[&parent_commit],
48    )?;
49
50    writeln!(stdout, "Locked submodule state")?;
51    Ok(())
52}