qtcloud-devops-cli 0.10.0-alpha.1

量潮DevOps云命令行工具
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
use crate::git::scan::*;
use crate::git::types::*;
use std::path::{Path, PathBuf};

// ===== git operations =====

pub struct GitSubmoduleEditor {
    root: PathBuf,
    offline: bool,
}

impl GitSubmoduleEditor {
    pub fn new(root: PathBuf) -> Self {
        Self {
            root,
            offline: false,
        }
    }

    pub fn set_offline(&mut self, offline: bool) {
        self.offline = offline;
    }

    /// 检测是否有远端。优先用 gix,回退到 CLI。
    fn has_remote(path: &Path) -> bool {
        if let Ok(repo) = git2::Repository::open(path) {
            repo.find_remote("origin").is_ok()
        } else {
            false
        }
    }

    /// 获取当前 branch 名。
    fn branch_name(path: &Path) -> Option<String> {
        std::process::Command::new("git")
            .args(["rev-parse", "--abbrev-ref", "HEAD"])
            .current_dir(path)
            .output()
            .ok()
            .and_then(|o| {
                if o.status.success() {
                    let b = String::from_utf8_lossy(&o.stdout).trim().to_string();
                    if b != "HEAD" && !b.is_empty() {
                        Some(b)
                    } else {
                        None
                    }
                } else {
                    None
                }
            })
    }

    pub fn fetch_submodule(path: &Path) -> Result<(), ()> {
        let repo = match git2::Repository::open(path) {
            Ok(r) => r,
            Err(_) => return Ok(()),
        };
        let mut remote = match repo.find_remote("origin") {
            Ok(r) => r,
            Err(_) => return Ok(()),
        };
        remote
            .fetch(&[] as &[&str], None, None)
            .map_err(|_| ())
            .ok();
        Ok(())
    }

    pub fn rebase_submodule(path: &Path) -> Result<(), String> {
        if !path.exists() {
            return Ok(());
        }
        let branch = Self::branch_name(path).unwrap_or_default();
        if branch.is_empty() || branch == "HEAD" {
            return Ok(());
        }
        if !Self::has_remote(path) {
            return Ok(());
        }
        let output = std::process::Command::new("git")
            .args(["rebase", &format!("origin/{}", branch)])
            .current_dir(path)
            .output()
            .map_err(|e| format!("git rebase 无法执行: {}", e))?;
        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
            if stderr.contains("up to date") || stderr.contains("up-to-date") {
                return Ok(());
            }
            return Err(format!(
                "rebase 冲突,需手动处理:解决冲突后 git rebase --continue,或 git rebase --abort 放弃\n{}",
                stderr
            ));
        }
        Ok(())
    }

    pub fn push_submodule(path: &Path) -> Result<(), String> {
        if !path.exists() {
            return Ok(());
        }
        let branch = Self::branch_name(path).unwrap_or_default();
        if branch.is_empty() || branch == "HEAD" {
            return Ok(());
        }
        if !Self::has_remote(path) {
            return Ok(());
        }
        let tracking = format!("origin/{}", branch);
        let ahead = std::process::Command::new("git")
            .args(["rev-list", "--count", &format!("{}..{}", tracking, branch)])
            .current_dir(path)
            .output()
            .ok()
            .and_then(|o| {
                if o.status.success() {
                    String::from_utf8_lossy(&o.stdout)
                        .trim()
                        .parse::<i32>()
                        .ok()
                } else {
                    None
                }
            })
            .unwrap_or(0);
        if ahead <= 0 {
            return Ok(());
        }
        std::process::Command::new("git")
            .args(["push", "origin", &branch])
            .current_dir(path)
            .output()
            .map(|o| {
                if o.status.success() {
                    Ok(())
                } else {
                    Err(String::from_utf8_lossy(&o.stderr).trim().to_string())
                }
            })
            .unwrap_or_else(|e| Err(format!("git push 无法执行: {}", e)))
    }

    /// 用 git2 更新父仓库的子模块指针并提交。
    pub fn update_parent_pointer(
        root: &Path,
        sm_path: &Path,
        name: &str,
    ) -> Result<(), Box<dyn std::error::Error>> {
        let repo = git2::Repository::open(root)?;
        // index.add_path + write_tree + commit
        let mut index = repo.index()?;
        index.add_path(sm_path)?;
        index.write()?;
        let tree_id = index.write_tree()?;
        let tree = repo.find_tree(tree_id)?;
        let parent = repo.head()?.peel_to_commit()?;
        let sig = repo.signature()?;
        match repo.commit(
            Some("HEAD"),
            &sig,
            &sig,
            &format!("chore: 更新子模块 '{}' 指针", name),
            &tree,
            &[&parent],
        ) {
            Ok(_) => Ok(()),
            Err(e) => {
                let msg = e.message();
                if msg.contains("nothing to commit") || msg.contains("no changes") {
                    Ok(())
                } else {
                    Err(Box::new(e))
                }
            }
        }
    }

    pub fn push_parent(root: &Path) -> Result<(), String> {
        if !Self::has_remote(root) {
            return Ok(());
        }
        let branch = Self::branch_name(root).unwrap_or_default();
        if branch.is_empty() || branch == "HEAD" {
            return Err("无法检测当前分支".into());
        }
        std::process::Command::new("git")
            .args(["push", "origin", &branch])
            .current_dir(root)
            .output()
            .map(|o| {
                if o.status.success() {
                    Ok(())
                } else {
                    Err(String::from_utf8_lossy(&o.stderr).trim().to_string())
                }
            })
            .unwrap_or_else(|e| Err(format!("git push 无法执行: {}", e)))
    }

    /// 用 git2 回滚最近一次提交(`git reset --hard HEAD~1`)。
    pub fn revert_parent_commit(root: &Path) {
        if let Ok(repo) = git2::Repository::open(root) {
            if let Ok(head) = repo.find_reference("HEAD") {
                if let Some(target) = head.target() {
                    if let Ok(commit) = repo.find_commit(target) {
                        if let Ok(parent) = commit.parent(0) {
                            repo.reset(parent.as_object(), git2::ResetType::Hard, None)
                                .ok();
                        }
                    }
                }
            }
        }
    }

    pub fn root(&self) -> &Path {
        &self.root
    }

    fn find_sm_path(&self, name: &str) -> Option<PathBuf> {
        let entries = parse_gitmodules(&self.root);
        entries
            .into_iter()
            .find(|(n, _, _, _)| n == name)
            .map(|(_, p, _, _)| p)
    }

    pub fn sync_to_parent(&self, name: &str) -> Result<(), Box<dyn std::error::Error>> {
        let sm_path = self
            .find_sm_path(name)
            .ok_or_else(|| format!(".gitmodules 中未找到子模块 '{}'", name))?;
        let full_sm_path = self.root.join(&sm_path);

        if full_sm_path.exists() {
            Self::fetch_submodule(&full_sm_path).ok();
            Self::rebase_submodule(&full_sm_path)?;
        }
        Self::push_submodule(&full_sm_path).map_err(|e| format!("子模块 push 失败: {}", e))?;
        Self::update_parent_pointer(&self.root, &sm_path, name)?;
        if let Err(e) = Self::push_parent(&self.root) {
            Self::revert_parent_commit(&self.root);
            return Err(format!("父仓库 push 失败 (已回滚提交): {}", e).into());
        }
        println!("{}", name);
        Ok(())
    }

    pub fn sync_all_to_parent(&self) -> Result<(), Box<dyn std::error::Error>> {
        let entries = parse_gitmodules(&self.root);
        let names: Vec<String> = entries.into_iter().map(|(n, _, _, _)| n).collect();
        println!("同步 {} 个子模块", names.len());
        for name in &names {
            match self.sync_to_parent(name) {
                Ok(()) => {}
                Err(e) => println!("  {:<35} ✗ 失败: {}", name, e),
            }
        }
        Ok(())
    }

    pub fn status(&self) -> Result<Vec<HealthIssue>, Box<dyn std::error::Error>> {
        let state = RepoState::scan(&self.root)?;
        let mut issues = Vec::new();
        for sm in &state.submodules {
            if sm.status != SubmoduleStatus::Clean {
                let (description, action) = describe_issue(&sm.status);
                issues.push(HealthIssue {
                    submodule_name: sm.name.clone(),
                    status: format!("{:?}", sm.status),
                    description,
                    suggested_action: action,
                });
            }
        }
        Ok(issues)
    }
}
#[cfg(test)]
mod tests {
    fn git_init(path: &std::path::Path) {
        let repo = git2::Repository::init(path).unwrap();
        let mut cfg = repo.config().unwrap();
        cfg.set_str("user.email", "t@t").unwrap();
        cfg.set_str("user.name", "t").unwrap();
    }

    fn git_commit(path: &std::path::Path, msg: &str) {
        std::fs::write(path.join("f"), msg).unwrap();
        let repo = git2::Repository::open(path).unwrap();
        let mut index = repo.index().unwrap();
        index.add_path(std::path::Path::new("f")).unwrap();
        index.write().unwrap();
        let tree_id = index.write_tree().unwrap();
        let tree = repo.find_tree(tree_id).unwrap();
        let sig = repo.signature().unwrap();
        let parent = repo.head().and_then(|h| h.peel_to_commit()).ok();
        let parents: Vec<&git2::Commit> = parent.iter().collect();
        repo.commit(Some("HEAD"), &sig, &sig, msg, &tree, &parents)
            .unwrap();
    }

    use super::*;
    use std::process::Command;

    fn setup_repo_with_submodule(tmp: &Path) -> PathBuf {
        let parent = tmp.join("parent");
        let sub = tmp.join("sub");
        std::fs::create_dir_all(&sub).unwrap();
        git_init(&sub);
        git_commit(&sub, "init sub");
        std::fs::create_dir_all(&parent).unwrap();
        git_init(&parent);
        git_commit(&parent, "init parent");
        Command::new("git")
            .args(["submodule", "add", &sub.to_string_lossy(), "libs/sub"])
            .current_dir(&parent)
            .output()
            .unwrap();
        Command::new("git")
            .args(["commit", "-m", "add submodule"])
            .current_dir(&parent)
            .output()
            .unwrap();
        parent
    }

    // ---- GitSubmoduleEditor ----
    #[test]
    fn test_editor_new_and_root() {
        let e = GitSubmoduleEditor::new(PathBuf::from("/tmp"));
        assert_eq!(e.root(), std::path::Path::new("/tmp"));
    }
    #[test]
    fn test_editor_sync_to_parent() {
        let t = tempfile::tempdir().unwrap();
        let p = setup_repo_with_submodule(t.path());
        assert!(
            GitSubmoduleEditor::new(p)
                .sync_to_parent("libs/sub")
                .is_ok(),
            "sync_to_parent failed"
        );
    }
    #[test]
    fn test_editor_sync_to_parent_nonexistent() {
        let t = tempfile::tempdir().unwrap();
        std::fs::create_dir_all(t.path().join(".git")).unwrap();
        assert!(GitSubmoduleEditor::new(t.path().to_path_buf())
            .sync_to_parent("no-such-module")
            .is_err());
    }
    #[test]
    fn test_editor_sync_all_to_parent() {
        let t = tempfile::tempdir().unwrap();
        let p = setup_repo_with_submodule(t.path());
        assert!(GitSubmoduleEditor::new(p).sync_all_to_parent().is_ok());
    }
    #[test]
    fn test_editor_sync_all_to_parent_no_submodules() {
        let t = tempfile::tempdir().unwrap();
        git_init(t.path());
        git_commit(t.path(), "initial");
        assert!(GitSubmoduleEditor::new(t.path().to_path_buf())
            .sync_all_to_parent()
            .is_ok());
    }
    #[test]
    fn test_editor_status() {
        let t = tempfile::tempdir().unwrap();
        let p = setup_repo_with_submodule(t.path());
        assert!(GitSubmoduleEditor::new(p).status().unwrap().is_empty());
    }
    #[test]
    fn test_editor_status_with_gitmodules_but_no_repo() {
        let t = tempfile::tempdir().unwrap();
        std::fs::write(t.path().join(".gitmodules"), "").unwrap();
        assert!(GitSubmoduleEditor::new(t.path().to_path_buf())
            .status()
            .is_err());
    }

    #[test]
    fn test_editor_sync_with_remote_push() {
        let tmp = tempfile::tempdir().unwrap();
        let bare_sub = tmp.path().join("bare-sub");
        let bare_parent = tmp.path().join("bare-parent");
        for b in [&bare_sub, &bare_parent] {
            Command::new("git")
                .args(["init", "--bare", &b.to_string_lossy()])
                .output()
                .unwrap();
        }
        let sub = tmp.path().join("sub");
        Command::new("git")
            .args(["clone", &bare_sub.to_string_lossy(), &sub.to_string_lossy()])
            .current_dir(tmp.path())
            .output()
            .unwrap();
        git_init(&sub);
        git_commit(&sub, "init");
        Command::new("git")
            .args(["push", "origin", "main"])
            .current_dir(&sub)
            .output()
            .unwrap();
        let parent = tmp.path().join("parent");
        std::fs::create_dir_all(&parent).unwrap();
        git_init(&parent);
        git_commit(&parent, "init parent");
        Command::new("git")
            .args(["remote", "add", "origin", &bare_parent.to_string_lossy()])
            .current_dir(&parent)
            .output()
            .unwrap();
        Command::new("git")
            .args(["submodule", "add", &bare_sub.to_string_lossy(), "libs/sub"])
            .current_dir(&parent)
            .output()
            .unwrap();
        Command::new("git")
            .args(["commit", "-m", "add submodule"])
            .current_dir(&parent)
            .output()
            .unwrap();
        git_commit(&sub, "ahead");
        Command::new("git")
            .args(["push", "origin", "main"])
            .current_dir(&sub)
            .output()
            .unwrap();
        Command::new("git")
            .args(["fetch", "origin"])
            .current_dir(&parent.join("libs/sub"))
            .output()
            .unwrap();
        let result = GitSubmoduleEditor::new(parent).sync_to_parent("libs/sub");
        assert!(result.is_ok(), "sync failed: {:?}", result.err());
    }

    #[test]
    fn test_editor_sync_rebase_catches_up() {
        let tmp = tempfile::tempdir().unwrap();
        let bare_sub = tmp.path().join("bare-sub");
        let bare_parent = tmp.path().join("bare-parent");
        for b in [&bare_sub, &bare_parent] {
            Command::new("git")
                .args(["init", "--bare", &b.to_string_lossy()])
                .output()
                .unwrap();
        }
        let sub = tmp.path().join("sub");
        Command::new("git")
            .args(["clone", &bare_sub.to_string_lossy(), &sub.to_string_lossy()])
            .current_dir(tmp.path())
            .output()
            .unwrap();
        git_init(&sub);
        git_commit(&sub, "init");
        Command::new("git")
            .args(["push", "origin", "main"])
            .current_dir(&sub)
            .output()
            .unwrap();
        let init_hash = String::from_utf8_lossy(
            &Command::new("git")
                .args(["rev-parse", "HEAD"])
                .current_dir(&sub)
                .output()
                .unwrap()
                .stdout,
        )
        .trim()
        .to_string();
        let parent = tmp.path().join("parent");
        std::fs::create_dir_all(&parent).unwrap();
        git_init(&parent);
        git_commit(&parent, "init parent");
        Command::new("git")
            .args(["remote", "add", "origin", &bare_parent.to_string_lossy()])
            .current_dir(&parent)
            .output()
            .unwrap();
        Command::new("git")
            .args(["submodule", "add", &bare_sub.to_string_lossy(), "libs/sub"])
            .current_dir(&parent)
            .output()
            .unwrap();
        Command::new("git")
            .args(["commit", "-m", "add submodule"])
            .current_dir(&parent)
            .output()
            .unwrap();
        let sm_path = parent.join("libs/sub");
        assert_eq!(
            String::from_utf8_lossy(
                &Command::new("git")
                    .args(["rev-parse", "HEAD"])
                    .current_dir(&sm_path)
                    .output()
                    .unwrap()
                    .stdout
            )
            .trim()
            .to_string(),
            init_hash,
            "submodule starts at init"
        );
        git_commit(&sub, "remote ahead");
        Command::new("git")
            .args(["push", "origin", "main"])
            .current_dir(&sub)
            .output()
            .unwrap();
        let remote_hash = String::from_utf8_lossy(
            &Command::new("git")
                .args(["rev-parse", "HEAD"])
                .current_dir(&sub)
                .output()
                .unwrap()
                .stdout,
        )
        .trim()
        .to_string();
        assert!(
            GitSubmoduleEditor::new(parent)
                .sync_to_parent("libs/sub")
                .is_ok(),
            "sync failed"
        );
        assert_eq!(
            String::from_utf8_lossy(
                &Command::new("git")
                    .args(["rev-parse", "HEAD"])
                    .current_dir(&sm_path)
                    .output()
                    .unwrap()
                    .stdout
            )
            .trim()
            .to_string(),
            remote_hash,
            "submodule caught up to remote after sync"
        );
    }

    #[test]
    fn test_editor_status_with_dirty_submodule() {
        let t = tempfile::tempdir().unwrap();
        let p = setup_repo_with_submodule(t.path());
        std::fs::write(p.join("libs/sub/new-file"), "content").unwrap();
        let issues = GitSubmoduleEditor::new(p).status().unwrap();
        assert!(!issues.is_empty());
        assert_eq!(issues[0].status, "Dirty");
    }
}