Skip to content
Draft
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
12 changes: 12 additions & 0 deletions src/Dialogs/BranchActions/BranchActionDialog.vala
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class Scratch.Dialogs.BranchActionDialog : Granite.MessageDialog {
}

public bool can_apply { get; set; default = false; }
public bool is_destructive { get; set; default = false; }
public FolderManager.ProjectFolderItem project { get; construct; }

private Gtk.Stack stack;
Expand All @@ -67,13 +68,24 @@ public class Scratch.Dialogs.BranchActionDialog : Granite.MessageDialog {

var apply_button = add_button (_("Apply"), Gtk.ResponseType.APPLY);
bind_property ("can-apply", apply_button, "sensitive", SYNC_CREATE);
notify["is-destructive"].connect (() => {
var ctx = apply_button.get_style_context ();
var scda = Gtk.STYLE_CLASS_DESTRUCTIVE_ACTION;
if (is_destructive && !ctx.has_class (scda)) {
ctx.add_class (scda);
} else if (!is_destructive && ctx.has_class (scda)) {
ctx.remove_class (scda);
}
});

var checkout_page = new BranchCheckoutPage (this);
var create_page = new BranchCreatePage (this);
var delete_page = new BranchDeletePage (this);

stack = new Gtk.Stack ();
stack.add_titled (checkout_page, BranchAction.CHECKOUT.to_string (), _("Checkout"));
stack.add_titled (create_page, BranchAction.CREATE.to_string (), _("New"));
stack.add_titled (delete_page, BranchAction.DELETE.to_string (), _("Delete"));

var sidebar = new Gtk.StackSidebar () {
stack = stack
Expand Down
77 changes: 77 additions & 0 deletions src/Dialogs/BranchActions/BranchDeletePage.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright 2025-2026 elementary, Inc. <https://elementary.io>
* SPDX-License-Identifier: GPL-3.0-or-later
*
* Authored by: Jeremy Wootten <jeremywootten@gmail.com>
*/

public class Scratch.Dialogs.BranchDeletePage : Gtk.Box, BranchActionPage {
public BranchAction action {
get {
return BranchAction.DELETE;
}
}

public Ggit.Ref? branch_ref {
get {
return list_box.get_selected_row ().bref;
}
}

public string new_branch_name {
get {
return "";
}
}

public BranchActionDialog dialog { get; construct; }

private BranchListBox list_box;
private Gtk.CheckButton delete_unmerged_check;

public BranchDeletePage (BranchActionDialog dialog) {
Object (
dialog: dialog
);
}

construct {
orientation = VERTICAL;
list_box = new BranchListBox (dialog, false) {
margin_bottom = 12
}; // No remotes
add (list_box);
delete_unmerged_check = new Gtk.CheckButton.with_label (_("Delete even if unmerged into default branch")) {
active = false,
valign = CENTER
};


var action_bar = new Gtk.ActionBar ();
action_bar.pack_start (delete_unmerged_check);
add (action_bar);

list_box.branch_changed.connect (() => {
delete_unmerged_check.active = false;
});

delete_unmerged_check.toggled.connect (() => {
update_can_apply ();
});


}

public override void focus_start_widget () {
list_box.grab_focus ();
}

private void update_can_apply () {
var text = list_box.text;
var exists = dialog.project.has_branch_name (text, null);
var is_current = dialog.project.get_current_branch_name () == text;
var is_merged = dialog.project.branch_name_is_merged (text);
dialog.can_apply = exists && !is_current && (is_merged || delete_unmerged_check.active);
dialog.is_destructive = delete_unmerged_check.active;
}
}
1 change: 1 addition & 0 deletions src/FolderManager/FileView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane
case MERGE:
break;
case DELETE:
dialog.project.delete_branch (dialog.branch_ref);
break;
case CREATE:
dialog.project.new_branch (dialog.new_branch_name);
Expand Down
12 changes: 12 additions & 0 deletions src/FolderManager/ProjectFolderItem.vala
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,10 @@ namespace Scratch.FolderManager {
}
}

public bool delete_branch (Ggit.Ref bref) {
return monitored_repo.delete_branch (bref);
}

public bool is_recent_ref (Ggit.Ref bref) {
return monitored_repo.is_recent_ref (bref);
}
Expand Down Expand Up @@ -446,6 +450,14 @@ namespace Scratch.FolderManager {
return is_git_repo ? monitored_repo.is_valid_new_local_branch_name (new_name) : false;
}

public bool branch_name_is_merged (string branch_name, Ggit.Ref? target = null) {
//TODO Implement checking whether one branch merged into another (target) branch
// A null target is to be treated as the default branch.
// It is not obvious how to do this with Ggit; moreover merging of a remote branch
// into a remote target will not be detectable anyway. So return false for now
return false;
}

// The parameter "is_explicit" indicates whether a global search was requested
// via a context menu on an explicitly chosen folder, in which case everything in that
// folder will be searched, or whether the hot-key was used in which case the search will
Expand Down
11 changes: 11 additions & 0 deletions src/Services/MonitoredRepository.vala
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,17 @@ namespace Scratch.Services {
return true;
}

public bool delete_branch (Ggit.Ref bref) {
try {
bref.@delete ();
} catch (Error e) {
warning ("Error deleting branch %s. %s", ((Ggit.Branch)bref).get_name (), e.message);
return false;
}

return true;
}

private bool do_update = false;
public void update_status_map () {
if (update_timer_id == 0) {
Expand Down
1 change: 1 addition & 0 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ code_files = files(
'Dialogs/BranchActions/BranchActionDialog.vala',
'Dialogs/BranchActions/BranchCheckoutPage.vala',
'Dialogs/BranchActions/BranchCreatePage.vala',
'Dialogs/BranchActions/BranchDeletePage.vala',
'Dialogs/BranchActions/BranchListBox.vala',
'Dialogs/BranchActions/BranchNameRow.vala',
'FolderManager/File.vala',
Expand Down