Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle error thrown while checking out a branch #1537

Merged
merged 7 commits into from
Mar 6, 2025
Merged
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
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