Skip to content

Commit cbe5415

Browse files
committed
fix(operating-modes): address review feedback
1 parent e13a6dc commit cbe5415

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

crates/gitbutler-operating-modes/src/lib.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,18 @@ pub enum OperatingMode {
7979
}
8080

8181
pub fn operating_mode(ctx: &Context) -> OperatingMode {
82+
let outside = || OperatingMode::OutsideWorkspace(outside_workspace_metadata(ctx).unwrap_or_default());
83+
8284
let Ok(repo) = ctx.repo.get() else {
83-
return OperatingMode::OutsideWorkspace(outside_workspace_metadata(ctx).unwrap_or_default());
85+
// If we can't even open/borrow the repo handle, attempting to compute metadata would just
86+
// try (and fail) to open it again.
87+
return OperatingMode::OutsideWorkspace(OutsideWorkspaceMetadata::default());
8488
};
8589
let Ok(head_ref) = repo.head() else {
86-
return OperatingMode::OutsideWorkspace(outside_workspace_metadata(ctx).unwrap_or_default());
90+
return outside();
8791
};
8892
let Some(head_ref_name) = head_ref.referent_name().map(|name| name.as_bstr()) else {
89-
return OperatingMode::OutsideWorkspace(outside_workspace_metadata(ctx).unwrap_or_default());
93+
return outside();
9094
};
9195

9296
if OPEN_WORKSPACE_REFS
@@ -104,11 +108,11 @@ pub fn operating_mode(ctx: &Context) -> OperatingMode {
104108
"Failed to open in edit mode, falling back to outside workspace {}",
105109
error
106110
);
107-
OperatingMode::OutsideWorkspace(outside_workspace_metadata(ctx).unwrap_or_default())
111+
outside()
108112
}
109113
}
110114
} else {
111-
OperatingMode::OutsideWorkspace(outside_workspace_metadata(ctx).unwrap_or_default())
115+
outside()
112116
}
113117
}
114118

crates/gitbutler-operating-modes/tests/operating_modes.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,26 @@ fn create_edit_mode_metadata(ctx: &Context) {
2222
}
2323

2424
mod operating_modes {
25+
mod invalid_repo_fallback {
26+
use gitbutler_testsupport::{Case, Suite};
27+
28+
#[test]
29+
fn operating_mode_falls_back_to_outside_workspace_when_repo_cannot_be_opened() {
30+
let suite = Suite::default();
31+
let Case { ctx, .. } = &suite.new_case();
32+
33+
// Make opening the repo fail by removing the `.git` directory.
34+
std::fs::remove_dir_all(&ctx.gitdir).unwrap();
35+
36+
assert_eq!(
37+
gitbutler_operating_modes::operating_mode(&ctx),
38+
gitbutler_operating_modes::OperatingMode::OutsideWorkspace(
39+
gitbutler_operating_modes::OutsideWorkspaceMetadata::default()
40+
)
41+
);
42+
}
43+
}
44+
2545
mod open_workspace_mode {
2646
use gitbutler_operating_modes::{ensure_open_workspace_mode, in_open_workspace_mode};
2747
use gitbutler_testsupport::{Case, Suite};

0 commit comments

Comments
 (0)