Skip to content

Commit bae164c

Browse files
Jeremy WoottenJeremy Wootten
authored andcommitted
Confirm overwriting uncommitted changes when checking out branch
1 parent 167185a commit bae164c

3 files changed

Lines changed: 63 additions & 1 deletion

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2025 elementary, Inc. (https://elementary.io)
3+
*
4+
* This program is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU Lesser General Public
6+
* License version 3 as published by the Free Software Foundation.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11+
* General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU Lesser General Public
14+
* License along with this program; if not, write to the
15+
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16+
* Boston, MA 02110-1301 USA
17+
*/
18+
19+
public class Scratch.Dialogs.OverwriteUncommittedConfirmationDialog : Granite.MessageDialog {
20+
21+
public string branch_name { get; construct; }
22+
public OverwriteUncommittedConfirmationDialog (Gtk.Window parent, string new_branch_name) {
23+
Object (
24+
buttons: Gtk.ButtonsType.NONE,
25+
transient_for: parent,
26+
branch_name: new_branch_name
27+
);
28+
}
29+
30+
construct {
31+
modal = true;
32+
image_icon = new ThemedIcon ("dialog-stop");
33+
34+
primary_text = _("There are uncommitted changes in the current branch");
35+
///TRANSLATORS '%s' is a placeholder for the name of the branch to be checked out
36+
secondary_text = _("Uncommitted changes will be permanently lost if <b>'%s'</b> is checked out now.\n\n<i>It is recommended that uncommitted changes are stashed, committed, or reverted before proceeding </i>").printf (branch_name);
37+
38+
var proceed_button = (Gtk.Button) add_button (_("Checkout and Overwrite"), Gtk.ResponseType.ACCEPT);
39+
proceed_button.get_style_context ().add_class (Gtk.STYLE_CLASS_DESTRUCTIVE_ACTION);
40+
var cancel_button = add_button (_("Do not Checkout"), Gtk.ResponseType.REJECT);
41+
cancel_button.get_style_context ().add_class (Gtk.STYLE_CLASS_SUGGESTED_ACTION);
42+
}
43+
}

src/Services/MonitoredRepository.vala

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,13 @@ namespace Scratch.Services {
254254
checkout_branch (local_branch);
255255
}
256256

257-
private void checkout_branch (Ggit.Branch new_head_branch) {
257+
private void checkout_branch (Ggit.Branch new_head_branch, bool confirm = true) {
258258
//TODO Check current head has no uncommitted changes.since we FORCE the checkout.
259+
if (confirm && has_uncommitted) {
260+
confirm_checkout_branch (new_head_branch);
261+
return;
262+
}
263+
259264
git_repo.set_head (((Ggit.Ref)new_head_branch).get_name ());
260265
// This will force all changes in current branch to be overwritten even if uncommitted.
261266
// Using SAFE results in unwanted diff files in the staging area even if there are no uncommitted changes
@@ -267,6 +272,19 @@ namespace Scratch.Services {
267272
branch_name = new_head_branch.get_name ();
268273
}
269274

275+
private void confirm_checkout_branch (Ggit.Branch new_head_branch) {
276+
var parent = ((Gtk.Application)(GLib.Application.get_default ())).get_active_window ();
277+
var dialog = new Scratch.Dialogs.OverwriteUncommittedConfirmationDialog (parent, new_head_branch.get_name ());
278+
dialog.response.connect ((res) => {
279+
dialog.destroy ();
280+
if (res == Gtk.ResponseType.ACCEPT) {
281+
checkout_branch (new_head_branch, false);
282+
}
283+
});
284+
285+
dialog.present ();
286+
}
287+
270288
public void create_new_branch (string name) throws Error {
271289
Ggit.Object git_object = git_repo.get_head ().lookup ();
272290
var new_branch = git_repo.create_branch (name, git_object, Ggit.CreateFlags.NONE);

src/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ code_files = files(
2020
'Utils.vala',
2121
'Dialogs/PreferencesDialog.vala',
2222
'Dialogs/RestoreConfirmationDialog.vala',
23+
'Dialogs/OverwriteUncommittedConfirmationDialog.vala',
2324
'Dialogs/GlobalSearchDialog.vala',
2425
'Dialogs/NewBranchDialog.vala',
2526
'FolderManager/File.vala',

0 commit comments

Comments
 (0)