1
0
mirror of https://github.com/git/git.git synced 2024-09-29 21:22:23 +02:00
git/lib/branch_delete.tcl
Shawn O. Pearce 7618e6b1c1 git-gui: Enhance choose_rev to handle hundreds of branches
One of my production repositories has hundreds of remote tracking
branches.  Trying to navigate these through a popup menu is just
not possible.  The list is far larger than the screen and it does
not scroll fast enough to efficiently select a branch name when
trying to create a branch or delete a branch.

This is major rewrite of the revision chooser mega-widget.  We
now use a single listbox for all three major types of named refs
(heads, tracking branches, tags) and a radio button group to pick
which of those namespaces should be shown in the listbox.  A filter
field is shown to the right allowing the end-user to key in a glob
specification to filter the list they are viewing.  The filter is
always taken as substring, so we assume * both starts and ends the
pattern the user wanted but otherwise treat it as a glob pattern.

This new picker works out really nicely.  What used to take me at
least a minute to find and select a branch now takes mere seconds.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
2007-07-08 21:12:52 -04:00

159 lines
3.5 KiB
Tcl

# git-gui branch delete support
# Copyright (C) 2007 Shawn Pearce
class branch_delete {
field w ; # widget path
field w_heads ; # listbox of local head names
field w_check ; # revision picker for merge test
field w_delete ; # delete button
constructor dialog {} {
global all_heads current_branch
make_toplevel top w
wm title $top "[appname] ([reponame]): Delete Branch"
if {$top ne {.}} {
wm geometry $top "+[winfo rootx .]+[winfo rooty .]"
}
label $w.header -text {Delete Local Branch} -font font_uibold
pack $w.header -side top -fill x
frame $w.buttons
set w_delete $w.buttons.delete
button $w_delete \
-text Delete \
-default active \
-state disabled \
-command [cb _delete]
pack $w_delete -side right
button $w.buttons.cancel \
-text {Cancel} \
-command [list destroy $w]
pack $w.buttons.cancel -side right -padx 5
pack $w.buttons -side bottom -fill x -pady 10 -padx 10
labelframe $w.list -text {Local Branches}
set w_heads $w.list.l
listbox $w_heads \
-height 10 \
-width 70 \
-selectmode extended \
-exportselection false \
-yscrollcommand [list $w.list.sby set]
scrollbar $w.list.sby -command [list $w.list.l yview]
pack $w.list.sby -side right -fill y
pack $w.list.l -side left -fill both -expand 1
pack $w.list -fill both -expand 1 -pady 5 -padx 5
set w_check [choose_rev::new \
$w.check \
{Delete Only If Merged Into} \
]
$w_check none {Always (Do not perform merge test.)}
pack $w.check -anchor nw -fill x -pady 5 -padx 5
foreach h $all_heads {
if {$h ne $current_branch} {
$w_heads insert end $h
}
}
bind $w_heads <<ListboxSelect>> [cb _select]
bind $w <Visibility> "
grab $w
focus $w
"
bind $w <Key-Escape> [list destroy $w]
bind $w <Key-Return> [cb _delete]\;break
tkwait window $w
}
method _select {} {
if {[$w_heads curselection] eq {}} {
$w_delete configure -state disabled
} else {
$w_delete configure -state normal
}
}
method _delete {} {
global all_heads
if {[catch {set check_cmt [$w_check commit_or_die]}]} {
return
}
set to_delete [list]
set not_merged [list]
foreach i [$w_heads curselection] {
set b [$w_heads get $i]
if {[catch {
set o [git rev-parse --verify "refs/heads/$b"]
}]} continue
if {$check_cmt ne {}} {
if {[catch {set m [git merge-base $o $check_cmt]}]} continue
if {$o ne $m} {
lappend not_merged $b
continue
}
}
lappend to_delete [list $b $o]
}
if {$not_merged ne {}} {
set msg "The following branches are not completely merged into [$w_check get]:
- [join $not_merged "\n - "]"
tk_messageBox \
-icon info \
-type ok \
-title [wm title $w] \
-parent $w \
-message $msg
}
if {$to_delete eq {}} return
if {$check_cmt eq {}} {
set msg {Recovering deleted branches is difficult.
Delete the selected branches?}
if {[tk_messageBox \
-icon warning \
-type yesno \
-title [wm title $w] \
-parent $w \
-message $msg] ne yes} {
return
}
}
set failed {}
foreach i $to_delete {
set b [lindex $i 0]
set o [lindex $i 1]
if {[catch {git update-ref -d "refs/heads/$b" $o} err]} {
append failed " - $b: $err\n"
} else {
set x [lsearch -sorted -exact $all_heads $b]
if {$x >= 0} {
set all_heads [lreplace $all_heads $x $x]
}
}
}
if {$failed ne {}} {
tk_messageBox \
-icon error \
-type ok \
-title [wm title $w] \
-parent $w \
-message "Failed to delete branches:\n$failed"
}
set all_heads [lsort $all_heads]
populate_branch_menu
destroy $w
}
}