Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions crates/gitbutler-operating-modes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,26 @@ pub enum OperatingMode {
}

pub fn operating_mode(ctx: &Context) -> OperatingMode {
let repo = ctx.git2_repo.get().unwrap();
let outside = || OperatingMode::OutsideWorkspace(outside_workspace_metadata(ctx).unwrap_or_default());

let Ok(repo) = ctx.repo.get() else {
// If we can't even open/borrow the repo handle, attempting to compute metadata would just
// try (and fail) to open it again.
return OperatingMode::OutsideWorkspace(OutsideWorkspaceMetadata::default());
};
let Ok(head_ref) = repo.head() else {
return OperatingMode::OutsideWorkspace(outside_workspace_metadata(ctx).unwrap_or_default());
return outside();
};

let Some(head_ref_name) = head_ref.name() else {
return OperatingMode::OutsideWorkspace(outside_workspace_metadata(ctx).unwrap_or_default());
let Some(head_ref_name) = head_ref.referent_name().map(|name| name.as_bstr()) else {
return outside();
};

if OPEN_WORKSPACE_REFS.contains(&head_ref_name) {
if OPEN_WORKSPACE_REFS
.iter()
.any(|workspace_ref| workspace_ref.as_bytes() == head_ref_name)
{
OperatingMode::OpenWorkspace
} else if head_ref_name == EDIT_BRANCH_REF {
} else if EDIT_BRANCH_REF.as_bytes() == head_ref_name {
let edit_mode_metadata = read_edit_mode_metadata(ctx);

match edit_mode_metadata {
Expand All @@ -100,11 +108,11 @@ pub fn operating_mode(ctx: &Context) -> OperatingMode {
"Failed to open in edit mode, falling back to outside workspace {}",
error
);
OperatingMode::OutsideWorkspace(outside_workspace_metadata(ctx).unwrap_or_default())
outside()
}
}
} else {
OperatingMode::OutsideWorkspace(outside_workspace_metadata(ctx).unwrap_or_default())
outside()
}
}

Expand Down
35 changes: 35 additions & 0 deletions crates/gitbutler-operating-modes/tests/operating_modes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,41 @@ fn create_edit_mode_metadata(ctx: &Context) {
}

mod operating_modes {
mod invalid_repo_fallback {
use gitbutler_testsupport::{Case, Suite};

#[test]
fn operating_mode_falls_back_to_outside_workspace_when_repo_cannot_be_opened() {
let suite = Suite::default();
let Case { ctx, .. } = &suite.new_case();

// Make opening the repo fail by removing the `.git` directory.
std::fs::remove_dir_all(&ctx.gitdir).unwrap();

assert_eq!(
gitbutler_operating_modes::operating_mode(ctx),
gitbutler_operating_modes::OperatingMode::OutsideWorkspace(
gitbutler_operating_modes::OutsideWorkspaceMetadata::default()
)
);
}
}

mod edit_branch_no_metadata_fallback {
use gitbutler_operating_modes::{OperatingMode, operating_mode};
use gitbutler_testsupport::{Case, Suite};

#[test]
fn operating_mode_falls_back_to_outside_workspace_when_on_edit_branch_without_metadata() {
let suite = Suite::default();
let Case { ctx, .. } = &suite.new_case();

crate::create_and_checkout_branch(ctx, "gitbutler/edit");

assert!(matches!(operating_mode(ctx), OperatingMode::OutsideWorkspace(_)));
}
}

mod open_workspace_mode {
use gitbutler_operating_modes::{ensure_open_workspace_mode, in_open_workspace_mode};
use gitbutler_testsupport::{Case, Suite};
Expand Down
Loading