gitpane 0.12.0

Multi-repo Git workspace dashboard TUI
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
use super::test_support::*;
use super::*;
use git2::SubmoduleStatus;
use std::{
    fs,
    path::{Path, PathBuf},
};
use tempfile::TempDir;

#[test]
fn test_clean_repo_reports_no_changes() {
    let (tmp, _repo) = init_temp_repo();
    let status = query_status(tmp.path(), &SubmoduleConfig::default()).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(), &SubmoduleConfig::default()).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(), &SubmoduleConfig::default()).unwrap();
    assert!(status.is_dirty);
    assert!(
        status
            .files
            .iter()
            .any(|f| f.status == FileStatus::Untracked)
    );
}

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

    let status = query_status(tmp.path(), &SubmoduleConfig::default()).unwrap();
    assert!(status.is_dirty);
    assert!(
        status
            .files
            .iter()
            .any(|f| f.status == FileStatus::Untracked)
    );
    assert!(
        !status
            .files
            .iter()
            .any(|f| f.path == Path::new("nested/file.txt"))
    );

    let recursive =
        query_status_inner(tmp.path(), false, true, &SubmoduleConfig::default()).unwrap();
    assert!(
        recursive
            .files
            .iter()
            .any(|f| f.path == Path::new("nested/file.txt"))
    );
}

#[test]
fn test_default_branch_name_resolves_origin_main() {
    let (_tmp, repo) = init_temp_repo();
    let oid = repo.head().unwrap().target().unwrap();
    repo.reference("refs/remotes/origin/main", oid, true, "test")
        .unwrap();
    assert_eq!(default_branch_name(&repo).as_deref(), Some("origin/main"));
}

#[test]
fn test_default_branch_name_follows_symbolic_head() {
    let (_tmp, repo) = init_temp_repo();
    let oid = repo.head().unwrap().target().unwrap();
    repo.reference("refs/remotes/origin/master", oid, true, "test")
        .unwrap();
    repo.reference_symbolic(
        "refs/remotes/origin/HEAD",
        "refs/remotes/origin/master",
        true,
        "test",
    )
    .unwrap();
    assert_eq!(default_branch_name(&repo).as_deref(), Some("origin/master"));
}

#[test]
fn test_default_branch_name_none_without_remote() {
    let (_tmp, repo) = init_temp_repo();
    assert_eq!(default_branch_name(&repo), None);
}

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

#[test]
fn test_worktree_info_reflects_linked_worktrees() {
    let (tmp, repo) = init_temp_repo();
    let head = repo.head().unwrap().peel_to_commit().unwrap();
    let branch = repo.branch("wt-branch", &head, false).unwrap();
    let reference = branch.into_reference();
    let mut opts = git2::WorktreeAddOptions::new();
    opts.reference(Some(&reference));

    let wt_tmp = TempDir::new().unwrap();
    let wt_dir = wt_tmp.path().join("wt1");
    repo.worktree("wt1", &wt_dir, Some(&opts)).unwrap();

    let status = query_status(tmp.path(), &SubmoduleConfig::default()).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");
    assert!(!status.worktree_info[0].is_dirty);
    assert_eq!(status.worktree_info[0].file_count, 0);
    assert!(!status.worktree_info[0].has_dirty_submodules);
    assert!(!status.worktree_info[0].has_unpushed_submodules);
}

#[test]
fn test_worktree_info_reflects_dirty_linked_worktree() {
    let (tmp, repo) = init_temp_repo();
    let head = repo.head().unwrap().peel_to_commit().unwrap();
    let branch = repo.branch("wt-dirty", &head, false).unwrap();
    let reference = branch.into_reference();
    let mut opts = git2::WorktreeAddOptions::new();
    opts.reference(Some(&reference));

    let wt_tmp = TempDir::new().unwrap();
    let wt_dir = wt_tmp.path().join("wt-dirty");
    repo.worktree("wt-dirty", &wt_dir, Some(&opts)).unwrap();
    fs::write(wt_dir.join("new.txt"), "new").unwrap();

    let status = query_status(tmp.path(), &SubmoduleConfig::default()).unwrap();
    let wt = status
        .worktree_info
        .iter()
        .find(|wt| wt.name == "wt-dirty")
        .unwrap();

    assert!(wt.is_dirty);
    assert_eq!(wt.file_count, 1);
    assert!(!wt.has_dirty_submodules);
    assert!(!wt.has_unpushed_submodules);
}

#[test]
fn test_worktree_info_reflects_submodule_signals() {
    let (tmp, _sub_source, _sub_repo) = init_repo_with_submodule();
    let repo = Repository::open(tmp.path()).unwrap();
    let head = repo.head().unwrap().peel_to_commit().unwrap();
    let branch = repo.branch("wt-submodule", &head, false).unwrap();
    let reference = branch.into_reference();
    let mut opts = git2::WorktreeAddOptions::new();
    opts.reference(Some(&reference));

    let wt_tmp = TempDir::new().unwrap();
    let wt_dir = wt_tmp.path().join("wt-submodule");
    repo.worktree("wt-submodule", &wt_dir, Some(&opts)).unwrap();

    let status = query_status(tmp.path(), &SubmoduleConfig::default()).unwrap();
    let wt = status
        .worktree_info
        .iter()
        .find(|wt| wt.name == "wt-submodule")
        .unwrap();

    assert!(wt.is_dirty);
    assert!(wt.file_count > 0);
    assert!(wt.has_dirty_submodules);
}

#[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(), &SubmoduleConfig::default()).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,
        staged: false,
        unstaged: true,
        is_submodule: true,
        submodule_state: Some(SubmoduleState::Modified),
        submodule_warn: SubmoduleWarn::default(),
        submodule_head: Some(SubmoduleHead::Branch("feature".to_string())),
    };
    assert!(entry.is_submodule);
    assert_eq!(entry.submodule_state, Some(SubmoduleState::Modified));
    assert!(entry.submodule_warn.is_clean());
    assert_eq!(
        entry.submodule_head,
        Some(SubmoduleHead::Branch("feature".to_string()))
    );

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

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

    // Commit an initial file.
    let file_path = tmp.path().join("test.txt");
    fs::write(&file_path, "one").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();

    // Stage a change, then modify again on disk: the index differs from HEAD
    // (staged) and the worktree differs from the index (unstaged).
    fs::write(&file_path, "two").unwrap();
    let mut index = repo.index().unwrap();
    index.add_path(Path::new("test.txt")).unwrap();
    index.write().unwrap();
    fs::write(&file_path, "three").unwrap();

    // An untracked file is unstaged-only.
    fs::write(tmp.path().join("new.txt"), "new").unwrap();

    let status = query_status(tmp.path(), &SubmoduleConfig::default()).unwrap();

    let tracked = status
        .files
        .iter()
        .find(|f| f.path == Path::new("test.txt"))
        .expect("tracked file present");
    assert!(tracked.staged && tracked.unstaged);

    let untracked = status
        .files
        .iter()
        .find(|f| f.path == Path::new("new.txt"))
        .expect("untracked file present");
    assert_eq!(untracked.status, FileStatus::Untracked);
    assert!(untracked.unstaged && !untracked.staged);
}

#[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(),
        &SubmoduleConfig {
            ignore_dirty: true,
            warn_unpushed: false,
        },
    )
    .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(),
        &SubmoduleConfig {
            ignore_dirty: true,
            warn_unpushed: false,
        },
    )
    .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
}

#[test]
fn test_default_branch_name_uses_non_origin_remote() {
    // Gerrit/mirror workspace: no origin, remote named `gerrit`.
    let (_tmp, repo) = init_temp_repo();
    repo.remote("gerrit", "https://example.com/repo.git")
        .unwrap();
    let oid = repo.head().unwrap().target().unwrap();
    repo.reference("refs/remotes/gerrit/main", oid, true, "test")
        .unwrap();
    assert_eq!(default_branch_name(&repo).as_deref(), Some("gerrit/main"));
}