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 for config_repo in &wok_config.repos {
13 if let Some(subrepo) = umbrella.get_subrepo_by_path(&config_repo.path) {
14 subrepo.switch(&config_repo.head)?;
16 }
17 }
18
19 let mut index = umbrella.git_repo.index()?;
21 for submodule in umbrella.git_repo.submodules()? {
22 let submodule_path = submodule.path();
23
24 if let Some(_submodule_oid) = submodule.head_id() {
26 index.add_path(submodule_path)?;
28 }
29 }
30 index.write()?;
31
32 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}