@matheusbsbs / matheus-bagliano / commits / 3206c63d0a
Fork 0

git-gui: Refactor the delete branch dialog to use class system

A simple refactoring of the delete branch dialog to allow use of the class construct to better organize the code and to reuse the revision selection code of our new choose_rev mega-widget. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Shawn O. Pearce committed Jul 3, 2007 at 23:33 UTC 3206c63d0a365b11a430225a31f6a673f7645c25
4 files changed +185 -165
git-gui.sh
+1 -1
@@ -1507,7 +1507,7 @@ if {[is_enabled branch]} {
1507 [.mbar.branch index last] -state]
1508
1509 .mbar.branch add command -label {Delete...} \
1510 - -command do_delete_branch
1510 + -command branch_delete::dialog
1511 lappend disable_on_lock [list .mbar.branch entryconf \
1512 [.mbar.branch index last] -state]
1513
lib/branch.tcl
-164
@@ -66,170 +66,6 @@ proc radio_selector {varname value args} {
66 set var $value
67 }
68
69 -proc do_delete_branch_action {w} {
70 - global all_heads
71 - global delete_branch_checktype delete_branch_head delete_branch_trackinghead
72 -
73 - set check_rev {}
74 - switch -- $delete_branch_checktype {
75 - head {set check_rev $delete_branch_head}
76 - tracking {set check_rev $delete_branch_trackinghead}
77 - always {set check_rev {:none}}
78 - }
79 - if {$check_rev eq {:none}} {
80 - set check_cmt {}
81 - } elseif {[catch {set check_cmt [git rev-parse --verify "${check_rev}^0"]}]} {
82 - tk_messageBox \
83 - -icon error \
84 - -type ok \
85 - -title [wm title $w] \
86 - -parent $w \
87 - -message "Invalid check revision: $check_rev"
88 - return
89 - }
90 -
91 - set to_delete [list]
92 - set not_merged [list]
93 - foreach i [$w.list.l curselection] {
94 - set b [$w.list.l get $i]
95 - if {[catch {set o [git rev-parse --verify $b]}]} continue
96 - if {$check_cmt ne {}} {
97 - if {$b eq $check_rev} continue
98 - if {[catch {set m [git merge-base $o $check_cmt]}]} continue
99 - if {$o ne $m} {
100 - lappend not_merged $b
101 - continue
102 - }
103 - }
104 - lappend to_delete [list $b $o]
105 - }
106 - if {$not_merged ne {}} {
107 - set msg "The following branches are not completely merged into $check_rev:
108 -
109 - - [join $not_merged "\n - "]"
110 - tk_messageBox \
111 - -icon info \
112 - -type ok \
113 - -title [wm title $w] \
114 - -parent $w \
115 - -message $msg
116 - }
117 - if {$to_delete eq {}} return
118 - if {$delete_branch_checktype eq {always}} {
119 - set msg {Recovering deleted branches is difficult.
120 -
121 -Delete the selected branches?}
122 - if {[tk_messageBox \
123 - -icon warning \
124 - -type yesno \
125 - -title [wm title $w] \
126 - -parent $w \
127 - -message $msg] ne yes} {
128 - return
129 - }
130 - }
131 -
132 - set failed {}
133 - foreach i $to_delete {
134 - set b [lindex $i 0]
135 - set o [lindex $i 1]
136 - if {[catch {git update-ref -d "refs/heads/$b" $o} err]} {
137 - append failed " - $b: $err\n"
138 - } else {
139 - set x [lsearch -sorted -exact $all_heads $b]
140 - if {$x >= 0} {
141 - set all_heads [lreplace $all_heads $x $x]
142 - }
143 - }
144 - }
145 -
146 - if {$failed ne {}} {
147 - tk_messageBox \
148 - -icon error \
149 - -type ok \
150 - -title [wm title $w] \
151 - -parent $w \
152 - -message "Failed to delete branches:\n$failed"
153 - }
154 -
155 - set all_heads [lsort $all_heads]
156 - populate_branch_menu
157 - destroy $w
158 -}
159 -
160 -proc do_delete_branch {} {
161 - global all_heads tracking_branches current_branch
162 - global delete_branch_checktype delete_branch_head delete_branch_trackinghead
163 -
164 - set w .branch_editor
165 - toplevel $w
166 - wm geometry $w "+[winfo rootx .]+[winfo rooty .]"
167 -
168 - label $w.header -text {Delete Local Branch} \
169 - -font font_uibold
170 - pack $w.header -side top -fill x
171 -
172 - frame $w.buttons
173 - button $w.buttons.create -text Delete \
174 - -command [list do_delete_branch_action $w]
175 - pack $w.buttons.create -side right
176 - button $w.buttons.cancel -text {Cancel} \
177 - -command [list destroy $w]
178 - pack $w.buttons.cancel -side right -padx 5
179 - pack $w.buttons -side bottom -fill x -pady 10 -padx 10
180 -
181 - labelframe $w.list -text {Local Branches}
182 - listbox $w.list.l \
183 - -height 10 \
184 - -width 70 \
185 - -selectmode extended \
186 - -yscrollcommand [list $w.list.sby set]
187 - foreach h $all_heads {
188 - if {$h ne $current_branch} {
189 - $w.list.l insert end $h
190 - }
191 - }
192 - scrollbar $w.list.sby -command [list $w.list.l yview]
193 - pack $w.list.sby -side right -fill y
194 - pack $w.list.l -side left -fill both -expand 1
195 - pack $w.list -fill both -expand 1 -pady 5 -padx 5
196 -
197 - labelframe $w.validate -text {Delete Only If}
198 - radiobutton $w.validate.head_r \
199 - -text {Merged Into Local Branch:} \
200 - -value head \
201 - -variable delete_branch_checktype
202 - eval tk_optionMenu $w.validate.head_m delete_branch_head $all_heads
203 - grid $w.validate.head_r $w.validate.head_m -sticky w
204 - set all_trackings [all_tracking_branches]
205 - if {$all_trackings ne {}} {
206 - set delete_branch_trackinghead [lindex $all_trackings 0]
207 - radiobutton $w.validate.tracking_r \
208 - -text {Merged Into Tracking Branch:} \
209 - -value tracking \
210 - -variable delete_branch_checktype
211 - eval tk_optionMenu $w.validate.tracking_m \
212 - delete_branch_trackinghead \
213 - $all_trackings
214 - grid $w.validate.tracking_r $w.validate.tracking_m -sticky w
215 - }
216 - radiobutton $w.validate.always_r \
217 - -text {Always (Do not perform merge checks)} \
218 - -value always \
219 - -variable delete_branch_checktype
220 - grid $w.validate.always_r -columnspan 2 -sticky w
221 - grid columnconfigure $w.validate 1 -weight 1
222 - pack $w.validate -anchor nw -fill x -pady 5 -padx 5
223 -
224 - set delete_branch_head $current_branch
225 - set delete_branch_checktype head
226 -
227 - bind $w <Visibility> "grab $w; focus $w"
228 - bind $w <Key-Escape> "destroy $w"
229 - wm title $w "[appname] ([reponame]): Delete Branch"
230 - tkwait window $w
231 -}
232 -
69 proc switch_branch {new_branch} {
70 global HEAD commit_type current_branch repo_config
71
lib/branch_delete.tcl new
+163
@@ -0,0 +1,163 @@
1 +# git-gui branch delete support
2 +# Copyright (C) 2007 Shawn Pearce
3 +
4 +class branch_delete {
5 +
6 +field w ; # widget path
7 +field w_heads ; # listbox of local head names
8 +field w_check ; # revision picker for merge test
9 +field w_delete ; # delete button
10 +
11 +constructor dialog {} {
12 + global all_heads current_branch
13 +
14 + make_toplevel top w
15 + wm title $top "[appname] ([reponame]): Delete Branch"
16 + if {$top ne {.}} {
17 + wm geometry $top "+[winfo rootx .]+[winfo rooty .]"
18 + }
19 +
20 + label $w.header -text {Delete Local Branch} -font font_uibold
21 + pack $w.header -side top -fill x
22 +
23 + frame $w.buttons
24 + set w_delete $w.buttons.delete
25 + button $w_delete \
26 + -text Delete \
27 + -default active \
28 + -state disabled \
29 + -command [cb _delete]
30 + pack $w_delete -side right
31 + button $w.buttons.cancel \
32 + -text {Cancel} \
33 + -command [list destroy $w]
34 + pack $w.buttons.cancel -side right -padx 5
35 + pack $w.buttons -side bottom -fill x -pady 10 -padx 10
36 +
37 + labelframe $w.list -text {Local Branches}
38 + set w_heads $w.list.l
39 + listbox $w_heads \
40 + -height 10 \
41 + -width 70 \
42 + -selectmode extended \
43 + -yscrollcommand [list $w.list.sby set]
44 + scrollbar $w.list.sby -command [list $w.list.l yview]
45 + pack $w.list.sby -side right -fill y
46 + pack $w.list.l -side left -fill both -expand 1
47 + pack $w.list -fill both -expand 1 -pady 5 -padx 5
48 +
49 + set w_check [choose_rev::new \
50 + $w.check \
51 + {Delete Only If Merged Into} \
52 + ]
53 + $w_check none {Always (Do not perform merge test.)}
54 + pack $w.check -anchor nw -fill x -pady 5 -padx 5
55 +
56 + foreach h $all_heads {
57 + if {$h ne $current_branch} {
58 + $w_heads insert end $h
59 + }
60 + }
61 +
62 + bind $w_heads <<ListboxSelect>> [cb _select]
63 + bind $w <Visibility> "
64 + grab $w
65 + focus $w
66 + "
67 + bind $w <Key-Escape> [list destroy $w]
68 + bind $w <Key-Return> [cb _delete]\;break
69 + tkwait window $w
70 +}
71 +
72 +method _select {} {
73 + if {[$w_heads curselection] eq {}} {
74 + $w_delete configure -state disabled
75 + } else {
76 + $w_delete configure -state normal
77 + }
78 +}
79 +
80 +method _delete {} {
81 + global all_heads
82 +
83 + if {[catch {set check_cmt [$w_check get_commit]} err]} {
84 + tk_messageBox \
85 + -icon error \
86 + -type ok \
87 + -title [wm title $w] \
88 + -parent $w \
89 + -message "Invalid revision: [$w_check get]"
90 + return
91 + }
92 +
93 + set to_delete [list]
94 + set not_merged [list]
95 + foreach i [$w_heads curselection] {
96 + set b [$w_heads get $i]
97 + if {[catch {
98 + set o [git rev-parse --verify "refs/heads/$b"]
99 + }]} continue
100 + if {$check_cmt ne {}} {
101 + if {[catch {set m [git merge-base $o $check_cmt]}]} continue
102 + if {$o ne $m} {
103 + lappend not_merged $b
104 + continue
105 + }
106 + }
107 + lappend to_delete [list $b $o]
108 + }
109 + if {$not_merged ne {}} {
110 + set msg "The following branches are not completely merged into [$w_check get]:
111 +
112 + - [join $not_merged "\n - "]"
113 + tk_messageBox \
114 + -icon info \
115 + -type ok \
116 + -title [wm title $w] \
117 + -parent $w \
118 + -message $msg
119 + }
120 + if {$to_delete eq {}} return
121 + if {$check_cmt eq {}} {
122 + set msg {Recovering deleted branches is difficult.
123 +
124 +Delete the selected branches?}
125 + if {[tk_messageBox \
126 + -icon warning \
127 + -type yesno \
128 + -title [wm title $w] \
129 + -parent $w \
130 + -message $msg] ne yes} {
131 + return
132 + }
133 + }
134 +
135 + set failed {}
136 + foreach i $to_delete {
137 + set b [lindex $i 0]
138 + set o [lindex $i 1]
139 + if {[catch {git update-ref -d "refs/heads/$b" $o} err]} {
140 + append failed " - $b: $err\n"
141 + } else {
142 + set x [lsearch -sorted -exact $all_heads $b]
143 + if {$x >= 0} {
144 + set all_heads [lreplace $all_heads $x $x]
145 + }
146 + }
147 + }
148 +
149 + if {$failed ne {}} {
150 + tk_messageBox \
151 + -icon error \
152 + -type ok \
153 + -title [wm title $w] \
154 + -parent $w \
155 + -message "Failed to delete branches:\n$failed"
156 + }
157 +
158 + set all_heads [lsort $all_heads]
159 + populate_branch_menu
160 + destroy $w
161 +}
162 +
163 +}
lib/choose_rev.tcl
+21
@@ -87,17 +87,38 @@ constructor new {path {title {}}} {
87 return $this
88 }
89
90 +method none {text} {
91 + if {[winfo exists $w.none_r]} {
92 + $w.none_r configure -text $text
93 + return
94 + }
95 +
96 + radiobutton $w.none_r \
97 + -anchor w \
98 + -text $text \
99 + -value none \
100 + -variable @revtype
101 + grid $w.none_r -sticky we -padx {0 5} -columnspan 2
102 + if {$revtype eq {}} {
103 + set revtype none
104 + }
105 +}
106 +
107 method get {} {
108 switch -- $revtype {
109 head { return $c_head }
110 trck { return $c_trck }
111 tag { return $c_tag }
112 expr { return $c_expr }
113 + none { return {} }
114 default { error "unknown type of revision" }
115 }
116 }
117
118 method get_commit {} {
119 + if {$revtype eq {none}} {
120 + return {}
121 + }
122 set rev [get $this]
123 return [git rev-parse --verify "${rev}^0"]
124 }