gitpane 0.5.2

Multi-repo Git workspace dashboard TUI
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
use git2::{Repository, StatusOptions, SubmoduleStatus};
use std::path::{Path, PathBuf};

#[derive(Clone, Debug)]
pub(crate) struct RepoStatus {
    pub branch: String,
    pub files: Vec<FileEntry>,
    pub ahead: usize,
    pub behind: usize,
    pub is_dirty: bool,
    /// Linked worktrees (excludes the main working tree)
    pub worktree_info: Vec<WorktreeEntry>,
    /// True when .gitmodules exists (repo uses submodules)
    pub has_submodules: bool,
    pub submodules: Vec<SubmoduleInfo>,
    pub has_dirty_submodules: bool,
    /// True when the last `git fetch` failed (auth, network, timeout)
    pub fetch_failed: bool,
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct FileEntry {
    pub path: PathBuf,
    pub status: FileStatus,
    pub is_submodule: bool,
    pub submodule_state: Option<SubmoduleState>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum FileStatus {
    Modified,
    Added,
    Deleted,
    Renamed,
    Untracked,
    Conflicted,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum SubmoduleState {
    Modified,
    Uninitialized,
    Dirty,
}

#[derive(Clone, Debug)]
#[allow(dead_code)]
pub(crate) struct WorktreeEntry {
    pub name: String,
    pub path: PathBuf,
    pub branch: String,
}

#[derive(Clone, Debug)]
#[allow(dead_code)]
pub(crate) struct SubmoduleInfo {
    pub name: String,
    pub path: PathBuf,
    pub state: SubmoduleState,
    pub head_oid: Option<String>,
    pub workdir_oid: Option<String>,
}

impl FileStatus {
    pub fn label(&self) -> &'static str {
        match self {
            Self::Modified => "M",
            Self::Added => "A",
            Self::Deleted => "D",
            Self::Renamed => "R",
            Self::Untracked => "?",
            Self::Conflicted => "C",
        }
    }
}

/// Fast local-only status query (no network). Used by filesystem watcher refreshes.
pub(crate) fn query_status(path: &Path, ignore_dirty_subs: bool) -> color_eyre::Result<RepoStatus> {
    query_status_inner(path, false, ignore_dirty_subs)
}

/// Status query with `git fetch` first. Used by explicit user refresh (`r` key).
pub(crate) fn query_status_with_fetch(
    path: &Path,
    ignore_dirty_subs: bool,
) -> color_eyre::Result<RepoStatus> {
    query_status_inner(path, true, ignore_dirty_subs)
}

fn query_status_inner(
    path: &Path,
    fetch: bool,
    ignore_dirty_subs: bool,
) -> color_eyre::Result<RepoStatus> {
    let repo = Repository::open(path)?;

    // Branch name
    let branch = match repo.head() {
        Ok(reference) => reference.shorthand().unwrap_or("HEAD").to_string(),
        Err(_) => "(no branch)".to_string(),
    };

    // Only fetch remote-tracking refs when explicitly requested
    let fetch_failed = if fetch {
        !fetch_remote_silent(path)
    } else {
        false
    };

    // Ahead/behind
    let (ahead, behind) = compute_ahead_behind(&repo);

    // File statuses
    let mut opts = StatusOptions::new();
    opts.include_untracked(true)
        .recurse_untracked_dirs(true)
        .renames_head_to_index(true);

    if ignore_dirty_subs {
        opts.exclude_submodules(true);
    }

    let statuses = repo.statuses(Some(&mut opts))?;
    let mut files = Vec::new();

    for entry in statuses.iter() {
        let s = entry.status();
        let file_path = PathBuf::from(entry.path().unwrap_or(""));

        let file_status = if s.is_conflicted() {
            FileStatus::Conflicted
        } else if s.is_index_new() || s.is_wt_new() {
            if s.is_wt_new() && !s.is_index_new() {
                FileStatus::Untracked
            } else {
                FileStatus::Added
            }
        } else if s.is_index_deleted() || s.is_wt_deleted() {
            FileStatus::Deleted
        } else if s.is_index_renamed() || s.is_wt_renamed() {
            FileStatus::Renamed
        } else if s.is_index_modified() || s.is_wt_modified() {
            FileStatus::Modified
        } else {
            continue;
        };

        files.push(FileEntry {
            path: file_path,
            status: file_status,
            is_submodule: false,
            submodule_state: None,
        });
    }

    let is_dirty = !files.is_empty();

    // Collect linked worktree details (excludes the main working tree)
    let worktree_info = collect_worktree_info(&repo);

    // Detect submodules by checking for .gitmodules
    let has_submodules = path.join(".gitmodules").is_file();

    // Submodule enumeration
    let mut submodules = Vec::new();
    let mut has_dirty_submodules = false;

    if has_submodules
        && !ignore_dirty_subs
        && let Ok(subs) = repo.submodules()
    {
        for sub in &subs {
            let name = sub.name().unwrap_or("").to_string();
            let sub_path = PathBuf::from(sub.path());
            let status = repo
                .submodule_status(&name, git2::SubmoduleIgnore::Unspecified)
                .unwrap_or(SubmoduleStatus::empty());

            let state = if status.is_wd_uninitialized() {
                Some(SubmoduleState::Uninitialized)
            } else if status.is_wd_wd_modified() || status.contains(SubmoduleStatus::WD_UNTRACKED) {
                Some(SubmoduleState::Dirty)
            } else if status.is_wd_modified() || status.contains(SubmoduleStatus::WD_INDEX_MODIFIED)
            {
                Some(SubmoduleState::Modified)
            } else {
                None
            };

            if let Some(state) = state {
                let head_oid = sub.head_id().map(|id| id.to_string());
                let workdir_oid = sub.workdir_id().map(|id| id.to_string());

                submodules.push(SubmoduleInfo {
                    name: name.clone(),
                    path: sub_path.clone(),
                    state: state.clone(),
                    head_oid,
                    workdir_oid,
                });

                // Cross-reference with files vec
                if let Some(file_entry) = files.iter_mut().find(|f| f.path == sub_path) {
                    file_entry.is_submodule = true;
                    file_entry.submodule_state = Some(state.clone());
                } else {
                    // Add synthetic FileEntry for dirty submodules not already in files
                    files.push(FileEntry {
                        path: sub_path,
                        status: FileStatus::Modified,
                        is_submodule: true,
                        submodule_state: Some(state),
                    });
                }

                has_dirty_submodules = true;
            }
        }
    }

    Ok(RepoStatus {
        branch,
        files,
        ahead,
        behind,
        is_dirty: is_dirty || has_dirty_submodules,
        worktree_info,
        has_submodules,
        submodules,
        has_dirty_submodules,
        fetch_failed,
    })
}

/// Collect details for each linked worktree using the git2 API.
/// Mirrors the pattern in `git/graph.rs::collect_worktree_branches`.
fn collect_worktree_info(repo: &Repository) -> Vec<WorktreeEntry> {
    let wt_names = match repo.worktrees() {
        Ok(names) => names,
        Err(_) => return Vec::new(),
    };
    let mut entries = Vec::new();
    for i in 0..wt_names.len() {
        let name = match wt_names.get(i) {
            Some(n) => n,
            None => continue,
        };
        let wt = match repo.find_worktree(name) {
            Ok(wt) => wt,
            Err(_) => continue,
        };
        let wt_path = wt.path().to_path_buf();
        let branch = match Repository::open(&wt_path) {
            Ok(wt_repo) => match wt_repo.head() {
                Ok(head) => head.shorthand().unwrap_or("HEAD").to_string(),
                Err(_) => "(no branch)".to_string(),
            },
            Err(_) => continue,
        };
        entries.push(WorktreeEntry {
            name: name.to_string(),
            path: wt_path,
            branch,
        });
    }
    entries
}

/// Run `git fetch` with a 30-second timeout to update remote-tracking refs.
/// Uses the CLI because git2 fetch doesn't support SSH agent / credential helpers
/// out of the box. Returns `true` on success, `false` on failure/timeout.
fn fetch_remote_silent(path: &Path) -> bool {
    use wait_timeout::ChildExt;

    let child = std::process::Command::new("git")
        .arg("-C")
        .arg(path)
        .arg("fetch")
        .arg("--quiet")
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .spawn();

    match child {
        Ok(mut c) => {
            match c.wait_timeout(std::time::Duration::from_secs(30)) {
                Ok(Some(status)) => status.success(),
                Ok(None) => {
                    // Timed out — kill the hung process
                    let _ = c.kill();
                    let _ = c.wait();
                    false
                }
                Err(_) => false,
            }
        }
        Err(_) => false,
    }
}

fn compute_ahead_behind(repo: &Repository) -> (usize, usize) {
    let head = match repo.head() {
        Ok(h) => h,
        Err(_) => return (0, 0),
    };

    let local_oid = match head.target() {
        Some(oid) => oid,
        None => return (0, 0),
    };

    let branch_name = match head.shorthand() {
        Some(name) => name.to_string(),
        None => return (0, 0),
    };

    // Use git2's branch upstream tracking instead of hardcoding "origin"
    let branch = match repo.find_branch(&branch_name, git2::BranchType::Local) {
        Ok(b) => b,
        Err(_) => return (0, 0),
    };

    let upstream = match branch.upstream() {
        Ok(u) => u,
        Err(_) => return (0, 0),
    };

    let upstream_oid = match upstream.get().target() {
        Some(oid) => oid,
        None => return (0, 0),
    };

    repo.graph_ahead_behind(local_oid, upstream_oid)
        .unwrap_or((0, 0))
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::TempDir;

    fn init_temp_repo() -> (TempDir, Repository) {
        let tmp = TempDir::new().unwrap();
        let repo = Repository::init(tmp.path()).unwrap();

        // Create initial commit so HEAD exists
        {
            let sig = git2::Signature::now("Test", "test@test.com").unwrap();
            let tree_id = repo.index().unwrap().write_tree().unwrap();
            let tree = repo.find_tree(tree_id).unwrap();
            repo.commit(Some("HEAD"), &sig, &sig, "Initial commit", &tree, &[])
                .unwrap();
        }

        (tmp, repo)
    }

    #[test]
    fn test_clean_repo_reports_no_changes() {
        let (tmp, _repo) = init_temp_repo();
        let status = query_status(tmp.path(), false).unwrap();
        assert!(!status.is_dirty);
        assert!(status.files.is_empty());
    }

    #[test]
    fn test_modified_file_detected() {
        let (tmp, repo) = init_temp_repo();

        // Add and commit a file
        let file_path = tmp.path().join("test.txt");
        fs::write(&file_path, "hello").unwrap();
        let mut index = repo.index().unwrap();
        index.add_path(Path::new("test.txt")).unwrap();
        index.write().unwrap();
        let tree_id = index.write_tree().unwrap();
        let tree = repo.find_tree(tree_id).unwrap();
        let head = repo.head().unwrap().peel_to_commit().unwrap();
        let sig = git2::Signature::now("Test", "test@test.com").unwrap();
        repo.commit(Some("HEAD"), &sig, &sig, "Add file", &tree, &[&head])
            .unwrap();

        // Modify it
        fs::write(&file_path, "world").unwrap();

        let status = query_status(tmp.path(), false).unwrap();
        assert!(status.is_dirty);
        assert!(
            status
                .files
                .iter()
                .any(|f| f.status == FileStatus::Modified)
        );
    }

    #[test]
    fn test_untracked_file_detected() {
        let (tmp, _repo) = init_temp_repo();
        fs::write(tmp.path().join("new.txt"), "new").unwrap();

        let status = query_status(tmp.path(), false).unwrap();
        assert!(status.is_dirty);
        assert!(
            status
                .files
                .iter()
                .any(|f| f.status == FileStatus::Untracked)
        );
    }

    #[test]
    fn test_worktree_info_empty_for_plain_repo() {
        let (tmp, _repo) = init_temp_repo();
        let status = query_status(tmp.path(), false).unwrap();
        assert!(status.worktree_info.is_empty());
    }

    #[test]
    fn test_worktree_info_reflects_linked_worktrees() {
        let (tmp, _repo) = init_temp_repo();
        // Create a linked worktree via git CLI
        let wt_dir = tmp.path().join("wt1");
        let output = std::process::Command::new("git")
            .arg("-C")
            .arg(tmp.path())
            .arg("worktree")
            .arg("add")
            .arg(&wt_dir)
            .arg("-b")
            .arg("wt-branch")
            .output()
            .unwrap();
        assert!(output.status.success(), "git worktree add failed");

        let status = query_status(tmp.path(), false).unwrap();
        assert_eq!(status.worktree_info.len(), 1);
        assert_eq!(status.worktree_info[0].branch, "wt-branch");
        assert_eq!(status.worktree_info[0].name, "wt1");
    }

    #[test]
    fn test_submodule_state_mapping() {
        // Test the flag-to-state conversion logic
        let flags = SubmoduleStatus::WD_UNINITIALIZED;
        assert!(flags.is_wd_uninitialized());

        let flags = SubmoduleStatus::WD_WD_MODIFIED;
        assert!(flags.is_wd_wd_modified());

        let flags = SubmoduleStatus::WD_UNTRACKED;
        assert!(flags.contains(SubmoduleStatus::WD_UNTRACKED));

        let flags = SubmoduleStatus::WD_MODIFIED;
        assert!(flags.is_wd_modified());

        let flags = SubmoduleStatus::WD_INDEX_MODIFIED;
        assert!(flags.contains(SubmoduleStatus::WD_INDEX_MODIFIED));
    }

    #[test]
    fn test_clean_repo_no_dirty_submodules() {
        let (tmp, _repo) = init_temp_repo();
        let status = query_status(tmp.path(), false).unwrap();
        assert!(!status.has_dirty_submodules);
        assert!(status.submodules.is_empty());
    }

    #[test]
    fn test_status_maps_correctly() {
        assert_eq!(FileStatus::Modified.label(), "M");
        assert_eq!(FileStatus::Added.label(), "A");
        assert_eq!(FileStatus::Deleted.label(), "D");
        assert_eq!(FileStatus::Renamed.label(), "R");
        assert_eq!(FileStatus::Untracked.label(), "?");
        assert_eq!(FileStatus::Conflicted.label(), "C");
    }

    #[test]
    fn test_file_entry_submodule_fields() {
        let entry = FileEntry {
            path: PathBuf::from("my-submodule"),
            status: FileStatus::Modified,
            is_submodule: true,
            submodule_state: Some(SubmoduleState::Modified),
        };
        assert!(entry.is_submodule);
        assert_eq!(entry.submodule_state, Some(SubmoduleState::Modified));

        let plain = FileEntry {
            path: PathBuf::from("src/main.rs"),
            status: FileStatus::Modified,
            is_submodule: false,
            submodule_state: None,
        };
        assert!(!plain.is_submodule);
        assert_eq!(plain.submodule_state, None);
    }

    #[test]
    fn test_submodule_state_equality_and_clone() {
        assert_eq!(SubmoduleState::Modified, SubmoduleState::Modified);
        assert_eq!(SubmoduleState::Dirty, SubmoduleState::Dirty);
        assert_eq!(SubmoduleState::Uninitialized, SubmoduleState::Uninitialized);
        assert_ne!(SubmoduleState::Modified, SubmoduleState::Dirty);
        assert_ne!(SubmoduleState::Dirty, SubmoduleState::Uninitialized);

        let state = SubmoduleState::Modified;
        let cloned = state.clone();
        assert_eq!(state, cloned);
    }

    #[test]
    fn test_ignore_dirty_subs_on_clean_repo() {
        // ignore_dirty_subs = true should work fine on repos without submodules
        let (tmp, _repo) = init_temp_repo();
        let status = query_status(tmp.path(), true).unwrap();
        assert!(!status.is_dirty);
        assert!(status.files.is_empty());
        assert!(status.submodules.is_empty());
        assert!(!status.has_dirty_submodules);
    }

    #[test]
    fn test_ignore_dirty_subs_still_detects_regular_changes() {
        let (tmp, _repo) = init_temp_repo();
        fs::write(tmp.path().join("new.txt"), "new").unwrap();

        let status = query_status(tmp.path(), true).unwrap();
        assert!(status.is_dirty);
        assert!(
            status
                .files
                .iter()
                .any(|f| f.status == FileStatus::Untracked)
        );
        // Submodule fields should be empty when ignored
        assert!(status.submodules.is_empty());
        assert!(!status.has_dirty_submodules);
    }

    #[test]
    fn test_submodule_state_priority_uninitialized_first() {
        // WD_UNINITIALIZED takes priority over other flags
        let flags = SubmoduleStatus::WD_UNINITIALIZED | SubmoduleStatus::WD_MODIFIED;
        assert!(flags.is_wd_uninitialized());
    }

    #[test]
    fn test_submodule_state_priority_dirty_over_modified() {
        // WD_WD_MODIFIED (dirty) is checked before WD_MODIFIED (pointer change)
        let flags = SubmoduleStatus::WD_WD_MODIFIED | SubmoduleStatus::WD_MODIFIED;
        assert!(flags.is_wd_wd_modified());
        assert!(flags.is_wd_modified());
        // Our mapping logic checks dirty first, so this should map to Dirty
    }

    /// Helper: creates a temp repo with a submodule, returns (parent_tmp, sub_source_tmp, sub_repo)
    fn init_repo_with_submodule() -> (TempDir, TempDir, Repository) {
        let (tmp, _repo) = init_temp_repo();

        let sub_source = TempDir::new().unwrap();
        let sub_repo = Repository::init(sub_source.path()).unwrap();
        {
            let sig = git2::Signature::now("Test", "test@test.com").unwrap();
            fs::write(sub_source.path().join("lib.rs"), "fn hello() {}").unwrap();
            let mut idx = sub_repo.index().unwrap();
            idx.add_path(Path::new("lib.rs")).unwrap();
            idx.write().unwrap();
            let tree_id = idx.write_tree().unwrap();
            let tree = sub_repo.find_tree(tree_id).unwrap();
            sub_repo
                .commit(Some("HEAD"), &sig, &sig, "init sub", &tree, &[])
                .unwrap();
        }

        // Add submodule (requires protocol.file.allow for local paths)
        let output = std::process::Command::new("git")
            .arg("-C")
            .arg(tmp.path())
            .args([
                "-c",
                "protocol.file.allow=always",
                "submodule",
                "add",
                sub_source.path().to_str().unwrap(),
                "my-sub",
            ])
            .output()
            .unwrap();
        assert!(
            output.status.success(),
            "git submodule add failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );

        // Commit the submodule addition (use -c user.* for CI environments without global git config)
        let output = std::process::Command::new("git")
            .arg("-C")
            .arg(tmp.path())
            .args(["add", "."])
            .output()
            .unwrap();
        assert!(output.status.success());
        let output = std::process::Command::new("git")
            .arg("-C")
            .arg(tmp.path())
            .args([
                "-c",
                "user.name=Test",
                "-c",
                "user.email=test@test.com",
                "commit",
                "-m",
                "add submodule",
            ])
            .output()
            .unwrap();
        assert!(
            output.status.success(),
            "git commit failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );

        (tmp, sub_source, sub_repo)
    }

    #[test]
    fn test_dirty_submodule_with_real_git_submodule() {
        let (tmp, _sub_source, _sub_repo) = init_repo_with_submodule();

        // Verify: clean state should show has_submodules but no dirty submodules
        let status = query_status(tmp.path(), false).unwrap();
        assert!(status.has_submodules);
        assert!(!status.has_dirty_submodules);
        assert!(status.submodules.is_empty());

        // Now make the submodule dirty by modifying a file inside it
        let sub_workdir = tmp.path().join("my-sub");
        fs::write(sub_workdir.join("lib.rs"), "fn hello() { /* changed */ }").unwrap();

        let status = query_status(tmp.path(), false).unwrap();
        assert!(status.has_submodules);
        assert!(status.has_dirty_submodules);
        assert!(!status.submodules.is_empty());

        let sub_info = &status.submodules[0];
        assert_eq!(sub_info.path, Path::new("my-sub"));
        assert_eq!(sub_info.state, SubmoduleState::Dirty);

        // Verify the file entry is annotated
        let file_entry = status.files.iter().find(|f| f.path == Path::new("my-sub"));
        assert!(file_entry.is_some());
        let file_entry = file_entry.unwrap();
        assert!(file_entry.is_submodule);
        assert_eq!(file_entry.submodule_state, Some(SubmoduleState::Dirty));
    }

    #[test]
    fn test_ignore_dirty_subs_hides_submodule_state() {
        let (tmp, _sub_source, _sub_repo) = init_repo_with_submodule();

        // Make the submodule dirty
        fs::write(tmp.path().join("my-sub/lib.rs"), "fn changed() {}").unwrap();

        // With ignore_dirty_subs = true, submodule state should be hidden
        let status = query_status(tmp.path(), true).unwrap();
        assert!(status.has_submodules); // .gitmodules still exists
        assert!(!status.has_dirty_submodules);
        assert!(status.submodules.is_empty());
        // No submodule-annotated entries
        assert!(!status.files.iter().any(|f| f.is_submodule));
    }

    #[test]
    fn test_submodule_modified_pointer() {
        let (tmp, _sub_source, sub_repo) = init_repo_with_submodule();

        // Add a new commit to the submodule source
        {
            let sig = git2::Signature::now("Test", "test@test.com").unwrap();
            fs::write(_sub_source.path().join("lib.rs"), "v2").unwrap();
            let mut idx = sub_repo.index().unwrap();
            idx.add_path(Path::new("lib.rs")).unwrap();
            idx.write().unwrap();
            let tree_id = idx.write_tree().unwrap();
            let tree = sub_repo.find_tree(tree_id).unwrap();
            let head = sub_repo.head().unwrap().peel_to_commit().unwrap();
            sub_repo
                .commit(Some("HEAD"), &sig, &sig, "v2", &tree, &[&head])
                .unwrap();
        }

        // Pull the new commit inside the submodule workdir
        let output = std::process::Command::new("git")
            .arg("-C")
            .arg(tmp.path().join("my-sub"))
            .args([
                "-c",
                "protocol.file.allow=always",
                "pull",
                "origin",
                "master",
            ])
            .output()
            .unwrap();
        // Try main if master fails
        if !output.status.success() {
            let _ = std::process::Command::new("git")
                .arg("-C")
                .arg(tmp.path().join("my-sub"))
                .args(["-c", "protocol.file.allow=always", "pull", "origin", "main"])
                .output();
        }

        // Now the submodule pointer has changed (HEAD in submodule != recorded in parent)
        let status = query_status(tmp.path(), false).unwrap();
        assert!(status.has_submodules);
        assert!(status.has_dirty_submodules);
        assert!(!status.submodules.is_empty());

        let sub_info = &status.submodules[0];
        assert_eq!(sub_info.path, Path::new("my-sub"));
        // Could be Modified or Dirty depending on exact git state
        assert!(
            sub_info.state == SubmoduleState::Modified || sub_info.state == SubmoduleState::Dirty,
            "expected Modified or Dirty, got {:?}",
            sub_info.state
        );

        // Verify OIDs are populated
        assert!(sub_info.head_oid.is_some());
        assert!(sub_info.workdir_oid.is_some());
        assert_ne!(sub_info.head_oid, sub_info.workdir_oid);
    }

    #[test]
    fn test_clean_submodule_not_reported() {
        let (tmp, _sub_source, _sub_repo) = init_repo_with_submodule();

        // Without any modifications, the submodule should be clean
        let status = query_status(tmp.path(), false).unwrap();
        assert!(status.has_submodules);
        assert!(!status.has_dirty_submodules);
        assert!(status.submodules.is_empty());
        assert!(!status.files.iter().any(|f| f.is_submodule));
    }

    #[test]
    fn test_dirty_submodule_makes_repo_dirty() {
        let (tmp, _sub_source, _sub_repo) = init_repo_with_submodule();

        // Start clean
        let status = query_status(tmp.path(), false).unwrap();
        assert!(!status.is_dirty);

        // Make submodule dirty
        fs::write(tmp.path().join("my-sub/lib.rs"), "dirty").unwrap();

        // Now repo should be dirty
        let status = query_status(tmp.path(), false).unwrap();
        assert!(status.is_dirty);
    }
}