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
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
//! Multi-provider E2E tests
//!
//! These tests verify gitgrip operations across GitHub, GitLab, and Azure DevOps.
//! They use real test repositories and require authentication.
//!
//! Test repos (consistent naming across platforms):
//! - GitHub: laynepenney/gitgrip-test-1, gitgrip-test-2, gitgrip-test-manifest
//! - GitLab: laynepenney/gitgrip-test-1, gitgrip-test-2, gitgrip-test-manifest
//! - Azure DevOps: laynepenney/gitgrip/gitgrip-test-1, gitgrip-test-2, gitgrip-test-manifest
//!
//! Run with: cargo test --features integration-tests --test multi_provider_e2e -- --ignored

use std::fs;
use std::path::Path;
use std::process::Command;
use tempfile::TempDir;

/// Test repo URLs for each platform
mod test_repos {
    pub mod github {
        pub const REPO1: &str = "git@github.com:laynepenney/gitgrip-test-1.git";
        pub const REPO2: &str = "git@github.com:laynepenney/gitgrip-test-2.git";
        pub const MANIFEST: &str = "git@github.com:laynepenney/gitgrip-test-manifest.git";
    }

    pub mod gitlab {
        pub const REPO1: &str = "git@gitlab.com:laynepenney/gitgrip-test-1.git";
        pub const REPO2: &str = "git@gitlab.com:laynepenney/gitgrip-test-2.git";
        pub const MANIFEST: &str = "git@gitlab.com:laynepenney/gitgrip-test-manifest.git";
    }

    pub mod azure {
        pub const REPO1: &str = "git@ssh.dev.azure.com:v3/laynepenney/gitgrip/gitgrip-test-1";
        pub const REPO2: &str = "git@ssh.dev.azure.com:v3/laynepenney/gitgrip/gitgrip-test-2";
        pub const MANIFEST: &str =
            "git@ssh.dev.azure.com:v3/laynepenney/gitgrip/gitgrip-test-manifest";
    }
}

fn random_suffix() -> String {
    use std::time::{SystemTime, UNIX_EPOCH};
    let duration = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
    format!("{}", duration.as_millis() % 1_000_000)
}

/// Get path to the gr binary (builds it if needed)
fn gr_binary() -> std::path::PathBuf {
    // Build the binary first
    let status = Command::new("cargo")
        .args(["build", "--quiet", "--bin", "gr"])
        .current_dir(env!("CARGO_MANIFEST_DIR"))
        .status()
        .expect("Failed to build gr");
    assert!(status.success(), "Failed to build gr binary");

    // Return path to the binary
    std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("target/debug/gr")
}

/// Run gr command in the given workspace directory
/// For init, uses -p flag. For other commands, runs from workspace dir.
fn run_gr(args: &[&str], workspace: &Path) -> std::process::Output {
    let binary = gr_binary();

    // Check if this is an init command (needs -p flag)
    if args.first() == Some(&"init") {
        Command::new(&binary)
            .args(args)
            .current_dir(env!("CARGO_MANIFEST_DIR"))
            .output()
            .expect("Failed to run gr")
    } else {
        // Other commands - run from workspace directory
        Command::new(&binary)
            .args(args)
            .current_dir(workspace)
            .output()
            .expect("Failed to run gr")
    }
}

/// Run gr command in a directory (alias for run_gr for clarity)
fn run_gr_in_dir(args: &[&str], workspace: &Path) -> std::process::Output {
    run_gr(args, workspace)
}

/// Clone a repo and return the path
fn clone_repo(url: &str, name: &str, parent: &Path) -> std::path::PathBuf {
    let repo_path = parent.join(name);
    let output = Command::new("git")
        .args(["clone", url, name])
        .current_dir(parent)
        .output()
        .expect("Failed to clone");

    if !output.status.success() {
        panic!(
            "Failed to clone {}: {}",
            url,
            String::from_utf8_lossy(&output.stderr)
        );
    }

    repo_path
}

/// Create a test file with unique content
fn create_test_file(repo_path: &Path, filename: &str) -> String {
    let content = format!("Test content: {}\n", random_suffix());
    let file_path = repo_path.join(filename);
    fs::write(&file_path, &content).expect("Failed to write test file");
    content
}

/// Git operations in a repo
fn git_in_repo(args: &[&str], repo_path: &Path) -> std::process::Output {
    Command::new("git")
        .args(args)
        .current_dir(repo_path)
        .output()
        .expect("Failed to run git")
}

// ==================== GitHub Tests ====================

#[cfg(feature = "integration-tests")]
mod github_tests {
    use super::*;

    #[test]
    #[ignore = "Requires GitHub SSH access"]
    fn test_github_init_from_cloned_repos() {
        let temp = TempDir::new().unwrap();
        let workspace = temp.path();

        // Clone test repos
        clone_repo(test_repos::github::REPO1, "repo1", workspace);
        clone_repo(test_repos::github::REPO2, "repo2", workspace);

        // Run gr init --from-dirs
        let output = run_gr(
            &["init", "--from-dirs", "-p", workspace.to_str().unwrap()],
            workspace,
        );

        assert!(
            output.status.success(),
            "init failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );

        // Verify manifest
        let manifest_path =
            gitgrip::core::manifest_paths::resolve_gripspace_manifest_path(workspace)
                .expect("manifest not found");
        let manifest = fs::read_to_string(&manifest_path).unwrap();
        assert!(manifest.contains("repo1"), "repo1 not in manifest");
        assert!(manifest.contains("repo2"), "repo2 not in manifest");
        assert!(
            manifest.contains("github.com"),
            "github.com not in manifest"
        );
    }

    #[test]
    #[ignore = "Requires GitHub SSH access"]
    fn test_github_init_from_manifest_url() {
        let temp = TempDir::new().unwrap();
        // Use a subdirectory that doesn't exist yet
        let workspace = temp.path().join("workspace");

        // Initialize from manifest URL
        let output = run_gr(
            &[
                "init",
                test_repos::github::MANIFEST,
                "-p",
                workspace.to_str().unwrap(),
            ],
            &workspace,
        );

        assert!(
            output.status.success(),
            "init from manifest failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );

        // Sync to clone the repos defined in the manifest
        let output = run_gr(&["sync"], &workspace);
        assert!(
            output.status.success(),
            "sync failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );

        // Verify repos were cloned
        assert!(workspace.join("repo1").exists(), "repo1 not cloned");
        assert!(workspace.join("repo2").exists(), "repo2 not cloned");
    }

    #[test]
    #[ignore = "Requires GitHub SSH and API access"]
    fn test_github_full_pr_workflow() {
        let temp = TempDir::new().unwrap();
        let workspace = temp.path();
        let branch_name = format!("test/e2e-{}", random_suffix());

        // Clone test repos
        clone_repo(test_repos::github::REPO1, "repo1", workspace);
        clone_repo(test_repos::github::REPO2, "repo2", workspace);

        // Initialize workspace
        let output = run_gr(
            &["init", "--from-dirs", "-p", workspace.to_str().unwrap()],
            workspace,
        );
        assert!(output.status.success(), "init failed");

        // Create branch
        let output = run_gr_in_dir(&["branch", &branch_name], workspace);
        assert!(
            output.status.success(),
            "branch failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );

        // Make changes
        create_test_file(&workspace.join("repo1"), "test-file.txt");
        create_test_file(&workspace.join("repo2"), "test-file.txt");

        // Stage and commit
        let output = run_gr_in_dir(&["add", "."], workspace);
        assert!(output.status.success(), "add failed");

        let output = run_gr_in_dir(&["commit", "-m", "test: e2e test commit"], workspace);
        assert!(
            output.status.success(),
            "commit failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );

        // Push
        let output = run_gr_in_dir(&["push", "-u"], workspace);
        assert!(
            output.status.success(),
            "push failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );

        // Create PR
        let output = run_gr_in_dir(&["pr", "create", "-t", "test: E2E test PR"], workspace);
        assert!(
            output.status.success(),
            "pr create failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );

        // Check PR status
        let output = run_gr_in_dir(&["pr", "status"], workspace);
        assert!(
            output.status.success(),
            "pr status failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );

        // Cleanup: close PRs and delete branch
        git_in_repo(
            &["push", "origin", "--delete", &branch_name],
            &workspace.join("repo1"),
        );
        git_in_repo(
            &["push", "origin", "--delete", &branch_name],
            &workspace.join("repo2"),
        );
    }

    #[test]
    #[ignore = "Requires GitHub SSH access"]
    fn test_github_status_shows_all_repos() {
        let temp = TempDir::new().unwrap();
        let workspace = temp.path();

        clone_repo(test_repos::github::REPO1, "repo1", workspace);
        clone_repo(test_repos::github::REPO2, "repo2", workspace);

        let output = run_gr(
            &["init", "--from-dirs", "-p", workspace.to_str().unwrap()],
            workspace,
        );
        assert!(output.status.success());

        let output = run_gr_in_dir(&["status"], workspace);
        assert!(
            output.status.success(),
            "status failed: stdout={}, stderr={}",
            String::from_utf8_lossy(&output.stdout),
            String::from_utf8_lossy(&output.stderr)
        );

        let stdout = String::from_utf8_lossy(&output.stdout);
        assert!(
            stdout.contains("repo1") || stdout.contains("gitgrip-test-1"),
            "repo1/gitgrip-test-1 not in status output: {}",
            stdout
        );
        assert!(
            stdout.contains("repo2") || stdout.contains("gitgrip-test-2"),
            "repo2/gitgrip-test-2 not in status output: {}",
            stdout
        );
    }
}

// ==================== GitLab Tests ====================

#[cfg(feature = "integration-tests")]
mod gitlab_tests {
    use super::*;

    #[test]
    #[ignore = "Requires GitLab SSH access"]
    fn test_gitlab_init_from_cloned_repos() {
        let temp = TempDir::new().unwrap();
        let workspace = temp.path();

        clone_repo(test_repos::gitlab::REPO1, "repo1", workspace);
        clone_repo(test_repos::gitlab::REPO2, "repo2", workspace);

        let output = run_gr(
            &["init", "--from-dirs", "-p", workspace.to_str().unwrap()],
            workspace,
        );

        assert!(
            output.status.success(),
            "init failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );

        let manifest_path =
            gitgrip::core::manifest_paths::resolve_gripspace_manifest_path(workspace)
                .expect("manifest not found");
        let manifest = fs::read_to_string(&manifest_path).unwrap();
        assert!(manifest.contains("repo1"));
        assert!(manifest.contains("repo2"));
        assert!(manifest.contains("gitlab.com"));
    }

    #[test]
    #[ignore = "Requires GitLab SSH access"]
    fn test_gitlab_init_from_manifest_url() {
        let temp = TempDir::new().unwrap();
        let workspace = temp.path().join("workspace");

        let output = run_gr(
            &[
                "init",
                test_repos::gitlab::MANIFEST,
                "-p",
                workspace.to_str().unwrap(),
            ],
            &workspace,
        );

        assert!(
            output.status.success(),
            "init from manifest failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );

        // Sync to clone the repos
        let output = run_gr(&["sync"], &workspace);
        assert!(
            output.status.success(),
            "sync failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );

        assert!(workspace.join("repo1").exists(), "repo1 not cloned");
        assert!(workspace.join("repo2").exists(), "repo2 not cloned");
    }

    #[test]
    #[ignore = "Requires GitLab SSH and API access"]
    fn test_gitlab_branch_and_push() {
        let temp = TempDir::new().unwrap();
        let workspace = temp.path();
        let branch_name = format!("test/e2e-{}", random_suffix());

        clone_repo(test_repos::gitlab::REPO1, "repo1", workspace);
        clone_repo(test_repos::gitlab::REPO2, "repo2", workspace);

        let output = run_gr(
            &["init", "--from-dirs", "-p", workspace.to_str().unwrap()],
            workspace,
        );
        assert!(output.status.success());

        // Create branch
        let output = run_gr_in_dir(&["branch", &branch_name], workspace);
        assert!(
            output.status.success(),
            "branch failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );

        // Make changes and commit
        create_test_file(&workspace.join("repo1"), "gitlab-test.txt");

        let output = run_gr_in_dir(&["add", "."], workspace);
        assert!(output.status.success());

        let output = run_gr_in_dir(&["commit", "-m", "test: gitlab e2e"], workspace);
        assert!(output.status.success());

        // Push
        let output = run_gr_in_dir(&["push", "-u"], workspace);
        assert!(
            output.status.success(),
            "push failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );

        // Cleanup
        git_in_repo(
            &["push", "origin", "--delete", &branch_name],
            &workspace.join("repo1"),
        );
    }

    #[test]
    #[ignore = "Requires GitLab SSH and API access"]
    fn test_gitlab_full_pr_workflow() {
        let temp = TempDir::new().unwrap();
        let workspace = temp.path();
        let branch_name = format!("test/e2e-{}", random_suffix());

        // Clone test repos
        clone_repo(test_repos::gitlab::REPO1, "repo1", workspace);
        clone_repo(test_repos::gitlab::REPO2, "repo2", workspace);

        // Initialize workspace
        let output = run_gr(
            &["init", "--from-dirs", "-p", workspace.to_str().unwrap()],
            workspace,
        );
        assert!(
            output.status.success(),
            "init failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );
        println!("Init output: {}", String::from_utf8_lossy(&output.stdout));

        // Read and print the manifest
        let manifest_path =
            gitgrip::core::manifest_paths::resolve_gripspace_manifest_path(workspace)
                .expect("manifest not found");
        if manifest_path.exists() {
            let manifest_content = fs::read_to_string(&manifest_path).unwrap();
            println!("Manifest content:\n{}", manifest_content);
        } else {
            println!(
                "WARNING: Manifest file does not exist at {:?}",
                manifest_path
            );
        }

        // Check initial status
        let output = run_gr_in_dir(&["status"], workspace);
        println!(
            "Initial status: {}",
            String::from_utf8_lossy(&output.stdout)
        );

        // Create branch
        let output = run_gr_in_dir(&["branch", &branch_name], workspace);
        assert!(
            output.status.success(),
            "branch failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );
        println!(
            "Branch created: {}",
            String::from_utf8_lossy(&output.stdout)
        );

        // Make changes in both repos
        create_test_file(&workspace.join("repo1"), "test-file.txt");
        create_test_file(&workspace.join("repo2"), "test-file.txt");

        // Check status after changes
        let output = run_gr_in_dir(&["status"], workspace);
        println!(
            "Status after changes: {}",
            String::from_utf8_lossy(&output.stdout)
        );

        // Stage and commit
        let output = run_gr_in_dir(&["add", "."], workspace);
        assert!(
            output.status.success(),
            "add failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );
        println!("Add output: {}", String::from_utf8_lossy(&output.stdout));

        let output = run_gr_in_dir(&["commit", "-m", "test: gitlab e2e PR test"], workspace);
        assert!(
            output.status.success(),
            "commit failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );
        println!("Commit output: {}", String::from_utf8_lossy(&output.stdout));

        // Check status after commit
        let output = run_gr_in_dir(&["status"], workspace);
        println!(
            "Status after commit: {}",
            String::from_utf8_lossy(&output.stdout)
        );

        // Push
        let output = run_gr_in_dir(&["push", "-u"], workspace);
        assert!(
            output.status.success(),
            "push failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );
        println!("Push output: {}", String::from_utf8_lossy(&output.stdout));

        // Check status after push
        let output = run_gr_in_dir(&["status"], workspace);
        println!(
            "Status after push: {}",
            String::from_utf8_lossy(&output.stdout)
        );

        // Debug: Check git references in repo1
        let output = git_in_repo(&["branch", "-a"], &workspace.join("repo1"));
        println!(
            "Git branches in repo1: {}",
            String::from_utf8_lossy(&output.stdout)
        );

        let output = git_in_repo(&["log", "--oneline", "-5"], &workspace.join("repo1"));
        println!(
            "Git log in repo1: {}",
            String::from_utf8_lossy(&output.stdout)
        );

        let output = git_in_repo(
            &["log", "--oneline", "-5", "origin/main"],
            &workspace.join("repo1"),
        );
        println!(
            "Git log origin/main in repo1: {}",
            String::from_utf8_lossy(&output.stdout)
        );

        // Create PR (merge request on GitLab)
        let output = run_gr_in_dir(
            &["pr", "create", "-t", "test: GitLab E2E test MR"],
            workspace,
        );

        let stdout = String::from_utf8_lossy(&output.stdout);
        let stderr = String::from_utf8_lossy(&output.stderr);
        println!("PR create stdout: {}", stdout);
        println!("PR create stderr: {}", stderr);

        assert!(
            output.status.success(),
            "pr create failed: stdout={}, stderr={}",
            stdout,
            stderr
        );

        // Verify PRs were created (check for actual creation, not just "PR" text)
        assert!(
            stdout.contains("Created") && stdout.contains("#"),
            "Expected PR creation with number in output, got: {}",
            stdout
        );

        // Check PR status
        let output = run_gr_in_dir(&["pr", "status"], workspace);
        assert!(
            output.status.success(),
            "pr status failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );

        let stdout = String::from_utf8_lossy(&output.stdout);
        println!("PR status output: {}", stdout);

        // Cleanup: delete remote branches (this will auto-close the MRs on GitLab)
        git_in_repo(
            &["push", "origin", "--delete", &branch_name],
            &workspace.join("repo1"),
        );
        git_in_repo(
            &["push", "origin", "--delete", &branch_name],
            &workspace.join("repo2"),
        );
    }
}

// ==================== Azure DevOps Tests ====================

#[cfg(feature = "integration-tests")]
mod azure_tests {
    use super::*;

    #[test]
    #[ignore = "Requires Azure DevOps SSH access"]
    fn test_azure_init_from_cloned_repos() {
        let temp = TempDir::new().unwrap();
        let workspace = temp.path();

        clone_repo(test_repos::azure::REPO1, "repo1", workspace);
        clone_repo(test_repos::azure::REPO2, "repo2", workspace);

        let output = run_gr(
            &["init", "--from-dirs", "-p", workspace.to_str().unwrap()],
            workspace,
        );

        assert!(
            output.status.success(),
            "init failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );

        let manifest_path =
            gitgrip::core::manifest_paths::resolve_gripspace_manifest_path(workspace)
                .expect("manifest not found");
        let manifest = fs::read_to_string(&manifest_path).unwrap();
        assert!(manifest.contains("repo1"));
        assert!(manifest.contains("repo2"));
        assert!(
            manifest.contains("dev.azure.com") || manifest.contains("ssh.dev.azure.com"),
            "Azure DevOps URL not in manifest"
        );
    }

    #[test]
    #[ignore = "Requires Azure DevOps SSH access"]
    fn test_azure_init_from_manifest_url() {
        let temp = TempDir::new().unwrap();
        let workspace = temp.path().join("workspace");

        let output = run_gr(
            &[
                "init",
                test_repos::azure::MANIFEST,
                "-p",
                workspace.to_str().unwrap(),
            ],
            &workspace,
        );

        assert!(
            output.status.success(),
            "init from manifest failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );

        // Sync to clone the repos
        let output = run_gr(&["sync"], &workspace);
        assert!(
            output.status.success(),
            "sync failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );

        assert!(workspace.join("repo1").exists(), "repo1 not cloned");
        assert!(workspace.join("repo2").exists(), "repo2 not cloned");
    }

    #[test]
    #[ignore = "Requires Azure DevOps SSH access"]
    fn test_azure_branch_and_push() {
        let temp = TempDir::new().unwrap();
        let workspace = temp.path();
        let branch_name = format!("test/e2e-{}", random_suffix());

        clone_repo(test_repos::azure::REPO1, "repo1", workspace);
        clone_repo(test_repos::azure::REPO2, "repo2", workspace);

        let output = run_gr(
            &["init", "--from-dirs", "-p", workspace.to_str().unwrap()],
            workspace,
        );
        assert!(output.status.success());

        // Create branch
        let output = run_gr_in_dir(&["branch", &branch_name], workspace);
        assert!(
            output.status.success(),
            "branch failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );

        // Make changes
        create_test_file(&workspace.join("repo1"), "azure-test.txt");

        let output = run_gr_in_dir(&["add", "."], workspace);
        assert!(output.status.success());

        let output = run_gr_in_dir(&["commit", "-m", "test: azure e2e"], workspace);
        assert!(output.status.success());

        // Push
        let output = run_gr_in_dir(&["push", "-u"], workspace);
        assert!(
            output.status.success(),
            "push failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );

        // Cleanup
        git_in_repo(
            &["push", "origin", "--delete", &branch_name],
            &workspace.join("repo1"),
        );
    }

    #[test]
    #[ignore = "Requires Azure DevOps SSH and API access"]
    fn test_azure_full_pr_workflow() {
        let temp = TempDir::new().unwrap();
        let workspace = temp.path();
        let branch_name = format!("test/e2e-{}", random_suffix());

        // Clone test repos
        clone_repo(test_repos::azure::REPO1, "repo1", workspace);
        clone_repo(test_repos::azure::REPO2, "repo2", workspace);

        // Initialize workspace
        let output = run_gr(
            &["init", "--from-dirs", "-p", workspace.to_str().unwrap()],
            workspace,
        );
        assert!(output.status.success(), "init failed");

        // Create branch
        let output = run_gr_in_dir(&["branch", &branch_name], workspace);
        assert!(
            output.status.success(),
            "branch failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );

        // Make changes in both repos
        create_test_file(&workspace.join("repo1"), "test-file.txt");
        create_test_file(&workspace.join("repo2"), "test-file.txt");

        // Stage and commit
        let output = run_gr_in_dir(&["add", "."], workspace);
        assert!(output.status.success(), "add failed");

        let output = run_gr_in_dir(&["commit", "-m", "test: azure e2e PR test"], workspace);
        assert!(
            output.status.success(),
            "commit failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );

        // Push
        let output = run_gr_in_dir(&["push", "-u"], workspace);
        assert!(
            output.status.success(),
            "push failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );

        // Create PR
        let output = run_gr_in_dir(
            &["pr", "create", "-t", "test: Azure DevOps E2E test PR"],
            workspace,
        );

        let stdout = String::from_utf8_lossy(&output.stdout);
        let stderr = String::from_utf8_lossy(&output.stderr);
        println!("PR create stdout: {}", stdout);
        println!("PR create stderr: {}", stderr);

        assert!(
            output.status.success(),
            "pr create failed: stdout={}, stderr={}",
            stdout,
            stderr
        );

        // Verify PRs were created (check for actual creation, not just "PR" text)
        assert!(
            stdout.contains("Created") && stdout.contains("#"),
            "Expected PR creation with number in output, got: {}",
            stdout
        );

        // Check PR status
        let output = run_gr_in_dir(&["pr", "status"], workspace);
        assert!(
            output.status.success(),
            "pr status failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );

        let stdout = String::from_utf8_lossy(&output.stdout);
        println!("PR status output: {}", stdout);

        // Cleanup: delete remote branches
        // Note: On Azure DevOps, deleting the source branch will abandon the PR
        git_in_repo(
            &["push", "origin", "--delete", &branch_name],
            &workspace.join("repo1"),
        );
        git_in_repo(
            &["push", "origin", "--delete", &branch_name],
            &workspace.join("repo2"),
        );
    }
}

// ==================== Mixed Platform Tests ====================

#[cfg(feature = "integration-tests")]
mod mixed_platform_tests {
    use super::*;

    #[test]
    #[ignore = "Requires SSH access to GitHub, GitLab, and Azure DevOps"]
    fn test_mixed_platform_init() {
        let temp = TempDir::new().unwrap();
        let workspace = temp.path();

        // Clone one repo from each platform
        clone_repo(test_repos::github::REPO1, "github-repo", workspace);
        clone_repo(test_repos::gitlab::REPO1, "gitlab-repo", workspace);
        clone_repo(test_repos::azure::REPO1, "azure-repo", workspace);

        let output = run_gr(
            &["init", "--from-dirs", "-p", workspace.to_str().unwrap()],
            workspace,
        );

        assert!(
            output.status.success(),
            "init failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );

        let manifest_path =
            gitgrip::core::manifest_paths::resolve_gripspace_manifest_path(workspace)
                .expect("manifest not found");
        let manifest = fs::read_to_string(&manifest_path).unwrap();

        // All repos should be in manifest
        assert!(
            manifest.contains("github-repo"),
            "github-repo not in manifest"
        );
        assert!(
            manifest.contains("gitlab-repo"),
            "gitlab-repo not in manifest"
        );
        assert!(
            manifest.contains("azure-repo"),
            "azure-repo not in manifest"
        );

        // All platforms should be detected
        assert!(manifest.contains("github.com"), "github.com not detected");
        assert!(manifest.contains("gitlab.com"), "gitlab.com not detected");
        assert!(
            manifest.contains("dev.azure.com") || manifest.contains("ssh.dev.azure.com"),
            "Azure DevOps not detected"
        );
    }

    #[test]
    #[ignore = "Requires SSH access to GitHub, GitLab, and Azure DevOps"]
    fn test_mixed_platform_status() {
        let temp = TempDir::new().unwrap();
        let workspace = temp.path();

        clone_repo(test_repos::github::REPO1, "github-repo", workspace);
        clone_repo(test_repos::gitlab::REPO1, "gitlab-repo", workspace);
        clone_repo(test_repos::azure::REPO1, "azure-repo", workspace);

        let output = run_gr(
            &["init", "--from-dirs", "-p", workspace.to_str().unwrap()],
            workspace,
        );
        assert!(output.status.success());

        let output = run_gr_in_dir(&["status"], workspace);
        assert!(output.status.success());

        let stdout = String::from_utf8_lossy(&output.stdout);
        assert!(stdout.contains("github-repo"));
        assert!(stdout.contains("gitlab-repo"));
        assert!(stdout.contains("azure-repo"));
    }

    #[test]
    #[ignore = "Requires SSH access to GitHub, GitLab, and Azure DevOps"]
    fn test_mixed_platform_branch_across_all() {
        let temp = TempDir::new().unwrap();
        let workspace = temp.path();
        let branch_name = format!("test/mixed-{}", random_suffix());

        clone_repo(test_repos::github::REPO1, "github-repo", workspace);
        clone_repo(test_repos::gitlab::REPO1, "gitlab-repo", workspace);
        clone_repo(test_repos::azure::REPO1, "azure-repo", workspace);

        let output = run_gr(
            &["init", "--from-dirs", "-p", workspace.to_str().unwrap()],
            workspace,
        );
        assert!(output.status.success());

        // Create branch across all platforms
        let output = run_gr_in_dir(&["branch", &branch_name], workspace);
        assert!(
            output.status.success(),
            "branch failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );

        // Verify branch exists in each repo
        for repo_name in &["github-repo", "gitlab-repo", "azure-repo"] {
            let output = git_in_repo(
                &["branch", "--list", &branch_name],
                &workspace.join(repo_name),
            );
            let stdout = String::from_utf8_lossy(&output.stdout);
            assert!(
                stdout.contains(&branch_name),
                "Branch not created in {}",
                repo_name
            );
        }

        // Make changes in each repo
        create_test_file(&workspace.join("github-repo"), "mixed-test.txt");
        create_test_file(&workspace.join("gitlab-repo"), "mixed-test.txt");
        create_test_file(&workspace.join("azure-repo"), "mixed-test.txt");

        // Stage, commit, and push
        let output = run_gr_in_dir(&["add", "."], workspace);
        assert!(output.status.success());

        let output = run_gr_in_dir(&["commit", "-m", "test: mixed platform e2e"], workspace);
        assert!(output.status.success());

        let output = run_gr_in_dir(&["push", "-u"], workspace);
        assert!(
            output.status.success(),
            "push failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );

        // Cleanup: delete remote branches
        git_in_repo(
            &["push", "origin", "--delete", &branch_name],
            &workspace.join("github-repo"),
        );
        git_in_repo(
            &["push", "origin", "--delete", &branch_name],
            &workspace.join("gitlab-repo"),
        );
        git_in_repo(
            &["push", "origin", "--delete", &branch_name],
            &workspace.join("azure-repo"),
        );
    }

    #[test]
    #[ignore = "Requires SSH access to GitHub and GitLab"]
    fn test_mixed_github_gitlab_pr_workflow() {
        let temp = TempDir::new().unwrap();
        let workspace = temp.path();
        let branch_name = format!("test/pr-mixed-{}", random_suffix());

        // Use GitHub and GitLab (Azure DevOps PRs work differently)
        clone_repo(test_repos::github::REPO1, "github-app", workspace);
        clone_repo(test_repos::gitlab::REPO1, "gitlab-app", workspace);

        let output = run_gr(
            &["init", "--from-dirs", "-p", workspace.to_str().unwrap()],
            workspace,
        );
        assert!(output.status.success());

        // Create branch
        let output = run_gr_in_dir(&["branch", &branch_name], workspace);
        assert!(output.status.success());

        // Make changes
        create_test_file(&workspace.join("github-app"), "pr-test.txt");
        create_test_file(&workspace.join("gitlab-app"), "pr-test.txt");

        // Commit and push
        let output = run_gr_in_dir(&["add", "."], workspace);
        assert!(output.status.success());

        let output = run_gr_in_dir(&["commit", "-m", "test: mixed PR test"], workspace);
        assert!(output.status.success());

        let output = run_gr_in_dir(&["push", "-u"], workspace);
        assert!(output.status.success());

        // Create PRs (should create on both platforms)
        let output = run_gr_in_dir(
            &["pr", "create", "-t", "test: Mixed platform PR"],
            workspace,
        );

        // Note: This might partially succeed if one platform fails
        let stdout = String::from_utf8_lossy(&output.stdout);
        let stderr = String::from_utf8_lossy(&output.stderr);
        println!("PR create stdout: {}", stdout);
        println!("PR create stderr: {}", stderr);

        // Cleanup
        git_in_repo(
            &["push", "origin", "--delete", &branch_name],
            &workspace.join("github-app"),
        );
        git_in_repo(
            &["push", "origin", "--delete", &branch_name],
            &workspace.join("gitlab-app"),
        );
    }
}

// ==================== Sync Tests ====================

#[cfg(feature = "integration-tests")]
mod sync_tests {
    use super::*;

    #[test]
    #[ignore = "Requires GitHub SSH access"]
    fn test_sync_pulls_latest_changes() {
        let temp = TempDir::new().unwrap();
        let workspace = temp.path();

        clone_repo(test_repos::github::REPO1, "repo1", workspace);
        clone_repo(test_repos::github::REPO2, "repo2", workspace);

        let output = run_gr(
            &["init", "--from-dirs", "-p", workspace.to_str().unwrap()],
            workspace,
        );
        assert!(output.status.success());

        // Run sync
        let output = run_gr_in_dir(&["sync"], workspace);
        assert!(
            output.status.success(),
            "sync failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );
    }
}

// ==================== Forall Tests ====================

#[cfg(feature = "integration-tests")]
mod forall_tests {
    use super::*;

    #[test]
    #[ignore = "Requires GitHub SSH access"]
    fn test_forall_runs_command_in_all_repos() {
        let temp = TempDir::new().unwrap();
        let workspace = temp.path();

        clone_repo(test_repos::github::REPO1, "repo1", workspace);
        clone_repo(test_repos::github::REPO2, "repo2", workspace);

        let output = run_gr(
            &["init", "--from-dirs", "-p", workspace.to_str().unwrap()],
            workspace,
        );
        assert!(output.status.success());

        // Run forall with a simple command (--all to include repos without changes)
        let output = run_gr_in_dir(&["forall", "--all", "-c", "pwd"], workspace);
        assert!(
            output.status.success(),
            "forall failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );

        let stdout = String::from_utf8_lossy(&output.stdout);
        assert!(stdout.contains("repo1"), "repo1 not in forall output");
        assert!(stdout.contains("repo2"), "repo2 not in forall output");
    }

    #[test]
    #[ignore = "Requires SSH access to all platforms"]
    fn test_forall_works_across_platforms() {
        let temp = TempDir::new().unwrap();
        let workspace = temp.path();

        clone_repo(test_repos::github::REPO1, "github-app", workspace);
        clone_repo(test_repos::gitlab::REPO1, "gitlab-app", workspace);
        clone_repo(test_repos::azure::REPO1, "azure-app", workspace);

        let output = run_gr(
            &["init", "--from-dirs", "-p", workspace.to_str().unwrap()],
            workspace,
        );
        assert!(output.status.success());

        // Run forall to show remote URLs (--all to include repos without changes)
        let output = run_gr_in_dir(
            &["forall", "--all", "-c", "git remote get-url origin"],
            workspace,
        );
        assert!(output.status.success());

        let stdout = String::from_utf8_lossy(&output.stdout);
        assert!(stdout.contains("github.com"));
        assert!(stdout.contains("gitlab.com"));
        assert!(stdout.contains("dev.azure.com") || stdout.contains("ssh.dev.azure.com"));
    }
}

// Non-feature-gated test to verify compilation
#[test]
fn test_multi_provider_e2e_module_compiles() {
    assert!(true);
}