gitgrip 0.20.0

Multi-repo workflow tool - manage multiple git repositories as one
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
//! Verify command implementation
//!
//! Provides boolean pass/fail assertions for agents.
//! Exit code 0 = pass, 1 = fail. With --json, always exits 0
//! and puts pass/fail in the JSON body.

use crate::cli::output::Output;
use crate::core::manifest::Manifest;
use crate::core::repo::{filter_repos, RepoInfo};
use crate::git::path_exists;
use crate::git::status::get_repo_status;
use std::path::{Path, PathBuf};

/// JSON-serializable verification result
#[derive(serde::Serialize)]
struct VerifyResult {
    pass: bool,
    checks: Vec<CheckResult>,
}

/// JSON-serializable individual check result
#[derive(serde::Serialize)]
struct CheckResult {
    name: String,
    pass: bool,
    details: Vec<serde_json::Value>,
}

/// Options for the verify command
pub struct VerifyOptions<'a> {
    pub workspace_root: &'a PathBuf,
    pub manifest: &'a Manifest,
    pub repos_filter: Option<&'a [String]>,
    pub group_filter: Option<&'a [String]>,
    pub json: bool,
    pub quiet: bool,
    pub clean: bool,
    pub links: bool,
    pub on_branch: Option<&'a str>,
    pub synced: bool,
}

/// Run the verify command
pub fn run_verify(opts: VerifyOptions) -> anyhow::Result<()> {
    let has_any_check = opts.clean || opts.links || opts.on_branch.is_some() || opts.synced;

    if !has_any_check {
        if opts.json {
            let result = VerifyResult {
                pass: false,
                checks: vec![CheckResult {
                    name: "no-checks".to_string(),
                    pass: false,
                    details: vec![serde_json::json!({
                        "error": "No verification flags provided. Use --clean, --links, --on-branch, or --synced."
                    })],
                }],
            };
            println!("{}", serde_json::to_string_pretty(&result)?);
            return Ok(());
        }
        anyhow::bail!(
            "No verification flags provided. Use --clean, --links, --on-branch, or --synced."
        );
    }

    let repos: Vec<RepoInfo> = filter_repos(
        opts.manifest,
        opts.workspace_root,
        opts.repos_filter,
        opts.group_filter,
        false,
    );

    let mut all_checks: Vec<CheckResult> = Vec::new();

    if opts.clean {
        all_checks.push(check_clean(&repos));
    }

    if opts.links {
        all_checks.push(check_links(opts.workspace_root, opts.manifest));
    }

    if let Some(branch) = opts.on_branch {
        all_checks.push(check_on_branch(&repos, branch));
    }

    if opts.synced {
        all_checks.push(check_synced(&repos));
    }

    let all_pass = all_checks.iter().all(|c| c.pass);

    if opts.json {
        let result = VerifyResult {
            pass: all_pass,
            checks: all_checks,
        };
        println!("{}", serde_json::to_string_pretty(&result)?);
        return Ok(());
    }

    // Human-readable output
    if !opts.quiet {
        Output::header("Verify");
        println!();
    }

    for check in &all_checks {
        if check.pass {
            Output::success(&format!("{}: passed", check.name));
        } else {
            Output::error(&format!("{}: failed", check.name));
            for detail in &check.details {
                if let Some(obj) = detail.as_object() {
                    let parts: Vec<String> = obj
                        .iter()
                        .map(|(k, v)| format!("{}={}", k, v.as_str().unwrap_or(&v.to_string())))
                        .collect();
                    println!("    {}", parts.join(", "));
                }
            }
        }
    }

    if !opts.quiet {
        println!();
    }

    if !all_pass {
        std::process::exit(1);
    }

    Ok(())
}

/// Check that all repos are clean (no uncommitted changes)
fn check_clean(repos: &[RepoInfo]) -> CheckResult {
    let mut details = Vec::new();
    let mut pass = true;

    for repo in repos {
        if !path_exists(&repo.absolute_path) {
            details.push(serde_json::json!({
                "repo": repo.name,
                "status": "not cloned"
            }));
            pass = false;
            continue;
        }

        let status = get_repo_status(repo);
        if !status.clean {
            details.push(serde_json::json!({
                "repo": repo.name,
                "staged": status.staged,
                "modified": status.modified,
                "untracked": status.untracked
            }));
            pass = false;
        }
    }

    CheckResult {
        name: "clean".to_string(),
        pass,
        details,
    }
}

/// Check that all file links (copyfile/linkfile) are valid
fn check_links(workspace_root: &Path, manifest: &Manifest) -> CheckResult {
    let mut details = Vec::new();
    let mut broken = 0;

    let repos: Vec<RepoInfo> = manifest
        .repos
        .iter()
        .filter_map(|(name, config)| {
            RepoInfo::from_config(
                name,
                config,
                workspace_root,
                &manifest.settings,
                manifest.remotes.as_ref(),
            )
        })
        .collect();

    for (name, config) in &manifest.repos {
        let repo = repos.iter().find(|r| &r.name == name);

        if let Some(ref copyfiles) = config.copyfile {
            for copyfile in copyfiles {
                let source = repo
                    .map(|r| r.absolute_path.join(&copyfile.src))
                    .unwrap_or_else(|| workspace_root.join(&config.path).join(&copyfile.src));
                let dest = workspace_root.join(&copyfile.dest);

                if !source.exists() || !dest.exists() {
                    broken += 1;
                    let reason = if !source.exists() {
                        "source missing"
                    } else {
                        "dest missing"
                    };
                    details.push(serde_json::json!({
                        "type": "copyfile",
                        "src": copyfile.src,
                        "dest": copyfile.dest,
                        "reason": reason
                    }));
                }
            }
        }

        if let Some(ref linkfiles) = config.linkfile {
            for linkfile in linkfiles {
                let source = repo
                    .map(|r| r.absolute_path.join(&linkfile.src))
                    .unwrap_or_else(|| workspace_root.join(&config.path).join(&linkfile.src));
                let dest = workspace_root.join(&linkfile.dest);

                let is_valid = source.exists() && dest.exists() && dest.is_symlink();
                if !is_valid {
                    broken += 1;
                    let reason = if !source.exists() {
                        "source missing"
                    } else if !dest.exists() {
                        "link missing"
                    } else {
                        "not a symlink"
                    };
                    details.push(serde_json::json!({
                        "type": "linkfile",
                        "src": linkfile.src,
                        "dest": linkfile.dest,
                        "reason": reason
                    }));
                }
            }
        }
    }

    CheckResult {
        name: "links".to_string(),
        pass: broken == 0,
        details,
    }
}

/// Check that all non-reference repos are on the expected branch
fn check_on_branch(repos: &[RepoInfo], expected_branch: &str) -> CheckResult {
    let mut details = Vec::new();
    let mut pass = true;

    for repo in repos {
        if !path_exists(&repo.absolute_path) {
            continue;
        }

        let status = get_repo_status(repo);
        if status.branch != expected_branch {
            details.push(serde_json::json!({
                "repo": repo.name,
                "expected": expected_branch,
                "actual": status.branch
            }));
            pass = false;
        }
    }

    CheckResult {
        name: "on-branch".to_string(),
        pass,
        details,
    }
}

/// Check that all repos are synced with their remote (not ahead or behind)
fn check_synced(repos: &[RepoInfo]) -> CheckResult {
    let mut details = Vec::new();
    let mut pass = true;

    for repo in repos {
        if !path_exists(&repo.absolute_path) {
            details.push(serde_json::json!({
                "repo": repo.name,
                "status": "not cloned"
            }));
            pass = false;
            continue;
        }

        let status = get_repo_status(repo);
        if status.ahead > 0 || status.behind > 0 {
            details.push(serde_json::json!({
                "repo": repo.name,
                "ahead": status.ahead,
                "behind": status.behind
            }));
            pass = false;
        }
    }

    CheckResult {
        name: "synced".to_string(),
        pass,
        details,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::manifest::PlatformType;

    fn make_repo_info(name: &str, path: &std::path::Path) -> RepoInfo {
        RepoInfo {
            name: name.to_string(),
            url: format!("file://{}", path.display()),
            path: name.to_string(),
            absolute_path: path.to_path_buf(),
            revision: "main".to_string(),
            target: "main".to_string(),
            sync_remote: "origin".to_string(),
            push_remote: "origin".to_string(),
            owner: "local".to_string(),
            repo: name.to_string(),
            platform_type: PlatformType::GitHub,
            platform_base_url: None,
            project: None,
            reference: false,
            groups: Vec::new(),
            agent: None,
            clone_strategy: crate::core::manifest::CloneStrategy::Clone,
        }
    }

    #[test]
    fn test_check_clean_with_clean_repos() {
        let temp = tempfile::TempDir::new().unwrap();
        let repo_path = temp.path().join("repo1");

        // Init a clean repo
        std::fs::create_dir_all(&repo_path).unwrap();
        std::process::Command::new("git")
            .args(["init", "-b", "main"])
            .current_dir(&repo_path)
            .output()
            .unwrap();
        std::process::Command::new("git")
            .args(["config", "user.email", "test@test.com"])
            .current_dir(&repo_path)
            .output()
            .unwrap();
        std::process::Command::new("git")
            .args(["config", "user.name", "Test"])
            .current_dir(&repo_path)
            .output()
            .unwrap();
        std::fs::write(repo_path.join("README.md"), "# test").unwrap();
        std::process::Command::new("git")
            .args(["add", "."])
            .current_dir(&repo_path)
            .output()
            .unwrap();
        std::process::Command::new("git")
            .args(["commit", "-m", "init"])
            .current_dir(&repo_path)
            .output()
            .unwrap();

        let repos = vec![make_repo_info("repo1", &repo_path)];
        let result = check_clean(&repos);
        assert!(result.pass);
        assert!(result.details.is_empty());
    }

    #[test]
    fn test_check_clean_with_dirty_repos() {
        let temp = tempfile::TempDir::new().unwrap();
        let repo_path = temp.path().join("repo1");

        // Init repo with uncommitted changes
        std::fs::create_dir_all(&repo_path).unwrap();
        std::process::Command::new("git")
            .args(["init", "-b", "main"])
            .current_dir(&repo_path)
            .output()
            .unwrap();
        std::process::Command::new("git")
            .args(["config", "user.email", "test@test.com"])
            .current_dir(&repo_path)
            .output()
            .unwrap();
        std::process::Command::new("git")
            .args(["config", "user.name", "Test"])
            .current_dir(&repo_path)
            .output()
            .unwrap();
        std::fs::write(repo_path.join("README.md"), "# test").unwrap();
        std::process::Command::new("git")
            .args(["add", "."])
            .current_dir(&repo_path)
            .output()
            .unwrap();
        std::process::Command::new("git")
            .args(["commit", "-m", "init"])
            .current_dir(&repo_path)
            .output()
            .unwrap();

        // Create an untracked file
        std::fs::write(repo_path.join("dirty.txt"), "dirty").unwrap();

        let repos = vec![make_repo_info("repo1", &repo_path)];
        let result = check_clean(&repos);
        assert!(!result.pass);
        assert_eq!(result.details.len(), 1);
        assert_eq!(result.details[0]["repo"], "repo1");
    }

    #[test]
    fn test_check_on_branch_matching() {
        let temp = tempfile::TempDir::new().unwrap();
        let repo_path = temp.path().join("repo1");

        std::fs::create_dir_all(&repo_path).unwrap();
        std::process::Command::new("git")
            .args(["init", "-b", "main"])
            .current_dir(&repo_path)
            .output()
            .unwrap();
        std::process::Command::new("git")
            .args(["config", "user.email", "test@test.com"])
            .current_dir(&repo_path)
            .output()
            .unwrap();
        std::process::Command::new("git")
            .args(["config", "user.name", "Test"])
            .current_dir(&repo_path)
            .output()
            .unwrap();
        std::fs::write(repo_path.join("README.md"), "# test").unwrap();
        std::process::Command::new("git")
            .args(["add", "."])
            .current_dir(&repo_path)
            .output()
            .unwrap();
        std::process::Command::new("git")
            .args(["commit", "-m", "init"])
            .current_dir(&repo_path)
            .output()
            .unwrap();

        let repos = vec![make_repo_info("repo1", &repo_path)];
        let result = check_on_branch(&repos, "main");
        assert!(result.pass);
        assert!(result.details.is_empty());
    }

    #[test]
    fn test_check_on_branch_mismatch() {
        let temp = tempfile::TempDir::new().unwrap();
        let repo_path = temp.path().join("repo1");

        std::fs::create_dir_all(&repo_path).unwrap();
        std::process::Command::new("git")
            .args(["init", "-b", "main"])
            .current_dir(&repo_path)
            .output()
            .unwrap();
        std::process::Command::new("git")
            .args(["config", "user.email", "test@test.com"])
            .current_dir(&repo_path)
            .output()
            .unwrap();
        std::process::Command::new("git")
            .args(["config", "user.name", "Test"])
            .current_dir(&repo_path)
            .output()
            .unwrap();
        std::fs::write(repo_path.join("README.md"), "# test").unwrap();
        std::process::Command::new("git")
            .args(["add", "."])
            .current_dir(&repo_path)
            .output()
            .unwrap();
        std::process::Command::new("git")
            .args(["commit", "-m", "init"])
            .current_dir(&repo_path)
            .output()
            .unwrap();

        let repos = vec![make_repo_info("repo1", &repo_path)];
        let result = check_on_branch(&repos, "feat/something");
        assert!(!result.pass);
        assert_eq!(result.details.len(), 1);
        assert_eq!(result.details[0]["expected"], "feat/something");
        assert_eq!(result.details[0]["actual"], "main");
    }
}