From bdc547fe64dee10a873c963cdbb8e3b806caf9cd Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Thu, 6 Mar 2025 14:05:15 +0000 Subject: [PATCH] Handle error thrown while checking out a branch (#1537) * Handle error when checking out branch * Show dialog on checkout error * Do not assume branch to checkout has a known name if error occurs --------- Co-authored-by: Jeremy Wootten Co-authored-by: Ryan Kornheisl Co-authored-by: Ryo Nakano --- po/POTFILES | 1 + src/Services/MonitoredRepository.vala | 41 ++++++++++++++++++--------- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/po/POTFILES b/po/POTFILES index 621cc2fdd..896a398e8 100644 --- a/po/POTFILES +++ b/po/POTFILES @@ -15,6 +15,7 @@ src/FolderManager/Item.vala src/FolderManager/ProjectFolderItem.vala src/Services/Document.vala src/Services/FileHandler.vala +src/Services/MonitoredRepository.vala src/Services/PluginManager.vala src/Services/Settings.vala src/Services/TemplateManager.vala diff --git a/src/Services/MonitoredRepository.vala b/src/Services/MonitoredRepository.vala index 9563a0070..33606af08 100644 --- a/src/Services/MonitoredRepository.vala +++ b/src/Services/MonitoredRepository.vala @@ -191,28 +191,41 @@ namespace Scratch.Services { checkout_branch (branch); } - private void checkout_branch (Ggit.Branch new_head_branch, bool confirm = true) throws Error { - if (confirm && has_uncommitted) { - confirm_checkout_branch (new_head_branch); - return; - } + private void checkout_branch (Ggit.Branch new_head_branch, bool confirm = true) { + var new_branch_name = ""; + try { + new_branch_name = new_head_branch.get_name (); + if (confirm && has_uncommitted) { + confirm_checkout_branch (new_head_branch); + return; + } - git_repo.set_head (((Ggit.Ref) new_head_branch).get_name ()); - var options = new Ggit.CheckoutOptions () { - //Ensure documents match checked out branch (deal with potential conflicts/losses beforehand) - strategy = Ggit.CheckoutStrategy.FORCE - }; + git_repo.set_head (((Ggit.Ref) new_head_branch).get_name ()); + var options = new Ggit.CheckoutOptions () { + //Ensure documents match checked out branch (deal with potential conflicts/losses beforehand) + strategy = Ggit.CheckoutStrategy.FORCE + }; - git_repo.checkout_head (options); + git_repo.checkout_head (options); + branch_name = new_branch_name; + } catch (Error e) { + var dialog = new Granite.MessageDialog.with_image_from_icon_name ( + _("An error occurred while checking out the requested branch"), + e.message, + "dialog-warning" + ); - branch_name = new_head_branch.get_name (); + dialog.run (); + dialog.destroy (); + } } - private void confirm_checkout_branch (Ggit.Branch new_head_branch) { + private void confirm_checkout_branch (Ggit.Branch new_head_branch) throws Error { var parent = ((Gtk.Application)(GLib.Application.get_default ())).get_active_window (); + var new_branch_name = new_head_branch.get_name (); var dialog = new Scratch.Dialogs.OverwriteUncommittedConfirmationDialog ( parent, - new_head_branch.get_name (), + new_branch_name, get_project_diff () ); dialog.response.connect ((res) => {