Skip to main content

qtcloud_devops_cli/code/
sync.rs

1use std::path::PathBuf;
2
3use crate::git::submodule::GitSubmoduleEditor;
4
5pub fn sync(root: PathBuf, name: &str) -> Result<(), String> {
6    let editor = GitSubmoduleEditor::new(root);
7    editor.sync_to_parent(name).map_err(|e| format!("同步失败: {}", e))
8}
9
10pub fn sync_all(root: PathBuf) -> Result<(), String> {
11    let editor = GitSubmoduleEditor::new(root);
12    editor.sync_all_to_parent().map_err(|e| format!("同步失败: {}", e))
13}
14
15#[cfg(test)]
16mod tests {
17    use super::*;
18
19    fn git_init(path: &std::path::Path) {
20        std::process::Command::new("git").args(["init", "-b", "main"]).current_dir(path).output().unwrap();
21        std::process::Command::new("git").args(["config", "user.email", "t@t"]).current_dir(path).output().unwrap();
22        std::process::Command::new("git").args(["config", "user.name", "t"]).current_dir(path).output().unwrap();
23    }
24
25    fn git_commit(path: &std::path::Path, msg: &str) {
26        std::fs::write(path.join("f"), msg).unwrap();
27        std::process::Command::new("git").args(["add", "."]).current_dir(path).output().unwrap();
28        std::process::Command::new("git").args(["commit", "-m", msg]).current_dir(path).output().unwrap();
29    }
30
31    #[test]
32    fn test_sync_nonexistent_name() {
33        let d = tempfile::tempdir().unwrap();
34        git_init(d.path()); git_commit(d.path(), "init");
35        assert!(sync(d.path().to_path_buf(), "no-such-module").is_err());
36    }
37
38    #[test]
39    fn test_sync_non_git_dir() {
40        let d = tempfile::tempdir().unwrap();
41        assert!(sync(d.path().to_path_buf(), "x").is_err());
42    }
43
44    #[test]
45    fn test_sync_all_empty_repo() {
46        let d = tempfile::tempdir().unwrap();
47        git_init(d.path()); git_commit(d.path(), "init");
48        assert!(sync_all(d.path().to_path_buf()).is_ok());
49    }
50}