Skip to content

Commit

Permalink
Handle error thrown while checking out a branch (#1537)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: Ryan Kornheisl <[email protected]>
Co-authored-by: Ryo Nakano <[email protected]>
  • Loading branch information
4 people authored Mar 6, 2025
1 parent 6eb0a44 commit bdc547f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
1 change: 1 addition & 0 deletions po/POTFILES
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 27 additions & 14 deletions src/Services/MonitoredRepository.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit bdc547f

Please sign in to comment.